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 | 55bb217 | 2018-02-27 20:25:51 -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 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 53 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 54 | return checkRecordingInternal(opPackageName, pid, uid, false); |
| 55 | } |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 56 | |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 57 | bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 58 | return checkRecordingInternal(opPackageName, pid, uid, true); |
| 59 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 60 | |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 61 | bool 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 | |
| 102 | void 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 Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 110 | AppOpsManager appOps; |
| 111 | const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio); |
| 112 | appOps.finishOp(op, uid, resolvedOpPackageName); |
| 113 | } |
Glenn Kasten | de07e37 | 2015-06-02 12:34:11 -0700 | [diff] [blame] | 114 | |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 115 | const String16 resolveCallingPackage(PermissionController& permissionController, |
| 116 | const String16& opPackageName, uid_t uid) { |
| 117 | if (opPackageName.size() > 0) { |
| 118 | return opPackageName; |
| 119 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 120 | // 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 Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 127 | Vector<String16> packages; |
| 128 | permissionController.getPackagesForUid(uid, packages); |
| 129 | if (packages.isEmpty()) { |
| 130 | ALOGE("No packages for uid %d", uid); |
| 131 | return opPackageName; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 132 | } |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 133 | return packages[0]; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 136 | bool captureAudioOutputAllowed(pid_t pid, uid_t uid) { |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 137 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 138 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 139 | bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 140 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
| 141 | return ok; |
| 142 | } |
| 143 | |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 144 | bool 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 Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 153 | if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 154 | return ok; |
| 155 | } |
| 156 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 157 | bool settingsAllowed() { |
| 158 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 159 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 160 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 161 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 162 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 163 | return ok; |
| 164 | } |
| 165 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 166 | bool modifyAudioRoutingAllowed() { |
| 167 | static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING"); |
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(sModifyAudioRoutingAllowed); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 170 | if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING"); |
| 171 | return ok; |
| 172 | } |
| 173 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 174 | bool dumpAllowed() { |
| 175 | // don't optimize for same pid, since mediaserver never dumps itself |
| 176 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 177 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 178 | 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 Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 184 | bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) { |
| 185 | static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE"); |
Svet Ganov | 55bb217 | 2018-02-27 20:25:51 -0800 | [diff] [blame^] | 186 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 187 | if (!ok) ALOGE("Request requires android.permission.MODIFY_PHONE_STATE"); |
| 188 | return ok; |
| 189 | } |
| 190 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 191 | } // namespace android |