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 | |
Philip P. Moltmann | c52e1fc | 2019-11-26 15:18:09 -0800 | [diff] [blame] | 33 | static const sp<IBinder>& getClientId() { |
Steven Moreland | 1e219c1 | 2020-02-27 16:27:49 -0800 | [diff] [blame] | 34 | static pthread_mutex_t gClientIdMutex = PTHREAD_MUTEX_INITIALIZER; |
| 35 | static sp<IBinder> gClientId; |
| 36 | |
Philip P. Moltmann | c52e1fc | 2019-11-26 15:18:09 -0800 | [diff] [blame] | 37 | pthread_mutex_lock(&gClientIdMutex); |
| 38 | if (gClientId == nullptr) { |
| 39 | gClientId = new BBinder(); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 40 | } |
Philip P. Moltmann | c52e1fc | 2019-11-26 15:18:09 -0800 | [diff] [blame] | 41 | pthread_mutex_unlock(&gClientIdMutex); |
| 42 | return gClientId; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 43 | } |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 44 | |
| 45 | thread_local uint64_t notedAppOpsInThisBinderTransaction[2]; |
| 46 | thread_local int32_t uidOfThisBinderTransaction = -1; |
| 47 | |
| 48 | // 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] | 49 | uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0}; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 50 | |
| 51 | AppOpsManager::AppOpsManager() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | sp<IAppOpsService> AppOpsManager::getService() |
| 56 | { |
Steven Moreland | 1e219c1 | 2020-02-27 16:27:49 -0800 | [diff] [blame] | 57 | static String16 _appops("appops"); |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 58 | |
Christopher Wiley | 5975c00 | 2016-02-12 15:41:08 -0800 | [diff] [blame] | 59 | std::lock_guard<Mutex> scoped_lock(mLock); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 60 | int64_t startTime = 0; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 61 | sp<IAppOpsService> service = mService; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 62 | while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 63 | sp<IBinder> binder = defaultServiceManager()->checkService(_appops); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 64 | if (binder == nullptr) { |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 65 | // Wait for the app ops service to come back... |
| 66 | if (startTime == 0) { |
| 67 | startTime = uptimeMillis(); |
| 68 | ALOGI("Waiting for app ops service"); |
| 69 | } else if ((uptimeMillis()-startTime) > 10000) { |
| 70 | ALOGW("Waiting too long for app ops service, giving up"); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 71 | service = nullptr; |
Christopher Wiley | 6dd4552 | 2016-02-05 09:06:30 -0800 | [diff] [blame] | 72 | break; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 73 | } |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 74 | sleep(1); |
| 75 | } else { |
| 76 | service = interface_cast<IAppOpsService>(binder); |
| 77 | mService = service; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 80 | return service; |
| 81 | } |
| 82 | |
| 83 | int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage) |
| 84 | { |
| 85 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 86 | return service != nullptr |
Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 87 | ? service->checkOperation(op, uid, callingPackage) |
Steven Moreland | 15a63a8 | 2020-02-27 16:31:13 -0800 | [diff] [blame^] | 88 | : AppOpsManager::MODE_IGNORED; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 91 | int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid, |
| 92 | const String16& callingPackage) { |
| 93 | sp<IAppOpsService> service = getService(); |
| 94 | return service != nullptr |
| 95 | ? service->checkAudioOperation(op, usage, uid, callingPackage) |
Steven Moreland | 15a63a8 | 2020-02-27 16:31:13 -0800 | [diff] [blame^] | 96 | : AppOpsManager::MODE_IGNORED; |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 99 | 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] | 100 | return noteOp(op, uid, callingPackage, std::unique_ptr<String16>(), |
| 101 | String16("Legacy AppOpsManager.noteOp call")); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | 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] | 105 | const std::unique_ptr<String16>& featureId, const String16& message) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 106 | sp<IAppOpsService> service = getService(); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 107 | int32_t mode = service != nullptr |
Philip P. Moltmann | 3879cf6 | 2019-12-20 11:22:37 -0800 | [diff] [blame] | 108 | ? service->noteOperation(op, uid, callingPackage, featureId, shouldCollectNotes(op), |
| 109 | message) |
Steven Moreland | 15a63a8 | 2020-02-27 16:31:13 -0800 | [diff] [blame^] | 110 | : AppOpsManager::MODE_IGNORED; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 111 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 112 | return mode; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 115 | int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage, |
| 116 | bool startIfModeDefault) { |
Philip P. Moltmann | aeaaf1c | 2019-11-05 12:34:36 -0800 | [diff] [blame] | 117 | return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, std::unique_ptr<String16>(), |
| 118 | String16("Legacy AppOpsManager.startOpNoThrow call")); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | 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] | 122 | bool startIfModeDefault, const std::unique_ptr<String16>& featureId, |
| 123 | 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 | c52e1fc | 2019-11-26 15:18:09 -0800 | [diff] [blame] | 126 | ? service->startOperation(getClientId(), op, uid, callingPackage, |
Philip P. Moltmann | 3879cf6 | 2019-12-20 11:22:37 -0800 | [diff] [blame] | 127 | featureId, startIfModeDefault, shouldCollectNotes(op), message) |
Steven Moreland | 15a63a8 | 2020-02-27 16:31:13 -0800 | [diff] [blame^] | 128 | : AppOpsManager::MODE_IGNORED; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 129 | |
| 130 | return mode; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | 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] | 134 | finishOp(op, uid, callingPackage, std::unique_ptr<String16>()); |
| 135 | } |
| 136 | |
| 137 | void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage, |
| 138 | const std::unique_ptr<String16>& callingFeatureId) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 139 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 140 | if (service != nullptr) { |
Philip P. Moltmann | c52e1fc | 2019-11-26 15:18:09 -0800 | [diff] [blame] | 141 | service->finishOperation(getClientId(), op, uid, callingPackage, callingFeatureId); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName, |
| 146 | const sp<IAppOpsCallback>& callback) { |
| 147 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 148 | if (service != nullptr) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 149 | service->startWatchingMode(op, packageName, callback); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) { |
| 154 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 155 | if (service != nullptr) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 156 | service->stopWatchingMode(callback); |
| 157 | } |
| 158 | } |
| 159 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 160 | int32_t AppOpsManager::permissionToOpCode(const String16& permission) { |
| 161 | sp<IAppOpsService> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 162 | if (service != nullptr) { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 163 | return service->permissionToOpCode(permission); |
| 164 | } |
| 165 | return -1; |
| 166 | } |
| 167 | |
Yin-Chia Yeh | 8e95ee8 | 2019-08-13 12:24:25 -0700 | [diff] [blame] | 168 | void AppOpsManager::setCameraAudioRestriction(int32_t mode) { |
| 169 | sp<IAppOpsService> service = getService(); |
| 170 | if (service != nullptr) { |
| 171 | service->setCameraAudioRestriction(mode); |
| 172 | } |
| 173 | } |
| 174 | |
Philip P. Moltmann | 3879cf6 | 2019-12-20 11:22:37 -0800 | [diff] [blame] | 175 | // check it the appops needs to be collected and cache result |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 176 | bool AppOpsManager::shouldCollectNotes(int32_t opcode) { |
Philip P. Moltmann | 3879cf6 | 2019-12-20 11:22:37 -0800 | [diff] [blame] | 177 | if (appOpsToNote[opcode] == 0) { |
| 178 | if (getService()->shouldCollectNotes(opcode)) { |
| 179 | appOpsToNote[opcode] = 2; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 180 | } else { |
Philip P. Moltmann | 3879cf6 | 2019-12-20 11:22:37 -0800 | [diff] [blame] | 181 | appOpsToNote[opcode] = 1; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Philip P. Moltmann | 3879cf6 | 2019-12-20 11:22:37 -0800 | [diff] [blame] | 185 | return appOpsToNote[opcode] == 2; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 186 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 187 | |
Steven Moreland | 6511af5 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 188 | } // namespace android |