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