blob: 599c44685c1f6854a63c378a55caae34ad97a6df [file] [log] [blame]
Glenn Kasten44deb052012-02-05 18:09:08 -08001/*
2 * Copyright (C) 2012 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
Eric Laurent9b11c022018-06-06 19:19:22 -070017#define LOG_TAG "ServiceUtilities"
18
Svet Ganovbe71aa22015-04-28 12:06:02 -070019#include <binder/AppOpsManager.h>
Glenn Kasten44deb052012-02-05 18:09:08 -080020#include <binder/IPCThreadState.h>
21#include <binder/IServiceManager.h>
22#include <binder/PermissionCache.h>
Andy Hungab7ef302018-05-15 19:35:29 -070023#include "mediautils/ServiceUtilities.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080024
Svet Ganovbe71aa22015-04-28 12:06:02 -070025/* When performing permission checks we do not use permission cache for
26 * runtime permissions (protection level dangerous) as they may change at
27 * runtime. All other permissions (protection level normal and dangerous)
28 * can be cached as they never change. Of course all permission checked
29 * here are platform defined.
30 */
31
Glenn Kasten44deb052012-02-05 18:09:08 -080032namespace android {
33
Svet Ganov5b81f552018-03-02 09:21:30 -080034static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO");
35
Svet Ganov5b81f552018-03-02 09:21:30 -080036static String16 resolveCallingPackage(PermissionController& permissionController,
37 const String16& opPackageName, uid_t uid) {
38 if (opPackageName.size() > 0) {
39 return opPackageName;
Svet Ganovbe71aa22015-04-28 12:06:02 -070040 }
Svet Ganovbe71aa22015-04-28 12:06:02 -070041 // In some cases the calling code has no access to the package it runs under.
42 // For example, code using the wilhelm framework's OpenSL-ES APIs. In this
43 // case we will get the packages for the calling UID and pick the first one
44 // for attributing the app op. This will work correctly for runtime permissions
45 // as for legacy apps we will toggle the app op for all packages in the UID.
46 // The caveat is that the operation may be attributed to the wrong package and
47 // stats based on app ops may be slightly off.
Svet Ganov5b81f552018-03-02 09:21:30 -080048 Vector<String16> packages;
49 permissionController.getPackagesForUid(uid, packages);
50 if (packages.isEmpty()) {
51 ALOGE("No packages for uid %d", uid);
52 return opPackageName; // empty string
53 }
54 return packages[0];
55}
Svetoslav Ganov599ec462018-03-01 22:19:55 +000056
Svet Ganov5b81f552018-03-02 09:21:30 -080057static bool checkRecordingInternal(const String16& opPackageName, pid_t pid,
58 uid_t uid, bool start) {
59 // Okay to not track in app ops as audio server is us and if
60 // device is rooted security model is considered compromised.
Andy Hung4ef19fa2018-05-15 19:35:29 -070061 if (isAudioServerOrRootUid(uid)) return true;
Svetoslav Ganov599ec462018-03-01 22:19:55 +000062
Svet Ganov5b81f552018-03-02 09:21:30 -080063 // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder)
64 // may open a record track on behalf of a client. Note that pid may be a tid.
65 // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE.
66 PermissionController permissionController;
67 const bool ok = permissionController.checkPermission(sAndroidPermissionRecordAudio, pid, uid);
68 if (!ok) {
69 ALOGE("Request requires %s", String8(sAndroidPermissionRecordAudio).c_str());
70 return false;
71 }
72
73 String16 resolvedOpPackageName = resolveCallingPackage(
74 permissionController, opPackageName, uid);
75 if (resolvedOpPackageName.size() == 0) {
76 return false;
Svet Ganovbe71aa22015-04-28 12:06:02 -070077 }
Svetoslav Ganov599ec462018-03-01 22:19:55 +000078
79 AppOpsManager appOps;
Svet Ganov5b81f552018-03-02 09:21:30 -080080 const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio);
81 if (start) {
82 if (appOps.startOpNoThrow(op, uid, resolvedOpPackageName, /*startIfModeDefault*/ false)
83 != AppOpsManager::MODE_ALLOWED) {
84 ALOGE("Request denied by app op: %d", op);
85 return false;
86 }
87 } else {
Eric Laurent5c5c8a12019-02-15 17:36:00 -080088 if (appOps.checkOp(op, uid, resolvedOpPackageName) != AppOpsManager::MODE_ALLOWED) {
Svet Ganov5b81f552018-03-02 09:21:30 -080089 ALOGE("Request denied by app op: %d", op);
90 return false;
91 }
Svetoslav Ganov599ec462018-03-01 22:19:55 +000092 }
93
94 return true;
Glenn Kasten44deb052012-02-05 18:09:08 -080095}
96
Svet Ganov5b81f552018-03-02 09:21:30 -080097bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) {
98 return checkRecordingInternal(opPackageName, pid, uid, /*start*/ false);
99}
100
101bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) {
102 return checkRecordingInternal(opPackageName, pid, uid, /*start*/ true);
103}
104
105void finishRecording(const String16& opPackageName, uid_t uid) {
106 // Okay to not track in app ops as audio server is us and if
107 // device is rooted security model is considered compromised.
Andy Hung4ef19fa2018-05-15 19:35:29 -0700108 if (isAudioServerOrRootUid(uid)) return;
Svet Ganov5b81f552018-03-02 09:21:30 -0800109
110 PermissionController permissionController;
111 String16 resolvedOpPackageName = resolveCallingPackage(
112 permissionController, opPackageName, uid);
113 if (resolvedOpPackageName.size() == 0) {
114 return;
115 }
116
117 AppOpsManager appOps;
118 const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio);
119 appOps.finishOp(op, uid, resolvedOpPackageName);
120}
121
Eric Laurentb2379ba2016-05-23 17:42:12 -0700122bool captureAudioOutputAllowed(pid_t pid, uid_t uid) {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700123 if (isAudioServerOrRootUid(uid)) return true;
Jeff Brown893a5642013-08-16 20:19:26 -0700124 static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT");
Svet Ganov5b81f552018-03-02 09:21:30 -0800125 bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid);
Jeff Brown893a5642013-08-16 20:19:26 -0700126 if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT");
127 return ok;
128}
129
Eric Laurent7504b9e2017-08-15 18:17:26 -0700130bool captureHotwordAllowed(pid_t pid, uid_t uid) {
131 // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission
132 bool ok = recordingAllowed(String16(""), pid, uid);
133
134 if (ok) {
135 static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD");
136 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
137 ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed);
138 }
Eric Laurent9a54bc22013-09-09 09:08:44 -0700139 if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD");
140 return ok;
141}
142
Glenn Kasten44deb052012-02-05 18:09:08 -0800143bool settingsAllowed() {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700144 // given this is a permission check, could this be isAudioServerOrRootUid()?
145 if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true;
Glenn Kasten44deb052012-02-05 18:09:08 -0800146 static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700147 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
148 bool ok = PermissionCache::checkCallingPermission(sAudioSettings);
Glenn Kasten44deb052012-02-05 18:09:08 -0800149 if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
150 return ok;
151}
152
Eric Laurent5284ed52014-05-29 14:37:38 -0700153bool modifyAudioRoutingAllowed() {
154 static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700155 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
156 bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed);
Eric Laurent5284ed52014-05-29 14:37:38 -0700157 if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING");
158 return ok;
159}
160
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700161bool modifyDefaultAudioEffectsAllowed() {
162 static const String16 sModifyDefaultAudioEffectsAllowed(
163 "android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS");
164 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
165 bool ok = PermissionCache::checkCallingPermission(sModifyDefaultAudioEffectsAllowed);
166
167#ifdef TARGET_ANDROID_THINGS
168 if (!ok) {
169 // Use a secondary permission on Android Things to allow a more lenient level of protection.
170 static const String16 sModifyDefaultAudioEffectsAndroidThingsAllowed(
171 "com.google.android.things.permission.MODIFY_DEFAULT_AUDIO_EFFECTS");
172 ok = PermissionCache::checkCallingPermission(
173 sModifyDefaultAudioEffectsAndroidThingsAllowed);
174 }
175 if (!ok) ALOGE("com.google.android.things.permission.MODIFY_DEFAULT_AUDIO_EFFECTS");
176#else
177 if (!ok) ALOGE("android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS");
178#endif
179 return ok;
180}
181
Glenn Kasten44deb052012-02-05 18:09:08 -0800182bool dumpAllowed() {
Glenn Kasten44deb052012-02-05 18:09:08 -0800183 static const String16 sDump("android.permission.DUMP");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700184 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
Glenn Kasten44deb052012-02-05 18:09:08 -0800185 bool ok = PermissionCache::checkCallingPermission(sDump);
186 // convention is for caller to dump an error message to fd instead of logging here
187 //if (!ok) ALOGE("Request requires android.permission.DUMP");
188 return ok;
189}
190
Nadav Bar766fb022018-01-07 12:18:03 +0200191bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) {
192 static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE");
Svet Ganov5b81f552018-03-02 09:21:30 -0800193 bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid);
Nadav Bar766fb022018-01-07 12:18:03 +0200194 if (!ok) ALOGE("Request requires android.permission.MODIFY_PHONE_STATE");
195 return ok;
196}
197
Eric Laurent9b11c022018-06-06 19:19:22 -0700198status_t checkIMemory(const sp<IMemory>& iMemory)
199{
200 if (iMemory == 0) {
201 ALOGE("%s check failed: NULL IMemory pointer", __FUNCTION__);
202 return BAD_VALUE;
203 }
204
205 sp<IMemoryHeap> heap = iMemory->getMemory();
206 if (heap == 0) {
207 ALOGE("%s check failed: NULL heap pointer", __FUNCTION__);
208 return BAD_VALUE;
209 }
210
211 off_t size = lseek(heap->getHeapID(), 0, SEEK_END);
212 lseek(heap->getHeapID(), 0, SEEK_SET);
213
214 if (iMemory->pointer() == NULL || size < (off_t)iMemory->size()) {
215 ALOGE("%s check failed: pointer %p size %zu fd size %u",
216 __FUNCTION__, iMemory->pointer(), iMemory->size(), (uint32_t)size);
217 return BAD_VALUE;
218 }
219
220 return NO_ERROR;
221}
222
Glenn Kasten44deb052012-02-05 18:09:08 -0800223} // namespace android