| 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 |  | 
|  | 24 | namespace android { | 
|  | 25 |  | 
| Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 26 | namespace { | 
|  | 27 |  | 
|  | 28 | #if defined(__BRILLO__) | 
|  | 29 | // Because Brillo has no application model, security policy is managed | 
|  | 30 | // statically (at build time) with SELinux controls. | 
|  | 31 | // As a consequence, it also never runs the AppOpsManager service. | 
|  | 32 | const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_ALLOWED; | 
|  | 33 | #else | 
|  | 34 | const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_IGNORED; | 
|  | 35 | #endif  // defined(__BRILLO__) | 
|  | 36 |  | 
|  | 37 | }  // namespace | 
|  | 38 |  | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 39 | static String16 _appops("appops"); | 
| Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 40 | static pthread_mutex_t gTokenMutex = PTHREAD_MUTEX_INITIALIZER; | 
|  | 41 | static sp<IBinder> gToken; | 
|  | 42 |  | 
|  | 43 | static const sp<IBinder>& getToken(const sp<IAppOpsService>& service) { | 
|  | 44 | pthread_mutex_lock(&gTokenMutex); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 45 | if (gToken == nullptr || gToken->pingBinder() != NO_ERROR) { | 
| Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 46 | gToken = service->getToken(new BBinder()); | 
|  | 47 | } | 
| Zhijun He | 20d0380 | 2013-07-22 17:09:35 -0700 | [diff] [blame] | 48 | pthread_mutex_unlock(&gTokenMutex); | 
| Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 49 | return gToken; | 
|  | 50 | } | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 51 |  | 
|  | 52 | AppOpsManager::AppOpsManager() | 
|  | 53 | { | 
|  | 54 | } | 
|  | 55 |  | 
| Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 56 | #if defined(__BRILLO__) | 
|  | 57 | // There is no AppOpsService on Brillo | 
|  | 58 | sp<IAppOpsService> AppOpsManager::getService() { return NULL; } | 
|  | 59 | #else | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 60 | sp<IAppOpsService> AppOpsManager::getService() | 
|  | 61 | { | 
| Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 62 |  | 
| Christopher Wiley | 5975c00 | 2016-02-12 15:41:08 -0800 | [diff] [blame] | 63 | std::lock_guard<Mutex> scoped_lock(mLock); | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 64 | int64_t startTime = 0; | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 65 | sp<IAppOpsService> service = mService; | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 66 | while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { | 
| Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 67 | sp<IBinder> binder = defaultServiceManager()->checkService(_appops); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 68 | if (binder == nullptr) { | 
| Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 69 | // Wait for the app ops service to come back... | 
|  | 70 | if (startTime == 0) { | 
|  | 71 | startTime = uptimeMillis(); | 
|  | 72 | ALOGI("Waiting for app ops service"); | 
|  | 73 | } else if ((uptimeMillis()-startTime) > 10000) { | 
|  | 74 | ALOGW("Waiting too long for app ops service, giving up"); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 75 | service = nullptr; | 
| Christopher Wiley | 6dd4552 | 2016-02-05 09:06:30 -0800 | [diff] [blame] | 76 | break; | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 77 | } | 
| Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 78 | sleep(1); | 
|  | 79 | } else { | 
|  | 80 | service = interface_cast<IAppOpsService>(binder); | 
|  | 81 | mService = service; | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 82 | } | 
|  | 83 | } | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 84 | return service; | 
|  | 85 | } | 
| Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 86 | #endif  // defined(__BRILLO__) | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 87 |  | 
|  | 88 | int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage) | 
|  | 89 | { | 
|  | 90 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 91 | return service != nullptr | 
| Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 92 | ? service->checkOperation(op, uid, callingPackage) | 
|  | 93 | : APP_OPS_MANAGER_UNAVAILABLE_MODE; | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 96 | int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid, | 
|  | 97 | const String16& callingPackage) { | 
|  | 98 | sp<IAppOpsService> service = getService(); | 
|  | 99 | return service != nullptr | 
|  | 100 | ? service->checkAudioOperation(op, usage, uid, callingPackage) | 
|  | 101 | : APP_OPS_MANAGER_UNAVAILABLE_MODE; | 
|  | 102 | } | 
|  | 103 |  | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 104 | int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) { | 
|  | 105 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 106 | return service != nullptr | 
| Christopher Wiley | 8ed4270 | 2016-02-05 09:08:23 -0800 | [diff] [blame] | 107 | ? service->noteOperation(op, uid, callingPackage) | 
|  | 108 | : APP_OPS_MANAGER_UNAVAILABLE_MODE; | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
| Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 111 | int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage, | 
|  | 112 | bool startIfModeDefault) { | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 113 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 114 | return service != nullptr | 
| Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 115 | ? service->startOperation(getToken(service), op, uid, callingPackage, | 
|  | 116 | startIfModeDefault) : APP_OPS_MANAGER_UNAVAILABLE_MODE; | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) { | 
|  | 120 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 121 | if (service != nullptr) { | 
| Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 122 | service->finishOperation(getToken(service), op, uid, callingPackage); | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 123 | } | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName, | 
|  | 127 | const sp<IAppOpsCallback>& callback) { | 
|  | 128 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 129 | if (service != nullptr) { | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 130 | service->startWatchingMode(op, packageName, callback); | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) { | 
|  | 135 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 136 | if (service != nullptr) { | 
| Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 137 | service->stopWatchingMode(callback); | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 141 | int32_t AppOpsManager::permissionToOpCode(const String16& permission) { | 
|  | 142 | sp<IAppOpsService> service = getService(); | 
| Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 143 | if (service != nullptr) { | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 144 | return service->permissionToOpCode(permission); | 
|  | 145 | } | 
|  | 146 | return -1; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 150 | } // namespace android |