blob: b407d02087e949ed52d0b7346a83c23006290690 [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>
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010025#include <private/android_filesystem_config.h>
Philip P. Moltmann66a87772019-06-24 16:30:00 -070026
27#ifdef LOG_TAG
28#undef LOG_TAG
29#endif
30#define LOG_TAG "AppOpsManager"
31
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080032namespace android {
33
Karishma Vakil14f7ae82023-08-21 17:40:30 +000034using ::android::String16;
35using ::android::String8;
36
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080037static const sp<IBinder>& getClientId() {
Steven Moreland1e219c12020-02-27 16:27:49 -080038 static pthread_mutex_t gClientIdMutex = PTHREAD_MUTEX_INITIALIZER;
39 static sp<IBinder> gClientId;
40
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080041 pthread_mutex_lock(&gClientIdMutex);
42 if (gClientId == nullptr) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +000043 gClientId = sp<BBinder>::make();
Dianne Hackborn913b63d2013-07-17 17:26:15 -070044 }
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080045 pthread_mutex_unlock(&gClientIdMutex);
46 return gClientId;
Dianne Hackborn913b63d2013-07-17 17:26:15 -070047}
Philip P. Moltmann66a87772019-06-24 16:30:00 -070048
Karishma Vakil14f7ae82023-08-21 17:40:30 +000049
50static std::string getString(const String16& stringToConvert) {
51 return std::string(String8(stringToConvert).c_str());
52}
53
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080054AppOpsManager::AppOpsManager()
55{
56}
57
58sp<IAppOpsService> AppOpsManager::getService()
59{
Steven Moreland1e219c12020-02-27 16:27:49 -080060 static String16 _appops("appops");
Christopher Wiley8ed42702016-02-05 09:08:23 -080061
Christopher Wiley5975c002016-02-12 15:41:08 -080062 std::lock_guard<Mutex> scoped_lock(mLock);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080063 int64_t startTime = 0;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080064 sp<IAppOpsService> service = mService;
Yi Kong91635562018-06-07 14:38:36 -070065 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080066 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
Yi Kong91635562018-06-07 14:38:36 -070067 if (binder == nullptr) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080068 // Wait for the app ops service to come back...
69 if (startTime == 0) {
70 startTime = uptimeMillis();
71 ALOGI("Waiting for app ops service");
72 } else if ((uptimeMillis()-startTime) > 10000) {
73 ALOGW("Waiting too long for app ops service, giving up");
Yi Kong91635562018-06-07 14:38:36 -070074 service = nullptr;
Christopher Wiley6dd45522016-02-05 09:06:30 -080075 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080076 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080077 sleep(1);
78 } else {
79 service = interface_cast<IAppOpsService>(binder);
80 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080081 }
82 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080083 return service;
84}
85
86int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
87{
88 sp<IAppOpsService> service = getService();
Karishma Vakil14f7ae82023-08-21 17:40:30 +000089 if (service == nullptr) {
90 return AppOpsManager::MODE_IGNORED;
91 }
92 AttributionSourceState attributionSourceState;
93 attributionSourceState.uid = uid;
94 attributionSourceState.packageName = getString(callingPackage);
95
96 return service->checkOperationWithState(op, attributionSourceState);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080097}
98
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -080099int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid,
100 const String16& callingPackage) {
101 sp<IAppOpsService> service = getService();
102 return service != nullptr
103 ? service->checkAudioOperation(op, usage, uid, callingPackage)
Steven Moreland15a63a82020-02-27 16:31:13 -0800104 : AppOpsManager::MODE_IGNORED;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800105}
106
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800107int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
Jooyung Han2d5878e2020-01-23 12:45:10 +0900108 return noteOp(op, uid, callingPackage, {},
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800109 String16("Legacy AppOpsManager.noteOp call"));
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700110}
111
112int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800113 const std::optional<String16>& attributionTag, const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800114 sp<IAppOpsService> service = getService();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000115 if (service == nullptr) {
116 return AppOpsManager::MODE_IGNORED;
117 }
118 AttributionSourceState attributionSourceState;
119 attributionSourceState.uid = uid;
120 attributionSourceState.packageName = getString(callingPackage);
121 if (attributionTag.has_value()) {
122 attributionSourceState.attributionTag = getString(attributionTag.value());
123 }
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700124
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000125 return service->noteOperationWithState(op, attributionSourceState,
126 shouldCollectNotes(op), message, uid == AID_SYSTEM);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800127}
128
Svet Ganov616554c2018-02-26 13:27:26 -0800129int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
130 bool startIfModeDefault) {
Jooyung Han2d5878e2020-01-23 12:45:10 +0900131 return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, {},
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800132 String16("Legacy AppOpsManager.startOpNoThrow call"));
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700133}
134
135int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800136 bool startIfModeDefault, const std::optional<String16>& attributionTag,
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800137 const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800138 sp<IAppOpsService> service = getService();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000139 if (service == nullptr) {
140 return AppOpsManager::MODE_IGNORED;
141 }
142 AttributionSourceState attributionSourceState;
143 attributionSourceState.uid = uid;
144 attributionSourceState.packageName = getString(callingPackage);
145 if (attributionTag.has_value()) {
146 attributionSourceState.attributionTag = getString(attributionTag.value());
147 }
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700148
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000149 return service->startOperationWithState(getClientId(), op, attributionSourceState,
150 startIfModeDefault,shouldCollectNotes(op), message, uid == AID_SYSTEM);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800151}
152
153void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
Jooyung Han2d5878e2020-01-23 12:45:10 +0900154 finishOp(op, uid, callingPackage, {});
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800155}
156
157void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800158 const std::optional<String16>& attributionTag) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800159 sp<IAppOpsService> service = getService();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000160 if (service == nullptr) {
161 return;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800162 }
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000163 AttributionSourceState attributionSourceState;
164 attributionSourceState.uid = uid;
165 attributionSourceState.packageName = getString(callingPackage);
166 if (attributionTag.has_value()) {
167 attributionSourceState.attributionTag = getString(attributionTag.value());
168 }
169 service->finishOperationWithState(getClientId(), op, attributionSourceState);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800170}
171
172void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
173 const sp<IAppOpsCallback>& callback) {
174 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700175 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800176 service->startWatchingMode(op, packageName, callback);
177 }
178}
179
Evan Severson949cb3d2023-04-04 14:46:06 -0700180void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName, int32_t flags,
181 const sp<IAppOpsCallback>& callback) {
182 sp<IAppOpsService> service = getService();
183 if (service != nullptr) {
184 service->startWatchingModeWithFlags(op, packageName, flags, callback);
185 }
186}
187
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800188void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
189 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700190 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800191 service->stopWatchingMode(callback);
192 }
193}
194
Svetoslavb412f6e2015-04-29 16:50:41 -0700195int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
196 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700197 if (service != nullptr) {
Svetoslavb412f6e2015-04-29 16:50:41 -0700198 return service->permissionToOpCode(permission);
199 }
200 return -1;
201}
202
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700203void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
204 sp<IAppOpsService> service = getService();
205 if (service != nullptr) {
206 service->setCameraAudioRestriction(mode);
207 }
208}
209
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800210// check it the appops needs to be collected and cache result
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700211bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
Steven Morelandd83ecb02020-03-04 18:02:02 -0800212 // Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
213 static uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
214
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800215 if (appOpsToNote[opcode] == 0) {
216 if (getService()->shouldCollectNotes(opcode)) {
217 appOpsToNote[opcode] = 2;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700218 } else {
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800219 appOpsToNote[opcode] = 1;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700220 }
221 }
222
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800223 return appOpsToNote[opcode] == 2;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700224}
Svetoslavb412f6e2015-04-29 16:50:41 -0700225
Steven Moreland6511af52019-09-26 16:05:45 -0700226} // namespace android