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