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 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 19 | #include <binder/AppOpsManager.h> |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 20 | #include <binder/IPCThreadState.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/PermissionCache.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 23 | #include "mediautils/ServiceUtilities.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 24 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 25 | #include <iterator> |
| 26 | #include <algorithm> |
| 27 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 28 | /* When performing permission checks we do not use permission cache for |
| 29 | * runtime permissions (protection level dangerous) as they may change at |
| 30 | * runtime. All other permissions (protection level normal and dangerous) |
| 31 | * can be cached as they never change. Of course all permission checked |
| 32 | * here are platform defined. |
| 33 | */ |
| 34 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 37 | static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO"); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 38 | static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE"); |
| 39 | static const String16 sModifyAudioRouting("android.permission.MODIFY_AUDIO_ROUTING"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 40 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 41 | static String16 resolveCallingPackage(PermissionController& permissionController, |
| 42 | const String16& opPackageName, uid_t uid) { |
| 43 | if (opPackageName.size() > 0) { |
| 44 | return opPackageName; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 45 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 46 | // In some cases the calling code has no access to the package it runs under. |
| 47 | // For example, code using the wilhelm framework's OpenSL-ES APIs. In this |
| 48 | // case we will get the packages for the calling UID and pick the first one |
| 49 | // for attributing the app op. This will work correctly for runtime permissions |
| 50 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 51 | // The caveat is that the operation may be attributed to the wrong package and |
| 52 | // stats based on app ops may be slightly off. |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 53 | Vector<String16> packages; |
| 54 | permissionController.getPackagesForUid(uid, packages); |
| 55 | if (packages.isEmpty()) { |
| 56 | ALOGE("No packages for uid %d", uid); |
| 57 | return opPackageName; // empty string |
| 58 | } |
| 59 | return packages[0]; |
| 60 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 61 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 62 | static bool checkRecordingInternal(const String16& opPackageName, pid_t pid, |
| 63 | uid_t uid, bool start) { |
| 64 | // Okay to not track in app ops as audio server is us and if |
| 65 | // device is rooted security model is considered compromised. |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 66 | if (isAudioServerOrRootUid(uid)) return true; |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 67 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 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 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 79 | permissionController, opPackageName, uid); |
| 80 | if (resolvedOpPackageName.size() == 0) { |
| 81 | return false; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 82 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 83 | |
| 84 | AppOpsManager appOps; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 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 { |
Eric Laurent | 5c5c8a1 | 2019-02-15 17:36:00 -0800 | [diff] [blame] | 93 | if (appOps.checkOp(op, uid, resolvedOpPackageName) != AppOpsManager::MODE_ALLOWED) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 94 | ALOGE("Request denied by app op: %d", op); |
| 95 | return false; |
| 96 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 102 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 103 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ false); |
| 104 | } |
| 105 | |
| 106 | bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 107 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ true); |
| 108 | } |
| 109 | |
| 110 | void finishRecording(const String16& opPackageName, uid_t uid) { |
| 111 | // Okay to not track in app ops as audio server is us and if |
| 112 | // device is rooted security model is considered compromised. |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 113 | if (isAudioServerOrRootUid(uid)) return; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 114 | |
| 115 | PermissionController permissionController; |
| 116 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 117 | permissionController, opPackageName, uid); |
| 118 | if (resolvedOpPackageName.size() == 0) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | AppOpsManager appOps; |
| 123 | const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio); |
| 124 | appOps.finishOp(op, uid, resolvedOpPackageName); |
| 125 | } |
| 126 | |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 127 | bool captureAudioOutputAllowed(pid_t pid, uid_t uid) { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 128 | if (isAudioServerOrRootUid(uid)) return true; |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 129 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 130 | bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid); |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 131 | if (!ok) ALOGV("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 132 | return ok; |
| 133 | } |
| 134 | |
Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 135 | bool captureMediaOutputAllowed(pid_t pid, uid_t uid) { |
| 136 | if (isAudioServerOrRootUid(uid)) return true; |
| 137 | static const String16 sCaptureMediaOutput("android.permission.CAPTURE_MEDIA_OUTPUT"); |
| 138 | bool ok = PermissionCache::checkPermission(sCaptureMediaOutput, pid, uid); |
| 139 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_MEDIA_OUTPUT"); |
| 140 | return ok; |
| 141 | } |
| 142 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 143 | bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 144 | // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 145 | bool ok = recordingAllowed(opPackageName, pid, uid); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 146 | |
| 147 | if (ok) { |
| 148 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 149 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 150 | ok = PermissionCache::checkPermission(sCaptureHotwordAllowed, pid, uid); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 151 | } |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 152 | if (!ok) ALOGV("android.permission.CAPTURE_AUDIO_HOTWORD"); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 153 | return ok; |
| 154 | } |
| 155 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 156 | bool settingsAllowed() { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 157 | // given this is a permission check, could this be isAudioServerOrRootUid()? |
| 158 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 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() { |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 167 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 168 | bool ok = PermissionCache::checkCallingPermission(sModifyAudioRouting); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 169 | if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING"); |
| 170 | return ok; |
| 171 | } |
| 172 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 173 | bool modifyDefaultAudioEffectsAllowed() { |
| 174 | static const String16 sModifyDefaultAudioEffectsAllowed( |
| 175 | "android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 176 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 177 | bool ok = PermissionCache::checkCallingPermission(sModifyDefaultAudioEffectsAllowed); |
| 178 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 179 | if (!ok) ALOGE("android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 180 | return ok; |
| 181 | } |
| 182 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 183 | bool dumpAllowed() { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 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) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 193 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 194 | ALOGE_IF(!ok, "Request requires %s", String8(sModifyPhoneState).c_str()); |
| 195 | return ok; |
| 196 | } |
| 197 | |
| 198 | // privileged behavior needed by Dialer, Settings, SetupWizard and CellBroadcastReceiver |
| 199 | bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid) { |
| 200 | static const String16 sWriteSecureSettings("android.permission.WRITE_SECURE_SETTINGS"); |
| 201 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid) |
| 202 | || PermissionCache::checkPermission(sWriteSecureSettings, pid, uid) |
| 203 | || PermissionCache::checkPermission(sModifyAudioRouting, pid, uid); |
| 204 | ALOGE_IF(!ok, "Request requires %s or %s", |
| 205 | String8(sModifyPhoneState).c_str(), String8(sWriteSecureSettings).c_str()); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 206 | return ok; |
| 207 | } |
| 208 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 209 | status_t checkIMemory(const sp<IMemory>& iMemory) |
| 210 | { |
| 211 | if (iMemory == 0) { |
| 212 | ALOGE("%s check failed: NULL IMemory pointer", __FUNCTION__); |
| 213 | return BAD_VALUE; |
| 214 | } |
| 215 | |
| 216 | sp<IMemoryHeap> heap = iMemory->getMemory(); |
| 217 | if (heap == 0) { |
| 218 | ALOGE("%s check failed: NULL heap pointer", __FUNCTION__); |
| 219 | return BAD_VALUE; |
| 220 | } |
| 221 | |
| 222 | off_t size = lseek(heap->getHeapID(), 0, SEEK_END); |
| 223 | lseek(heap->getHeapID(), 0, SEEK_SET); |
| 224 | |
| 225 | if (iMemory->pointer() == NULL || size < (off_t)iMemory->size()) { |
| 226 | ALOGE("%s check failed: pointer %p size %zu fd size %u", |
| 227 | __FUNCTION__, iMemory->pointer(), iMemory->size(), (uint32_t)size); |
| 228 | return BAD_VALUE; |
| 229 | } |
| 230 | |
| 231 | return NO_ERROR; |
| 232 | } |
| 233 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 234 | sp<content::pm::IPackageManagerNative> MediaPackageManager::retreivePackageManager() { |
| 235 | const sp<IServiceManager> sm = defaultServiceManager(); |
| 236 | if (sm == nullptr) { |
| 237 | ALOGW("%s: failed to retrieve defaultServiceManager", __func__); |
| 238 | return nullptr; |
| 239 | } |
| 240 | sp<IBinder> packageManager = sm->checkService(String16(nativePackageManagerName)); |
| 241 | if (packageManager == nullptr) { |
| 242 | ALOGW("%s: failed to retrieve native package manager", __func__); |
| 243 | return nullptr; |
| 244 | } |
| 245 | return interface_cast<content::pm::IPackageManagerNative>(packageManager); |
| 246 | } |
| 247 | |
| 248 | std::optional<bool> MediaPackageManager::doIsAllowed(uid_t uid) { |
| 249 | if (mPackageManager == nullptr) { |
| 250 | /** Can not fetch package manager at construction it may not yet be registered. */ |
| 251 | mPackageManager = retreivePackageManager(); |
| 252 | if (mPackageManager == nullptr) { |
| 253 | ALOGW("%s: Playback capture is denied as package manager is not reachable", __func__); |
| 254 | return std::nullopt; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | std::vector<std::string> packageNames; |
| 259 | auto status = mPackageManager->getNamesForUids({(int32_t)uid}, &packageNames); |
| 260 | if (!status.isOk()) { |
| 261 | ALOGW("%s: Playback capture is denied for uid %u as the package names could not be " |
| 262 | "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 263 | return std::nullopt; |
| 264 | } |
| 265 | if (packageNames.empty()) { |
| 266 | ALOGW("%s: Playback capture for uid %u is denied as no package name could be retrieved " |
| 267 | "from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 268 | return std::nullopt; |
| 269 | } |
| 270 | std::vector<bool> isAllowed; |
| 271 | status = mPackageManager->isAudioPlaybackCaptureAllowed(packageNames, &isAllowed); |
| 272 | if (!status.isOk()) { |
| 273 | ALOGW("%s: Playback capture is denied for uid %u as the manifest property could not be " |
| 274 | "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 275 | return std::nullopt; |
| 276 | } |
| 277 | if (packageNames.size() != isAllowed.size()) { |
| 278 | ALOGW("%s: Playback capture is denied for uid %u as the package manager returned incoherent" |
| 279 | " response size: %zu != %zu", __func__, uid, packageNames.size(), isAllowed.size()); |
| 280 | return std::nullopt; |
| 281 | } |
| 282 | |
| 283 | // Zip together packageNames and isAllowed for debug logs |
| 284 | Packages& packages = mDebugLog[uid]; |
| 285 | packages.resize(packageNames.size()); // Reuse all objects |
| 286 | std::transform(begin(packageNames), end(packageNames), begin(isAllowed), |
| 287 | begin(packages), [] (auto& name, bool isAllowed) -> Package { |
| 288 | return {std::move(name), isAllowed}; |
| 289 | }); |
| 290 | |
| 291 | // Only allow playback record if all packages in this UID allow it |
| 292 | bool playbackCaptureAllowed = std::all_of(begin(isAllowed), end(isAllowed), |
| 293 | [](bool b) { return b; }); |
| 294 | |
| 295 | return playbackCaptureAllowed; |
| 296 | } |
| 297 | |
| 298 | void MediaPackageManager::dump(int fd, int spaces) const { |
| 299 | dprintf(fd, "%*sAllow playback capture log:\n", spaces, ""); |
| 300 | if (mPackageManager == nullptr) { |
| 301 | dprintf(fd, "%*sNo package manager\n", spaces + 2, ""); |
| 302 | } |
| 303 | dprintf(fd, "%*sPackage manager errors: %u\n", spaces + 2, "", mPackageManagerErrors); |
| 304 | |
| 305 | for (const auto& uidCache : mDebugLog) { |
| 306 | for (const auto& package : std::get<Packages>(uidCache)) { |
| 307 | dprintf(fd, "%*s- uid=%5u, allowPlaybackCapture=%s, packageName=%s\n", spaces + 2, "", |
| 308 | std::get<const uid_t>(uidCache), |
| 309 | package.playbackCaptureAllowed ? "true " : "false", |
| 310 | package.name.c_str()); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 315 | } // namespace android |