| LuK1337 | 68fc487 | 2020-09-16 19:35:05 +0200 | [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 | #include <android/binder_manager.h> |
| 18 | #include <android/content/pm/IPackageManagerNative.h> |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/properties.h> |
| 22 | #include <android-base/strings.h> |
| 23 | #include <binder/IBinder.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <cutils/multiuser.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #include "adbroot_service.h" |
| 29 | |
| 30 | namespace { |
| 31 | const std::string kStoragePath = "/data/adbroot/"; |
| 32 | const std::string kEnabled = "enabled"; |
| 33 | |
| 34 | static ndk::ScopedAStatus SecurityException(const std::string& msg) { |
| 35 | LOG(ERROR) << msg; |
| 36 | return ndk::ScopedAStatus(AStatus_fromExceptionCodeWithMessage(EX_SECURITY, msg.c_str())); |
| 37 | } |
| 38 | } // anonymous namespace |
| 39 | |
| 40 | namespace android { |
| 41 | static bool isAutomotive() { |
| 42 | sp<IServiceManager> serviceManager = defaultServiceManager(); |
| 43 | if (serviceManager.get() == nullptr) { |
| 44 | LOG(ERROR) << "Unable to access native ServiceManager"; |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | sp<content::pm::IPackageManagerNative> packageManager; |
| 49 | sp<IBinder> binder = serviceManager->waitForService(String16("package_native")); |
| 50 | packageManager = interface_cast<content::pm::IPackageManagerNative>(binder); |
| 51 | if (packageManager == nullptr) { |
| 52 | LOG(ERROR) << "Unable to access native PackageManager"; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | bool isAutomotive = false; |
| 57 | binder::Status status = |
| 58 | packageManager->hasSystemFeature(String16("android.hardware.type.automotive"), 0, |
| 59 | &isAutomotive); |
| 60 | if (!status.isOk()) { |
| 61 | LOG(ERROR) << "Calling hasSystemFeature failed: " << status.exceptionMessage().c_str(); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return isAutomotive; |
| 66 | } |
| 67 | } // namespace android |
| 68 | |
| 69 | namespace aidl { |
| 70 | namespace android { |
| 71 | namespace adbroot { |
| 72 | |
| 73 | using ::android::AutoMutex; |
| 74 | using ::android::base::ReadFileToString; |
| 75 | using ::android::base::SetProperty; |
| 76 | using ::android::base::Trim; |
| 77 | using ::android::base::WriteStringToFile; |
| 78 | |
| 79 | static inline bool isAutomotive(uid_t uid) { |
| 80 | appid_t appid = multiuser_get_app_id(uid); |
| 81 | |
| 82 | return appid == AID_SYSTEM && ::android::isAutomotive(); |
| 83 | } |
| 84 | |
| 85 | ADBRootService::ADBRootService() : enabled_(false) { |
| 86 | std::string buf; |
| 87 | if (ReadFileToString(kStoragePath + kEnabled, &buf)) { |
| 88 | enabled_ = Trim(buf) == "1"; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void ADBRootService::Register() { |
| 93 | auto service = ndk::SharedRefBase::make<ADBRootService>(); |
| 94 | binder_status_t status = AServiceManager_addService( |
| 95 | service->asBinder().get(), getServiceName()); |
| 96 | |
| 97 | if (status != STATUS_OK) { |
| 98 | LOG(FATAL) << "Could not register adbroot service: " << status; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | ndk::ScopedAStatus ADBRootService::isSupported(bool* _aidl_return) { |
| 103 | uid_t uid = AIBinder_getCallingUid(); |
| 104 | if (uid != AID_SYSTEM && uid != AID_SHELL && !isAutomotive(uid)) { |
| 105 | return SecurityException("Caller must be system or shell"); |
| 106 | } |
| 107 | |
| 108 | AutoMutex _l(lock_); |
| 109 | *_aidl_return = __android_log_is_debuggable(); |
| 110 | return ndk::ScopedAStatus::ok(); |
| 111 | } |
| 112 | |
| 113 | ndk::ScopedAStatus ADBRootService::setEnabled(bool enabled) { |
| 114 | uid_t uid = AIBinder_getCallingUid(); |
| 115 | if (uid != AID_SYSTEM && !isAutomotive(uid)) { |
| 116 | return SecurityException("Caller must be system"); |
| 117 | } |
| 118 | |
| 119 | AutoMutex _l(lock_); |
| 120 | |
| 121 | if (enabled_ != enabled) { |
| 122 | enabled_ = enabled; |
| 123 | WriteStringToFile(std::to_string(enabled), kStoragePath + kEnabled); |
| 124 | |
| 125 | // Turning off adb root, restart adbd. |
| 126 | if (!enabled) { |
| 127 | SetProperty("service.adb.root", "0"); |
| 128 | SetProperty("ctl.restart", "adbd"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return ndk::ScopedAStatus::ok(); |
| 133 | } |
| 134 | |
| 135 | ndk::ScopedAStatus ADBRootService::getEnabled(bool* _aidl_return) { |
| 136 | uid_t uid = AIBinder_getCallingUid(); |
| 137 | if (uid != AID_SYSTEM && uid != AID_SHELL && !isAutomotive(uid)) { |
| 138 | return SecurityException("Caller must be system or shell"); |
| 139 | } |
| 140 | |
| 141 | AutoMutex _l(lock_); |
| 142 | *_aidl_return = enabled_; |
| 143 | return ndk::ScopedAStatus::ok(); |
| 144 | } |
| 145 | |
| 146 | } // namespace adbroot |
| 147 | } // namespace android |
| 148 | } // namespace aidl |