blob: 9da9c13e54e250c7949daebe7de49c2cba03308d [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");
Dianne Hackborn913b63d2013-07-17 17:26:15 -070047static pthread_mutex_t gTokenMutex = PTHREAD_MUTEX_INITIALIZER;
48static sp<IBinder> gToken;
49
50static const sp<IBinder>& getToken(const sp<IAppOpsService>& service) {
51 pthread_mutex_lock(&gTokenMutex);
Yi Kong91635562018-06-07 14:38:36 -070052 if (gToken == nullptr || gToken->pingBinder() != NO_ERROR) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -070053 gToken = service->getToken(new BBinder());
54 }
Zhijun He20d03802013-07-22 17:09:35 -070055 pthread_mutex_unlock(&gTokenMutex);
Dianne Hackborn913b63d2013-07-17 17:26:15 -070056 return gToken;
57}
Philip P. Moltmann66a87772019-06-24 16:30:00 -070058
59thread_local uint64_t notedAppOpsInThisBinderTransaction[2];
60thread_local int32_t uidOfThisBinderTransaction = -1;
61
62// Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
Philip P. Moltmann66a87772019-06-24 16:30:00 -070063uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080064
65AppOpsManager::AppOpsManager()
66{
67}
68
Christopher Wiley8ed42702016-02-05 09:08:23 -080069#if defined(__BRILLO__)
70// There is no AppOpsService on Brillo
71sp<IAppOpsService> AppOpsManager::getService() { return NULL; }
72#else
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080073sp<IAppOpsService> AppOpsManager::getService()
74{
Christopher Wiley8ed42702016-02-05 09:08:23 -080075
Christopher Wiley5975c002016-02-12 15:41:08 -080076 std::lock_guard<Mutex> scoped_lock(mLock);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080077 int64_t startTime = 0;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080078 sp<IAppOpsService> service = mService;
Yi Kong91635562018-06-07 14:38:36 -070079 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080080 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
Yi Kong91635562018-06-07 14:38:36 -070081 if (binder == nullptr) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080082 // 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 Kong91635562018-06-07 14:38:36 -070088 service = nullptr;
Christopher Wiley6dd45522016-02-05 09:06:30 -080089 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080090 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080091 sleep(1);
92 } else {
93 service = interface_cast<IAppOpsService>(binder);
94 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080095 }
96 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080097 return service;
98}
Christopher Wiley8ed42702016-02-05 09:08:23 -080099#endif // defined(__BRILLO__)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800100
101int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
102{
103 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700104 return service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -0800105 ? service->checkOperation(op, uid, callingPackage)
106 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800107}
108
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800109int32_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 Hackborn5da5ca52013-02-12 15:12:21 -0800117int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700118 return noteOp(op, uid, callingPackage, String16("noteOp from native code"));
119}
120
121int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
122 const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800123 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700124 int32_t mode = service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -0800125 ? service->noteOperation(op, uid, callingPackage)
126 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700127
128 if (mode == AppOpsManager::MODE_ALLOWED) {
129 markAppOpNoted(uid, callingPackage, op, message);
130 }
131
132 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800133}
134
Svet Ganov616554c2018-02-26 13:27:26 -0800135int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
136 bool startIfModeDefault) {
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700137 return startOpNoThrow(op, uid, callingPackage, startIfModeDefault,
138 String16("startOpNoThrow from native code"));
139}
140
141int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
142 bool startIfModeDefault, const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800143 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700144 int32_t mode = service != nullptr
Svet Ganov616554c2018-02-26 13:27:26 -0800145 ? service->startOperation(getToken(service), op, uid, callingPackage,
146 startIfModeDefault) : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700147
148 if (mode == AppOpsManager::MODE_ALLOWED) {
149 markAppOpNoted(uid, callingPackage, op, message);
150 }
151
152 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800153}
154
155void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
156 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700157 if (service != nullptr) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700158 service->finishOperation(getToken(service), op, uid, callingPackage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800159 }
160}
161
162void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
163 const sp<IAppOpsCallback>& callback) {
164 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700165 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800166 service->startWatchingMode(op, packageName, callback);
167 }
168}
169
170void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
171 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700172 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800173 service->stopWatchingMode(callback);
174 }
175}
176
Svetoslavb412f6e2015-04-29 16:50:41 -0700177int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
178 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700179 if (service != nullptr) {
Svetoslavb412f6e2015-04-29 16:50:41 -0700180 return service->permissionToOpCode(permission);
181 }
182 return -1;
183}
184
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700185void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
186 sp<IAppOpsService> service = getService();
187 if (service != nullptr) {
188 service->setCameraAudioRestriction(mode);
189 }
190}
191
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700192bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
193 sp<IAppOpsService> service = getService();
194 if (service != nullptr) {
195 return service->shouldCollectNotes(opcode);
196 }
197 return false;
198}
199
200void AppOpsManager::markAppOpNoted(int32_t uid, const String16& packageName, int32_t opCode,
201 const String16& message) {
202 // check it the appops needs to be collected and cache result
203 if (appOpsToNote[opCode] == 0) {
204 if (shouldCollectNotes(opCode)) {
205 appOpsToNote[opCode] = 2;
206 } else {
207 appOpsToNote[opCode] = 1;
208 }
209 }
210
211 if (appOpsToNote[opCode] != 2) {
212 return;
213 }
214
215 noteAsyncOp(String16(), uid, packageName, opCode, message);
216}
217
218void AppOpsManager::noteAsyncOp(const String16& callingPackageName, int32_t uid,
219 const String16& packageName, int32_t opCode, const String16& message) {
220 sp<IAppOpsService> service = getService();
221 if (service != nullptr) {
222 return service->noteAsyncOp(callingPackageName, uid, packageName, opCode, message);
223 }
224}
Svetoslavb412f6e2015-04-29 16:50:41 -0700225
Steven Moreland6511af52019-09-26 16:05:45 -0700226} // namespace android