blob: e2af01c16113a85c23c96f6fabe7451d213a773b [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
Philip P. Moltmann66a87772019-06-24 16:30:00 -070035#ifndef __ANDROID_VNDK__
Christopher Wiley8ed42702016-02-05 09:08:23 -080036#if defined(__BRILLO__)
37// Because Brillo has no application model, security policy is managed
38// statically (at build time) with SELinux controls.
39// As a consequence, it also never runs the AppOpsManager service.
40const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_ALLOWED;
41#else
42const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_IGNORED;
43#endif // defined(__BRILLO__)
Philip P. Moltmann66a87772019-06-24 16:30:00 -070044#endif // __ANDROID_VNDK__
Christopher Wiley8ed42702016-02-05 09:08:23 -080045
46} // namespace
47
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080048static String16 _appops("appops");
Philip P. Moltmann66a87772019-06-24 16:30:00 -070049#ifndef __ANDROID_VNDK__
Dianne Hackborn913b63d2013-07-17 17:26:15 -070050static pthread_mutex_t gTokenMutex = PTHREAD_MUTEX_INITIALIZER;
Philip P. Moltmann66a87772019-06-24 16:30:00 -070051#endif // __ANDROID_VNDK__
Dianne Hackborn913b63d2013-07-17 17:26:15 -070052static sp<IBinder> gToken;
53
Philip P. Moltmann66a87772019-06-24 16:30:00 -070054#ifndef __ANDROID_VNDK__
Dianne Hackborn913b63d2013-07-17 17:26:15 -070055static const sp<IBinder>& getToken(const sp<IAppOpsService>& service) {
56 pthread_mutex_lock(&gTokenMutex);
Yi Kong91635562018-06-07 14:38:36 -070057 if (gToken == nullptr || gToken->pingBinder() != NO_ERROR) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -070058 gToken = service->getToken(new BBinder());
59 }
Zhijun He20d03802013-07-22 17:09:35 -070060 pthread_mutex_unlock(&gTokenMutex);
Dianne Hackborn913b63d2013-07-17 17:26:15 -070061 return gToken;
62}
Philip P. Moltmann66a87772019-06-24 16:30:00 -070063#endif // __ANDROID_VNDK__
64
65thread_local uint64_t notedAppOpsInThisBinderTransaction[2];
66thread_local int32_t uidOfThisBinderTransaction = -1;
67
68// Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
69#ifndef __ANDROID_VNDK__
70uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
71#else
72uint8_t appOpsToNote[128] = {0};
73#endif // __ANDROID_VNDK__
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080074
75AppOpsManager::AppOpsManager()
76{
77}
78
Christopher Wiley8ed42702016-02-05 09:08:23 -080079#if defined(__BRILLO__)
80// There is no AppOpsService on Brillo
81sp<IAppOpsService> AppOpsManager::getService() { return NULL; }
82#else
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080083sp<IAppOpsService> AppOpsManager::getService()
84{
Christopher Wiley8ed42702016-02-05 09:08:23 -080085
Christopher Wiley5975c002016-02-12 15:41:08 -080086 std::lock_guard<Mutex> scoped_lock(mLock);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080087 int64_t startTime = 0;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080088 sp<IAppOpsService> service = mService;
Yi Kong91635562018-06-07 14:38:36 -070089 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080090 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
Yi Kong91635562018-06-07 14:38:36 -070091 if (binder == nullptr) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080092 // Wait for the app ops service to come back...
93 if (startTime == 0) {
94 startTime = uptimeMillis();
95 ALOGI("Waiting for app ops service");
96 } else if ((uptimeMillis()-startTime) > 10000) {
97 ALOGW("Waiting too long for app ops service, giving up");
Yi Kong91635562018-06-07 14:38:36 -070098 service = nullptr;
Christopher Wiley6dd45522016-02-05 09:06:30 -080099 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800100 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -0800101 sleep(1);
102 } else {
103 service = interface_cast<IAppOpsService>(binder);
104 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800105 }
106 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800107 return service;
108}
Christopher Wiley8ed42702016-02-05 09:08:23 -0800109#endif // defined(__BRILLO__)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800110
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700111#ifndef __ANDROID_VNDK__
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800112int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
113{
114 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700115 return service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -0800116 ? service->checkOperation(op, uid, callingPackage)
117 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800118}
119
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800120int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid,
121 const String16& callingPackage) {
122 sp<IAppOpsService> service = getService();
123 return service != nullptr
124 ? service->checkAudioOperation(op, usage, uid, callingPackage)
125 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
126}
127
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800128int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700129 return noteOp(op, uid, callingPackage, String16("noteOp from native code"));
130}
131
132int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
133 const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800134 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700135 int32_t mode = service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -0800136 ? service->noteOperation(op, uid, callingPackage)
137 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700138
139 if (mode == AppOpsManager::MODE_ALLOWED) {
140 markAppOpNoted(uid, callingPackage, op, message);
141 }
142
143 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800144}
145
Svet Ganov616554c2018-02-26 13:27:26 -0800146int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
147 bool startIfModeDefault) {
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700148 return startOpNoThrow(op, uid, callingPackage, startIfModeDefault,
149 String16("startOpNoThrow from native code"));
150}
151
152int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
153 bool startIfModeDefault, const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800154 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700155 int32_t mode = service != nullptr
Svet Ganov616554c2018-02-26 13:27:26 -0800156 ? service->startOperation(getToken(service), op, uid, callingPackage,
157 startIfModeDefault) : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700158
159 if (mode == AppOpsManager::MODE_ALLOWED) {
160 markAppOpNoted(uid, callingPackage, op, message);
161 }
162
163 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800164}
165
166void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
167 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700168 if (service != nullptr) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700169 service->finishOperation(getToken(service), op, uid, callingPackage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800170 }
171}
172
173void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
174 const sp<IAppOpsCallback>& callback) {
175 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700176 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800177 service->startWatchingMode(op, packageName, callback);
178 }
179}
180
181void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
182 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700183 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800184 service->stopWatchingMode(callback);
185 }
186}
187
Svetoslavb412f6e2015-04-29 16:50:41 -0700188int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
189 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700190 if (service != nullptr) {
Svetoslavb412f6e2015-04-29 16:50:41 -0700191 return service->permissionToOpCode(permission);
192 }
193 return -1;
194}
195
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700196void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
197 sp<IAppOpsService> service = getService();
198 if (service != nullptr) {
199 service->setCameraAudioRestriction(mode);
200 }
201}
202
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700203#endif // __ANDROID_VNDK__
204
205bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
206 sp<IAppOpsService> service = getService();
207 if (service != nullptr) {
208 return service->shouldCollectNotes(opcode);
209 }
210 return false;
211}
212
213void AppOpsManager::markAppOpNoted(int32_t uid, const String16& packageName, int32_t opCode,
214 const String16& message) {
215 // check it the appops needs to be collected and cache result
216 if (appOpsToNote[opCode] == 0) {
217 if (shouldCollectNotes(opCode)) {
218 appOpsToNote[opCode] = 2;
219 } else {
220 appOpsToNote[opCode] = 1;
221 }
222 }
223
224 if (appOpsToNote[opCode] != 2) {
225 return;
226 }
227
228 noteAsyncOp(String16(), uid, packageName, opCode, message);
229}
230
231void AppOpsManager::noteAsyncOp(const String16& callingPackageName, int32_t uid,
232 const String16& packageName, int32_t opCode, const String16& message) {
233 sp<IAppOpsService> service = getService();
234 if (service != nullptr) {
235 return service->noteAsyncOp(callingPackageName, uid, packageName, opCode, message);
236 }
237}
Svetoslavb412f6e2015-04-29 16:50:41 -0700238
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800239}; // namespace android