blob: 0bba6ca19b4f381b9f5ff0a00e50a550aaf956be [file] [log] [blame]
Dianne Hackborn5da5ca52013-02-12 15:12:21 -08001/*
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 Wiley5975c002016-02-12 15:41:08 -080017#include <mutex>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080018#include <binder/AppOpsManager.h>
Dianne Hackborn913b63d2013-07-17 17:26:15 -070019#include <binder/Binder.h>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080020#include <binder/IServiceManager.h>
21
22#include <utils/SystemClock.h>
23
Philip P. Moltmann66a87772019-06-24 16:30:00 -070024#include <sys/types.h>
25
26#ifdef LOG_TAG
27#undef LOG_TAG
28#endif
29#define LOG_TAG "AppOpsManager"
30
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080031namespace android {
32
Christopher Wiley8ed42702016-02-05 09:08:23 -080033namespace {
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.
39const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_ALLOWED;
40#else
41const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_IGNORED;
42#endif // defined(__BRILLO__)
43
44} // namespace
45
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080046static String16 _appops("appops");
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080047static pthread_mutex_t gClientIdMutex = PTHREAD_MUTEX_INITIALIZER;
48static sp<IBinder> gClientId;
Dianne Hackborn913b63d2013-07-17 17:26:15 -070049
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080050static const sp<IBinder>& getClientId() {
51 pthread_mutex_lock(&gClientIdMutex);
52 if (gClientId == nullptr) {
53 gClientId = new BBinder();
Dianne Hackborn913b63d2013-07-17 17:26:15 -070054 }
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080055 pthread_mutex_unlock(&gClientIdMutex);
56 return gClientId;
Dianne Hackborn913b63d2013-07-17 17:26:15 -070057}
Philip P. Moltmann66a87772019-06-24 16:30:00 -070058
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080059AppOpsManager::AppOpsManager()
60{
61}
62
Christopher Wiley8ed42702016-02-05 09:08:23 -080063#if defined(__BRILLO__)
64// There is no AppOpsService on Brillo
65sp<IAppOpsService> AppOpsManager::getService() { return NULL; }
66#else
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080067sp<IAppOpsService> AppOpsManager::getService()
68{
Christopher Wiley8ed42702016-02-05 09:08:23 -080069
Christopher Wiley5975c002016-02-12 15:41:08 -080070 std::lock_guard<Mutex> scoped_lock(mLock);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080071 int64_t startTime = 0;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080072 sp<IAppOpsService> service = mService;
Yi Kong91635562018-06-07 14:38:36 -070073 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080074 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
Yi Kong91635562018-06-07 14:38:36 -070075 if (binder == nullptr) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080076 // Wait for the app ops service to come back...
77 if (startTime == 0) {
78 startTime = uptimeMillis();
79 ALOGI("Waiting for app ops service");
80 } else if ((uptimeMillis()-startTime) > 10000) {
81 ALOGW("Waiting too long for app ops service, giving up");
Yi Kong91635562018-06-07 14:38:36 -070082 service = nullptr;
Christopher Wiley6dd45522016-02-05 09:06:30 -080083 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080084 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080085 sleep(1);
86 } else {
87 service = interface_cast<IAppOpsService>(binder);
88 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080089 }
90 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080091 return service;
92}
Christopher Wiley8ed42702016-02-05 09:08:23 -080093#endif // defined(__BRILLO__)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080094
95int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
96{
97 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -070098 return service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -080099 ? service->checkOperation(op, uid, callingPackage)
100 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800101}
102
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800103int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid,
104 const String16& callingPackage) {
105 sp<IAppOpsService> service = getService();
106 return service != nullptr
107 ? service->checkAudioOperation(op, usage, uid, callingPackage)
108 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
109}
110
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800111int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800112 return noteOp(op, uid, callingPackage, std::unique_ptr<String16>(),
113 String16("Legacy AppOpsManager.noteOp call"));
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700114}
115
116int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800117 const std::unique_ptr<String16>& featureId, const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800118 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700119 int32_t mode = service != nullptr
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800120 ? service->noteOperation(op, uid, callingPackage, featureId, shouldCollectNotes(op),
121 message)
Christopher Wiley8ed42702016-02-05 09:08:23 -0800122 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700123
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700124 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800125}
126
Svet Ganov616554c2018-02-26 13:27:26 -0800127int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
128 bool startIfModeDefault) {
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800129 return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, std::unique_ptr<String16>(),
130 String16("Legacy AppOpsManager.startOpNoThrow call"));
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700131}
132
133int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800134 bool startIfModeDefault, const std::unique_ptr<String16>& featureId,
135 const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800136 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700137 int32_t mode = service != nullptr
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -0800138 ? service->startOperation(getClientId(), op, uid, callingPackage,
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800139 featureId, startIfModeDefault, shouldCollectNotes(op), message)
140 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700141
142 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800143}
144
145void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800146 finishOp(op, uid, callingPackage, std::unique_ptr<String16>());
147}
148
149void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage,
150 const std::unique_ptr<String16>& callingFeatureId) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800151 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700152 if (service != nullptr) {
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -0800153 service->finishOperation(getClientId(), op, uid, callingPackage, callingFeatureId);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800154 }
155}
156
157void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
158 const sp<IAppOpsCallback>& callback) {
159 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700160 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800161 service->startWatchingMode(op, packageName, callback);
162 }
163}
164
165void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
166 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700167 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800168 service->stopWatchingMode(callback);
169 }
170}
171
Svetoslavb412f6e2015-04-29 16:50:41 -0700172int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
173 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700174 if (service != nullptr) {
Svetoslavb412f6e2015-04-29 16:50:41 -0700175 return service->permissionToOpCode(permission);
176 }
177 return -1;
178}
179
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700180void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
181 sp<IAppOpsService> service = getService();
182 if (service != nullptr) {
183 service->setCameraAudioRestriction(mode);
184 }
185}
186
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800187// check it the appops needs to be collected and cache result
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700188bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
Steven Morelandd83ecb02020-03-04 18:02:02 -0800189 // Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
190 static uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
191
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800192 if (appOpsToNote[opcode] == 0) {
193 if (getService()->shouldCollectNotes(opcode)) {
194 appOpsToNote[opcode] = 2;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700195 } else {
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800196 appOpsToNote[opcode] = 1;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700197 }
198 }
199
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800200 return appOpsToNote[opcode] == 2;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700201}
Svetoslavb412f6e2015-04-29 16:50:41 -0700202
Steven Moreland6511af52019-09-26 16:05:45 -0700203} // namespace android