blob: adf698bc32223039caf5ea06ad3fcd255ade6475 [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
19#include <set>
20#include <vector>
21
22#include <android-base/stringprintf.h>
23#include <android-base/strings.h>
24#include <android/binder_manager.h>
25#include <android/binder_process.h>
26#include <log/log.h>
27#include <private/android_filesystem_config.h> // AID_SYSTEM
28
29#include "DnsResolverService.h"
30#include "NetdPermissions.h" // PERM_*
31
32using android::base::Join;
33using android::base::StringPrintf;
34
35namespace android {
36namespace net {
37
38namespace {
39
40#define ENFORCE_ANY_PERMISSION(...) \
41 do { \
42 ::ndk::ScopedAStatus status = checkAnyPermission({__VA_ARGS__}); \
43 if (!status.isOk()) { \
44 return status; \
45 } \
46 } while (0)
47
48#define ENFORCE_INTERNAL_PERMISSIONS() \
49 ENFORCE_ANY_PERMISSION(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK)
50
51#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
52 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
53
54} // namespace
55
56binder_status_t DnsResolverService::start(const dnsproxylistener_callbacks& callbacks) {
57 // TODO: Add disableBackgroundScheduling(true) after libbinder_ndk support it. b/126506010
58 // NetdNativeService does call disableBackgroundScheduling currently, so it is fine now.
59 DnsResolverService* resolverService = new DnsResolverService(callbacks);
60 binder_status_t status =
61 AServiceManager_addService(resolverService->asBinder().get(), getServiceName());
62 if (status != STATUS_OK) {
63 return status;
64 }
65
66 ABinderProcess_startThreadPool();
67
68 // TODO: register log callback if binder NDK backend support it. b/126501406
69
70 return STATUS_OK;
71}
72
73::ndk::ScopedAStatus DnsResolverService::isAlive(bool* alive) {
74 ENFORCE_INTERNAL_PERMISSIONS();
75
76 *alive = true;
77
78 return ::ndk::ScopedAStatus(AStatus_newOk());
79}
80
81::ndk::ScopedAStatus DnsResolverService::checkAnyPermission(
82 const std::vector<const char*>& permissions) {
83 // TODO: Remove callback and move this to unnamed namespace after libbiner_ndk supports
84 // check_permission.
85 if (!mCallbacks.check_calling_permission) {
86 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(
87 EX_NULL_POINTER, "check_calling_permission is null"));
88 }
89 pid_t pid = AIBinder_getCallingPid();
90 uid_t uid = AIBinder_getCallingUid();
91
92 // If the caller is the system UID, don't check permissions.
93 // Otherwise, if the system server's binder thread pool is full, and all the threads are
94 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
95 //
96 // From a security perspective, there is currently no difference, because:
97 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
98 // and NETWORK_STACK, which the system server always has (or MAINLINE_NETWORK_STACK, which
99 // is equivalent to having both CONNECTIVITY_INTERNAL and NETWORK_STACK).
100 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
101 if (uid == AID_SYSTEM) {
102 return ::ndk::ScopedAStatus(AStatus_newOk());
103 }
104
105 for (const char* permission : permissions) {
106 if (mCallbacks.check_calling_permission(permission)) {
107 return ::ndk::ScopedAStatus(AStatus_newOk());
108 }
109 }
110
111 auto err = StringPrintf("UID %d / PID %d does not have any of the following permissions: %s",
112 uid, pid, Join(permissions, ',').c_str());
113 return ::ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(EX_SECURITY, err.c_str()));
114}
115
116} // namespace net
117} // namespace android