Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Christopher Wiley | 5975c00 | 2016-02-12 15:41:08 -0800 | [diff] [blame] | 17 | #include <mutex> |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 18 | #include <binder/AppOpsManager.h> |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 19 | #include <binder/Binder.h> |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 20 | #include <binder/IServiceManager.h> |
| 21 | |
| 22 | #include <utils/SystemClock.h> |
| 23 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 24 | #include <sys/types.h> |
| 25 | |
| 26 | #ifdef LOG_TAG |
| 27 | #undef LOG_TAG |
| 28 | #endif |
| 29 | #define LOG_TAG "AppOpsManager" |
| 30 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 33 | namespace { |
| 34 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 35 | #ifndef __ANDROID_VNDK__ |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 36 | #if defined(__BRILLO__) |
| 37 | // Because Brillo has no application model, security policy is managed |
| 38 | // statically (at build time) with SELinux controls. |
| 39 | // As a consequence, it also never runs the AppOpsManager service. |
| 40 | const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_ALLOWED; |
| 41 | #else |
| 42 | const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_IGNORED; |
| 43 | #endif // defined(__BRILLO__) |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 44 | #endif // __ANDROID_VNDK__ |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 45 | |
| 46 | } // namespace |
| 47 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 48 | static String16 _appops("appops"); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 49 | #ifndef __ANDROID_VNDK__ |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 50 | static pthread_mutex_t gTokenMutex = PTHREAD_MUTEX_INITIALIZER; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 51 | #endif // __ANDROID_VNDK__ |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 52 | static sp<IBinder> gToken; |
| 53 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 54 | #ifndef __ANDROID_VNDK__ |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 55 | static const sp<IBinder>& getToken(const sp<IAppOpsService>& service) { |
| 56 | pthread_mutex_lock(&gTokenMutex); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 57 | if (gToken == nullptr || gToken->pingBinder() != NO_ERROR) { |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 58 | gToken = service->getToken(new BBinder()); |
| 59 | } |
Zhijun He | 20d0380 | 2013-07-22 17:09:35 -0700 | [diff] [blame] | 60 | pthread_mutex_unlock(&gTokenMutex); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 61 | return gToken; |
| 62 | } |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 63 | #endif // __ANDROID_VNDK__ |
| 64 | |
| 65 | thread_local uint64_t notedAppOpsInThisBinderTransaction[2]; |
| 66 | thread_local int32_t uidOfThisBinderTransaction = -1; |
| 67 | |
| 68 | // Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note |
| 69 | #ifndef __ANDROID_VNDK__ |
| 70 | uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0}; |
| 71 | #else |
| 72 | uint8_t appOpsToNote[128] = {0}; |
| 73 | #endif // __ANDROID_VNDK__ |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 74 | |
| 75 | AppOpsManager::AppOpsManager() |
| 76 | { |
| 77 | } |
| 78 | |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 79 | #if defined(__BRILLO__) |
| 80 | // There is no AppOpsService on Brillo |
| 81 | sp<IAppOpsService> AppOpsManager::getService() { return NULL; } |
| 82 | #else |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 83 | sp<IAppOpsService> AppOpsManager::getService() |
| 84 | { |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 85 | |
Christopher Wiley | 5975c00 | 2016-02-12 15:41:08 -0800 | [diff] [blame] | 86 | std::lock_guard<Mutex> scoped_lock(mLock); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 87 | int64_t startTime = 0; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 88 | sp<IAppOpsService> service = mService; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 89 | while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 90 | sp<IBinder> binder = defaultServiceManager()->checkService(_appops); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 91 | if (binder == nullptr) { |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 92 | // Wait for the app ops service to come back... |
| 93 | if (startTime == 0) { |
| 94 | startTime = uptimeMillis(); |
| 95 | ALOGI("Waiting for app ops service"); |
| 96 | } else if ((uptimeMillis()-startTime) > 10000) { |
| 97 | ALOGW("Waiting too long for app ops service, giving up"); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 98 | service = nullptr; |
Christopher Wiley | 6dd4552 | 2016-02-05 09:06:30 -0800 | [diff] [blame] | 99 | break; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 100 | } |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 101 | sleep(1); |
| 102 | } else { |
| 103 | service = interface_cast<IAppOpsService>(binder); |
| 104 | mService = service; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 107 | return service; |
| 108 | } |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 109 | #endif // defined(__BRILLO__) |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 110 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 111 | #ifndef __ANDROID_VNDK__ |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 112 | int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage) |
| 113 | { |
| 114 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 115 | return service != nullptr |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 116 | ? service->checkOperation(op, uid, callingPackage) |
| 117 | : APP_OPS_MANAGER_UNAVAILABLE_MODE; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 120 | int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid, |
| 121 | const String16& callingPackage) { |
| 122 | sp<IAppOpsService> service = getService(); |
| 123 | return service != nullptr |
| 124 | ? service->checkAudioOperation(op, usage, uid, callingPackage) |
| 125 | : APP_OPS_MANAGER_UNAVAILABLE_MODE; |
| 126 | } |
| 127 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 128 | int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) { |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 129 | return noteOp(op, uid, callingPackage, String16("noteOp from native code")); |
| 130 | } |
| 131 | |
| 132 | int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage, |
| 133 | const String16& message) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 134 | sp<IAppOpsService> service = getService(); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 135 | int32_t mode = service != nullptr |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 136 | ? service->noteOperation(op, uid, callingPackage) |
| 137 | : APP_OPS_MANAGER_UNAVAILABLE_MODE; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 138 | |
| 139 | if (mode == AppOpsManager::MODE_ALLOWED) { |
| 140 | markAppOpNoted(uid, callingPackage, op, message); |
| 141 | } |
| 142 | |
| 143 | return mode; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 146 | int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage, |
| 147 | bool startIfModeDefault) { |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 148 | return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, |
| 149 | String16("startOpNoThrow from native code")); |
| 150 | } |
| 151 | |
| 152 | int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage, |
| 153 | bool startIfModeDefault, const String16& message) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 154 | sp<IAppOpsService> service = getService(); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 155 | int32_t mode = service != nullptr |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 156 | ? service->startOperation(getToken(service), op, uid, callingPackage, |
| 157 | startIfModeDefault) : APP_OPS_MANAGER_UNAVAILABLE_MODE; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 158 | |
| 159 | if (mode == AppOpsManager::MODE_ALLOWED) { |
| 160 | markAppOpNoted(uid, callingPackage, op, message); |
| 161 | } |
| 162 | |
| 163 | return mode; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) { |
| 167 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 168 | if (service != nullptr) { |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 169 | service->finishOperation(getToken(service), op, uid, callingPackage); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName, |
| 174 | const sp<IAppOpsCallback>& callback) { |
| 175 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 176 | if (service != nullptr) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 177 | service->startWatchingMode(op, packageName, callback); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) { |
| 182 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 183 | if (service != nullptr) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 184 | service->stopWatchingMode(callback); |
| 185 | } |
| 186 | } |
| 187 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 188 | int32_t AppOpsManager::permissionToOpCode(const String16& permission) { |
| 189 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 190 | if (service != nullptr) { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 191 | return service->permissionToOpCode(permission); |
| 192 | } |
| 193 | return -1; |
| 194 | } |
| 195 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 196 | #endif // __ANDROID_VNDK__ |
| 197 | |
| 198 | bool AppOpsManager::shouldCollectNotes(int32_t opcode) { |
| 199 | sp<IAppOpsService> service = getService(); |
| 200 | if (service != nullptr) { |
| 201 | return service->shouldCollectNotes(opcode); |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | void AppOpsManager::markAppOpNoted(int32_t uid, const String16& packageName, int32_t opCode, |
| 207 | const String16& message) { |
| 208 | // check it the appops needs to be collected and cache result |
| 209 | if (appOpsToNote[opCode] == 0) { |
| 210 | if (shouldCollectNotes(opCode)) { |
| 211 | appOpsToNote[opCode] = 2; |
| 212 | } else { |
| 213 | appOpsToNote[opCode] = 1; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (appOpsToNote[opCode] != 2) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | noteAsyncOp(String16(), uid, packageName, opCode, message); |
| 222 | } |
| 223 | |
| 224 | void AppOpsManager::noteAsyncOp(const String16& callingPackageName, int32_t uid, |
| 225 | const String16& packageName, int32_t opCode, const String16& message) { |
| 226 | sp<IAppOpsService> service = getService(); |
| 227 | if (service != nullptr) { |
| 228 | return service->noteAsyncOp(callingPackageName, uid, packageName, opCode, message); |
| 229 | } |
| 230 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 231 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 232 | }; // namespace android |