Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 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 "DnsResolverService" |
| 18 | |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 19 | #include "DnsResolverService.h" |
| 20 | |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 21 | #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 Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 31 | #include "DnsResolver.h" |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 32 | #include "NetdPermissions.h" // PERM_* |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame^] | 33 | #include "ResolverEventReporter.h" |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 34 | |
| 35 | using android::base::Join; |
| 36 | using android::base::StringPrintf; |
| 37 | |
| 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
| 41 | namespace { |
| 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 Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame^] | 57 | inline ::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 Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 65 | } // namespace |
| 66 | |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 67 | binder_status_t DnsResolverService::start() { |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 68 | // TODO: Add disableBackgroundScheduling(true) after libbinder_ndk support it. b/126506010 |
| 69 | // NetdNativeService does call disableBackgroundScheduling currently, so it is fine now. |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 70 | DnsResolverService* resolverService = new DnsResolverService(); |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 71 | 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 Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame^] | 92 | ::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 Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 101 | ::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 Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 105 | if (!gResNetdCallbacks.check_calling_permission) { |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 106 | 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 Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 126 | if (gResNetdCallbacks.check_calling_permission(permission)) { |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 127 | 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 |