blob: 7916068b2d2f7c89bbf4deee4e9d0e73eac8ae8c [file] [log] [blame]
Luke Huang403c0442019-02-25 15:33:42 +08001/**
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 "DnsResolverService"
18
Luke Huang36796f32019-03-13 02:54:45 +080019#include "DnsResolverService.h"
20
Luke Huang403c0442019-02-25 15:33:42 +080021#include <set>
22#include <vector>
23
24#include <android-base/stringprintf.h>
25#include <android-base/strings.h>
26#include <android/binder_manager.h>
27#include <android/binder_process.h>
28#include <log/log.h>
29#include <private/android_filesystem_config.h> // AID_SYSTEM
30
Luke Huang36796f32019-03-13 02:54:45 +080031#include "DnsResolver.h"
Luke Huang403c0442019-02-25 15:33:42 +080032#include "NetdPermissions.h" // PERM_*
Hungming Chena32c8c12019-01-25 10:47:40 +080033#include "ResolverEventReporter.h"
Luke Huang403c0442019-02-25 15:33:42 +080034
35using android::base::Join;
36using android::base::StringPrintf;
37
38namespace android {
39namespace net {
40
41namespace {
42
43#define ENFORCE_ANY_PERMISSION(...) \
44 do { \
45 ::ndk::ScopedAStatus status = checkAnyPermission({__VA_ARGS__}); \
46 if (!status.isOk()) { \
47 return status; \
48 } \
49 } while (0)
50
51#define ENFORCE_INTERNAL_PERMISSIONS() \
52 ENFORCE_ANY_PERMISSION(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK)
53
54#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
55 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
56
Hungming Chena32c8c12019-01-25 10:47:40 +080057inline ::ndk::ScopedAStatus statusFromErrcode(int ret) {
58 if (ret) {
59 return ::ndk::ScopedAStatus(
60 AStatus_fromServiceSpecificErrorWithMessage(-ret, strerror(-ret)));
61 }
62 return ::ndk::ScopedAStatus(AStatus_newOk());
63}
64
Luke Huang403c0442019-02-25 15:33:42 +080065} // namespace
66
Luke Huang36796f32019-03-13 02:54:45 +080067binder_status_t DnsResolverService::start() {
Luke Huang403c0442019-02-25 15:33:42 +080068 // TODO: Add disableBackgroundScheduling(true) after libbinder_ndk support it. b/126506010
69 // NetdNativeService does call disableBackgroundScheduling currently, so it is fine now.
Luke Huang36796f32019-03-13 02:54:45 +080070 DnsResolverService* resolverService = new DnsResolverService();
Luke Huang403c0442019-02-25 15:33:42 +080071 binder_status_t status =
72 AServiceManager_addService(resolverService->asBinder().get(), getServiceName());
73 if (status != STATUS_OK) {
74 return status;
75 }
76
77 ABinderProcess_startThreadPool();
78
79 // TODO: register log callback if binder NDK backend support it. b/126501406
80
81 return STATUS_OK;
82}
83
84::ndk::ScopedAStatus DnsResolverService::isAlive(bool* alive) {
85 ENFORCE_INTERNAL_PERMISSIONS();
86
87 *alive = true;
88
89 return ::ndk::ScopedAStatus(AStatus_newOk());
90}
91
Hungming Chena32c8c12019-01-25 10:47:40 +080092::ndk::ScopedAStatus DnsResolverService::registerEventListener(
93 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) {
94 ENFORCE_NETWORK_STACK_PERMISSIONS();
95
96 int res = ResolverEventReporter::getInstance().addListener(listener);
97
98 return statusFromErrcode(res);
99}
100
Luke Huang403c0442019-02-25 15:33:42 +0800101::ndk::ScopedAStatus DnsResolverService::checkAnyPermission(
102 const std::vector<const char*>& permissions) {
103 // TODO: Remove callback and move this to unnamed namespace after libbiner_ndk supports
104 // check_permission.
Luke Huang36796f32019-03-13 02:54:45 +0800105 if (!gResNetdCallbacks.check_calling_permission) {
Luke Huang403c0442019-02-25 15:33:42 +0800106 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(
107 EX_NULL_POINTER, "check_calling_permission is null"));
108 }
109 pid_t pid = AIBinder_getCallingPid();
110 uid_t uid = AIBinder_getCallingUid();
111
112 // If the caller is the system UID, don't check permissions.
113 // Otherwise, if the system server's binder thread pool is full, and all the threads are
114 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
115 //
116 // From a security perspective, there is currently no difference, because:
117 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
118 // and NETWORK_STACK, which the system server always has (or MAINLINE_NETWORK_STACK, which
119 // is equivalent to having both CONNECTIVITY_INTERNAL and NETWORK_STACK).
120 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
121 if (uid == AID_SYSTEM) {
122 return ::ndk::ScopedAStatus(AStatus_newOk());
123 }
124
125 for (const char* permission : permissions) {
Luke Huang36796f32019-03-13 02:54:45 +0800126 if (gResNetdCallbacks.check_calling_permission(permission)) {
Luke Huang403c0442019-02-25 15:33:42 +0800127 return ::ndk::ScopedAStatus(AStatus_newOk());
128 }
129 }
130
131 auto err = StringPrintf("UID %d / PID %d does not have any of the following permissions: %s",
132 uid, pid, Join(permissions, ',').c_str());
133 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(EX_SECURITY, err.c_str()));
134}
135
136} // namespace net
137} // namespace android