blob: 33bd41651f5f6e6be2d08e02727232136c1a6c5d [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
Svet Ganovbe71aa22015-04-28 12:06:02 -070017#include <binder/AppOpsManager.h>
Glenn Kasten44deb052012-02-05 18:09:08 -080018#include <binder/IPCThreadState.h>
19#include <binder/IServiceManager.h>
20#include <binder/PermissionCache.h>
21#include "ServiceUtilities.h"
22
Svet Ganovbe71aa22015-04-28 12:06:02 -070023/* When performing permission checks we do not use permission cache for
24 * runtime permissions (protection level dangerous) as they may change at
25 * runtime. All other permissions (protection level normal and dangerous)
26 * can be cached as they never change. Of course all permission checked
27 * here are platform defined.
28 */
29
Glenn Kasten44deb052012-02-05 18:09:08 -080030namespace android {
31
Glenn Kasten949a9262013-04-16 12:35:20 -070032// Not valid until initialized by AudioFlinger constructor. It would have to be
33// re-initialized if the process containing AudioFlinger service forks (which it doesn't).
34pid_t getpid_cached;
Glenn Kasten44deb052012-02-05 18:09:08 -080035
Svet Ganovbe71aa22015-04-28 12:06:02 -070036bool recordingAllowed(const String16& opPackageName) {
37 // Note: We are getting the UID from the calling IPC thread state because all
38 // clients that perform recording create AudioRecord in their own processes
39 // and the system does not create AudioRecord objects on behalf of apps. This
40 // differs from playback where in some situations the system recreates AudioTrack
41 // instances associated with a client's MediaPlayer on behalf of this client.
42 // In the latter case we have to store the client UID and pass in along for
43 // security checks.
44
Glenn Kasten44deb052012-02-05 18:09:08 -080045 if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true;
46 static const String16 sRecordAudio("android.permission.RECORD_AUDIO");
Svet Ganovbe71aa22015-04-28 12:06:02 -070047
48 // IMPORTANT: Don't use PermissionCache - a runtime permission and may change.
49 const bool ok = checkCallingPermission(sRecordAudio);
50 if (!ok) {
51 ALOGE("Request requires android.permission.RECORD_AUDIO");
52 return false;
53 }
54
55 const uid_t uid = IPCThreadState::self()->getCallingUid();
56 String16 checkedOpPackageName = opPackageName;
57
58 // In some cases the calling code has no access to the package it runs under.
59 // For example, code using the wilhelm framework's OpenSL-ES APIs. In this
60 // case we will get the packages for the calling UID and pick the first one
61 // for attributing the app op. This will work correctly for runtime permissions
62 // as for legacy apps we will toggle the app op for all packages in the UID.
63 // The caveat is that the operation may be attributed to the wrong package and
64 // stats based on app ops may be slightly off.
65 if (checkedOpPackageName.size() <= 0) {
66 sp<IServiceManager> sm = defaultServiceManager();
67 sp<IBinder> binder = sm->getService(String16("permission"));
68 if (binder == 0) {
69 ALOGE("Cannot get permission service");
70 return false;
71 }
72
73 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
74 Vector<String16> packages;
75
76 permCtrl->getPackagesForUid(uid, packages);
77
78 if (packages.isEmpty()) {
79 ALOGE("No packages for calling UID");
80 return false;
81 }
82 checkedOpPackageName = packages[0];
83 }
84
85 AppOpsManager appOps;
Eric Laurent617575b2015-05-12 18:32:04 -070086 if (appOps.noteOp(AppOpsManager::OP_RECORD_AUDIO, uid, checkedOpPackageName)
Svet Ganovbe71aa22015-04-28 12:06:02 -070087 != AppOpsManager::MODE_ALLOWED) {
88 ALOGE("Request denied by app op OP_RECORD_AUDIO");
89 return false;
90 }
91
92 return true;
Glenn Kasten44deb052012-02-05 18:09:08 -080093}
94
Jeff Brown893a5642013-08-16 20:19:26 -070095bool captureAudioOutputAllowed() {
96 if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true;
97 static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT");
Svet Ganovbe71aa22015-04-28 12:06:02 -070098 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
99 bool ok = PermissionCache::checkCallingPermission(sCaptureAudioOutput);
Jeff Brown893a5642013-08-16 20:19:26 -0700100 if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT");
101 return ok;
102}
103
Eric Laurent9a54bc22013-09-09 09:08:44 -0700104bool captureHotwordAllowed() {
105 static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700106 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
107 bool ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed);
Eric Laurent9a54bc22013-09-09 09:08:44 -0700108 if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD");
109 return ok;
110}
111
Glenn Kasten44deb052012-02-05 18:09:08 -0800112bool settingsAllowed() {
113 if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true;
114 static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700115 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
116 bool ok = PermissionCache::checkCallingPermission(sAudioSettings);
Glenn Kasten44deb052012-02-05 18:09:08 -0800117 if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
118 return ok;
119}
120
Eric Laurent5284ed52014-05-29 14:37:38 -0700121bool modifyAudioRoutingAllowed() {
122 static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700123 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
124 bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed);
Eric Laurent5284ed52014-05-29 14:37:38 -0700125 if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING");
126 return ok;
127}
128
Glenn Kasten44deb052012-02-05 18:09:08 -0800129bool dumpAllowed() {
130 // don't optimize for same pid, since mediaserver never dumps itself
131 static const String16 sDump("android.permission.DUMP");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700132 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
Glenn Kasten44deb052012-02-05 18:09:08 -0800133 bool ok = PermissionCache::checkCallingPermission(sDump);
134 // convention is for caller to dump an error message to fd instead of logging here
135 //if (!ok) ALOGE("Request requires android.permission.DUMP");
136 return ok;
137}
138
139} // namespace android