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 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 17 | #define LOG_TAG "ServiceUtilities" |
| 18 | |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 19 | #include <audio_utils/clock.h> |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 20 | #include <binder/AppOpsManager.h> |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <binder/PermissionCache.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 24 | #include "mediautils/ServiceUtilities.h" |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 25 | #include <system/audio-hal-enums.h> |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 26 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 27 | #include <iterator> |
| 28 | #include <algorithm> |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 29 | #include <pwd.h> |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 30 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 31 | /* When performing permission checks we do not use permission cache for |
| 32 | * runtime permissions (protection level dangerous) as they may change at |
| 33 | * runtime. All other permissions (protection level normal and dangerous) |
| 34 | * can be cached as they never change. Of course all permission checked |
| 35 | * here are platform defined. |
| 36 | */ |
| 37 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 40 | static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO"); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 41 | static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE"); |
| 42 | static const String16 sModifyAudioRouting("android.permission.MODIFY_AUDIO_ROUTING"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 43 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 44 | static String16 resolveCallingPackage(PermissionController& permissionController, |
| 45 | const String16& opPackageName, uid_t uid) { |
| 46 | if (opPackageName.size() > 0) { |
| 47 | return opPackageName; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 48 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 49 | // In some cases the calling code has no access to the package it runs under. |
| 50 | // For example, code using the wilhelm framework's OpenSL-ES APIs. In this |
| 51 | // case we will get the packages for the calling UID and pick the first one |
| 52 | // for attributing the app op. This will work correctly for runtime permissions |
| 53 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 54 | // The caveat is that the operation may be attributed to the wrong package and |
| 55 | // stats based on app ops may be slightly off. |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 56 | Vector<String16> packages; |
| 57 | permissionController.getPackagesForUid(uid, packages); |
| 58 | if (packages.isEmpty()) { |
| 59 | ALOGE("No packages for uid %d", uid); |
| 60 | return opPackageName; // empty string |
| 61 | } |
| 62 | return packages[0]; |
| 63 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 64 | |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 65 | static int32_t getOpForSource(audio_source_t source) { |
| 66 | switch (source) { |
| 67 | case AUDIO_SOURCE_HOTWORD: |
| 68 | return AppOpsManager::OP_RECORD_AUDIO_HOTWORD; |
| 69 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 70 | return AppOpsManager::OP_RECORD_AUDIO_OUTPUT; |
| 71 | case AUDIO_SOURCE_DEFAULT: |
| 72 | default: |
| 73 | return AppOpsManager::OP_RECORD_AUDIO; |
| 74 | } |
| 75 | } |
| 76 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 77 | static bool checkRecordingInternal(const String16& opPackageName, pid_t pid, |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 78 | uid_t uid, bool start, audio_source_t source) { |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 79 | // Okay to not track in app ops as audio server or media server is us and if |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 80 | // device is rooted security model is considered compromised. |
Jeffrey Carlyle | 62cc92b | 2019-09-17 11:15:15 -0700 | [diff] [blame] | 81 | // system_server loses its RECORD_AUDIO permission when a secondary |
| 82 | // user is active, but it is a core system service so let it through. |
| 83 | // TODO(b/141210120): UserManager.DISALLOW_RECORD_AUDIO should not affect system user 0 |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 84 | if (isAudioServerOrMediaServerOrSystemServerOrRootUid(uid)) return true; |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 85 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 86 | // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder) |
| 87 | // may open a record track on behalf of a client. Note that pid may be a tid. |
| 88 | // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE. |
| 89 | PermissionController permissionController; |
| 90 | const bool ok = permissionController.checkPermission(sAndroidPermissionRecordAudio, pid, uid); |
| 91 | if (!ok) { |
| 92 | ALOGE("Request requires %s", String8(sAndroidPermissionRecordAudio).c_str()); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 97 | permissionController, opPackageName, uid); |
| 98 | if (resolvedOpPackageName.size() == 0) { |
| 99 | return false; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 100 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 101 | |
| 102 | AppOpsManager appOps; |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 103 | const int32_t op = getOpForSource(source); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 104 | if (start) { |
Mikhail Naganov | 70ff237 | 2020-12-07 22:18:49 +0000 | [diff] [blame] | 105 | if (int32_t mode = appOps.startOpNoThrow( |
| 106 | op, uid, resolvedOpPackageName, /*startIfModeDefault*/ false); |
| 107 | mode != AppOpsManager::MODE_ALLOWED) { |
| 108 | ALOGE("Request start for \"%s\" (uid %d) denied by app op: %d, mode: %d", |
| 109 | String8(resolvedOpPackageName).c_str(), uid, op, mode); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | } else { |
Narayan Kamath | bf85d8b | 2020-08-20 17:19:57 +0100 | [diff] [blame] | 113 | // Always use OP_RECORD_AUDIO for checks at creation time. |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 114 | if (int32_t mode = appOps.checkOp(op, uid, resolvedOpPackageName); |
Mikhail Naganov | 70ff237 | 2020-12-07 22:18:49 +0000 | [diff] [blame] | 115 | mode != AppOpsManager::MODE_ALLOWED) { |
| 116 | ALOGE("Request check for \"%s\" (uid %d) denied by app op: %d, mode: %d", |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 117 | String8(resolvedOpPackageName).c_str(), uid, op, mode); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 118 | return false; |
| 119 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 125 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 126 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ false, AUDIO_SOURCE_DEFAULT); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 129 | bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid, audio_source_t source) { |
| 130 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ true, source); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 133 | void finishRecording(const String16& opPackageName, uid_t uid, audio_source_t source) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 134 | // Okay to not track in app ops as audio server is us and if |
| 135 | // device is rooted security model is considered compromised. |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 136 | if (isAudioServerOrRootUid(uid)) return; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 137 | |
| 138 | PermissionController permissionController; |
| 139 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 140 | permissionController, opPackageName, uid); |
| 141 | if (resolvedOpPackageName.size() == 0) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | AppOpsManager appOps; |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 146 | |
| 147 | const int32_t op = getOpForSource(source); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 148 | appOps.finishOp(op, uid, resolvedOpPackageName); |
| 149 | } |
| 150 | |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 151 | bool captureAudioOutputAllowed(pid_t pid, uid_t uid) { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 152 | if (isAudioServerOrRootUid(uid)) return true; |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 153 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 154 | bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid); |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 155 | if (!ok) ALOGV("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 156 | return ok; |
| 157 | } |
| 158 | |
Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 159 | bool captureMediaOutputAllowed(pid_t pid, uid_t uid) { |
| 160 | if (isAudioServerOrRootUid(uid)) return true; |
| 161 | static const String16 sCaptureMediaOutput("android.permission.CAPTURE_MEDIA_OUTPUT"); |
| 162 | bool ok = PermissionCache::checkPermission(sCaptureMediaOutput, pid, uid); |
| 163 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_MEDIA_OUTPUT"); |
| 164 | return ok; |
| 165 | } |
| 166 | |
Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 167 | bool captureVoiceCommunicationOutputAllowed(pid_t pid, uid_t uid) { |
| 168 | if (isAudioServerOrRootUid(uid)) return true; |
| 169 | static const String16 sCaptureVoiceCommOutput( |
| 170 | "android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT"); |
| 171 | bool ok = PermissionCache::checkPermission(sCaptureVoiceCommOutput, pid, uid); |
| 172 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT"); |
| 173 | return ok; |
| 174 | } |
| 175 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 176 | bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 177 | // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 178 | bool ok = recordingAllowed(opPackageName, pid, uid); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 179 | |
| 180 | if (ok) { |
| 181 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 182 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 183 | ok = PermissionCache::checkPermission(sCaptureHotwordAllowed, pid, uid); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 184 | } |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 185 | if (!ok) ALOGV("android.permission.CAPTURE_AUDIO_HOTWORD"); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 186 | return ok; |
| 187 | } |
| 188 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 189 | bool settingsAllowed() { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 190 | // given this is a permission check, could this be isAudioServerOrRootUid()? |
| 191 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 192 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 193 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 194 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 195 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 196 | return ok; |
| 197 | } |
| 198 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 199 | bool modifyAudioRoutingAllowed() { |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 200 | return modifyAudioRoutingAllowed( |
| 201 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); |
| 202 | } |
| 203 | |
| 204 | bool modifyAudioRoutingAllowed(pid_t pid, uid_t uid) { |
| 205 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 206 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 207 | bool ok = PermissionCache::checkPermission(sModifyAudioRouting, pid, uid); |
| 208 | if (!ok) ALOGE("%s(): android.permission.MODIFY_AUDIO_ROUTING denied for uid %d", |
| 209 | __func__, uid); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 210 | return ok; |
| 211 | } |
| 212 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 213 | bool modifyDefaultAudioEffectsAllowed() { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 214 | return modifyDefaultAudioEffectsAllowed( |
| 215 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); |
| 216 | } |
| 217 | |
| 218 | bool modifyDefaultAudioEffectsAllowed(pid_t pid, uid_t uid) { |
| 219 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
| 220 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 221 | static const String16 sModifyDefaultAudioEffectsAllowed( |
| 222 | "android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 223 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 224 | bool ok = PermissionCache::checkPermission(sModifyDefaultAudioEffectsAllowed, pid, uid); |
| 225 | ALOGE_IF(!ok, "%s(): android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS denied for uid %d", |
| 226 | __func__, uid); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 227 | return ok; |
| 228 | } |
| 229 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 230 | bool dumpAllowed() { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 231 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 232 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 233 | bool ok = PermissionCache::checkCallingPermission(sDump); |
| 234 | // convention is for caller to dump an error message to fd instead of logging here |
| 235 | //if (!ok) ALOGE("Request requires android.permission.DUMP"); |
| 236 | return ok; |
| 237 | } |
| 238 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 239 | bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 240 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 241 | ALOGE_IF(!ok, "Request requires %s", String8(sModifyPhoneState).c_str()); |
| 242 | return ok; |
| 243 | } |
| 244 | |
| 245 | // privileged behavior needed by Dialer, Settings, SetupWizard and CellBroadcastReceiver |
| 246 | bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid) { |
| 247 | static const String16 sWriteSecureSettings("android.permission.WRITE_SECURE_SETTINGS"); |
| 248 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid) |
| 249 | || PermissionCache::checkPermission(sWriteSecureSettings, pid, uid) |
| 250 | || PermissionCache::checkPermission(sModifyAudioRouting, pid, uid); |
| 251 | ALOGE_IF(!ok, "Request requires %s or %s", |
| 252 | String8(sModifyPhoneState).c_str(), String8(sWriteSecureSettings).c_str()); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 253 | return ok; |
| 254 | } |
| 255 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 256 | status_t checkIMemory(const sp<IMemory>& iMemory) |
| 257 | { |
| 258 | if (iMemory == 0) { |
| 259 | ALOGE("%s check failed: NULL IMemory pointer", __FUNCTION__); |
| 260 | return BAD_VALUE; |
| 261 | } |
| 262 | |
| 263 | sp<IMemoryHeap> heap = iMemory->getMemory(); |
| 264 | if (heap == 0) { |
| 265 | ALOGE("%s check failed: NULL heap pointer", __FUNCTION__); |
| 266 | return BAD_VALUE; |
| 267 | } |
| 268 | |
| 269 | off_t size = lseek(heap->getHeapID(), 0, SEEK_END); |
| 270 | lseek(heap->getHeapID(), 0, SEEK_SET); |
| 271 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 272 | if (iMemory->unsecurePointer() == NULL || size < (off_t)iMemory->size()) { |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 273 | ALOGE("%s check failed: pointer %p size %zu fd size %u", |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 274 | __FUNCTION__, iMemory->unsecurePointer(), iMemory->size(), (uint32_t)size); |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 275 | return BAD_VALUE; |
| 276 | } |
| 277 | |
| 278 | return NO_ERROR; |
| 279 | } |
| 280 | |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 281 | sp<content::pm::IPackageManagerNative> MediaPackageManager::retreivePackageManager() { |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 282 | const sp<IServiceManager> sm = defaultServiceManager(); |
| 283 | if (sm == nullptr) { |
| 284 | ALOGW("%s: failed to retrieve defaultServiceManager", __func__); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 285 | return nullptr; |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 286 | } |
| 287 | sp<IBinder> packageManager = sm->checkService(String16(nativePackageManagerName)); |
| 288 | if (packageManager == nullptr) { |
| 289 | ALOGW("%s: failed to retrieve native package manager", __func__); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 290 | return nullptr; |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 291 | } |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 292 | return interface_cast<content::pm::IPackageManagerNative>(packageManager); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | std::optional<bool> MediaPackageManager::doIsAllowed(uid_t uid) { |
| 296 | if (mPackageManager == nullptr) { |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 297 | /** Can not fetch package manager at construction it may not yet be registered. */ |
| 298 | mPackageManager = retreivePackageManager(); |
| 299 | if (mPackageManager == nullptr) { |
| 300 | ALOGW("%s: Playback capture is denied as package manager is not reachable", __func__); |
| 301 | return std::nullopt; |
| 302 | } |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | std::vector<std::string> packageNames; |
| 306 | auto status = mPackageManager->getNamesForUids({(int32_t)uid}, &packageNames); |
| 307 | if (!status.isOk()) { |
| 308 | ALOGW("%s: Playback capture is denied for uid %u as the package names could not be " |
| 309 | "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 310 | return std::nullopt; |
| 311 | } |
| 312 | if (packageNames.empty()) { |
| 313 | ALOGW("%s: Playback capture for uid %u is denied as no package name could be retrieved " |
| 314 | "from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 315 | return std::nullopt; |
| 316 | } |
| 317 | std::vector<bool> isAllowed; |
| 318 | status = mPackageManager->isAudioPlaybackCaptureAllowed(packageNames, &isAllowed); |
| 319 | if (!status.isOk()) { |
| 320 | ALOGW("%s: Playback capture is denied for uid %u as the manifest property could not be " |
| 321 | "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 322 | return std::nullopt; |
| 323 | } |
| 324 | if (packageNames.size() != isAllowed.size()) { |
| 325 | ALOGW("%s: Playback capture is denied for uid %u as the package manager returned incoherent" |
| 326 | " response size: %zu != %zu", __func__, uid, packageNames.size(), isAllowed.size()); |
| 327 | return std::nullopt; |
| 328 | } |
| 329 | |
| 330 | // Zip together packageNames and isAllowed for debug logs |
| 331 | Packages& packages = mDebugLog[uid]; |
| 332 | packages.resize(packageNames.size()); // Reuse all objects |
| 333 | std::transform(begin(packageNames), end(packageNames), begin(isAllowed), |
| 334 | begin(packages), [] (auto& name, bool isAllowed) -> Package { |
| 335 | return {std::move(name), isAllowed}; |
| 336 | }); |
| 337 | |
| 338 | // Only allow playback record if all packages in this UID allow it |
| 339 | bool playbackCaptureAllowed = std::all_of(begin(isAllowed), end(isAllowed), |
| 340 | [](bool b) { return b; }); |
| 341 | |
| 342 | return playbackCaptureAllowed; |
| 343 | } |
| 344 | |
| 345 | void MediaPackageManager::dump(int fd, int spaces) const { |
| 346 | dprintf(fd, "%*sAllow playback capture log:\n", spaces, ""); |
| 347 | if (mPackageManager == nullptr) { |
| 348 | dprintf(fd, "%*sNo package manager\n", spaces + 2, ""); |
| 349 | } |
| 350 | dprintf(fd, "%*sPackage manager errors: %u\n", spaces + 2, "", mPackageManagerErrors); |
| 351 | |
| 352 | for (const auto& uidCache : mDebugLog) { |
| 353 | for (const auto& package : std::get<Packages>(uidCache)) { |
| 354 | dprintf(fd, "%*s- uid=%5u, allowPlaybackCapture=%s, packageName=%s\n", spaces + 2, "", |
| 355 | std::get<const uid_t>(uidCache), |
| 356 | package.playbackCaptureAllowed ? "true " : "false", |
| 357 | package.name.c_str()); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 362 | // How long we hold info before we re-fetch it (24 hours) if we found it previously. |
| 363 | static constexpr nsecs_t INFO_EXPIRATION_NS = 24 * 60 * 60 * NANOS_PER_SECOND; |
| 364 | // Maximum info records we retain before clearing everything. |
| 365 | static constexpr size_t INFO_CACHE_MAX = 1000; |
| 366 | |
| 367 | // The original code is from MediaMetricsService.cpp. |
| 368 | mediautils::UidInfo::Info mediautils::UidInfo::getInfo(uid_t uid) |
| 369 | { |
| 370 | const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 371 | struct mediautils::UidInfo::Info info; |
| 372 | { |
| 373 | std::lock_guard _l(mLock); |
| 374 | auto it = mInfoMap.find(uid); |
| 375 | if (it != mInfoMap.end()) { |
| 376 | info = it->second; |
| 377 | ALOGV("%s: uid %d expiration %lld now %lld", |
| 378 | __func__, uid, (long long)info.expirationNs, (long long)now); |
| 379 | if (info.expirationNs <= now) { |
| 380 | // purge the stale entry and fall into re-fetching |
| 381 | ALOGV("%s: entry for uid %d expired, now %lld", |
| 382 | __func__, uid, (long long)now); |
| 383 | mInfoMap.erase(it); |
| 384 | info.uid = (uid_t)-1; // this is always fully overwritten |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // if we did not find it in our map, look it up |
| 390 | if (info.uid == (uid_t)(-1)) { |
| 391 | sp<IServiceManager> sm = defaultServiceManager(); |
| 392 | sp<content::pm::IPackageManagerNative> package_mgr; |
| 393 | if (sm.get() == nullptr) { |
| 394 | ALOGE("%s: Cannot find service manager", __func__); |
| 395 | } else { |
| 396 | sp<IBinder> binder = sm->getService(String16("package_native")); |
| 397 | if (binder.get() == nullptr) { |
| 398 | ALOGE("%s: Cannot find package_native", __func__); |
| 399 | } else { |
| 400 | package_mgr = interface_cast<content::pm::IPackageManagerNative>(binder); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // find package name |
| 405 | std::string pkg; |
| 406 | if (package_mgr != nullptr) { |
| 407 | std::vector<std::string> names; |
| 408 | binder::Status status = package_mgr->getNamesForUids({(int)uid}, &names); |
| 409 | if (!status.isOk()) { |
| 410 | ALOGE("%s: getNamesForUids failed: %s", |
| 411 | __func__, status.exceptionMessage().c_str()); |
| 412 | } else { |
| 413 | if (!names[0].empty()) { |
| 414 | pkg = names[0].c_str(); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (pkg.empty()) { |
| 420 | struct passwd pw{}, *result; |
| 421 | char buf[8192]; // extra buffer space - should exceed what is |
| 422 | // required in struct passwd_pw (tested), |
| 423 | // and even then this is only used in backup |
| 424 | // when the package manager is unavailable. |
| 425 | if (getpwuid_r(uid, &pw, buf, sizeof(buf), &result) == 0 |
| 426 | && result != nullptr |
| 427 | && result->pw_name != nullptr) { |
| 428 | pkg = result->pw_name; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // strip any leading "shared:" strings that came back |
| 433 | if (pkg.compare(0, 7, "shared:") == 0) { |
| 434 | pkg.erase(0, 7); |
| 435 | } |
| 436 | |
| 437 | // determine how pkg was installed and the versionCode |
| 438 | std::string installer; |
| 439 | int64_t versionCode = 0; |
| 440 | bool notFound = false; |
| 441 | if (pkg.empty()) { |
| 442 | pkg = std::to_string(uid); // not found |
| 443 | notFound = true; |
| 444 | } else if (strchr(pkg.c_str(), '.') == nullptr) { |
| 445 | // not of form 'com.whatever...'; assume internal |
| 446 | // so we don't need to look it up in package manager. |
Andy Hung | e6a65ac | 2020-01-08 16:56:17 -0800 | [diff] [blame] | 447 | } else if (strncmp(pkg.c_str(), "android.", 8) == 0) { |
| 448 | // android.* packages are assumed fine |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 449 | } else if (package_mgr.get() != nullptr) { |
| 450 | String16 pkgName16(pkg.c_str()); |
| 451 | binder::Status status = package_mgr->getInstallerForPackage(pkgName16, &installer); |
| 452 | if (!status.isOk()) { |
| 453 | ALOGE("%s: getInstallerForPackage failed: %s", |
| 454 | __func__, status.exceptionMessage().c_str()); |
| 455 | } |
| 456 | |
| 457 | // skip if we didn't get an installer |
| 458 | if (status.isOk()) { |
| 459 | status = package_mgr->getVersionCodeForPackage(pkgName16, &versionCode); |
| 460 | if (!status.isOk()) { |
| 461 | ALOGE("%s: getVersionCodeForPackage failed: %s", |
| 462 | __func__, status.exceptionMessage().c_str()); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | ALOGV("%s: package '%s' installed by '%s' versioncode %lld", |
| 467 | __func__, pkg.c_str(), installer.c_str(), (long long)versionCode); |
| 468 | } |
| 469 | |
| 470 | // add it to the map, to save a subsequent lookup |
| 471 | std::lock_guard _l(mLock); |
| 472 | // first clear if we have too many cached elements. This would be rare. |
| 473 | if (mInfoMap.size() >= INFO_CACHE_MAX) mInfoMap.clear(); |
| 474 | |
| 475 | // always overwrite |
| 476 | info.uid = uid; |
| 477 | info.package = std::move(pkg); |
| 478 | info.installer = std::move(installer); |
| 479 | info.versionCode = versionCode; |
| 480 | info.expirationNs = now + (notFound ? 0 : INFO_EXPIRATION_NS); |
| 481 | ALOGV("%s: adding uid %d package '%s' expirationNs: %lld", |
| 482 | __func__, uid, info.package.c_str(), (long long)info.expirationNs); |
| 483 | mInfoMap[uid] = info; |
| 484 | } |
| 485 | return info; |
| 486 | } |
| 487 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 488 | } // namespace android |