Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 1 | /* |
| 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 Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 17 | #include <binder/AppOpsManager.h> |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 18 | #include <binder/IPCThreadState.h> |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <binder/PermissionCache.h> |
Glenn Kasten | 40b2647 | 2015-06-02 14:42:49 -0700 | [diff] [blame] | 21 | #include <private/android_filesystem_config.h> |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 22 | #include "ServiceUtilities.h" |
| 23 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 24 | /* 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 Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 33 | static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO"); |
| 34 | |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 35 | // 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 Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 37 | // This is often used to validate binder interface calls within audioserver |
| 38 | // (e.g. AudioPolicyManager to AudioFlinger). |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 39 | pid_t getpid_cached; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 40 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 41 | // 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. |
| 43 | bool 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 Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 52 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 53 | static String16 resolveCallingPackage(PermissionController& permissionController, |
| 54 | const String16& opPackageName, uid_t uid) { |
| 55 | if (opPackageName.size() > 0) { |
| 56 | return opPackageName; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 57 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 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. |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 65 | Vector<String16> packages; |
| 66 | permissionController.getPackagesForUid(uid, packages); |
| 67 | if (packages.isEmpty()) { |
| 68 | ALOGE("No packages for uid %d", uid); |
| 69 | return opPackageName; // empty string |
| 70 | } |
| 71 | return packages[0]; |
| 72 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 73 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 74 | static inline bool isAudioServerOrRoot(uid_t uid) { |
| 75 | // AID_ROOT is OK for command-line tests. Native unforked audioserver always OK. |
| 76 | return uid == AID_ROOT || uid == AID_AUDIOSERVER ; |
| 77 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 78 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 79 | static bool checkRecordingInternal(const String16& opPackageName, pid_t pid, |
| 80 | uid_t uid, bool start) { |
| 81 | // Okay to not track in app ops as audio server is us and if |
| 82 | // device is rooted security model is considered compromised. |
| 83 | if (isAudioServerOrRoot(uid)) return true; |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 84 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 85 | // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder) |
| 86 | // may open a record track on behalf of a client. Note that pid may be a tid. |
| 87 | // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE. |
| 88 | PermissionController permissionController; |
| 89 | const bool ok = permissionController.checkPermission(sAndroidPermissionRecordAudio, pid, uid); |
| 90 | if (!ok) { |
| 91 | ALOGE("Request requires %s", String8(sAndroidPermissionRecordAudio).c_str()); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 96 | permissionController, opPackageName, uid); |
| 97 | if (resolvedOpPackageName.size() == 0) { |
| 98 | return false; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 99 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 100 | |
| 101 | AppOpsManager appOps; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 102 | const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio); |
| 103 | if (start) { |
| 104 | if (appOps.startOpNoThrow(op, uid, resolvedOpPackageName, /*startIfModeDefault*/ false) |
| 105 | != AppOpsManager::MODE_ALLOWED) { |
| 106 | ALOGE("Request denied by app op: %d", op); |
| 107 | return false; |
| 108 | } |
| 109 | } else { |
| 110 | if (appOps.noteOp(op, uid, resolvedOpPackageName) != AppOpsManager::MODE_ALLOWED) { |
| 111 | ALOGE("Request denied by app op: %d", op); |
| 112 | return false; |
| 113 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 119 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 120 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ false); |
| 121 | } |
| 122 | |
| 123 | bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 124 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ true); |
| 125 | } |
| 126 | |
| 127 | void finishRecording(const String16& opPackageName, uid_t uid) { |
| 128 | // Okay to not track in app ops as audio server is us and if |
| 129 | // device is rooted security model is considered compromised. |
| 130 | if (isAudioServerOrRoot(uid)) return; |
| 131 | |
| 132 | PermissionController permissionController; |
| 133 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 134 | permissionController, opPackageName, uid); |
| 135 | if (resolvedOpPackageName.size() == 0) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | AppOpsManager appOps; |
| 140 | const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio); |
| 141 | appOps.finishOp(op, uid, resolvedOpPackageName); |
| 142 | } |
| 143 | |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 144 | bool captureAudioOutputAllowed(pid_t pid, uid_t uid) { |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 145 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 146 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 147 | bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 148 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
| 149 | return ok; |
| 150 | } |
| 151 | |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 152 | bool captureHotwordAllowed(pid_t pid, uid_t uid) { |
| 153 | // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission |
| 154 | bool ok = recordingAllowed(String16(""), pid, uid); |
| 155 | |
| 156 | if (ok) { |
| 157 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 158 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 159 | ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed); |
| 160 | } |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 161 | if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 162 | return ok; |
| 163 | } |
| 164 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 165 | bool settingsAllowed() { |
| 166 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 167 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 168 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 169 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 170 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 171 | return ok; |
| 172 | } |
| 173 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 174 | bool modifyAudioRoutingAllowed() { |
| 175 | static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 176 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 177 | bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 178 | if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING"); |
| 179 | return ok; |
| 180 | } |
| 181 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 182 | bool dumpAllowed() { |
| 183 | // don't optimize for same pid, since mediaserver never dumps itself |
| 184 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 185 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 186 | bool ok = PermissionCache::checkCallingPermission(sDump); |
| 187 | // convention is for caller to dump an error message to fd instead of logging here |
| 188 | //if (!ok) ALOGE("Request requires android.permission.DUMP"); |
| 189 | return ok; |
| 190 | } |
| 191 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 192 | bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) { |
| 193 | static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 194 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 195 | if (!ok) ALOGE("Request requires android.permission.MODIFY_PHONE_STATE"); |
| 196 | return ok; |
| 197 | } |
| 198 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 199 | } // namespace android |