1 /**
2 * Copyright (c) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "resolv"
18
19 #include "DnsResolverService.h"
20
21 #include <set>
22 #include <vector>
23
24 #include <BinderUtil.h>
25 #include <android-base/strings.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <netdutils/DumpWriter.h>
29 #include <private/android_filesystem_config.h> // AID_SYSTEM
30
31 #include "DnsResolver.h"
32 #include "Experiments.h"
33 #include "NetdPermissions.h" // PERM_*
34 #include "PrivateDnsConfiguration.h"
35 #include "ResolverEventReporter.h"
36 #include "resolv_cache.h"
37
38 using aidl::android::net::ResolverOptionsParcel;
39 using aidl::android::net::ResolverParamsParcel;
40 using android::base::Join;
41 using android::netdutils::DumpWriter;
42 using android::netdutils::IPPrefix;
43
44 namespace android {
45 namespace net {
46
47 namespace {
48
49 #define ENFORCE_ANY_PERMISSION(...) \
50 do { \
51 ::ndk::ScopedAStatus status = checkAnyPermission({__VA_ARGS__}); \
52 if (!status.isOk()) { \
53 return status; \
54 } \
55 } while (0)
56
57 #define ENFORCE_INTERNAL_PERMISSIONS() \
58 ENFORCE_ANY_PERMISSION(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK)
59
60 #define ENFORCE_NETWORK_STACK_PERMISSIONS() \
61 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
62
statusFromErrcode(int ret)63 inline ::ndk::ScopedAStatus statusFromErrcode(int ret) {
64 if (ret) {
65 return ::ndk::ScopedAStatus(
66 AStatus_fromServiceSpecificErrorWithMessage(-ret, strerror(-ret)));
67 }
68 return ::ndk::ScopedAStatus(AStatus_newOk());
69 }
70
71 } // namespace
72
DnsResolverService()73 DnsResolverService::DnsResolverService() {
74 // register log callback to BnDnsResolver::logFunc
75 BnDnsResolver::logFunc = [](const auto& log) {
76 binderCallLogFn(log, [](const std::string& msg) { gResNetdCallbacks.log(msg.c_str()); });
77 };
78 }
79
start()80 binder_status_t DnsResolverService::start() {
81 // TODO: Add disableBackgroundScheduling(true) after libbinder_ndk support it. b/126506010
82 // NetdNativeService does call disableBackgroundScheduling currently, so it is fine now.
83 std::shared_ptr<DnsResolverService> resolverService =
84 ::ndk::SharedRefBase::make<DnsResolverService>();
85 binder_status_t status =
86 AServiceManager_addService(resolverService->asBinder().get(), getServiceName());
87 if (status != STATUS_OK) {
88 return status;
89 }
90
91 ABinderProcess_startThreadPool();
92
93 return STATUS_OK;
94 }
95
dump(int fd,const char ** args,uint32_t numArgs)96 binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t numArgs) {
97 auto dump_permission = checkAnyPermission({PERM_DUMP});
98 if (!dump_permission.isOk()) {
99 return STATUS_PERMISSION_DENIED;
100 }
101
102 // This method does not grab any locks. If individual classes need locking
103 // their dump() methods MUST handle locking appropriately.
104 DumpWriter dw(fd);
105
106 if (numArgs == 1 && std::string(args[0]) == DnsQueryLog::DUMP_KEYWORD) {
107 dw.blankline();
108 gDnsResolv->dnsQueryLog().dump(dw);
109 dw.blankline();
110 return STATUS_OK;
111 }
112
113 for (auto netId : resolv_list_caches()) {
114 dw.println("NetId: %u", netId);
115 gDnsResolv->resolverCtrl.dump(dw, netId);
116 dw.blankline();
117 }
118
119 PrivateDnsConfiguration::getInstance().dump(dw);
120 Experiments::getInstance()->dump(dw);
121 return STATUS_OK;
122 }
123
isAlive(bool * alive)124 ::ndk::ScopedAStatus DnsResolverService::isAlive(bool* alive) {
125 ENFORCE_INTERNAL_PERMISSIONS();
126
127 *alive = true;
128
129 return ::ndk::ScopedAStatus(AStatus_newOk());
130 }
131
registerEventListener(const std::shared_ptr<aidl::android::net::metrics::INetdEventListener> & listener)132 ::ndk::ScopedAStatus DnsResolverService::registerEventListener(
133 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) {
134 ENFORCE_NETWORK_STACK_PERMISSIONS();
135
136 int res = ResolverEventReporter::getInstance().addListener(listener);
137
138 return statusFromErrcode(res);
139 }
140
registerUnsolicitedEventListener(const std::shared_ptr<aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener> & listener)141 ::ndk::ScopedAStatus DnsResolverService::registerUnsolicitedEventListener(
142 const std::shared_ptr<
143 aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>& listener) {
144 ENFORCE_NETWORK_STACK_PERMISSIONS();
145
146 int res = ResolverEventReporter::getInstance().addUnsolEventListener(listener);
147
148 return statusFromErrcode(res);
149 }
150
checkAnyPermission(const std::vector<const char * > & permissions)151 ::ndk::ScopedAStatus DnsResolverService::checkAnyPermission(
152 const std::vector<const char*>& permissions) {
153 // TODO: Remove callback and move this to unnamed namespace after libbiner_ndk supports
154 // check_permission.
155 if (!gResNetdCallbacks.check_calling_permission) {
156 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(
157 EX_NULL_POINTER, "check_calling_permission is null"));
158 }
159 pid_t pid = AIBinder_getCallingPid();
160 uid_t uid = AIBinder_getCallingUid();
161
162 // If the caller is the system UID, don't check permissions.
163 // Otherwise, if the system server's binder thread pool is full, and all the threads are
164 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
165 //
166 // From a security perspective, there is currently no difference, because:
167 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
168 // and NETWORK_STACK, which the system server always has (or MAINLINE_NETWORK_STACK, which
169 // is equivalent to having both CONNECTIVITY_INTERNAL and NETWORK_STACK).
170 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
171 if (uid == AID_SYSTEM) {
172 return ::ndk::ScopedAStatus(AStatus_newOk());
173 }
174
175 for (const char* permission : permissions) {
176 if (gResNetdCallbacks.check_calling_permission(permission)) {
177 return ::ndk::ScopedAStatus(AStatus_newOk());
178 }
179 }
180
181 auto err = fmt::format("UID {} / PID {} does not have any of the following permissions: {}",
182 uid, pid, Join(permissions, ','));
183 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(EX_SECURITY, err.c_str()));
184 }
185
setResolverConfiguration(const ResolverParamsParcel & resolverParams)186 ::ndk::ScopedAStatus DnsResolverService::setResolverConfiguration(
187 const ResolverParamsParcel& resolverParams) {
188 // Locking happens in PrivateDnsConfiguration and res_* functions.
189 ENFORCE_INTERNAL_PERMISSIONS();
190
191 // TODO@: Switch to selinux based permission check if AIBinder_getCallingSid and
192 // AIBinder_setRequestingSid can be supported by libbinder_dnk (b/159135973).
193 uid_t uid = AIBinder_getCallingUid();
194 // CAUTION: caCertificate should NOT be used except for internal testing.
195 if (resolverParams.caCertificate.size() != 0 && uid != AID_ROOT) {
196 auto err = fmt::format("UID {} is not authorized to set a non-empty CA certificate", uid);
197 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(EX_SECURITY, err.c_str()));
198 }
199
200 int res = gDnsResolv->resolverCtrl.setResolverConfiguration(resolverParams);
201 return statusFromErrcode(res);
202 }
203
getResolverInfo(int32_t netId,std::vector<std::string> * servers,std::vector<std::string> * domains,std::vector<std::string> * tlsServers,std::vector<int32_t> * params,std::vector<int32_t> * stats,std::vector<int32_t> * wait_for_pending_req_timeout_count)204 ::ndk::ScopedAStatus DnsResolverService::getResolverInfo(
205 int32_t netId, std::vector<std::string>* servers, std::vector<std::string>* domains,
206 std::vector<std::string>* tlsServers, std::vector<int32_t>* params,
207 std::vector<int32_t>* stats, std::vector<int32_t>* wait_for_pending_req_timeout_count) {
208 // Locking happens in PrivateDnsConfiguration and res_* functions.
209 ENFORCE_NETWORK_STACK_PERMISSIONS();
210
211 // The interface names aren't part of the IPC signature, so cannot be passed back to the caller.
212 // TODO: consider defining a new IP method that returns a parcel instead.
213 std::vector<std::string> interfaceNames;
214
215 int timeout_count = 0;
216 int res = gDnsResolv->resolverCtrl.getResolverInfo(
217 netId, servers, domains, tlsServers, &interfaceNames, params, stats, &timeout_count);
218 // Due to historical reason, wait_for_pending_req_timeout_count couldn't be
219 // an int but a vector. See aosp/858377 for more details.
220 wait_for_pending_req_timeout_count->clear();
221 wait_for_pending_req_timeout_count->push_back(timeout_count);
222
223 return statusFromErrcode(res);
224 }
225
startPrefix64Discovery(int32_t netId)226 ::ndk::ScopedAStatus DnsResolverService::startPrefix64Discovery(int32_t netId) {
227 // Locking happens in Dns64Configuration.
228 ENFORCE_NETWORK_STACK_PERMISSIONS();
229
230 gDnsResolv->resolverCtrl.startPrefix64Discovery(netId);
231
232 return ::ndk::ScopedAStatus(AStatus_newOk());
233 }
234
stopPrefix64Discovery(int32_t netId)235 ::ndk::ScopedAStatus DnsResolverService::stopPrefix64Discovery(int32_t netId) {
236 // Locking happens in Dns64Configuration.
237 ENFORCE_NETWORK_STACK_PERMISSIONS();
238
239 gDnsResolv->resolverCtrl.stopPrefix64Discovery(netId);
240
241 return ::ndk::ScopedAStatus(AStatus_newOk());
242 }
243
getPrefix64(int netId,std::string * stringPrefix)244 ::ndk::ScopedAStatus DnsResolverService::getPrefix64(int netId, std::string* stringPrefix) {
245 ENFORCE_NETWORK_STACK_PERMISSIONS();
246
247 netdutils::IPPrefix prefix{};
248 int res = gDnsResolv->resolverCtrl.getPrefix64(netId, &prefix);
249 *stringPrefix = prefix.toString();
250
251 return statusFromErrcode(res);
252 }
253
setPrefix64(int netId,const std::string & stringPrefix)254 ::ndk::ScopedAStatus DnsResolverService::setPrefix64(int netId, const std::string& stringPrefix) {
255 ENFORCE_NETWORK_STACK_PERMISSIONS();
256
257 if (stringPrefix.empty()) {
258 return statusFromErrcode(gDnsResolv->resolverCtrl.clearPrefix64(netId));
259 }
260
261 IPPrefix prefix;
262 if (!IPPrefix::forString(stringPrefix, &prefix)) {
263 return statusFromErrcode(-EINVAL);
264 }
265
266 return statusFromErrcode(gDnsResolv->resolverCtrl.setPrefix64(netId, prefix));
267 }
268
setLogSeverity(int32_t logSeverity)269 ::ndk::ScopedAStatus DnsResolverService::setLogSeverity(int32_t logSeverity) {
270 ENFORCE_NETWORK_STACK_PERMISSIONS();
271
272 int res = gDnsResolv->setLogSeverity(logSeverity);
273
274 return statusFromErrcode(res);
275 }
276
destroyNetworkCache(int netId)277 ::ndk::ScopedAStatus DnsResolverService::destroyNetworkCache(int netId) {
278 // Locking happens in res_cache.cpp functions.
279 ENFORCE_NETWORK_STACK_PERMISSIONS();
280
281 gDnsResolv->resolverCtrl.destroyNetworkCache(netId);
282 Experiments::getInstance()->update();
283 return ::ndk::ScopedAStatus(AStatus_newOk());
284 }
285
createNetworkCache(int netId)286 ::ndk::ScopedAStatus DnsResolverService::createNetworkCache(int netId) {
287 // Locking happens in res_cache.cpp functions.
288 ENFORCE_NETWORK_STACK_PERMISSIONS();
289
290 int res = gDnsResolv->resolverCtrl.createNetworkCache(netId);
291 Experiments::getInstance()->update();
292 return statusFromErrcode(res);
293 }
294
flushNetworkCache(int netId)295 ::ndk::ScopedAStatus DnsResolverService::flushNetworkCache(int netId) {
296 // Locking happens in res_cache.cpp functions.
297 ENFORCE_NETWORK_STACK_PERMISSIONS();
298
299 int res = gDnsResolv->resolverCtrl.flushNetworkCache(netId);
300
301 return statusFromErrcode(res);
302 }
303
setResolverOptions(int32_t netId,const ResolverOptionsParcel & options)304 ::ndk::ScopedAStatus DnsResolverService::setResolverOptions(int32_t netId,
305 const ResolverOptionsParcel& options) {
306 // Locking happens in res_cache.cpp functions.
307 ENFORCE_NETWORK_STACK_PERMISSIONS();
308
309 return statusFromErrcode(resolv_set_options(netId, options));
310 }
311
312 } // namespace net
313 } // namespace android
314