• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <android-base/format.h>
32 #include <arpa/nameser.h>
33 #include <netinet/in.h>
34 
35 /*
36  * Passing NETID_UNSET as the netId causes system/netd/resolv/DnsProxyListener.cpp to
37  * fill in the appropriate default netId for the query.
38  */
39 #define NETID_UNSET 0u
40 
41 /*
42  * MARK_UNSET represents the default (i.e. unset) value for a socket mark.
43  */
44 #define MARK_UNSET 0u
45 
46 #define NET_CONTEXT_INVALID_UID ((uid_t)-1)
47 #define NET_CONTEXT_INVALID_PID ((pid_t)-1)
48 
49 /*
50  * A struct to capture context relevant to network operations.
51  *
52  * Application and DNS netids/marks can differ from one another under certain
53  * circumstances, notably when a VPN applies to the given uid's traffic but the
54  * VPN network does not have its own DNS servers explicitly provisioned.
55  *
56  * The introduction of per-UID routing means the uid is also an essential part
57  * of the evaluation context. Its proper uninitialized value is
58  * NET_CONTEXT_INVALID_UID.
59  */
60 struct android_net_context {
61     unsigned app_netid;
62     unsigned app_mark;
63     unsigned dns_netid;
64     unsigned dns_mark;
65     uid_t uid = NET_CONTEXT_INVALID_UID;
66     unsigned flags;
67     // Variable to store the pid of the application sending DNS query.
68     pid_t pid = NET_CONTEXT_INVALID_PID;
69 
toStringandroid_net_context70     std::string toString() const {
71         return fmt::format("{} {} {} {} {} {}", app_netid, app_mark, dns_netid, dns_mark, uid,
72                            flags);
73     }
74 };
75 
76 #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
77 #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
78 #define NET_CONTEXT_FLAG_USE_DNS_OVER_TLS 0x00000004
79 
80 // TODO: investigate having the resolver check permissions itself, either by adding support to
81 // libbinder_ndk or by converting IPermissionController into a stable AIDL interface.
82 typedef bool (*check_calling_permission_callback)(const char* permission);
83 typedef void (*get_network_context_callback)(unsigned netid, uid_t uid,
84                                              android_net_context* netcontext);
85 typedef void (*log_callback)(const char* msg);
86 typedef int (*tagSocketCallback)(int sockFd, uint32_t tag, uid_t uid, pid_t pid);
87 
88 // The DnsResolver module invokes this callback once before starting each DNS
89 // lookup. The callback receives the android_net_context associated with the
90 // request, and the (possibly unqualified) hostname requested by the app via
91 // getaddrinfo() or gethostbyname().
92 //
93 // If the callback returns false, the DnsResolver will abort the request
94 // returning EAI_SYSTEM. If the callback returns true, the query will proceed as
95 // usual.
96 //
97 // If this callback is not present (i.e. set to nullptr), the effect is the same
98 // of returning true.
99 //
100 // This callback *will* be invoked concurrently from multiple threads. It must
101 // peform its own locking when accessing shared data structures. Furthermore,
102 // the callback must not sleep nor perform RPC requests.
103 //
104 // Be mindful that hostnames could contain sensitive user data. Do not log them
105 // and do not transmit them to third parties without explicit user
106 // authorization.
107 //
108 typedef bool (*evaluate_domain_name_callback)(
109     const android_net_context &netcontext, const char *host);
110 
111 /*
112  * Some functions needed by the resolver (e.g. checkCallingPermission()) live in
113  * libraries with no ABI stability guarantees, such as libbinder.so.
114  * As a temporary workaround, we keep these functions in netd and call them via
115  * function pointers.
116  */
117 struct ResolverNetdCallbacks {
118     check_calling_permission_callback check_calling_permission;
119     get_network_context_callback get_network_context;
120     log_callback log;
121     tagSocketCallback tagSocket;
122     evaluate_domain_name_callback evaluate_domain_name;
123 };
124 
125 #define TAG_SYSTEM_DNS 0xFFFFFF82
126 
127 #define LIBNETD_RESOLV_PUBLIC extern "C" [[gnu::visibility("default")]]
128 
129 LIBNETD_RESOLV_PUBLIC bool resolv_has_nameservers(unsigned netid);
130 
131 // Set callbacks and bring DnsResolver up.
132 LIBNETD_RESOLV_PUBLIC bool resolv_init(const ResolverNetdCallbacks* callbacks);
133 
134 // Function that performs RDNS in local cache. The |domain_name_size| is the size of domain_name
135 // buffer, which is recommended to NS_MAXDNAME. Function return false if hostname not found or
136 // domain_name_size > NS_MAXDNAME.
137 LIBNETD_RESOLV_PUBLIC bool resolv_gethostbyaddr_from_cache(unsigned netId, char domain_name[],
138                                                            size_t domain_name_size,
139                                                            const char* ip_address, int af);
140