blob: 84b43cef01e19fb54611ef50d734f8250860b3df [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>
Glenn Kasten40b26472015-06-02 14:42:49 -070021#include <private/android_filesystem_config.h>
Glenn Kasten44deb052012-02-05 18:09:08 -080022#include "ServiceUtilities.h"
23
Svet Ganovbe71aa22015-04-28 12:06:02 -070024/* When performing permission checks we do not use permission cache for
25 * runtime permissions (protection level dangerous) as they may change at
26 * runtime. All other permissions (protection level normal and dangerous)
27 * can be cached as they never change. Of course all permission checked
28 * here are platform defined.
29 */
30
Glenn Kasten44deb052012-02-05 18:09:08 -080031namespace android {
32
Svet Ganov55bb2172018-02-27 20:25:51 -080033static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO");
34
Glenn Kasten949a9262013-04-16 12:35:20 -070035// Not valid until initialized by AudioFlinger constructor. It would have to be
36// re-initialized if the process containing AudioFlinger service forks (which it doesn't).
Marco Nelissendcb346b2015-09-09 10:47:29 -070037// This is often used to validate binder interface calls within audioserver
38// (e.g. AudioPolicyManager to AudioFlinger).
Glenn Kasten949a9262013-04-16 12:35:20 -070039pid_t getpid_cached;
Glenn Kasten44deb052012-02-05 18:09:08 -080040
Marco Nelissendcb346b2015-09-09 10:47:29 -070041// A trusted calling UID may specify the client UID as part of a binder interface call.
42// otherwise the calling UID must be equal to the client UID.
43bool isTrustedCallingUid(uid_t uid) {
44 switch (uid) {
45 case AID_MEDIA:
46 case AID_AUDIOSERVER:
47 return true;
48 default:
49 return false;
50 }
51}
Svet Ganovbe71aa22015-04-28 12:06:02 -070052
Marco Nelissendcb346b2015-09-09 10:47:29 -070053bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) {
Svet Ganov55bb2172018-02-27 20:25:51 -080054 return checkRecordingInternal(opPackageName, pid, uid, false);
55}
Marco Nelissendcb346b2015-09-09 10:47:29 -070056
Svet Ganov55bb2172018-02-27 20:25:51 -080057bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) {
58 return checkRecordingInternal(opPackageName, pid, uid, true);
59}
Svet Ganovbe71aa22015-04-28 12:06:02 -070060
Svet Ganov55bb2172018-02-27 20:25:51 -080061bool checkRecordingInternal(const String16& opPackageName, pid_t pid, uid_t uid, bool start) {
62 // we're always OK.
63 if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true;
64
65 // To permit command-line native tests
66 if (uid == AID_ROOT) return true;
67
68 // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder)
69 // may open a record track on behalf of a client. Note that pid may be a tid.
70 // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE.
71 PermissionController permissionController;
72 const bool ok = permissionController.checkPermission(sAndroidPermissionRecordAudio, pid, uid);
73 if (!ok) {
74 ALOGE("Request requires %s", String8(sAndroidPermissionRecordAudio).c_str());
75 return false;
76 }
77
78 const String16 resolvedOpPackageName = resolveCallingPackage(
79 permissionController, opPackageName, uid);
80 if (opPackageName.size() <= 0) {
81 return false;
82 }
83
84 AppOpsManager appOps;
85 const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio);
86 if (start) {
87 if (appOps.startOpNoThrow(op, uid, resolvedOpPackageName, /*startIfModeDefault*/ false)
88 != AppOpsManager::MODE_ALLOWED) {
89 ALOGE("Request denied by app op: %d", op);
90 return false;
91 }
92 } else {
93 if (appOps.noteOp(op, uid, resolvedOpPackageName) != AppOpsManager::MODE_ALLOWED) {
94 ALOGE("Request denied by app op: %d", op);
95 return false;
96 }
97 }
98
99 return true;
100}
101
102void finishRecording(const String16& opPackageName, uid_t uid) {
103 PermissionController permissionController;
104 const String16 resolvedOpPackageName = resolveCallingPackage(
105 permissionController, opPackageName, uid);
106 if (opPackageName.size() <= 0) {
107 return;
Svet Ganovbe71aa22015-04-28 12:06:02 -0700108 }
109
Svet Ganov55bb2172018-02-27 20:25:51 -0800110 AppOpsManager appOps;
111 const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio);
112 appOps.finishOp(op, uid, resolvedOpPackageName);
113}
Glenn Kastende07e372015-06-02 12:34:11 -0700114
Svet Ganov55bb2172018-02-27 20:25:51 -0800115const String16 resolveCallingPackage(PermissionController& permissionController,
116 const String16& opPackageName, uid_t uid) {
117 if (opPackageName.size() > 0) {
118 return opPackageName;
119 }
Svet Ganovbe71aa22015-04-28 12:06:02 -0700120 // In some cases the calling code has no access to the package it runs under.
121 // For example, code using the wilhelm framework's OpenSL-ES APIs. In this
122 // case we will get the packages for the calling UID and pick the first one
123 // for attributing the app op. This will work correctly for runtime permissions
124 // as for legacy apps we will toggle the app op for all packages in the UID.
125 // The caveat is that the operation may be attributed to the wrong package and
126 // stats based on app ops may be slightly off.
Svet Ganov55bb2172018-02-27 20:25:51 -0800127 Vector<String16> packages;
128 permissionController.getPackagesForUid(uid, packages);
129 if (packages.isEmpty()) {
130 ALOGE("No packages for uid %d", uid);
131 return opPackageName;
Svet Ganovbe71aa22015-04-28 12:06:02 -0700132 }
Svet Ganov55bb2172018-02-27 20:25:51 -0800133 return packages[0];
Glenn Kasten44deb052012-02-05 18:09:08 -0800134}
135
Eric Laurentb2379ba2016-05-23 17:42:12 -0700136bool captureAudioOutputAllowed(pid_t pid, uid_t uid) {
Jeff Brown893a5642013-08-16 20:19:26 -0700137 if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true;
138 static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT");
Svet Ganov55bb2172018-02-27 20:25:51 -0800139 bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid);
Jeff Brown893a5642013-08-16 20:19:26 -0700140 if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT");
141 return ok;
142}
143
Eric Laurent7504b9e2017-08-15 18:17:26 -0700144bool captureHotwordAllowed(pid_t pid, uid_t uid) {
145 // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission
146 bool ok = recordingAllowed(String16(""), pid, uid);
147
148 if (ok) {
149 static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD");
150 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
151 ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed);
152 }
Eric Laurent9a54bc22013-09-09 09:08:44 -0700153 if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD");
154 return ok;
155}
156
Glenn Kasten44deb052012-02-05 18:09:08 -0800157bool settingsAllowed() {
158 if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true;
159 static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700160 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
161 bool ok = PermissionCache::checkCallingPermission(sAudioSettings);
Glenn Kasten44deb052012-02-05 18:09:08 -0800162 if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
163 return ok;
164}
165
Eric Laurent5284ed52014-05-29 14:37:38 -0700166bool modifyAudioRoutingAllowed() {
167 static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700168 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
169 bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed);
Eric Laurent5284ed52014-05-29 14:37:38 -0700170 if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING");
171 return ok;
172}
173
Glenn Kasten44deb052012-02-05 18:09:08 -0800174bool dumpAllowed() {
175 // don't optimize for same pid, since mediaserver never dumps itself
176 static const String16 sDump("android.permission.DUMP");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700177 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
Glenn Kasten44deb052012-02-05 18:09:08 -0800178 bool ok = PermissionCache::checkCallingPermission(sDump);
179 // convention is for caller to dump an error message to fd instead of logging here
180 //if (!ok) ALOGE("Request requires android.permission.DUMP");
181 return ok;
182}
183
Nadav Bar766fb022018-01-07 12:18:03 +0200184bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) {
185 static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE");
Svet Ganov55bb2172018-02-27 20:25:51 -0800186 bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid);
Nadav Bar766fb022018-01-07 12:18:03 +0200187 if (!ok) ALOGE("Request requires android.permission.MODIFY_PHONE_STATE");
188 return ok;
189}
190
Glenn Kasten44deb052012-02-05 18:09:08 -0800191} // namespace android