• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <mutex>
18 #include <binder/AppOpsManager.h>
19 #include <binder/Binder.h>
20 #include <binder/IServiceManager.h>
21 
22 #include <utils/SystemClock.h>
23 
24 #include <sys/types.h>
25 #include <private/android_filesystem_config.h>
26 
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30 #define LOG_TAG "AppOpsManager"
31 
32 namespace android {
33 
getClientId()34 static const sp<IBinder>& getClientId() {
35     static pthread_mutex_t gClientIdMutex = PTHREAD_MUTEX_INITIALIZER;
36     static sp<IBinder> gClientId;
37 
38     pthread_mutex_lock(&gClientIdMutex);
39     if (gClientId == nullptr) {
40         gClientId = sp<BBinder>::make();
41     }
42     pthread_mutex_unlock(&gClientIdMutex);
43     return gClientId;
44 }
45 
AppOpsManager()46 AppOpsManager::AppOpsManager()
47 {
48 }
49 
getService()50 sp<IAppOpsService> AppOpsManager::getService()
51 {
52     static String16 _appops("appops");
53 
54     std::lock_guard<Mutex> scoped_lock(mLock);
55     int64_t startTime = 0;
56     sp<IAppOpsService> service = mService;
57     while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
58         sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
59         if (binder == nullptr) {
60             // Wait for the app ops service to come back...
61             if (startTime == 0) {
62                 startTime = uptimeMillis();
63                 ALOGI("Waiting for app ops service");
64             } else if ((uptimeMillis()-startTime) > 10000) {
65                 ALOGW("Waiting too long for app ops service, giving up");
66                 service = nullptr;
67                 break;
68             }
69             sleep(1);
70         } else {
71             service = interface_cast<IAppOpsService>(binder);
72             mService = service;
73         }
74     }
75     return service;
76 }
77 
checkOp(int32_t op,int32_t uid,const String16 & callingPackage)78 int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
79 {
80     sp<IAppOpsService> service = getService();
81     return service != nullptr
82             ? service->checkOperation(op, uid, callingPackage)
83             : AppOpsManager::MODE_IGNORED;
84 }
85 
checkAudioOpNoThrow(int32_t op,int32_t usage,int32_t uid,const String16 & callingPackage)86 int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid,
87         const String16& callingPackage) {
88     sp<IAppOpsService> service = getService();
89     return service != nullptr
90            ? service->checkAudioOperation(op, usage, uid, callingPackage)
91            : AppOpsManager::MODE_IGNORED;
92 }
93 
noteOp(int32_t op,int32_t uid,const String16 & callingPackage)94 int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
95     return noteOp(op, uid, callingPackage, {},
96             String16("Legacy AppOpsManager.noteOp call"));
97 }
98 
noteOp(int32_t op,int32_t uid,const String16 & callingPackage,const std::optional<String16> & attributionTag,const String16 & message)99 int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
100         const std::optional<String16>& attributionTag, const String16& message) {
101     sp<IAppOpsService> service = getService();
102     int32_t mode = service != nullptr
103             ? service->noteOperation(op, uid, callingPackage, attributionTag,
104                     shouldCollectNotes(op), message, uid == AID_SYSTEM)
105             : AppOpsManager::MODE_IGNORED;
106 
107     return mode;
108 }
109 
startOpNoThrow(int32_t op,int32_t uid,const String16 & callingPackage,bool startIfModeDefault)110 int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
111         bool startIfModeDefault) {
112     return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, {},
113             String16("Legacy AppOpsManager.startOpNoThrow call"));
114 }
115 
startOpNoThrow(int32_t op,int32_t uid,const String16 & callingPackage,bool startIfModeDefault,const std::optional<String16> & attributionTag,const String16 & message)116 int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
117         bool startIfModeDefault, const std::optional<String16>& attributionTag,
118         const String16& message) {
119     sp<IAppOpsService> service = getService();
120     int32_t mode = service != nullptr
121             ? service->startOperation(getClientId(), op, uid, callingPackage,
122                     attributionTag, startIfModeDefault, shouldCollectNotes(op), message,
123                     uid == AID_SYSTEM)
124             : AppOpsManager::MODE_IGNORED;
125 
126     return mode;
127 }
128 
finishOp(int32_t op,int32_t uid,const String16 & callingPackage)129 void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
130     finishOp(op, uid, callingPackage, {});
131 }
132 
finishOp(int32_t op,int32_t uid,const String16 & callingPackage,const std::optional<String16> & attributionTag)133 void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage,
134         const std::optional<String16>& attributionTag) {
135     sp<IAppOpsService> service = getService();
136     if (service != nullptr) {
137         service->finishOperation(getClientId(), op, uid, callingPackage, attributionTag);
138     }
139 }
140 
startWatchingMode(int32_t op,const String16 & packageName,const sp<IAppOpsCallback> & callback)141 void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
142         const sp<IAppOpsCallback>& callback) {
143     sp<IAppOpsService> service = getService();
144     if (service != nullptr) {
145         service->startWatchingMode(op, packageName, callback);
146     }
147 }
148 
startWatchingMode(int32_t op,const String16 & packageName,int32_t flags,const sp<IAppOpsCallback> & callback)149 void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName, int32_t flags,
150         const sp<IAppOpsCallback>& callback) {
151     sp<IAppOpsService> service = getService();
152     if (service != nullptr) {
153         service->startWatchingModeWithFlags(op, packageName, flags, callback);
154     }
155 }
156 
stopWatchingMode(const sp<IAppOpsCallback> & callback)157 void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
158     sp<IAppOpsService> service = getService();
159     if (service != nullptr) {
160         service->stopWatchingMode(callback);
161     }
162 }
163 
permissionToOpCode(const String16 & permission)164 int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
165     sp<IAppOpsService> service = getService();
166     if (service != nullptr) {
167         return service->permissionToOpCode(permission);
168     }
169     return -1;
170 }
171 
setCameraAudioRestriction(int32_t mode)172 void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
173     sp<IAppOpsService> service = getService();
174     if (service != nullptr) {
175         service->setCameraAudioRestriction(mode);
176     }
177 }
178 
179 // check it the appops needs to be collected and cache result
shouldCollectNotes(int32_t opcode)180 bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
181     // Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
182     static uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
183 
184     if (appOpsToNote[opcode] == 0) {
185         if (getService()->shouldCollectNotes(opcode)) {
186             appOpsToNote[opcode] = 2;
187         } else {
188             appOpsToNote[opcode] = 1;
189         }
190     }
191 
192     return appOpsToNote[opcode] == 2;
193 }
194 
195 } // namespace android
196