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 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 25 | /* When performing permission checks we do not use permission cache for |
| 26 | * runtime permissions (protection level dangerous) as they may change at |
| 27 | * runtime. All other permissions (protection level normal and dangerous) |
| 28 | * can be cached as they never change. Of course all permission checked |
| 29 | * here are platform defined. |
| 30 | */ |
| 31 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 34 | static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO"); |
| 35 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 36 | static String16 resolveCallingPackage(PermissionController& permissionController, |
| 37 | const String16& opPackageName, uid_t uid) { |
| 38 | if (opPackageName.size() > 0) { |
| 39 | return opPackageName; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 40 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 41 | // In some cases the calling code has no access to the package it runs under. |
| 42 | // For example, code using the wilhelm framework's OpenSL-ES APIs. In this |
| 43 | // case we will get the packages for the calling UID and pick the first one |
| 44 | // for attributing the app op. This will work correctly for runtime permissions |
| 45 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 46 | // The caveat is that the operation may be attributed to the wrong package and |
| 47 | // stats based on app ops may be slightly off. |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 48 | Vector<String16> packages; |
| 49 | permissionController.getPackagesForUid(uid, packages); |
| 50 | if (packages.isEmpty()) { |
| 51 | ALOGE("No packages for uid %d", uid); |
| 52 | return opPackageName; // empty string |
| 53 | } |
| 54 | return packages[0]; |
| 55 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 56 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 57 | static bool checkRecordingInternal(const String16& opPackageName, pid_t pid, |
| 58 | uid_t uid, bool start) { |
| 59 | // Okay to not track in app ops as audio server is us and if |
| 60 | // device is rooted security model is considered compromised. |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 61 | if (isAudioServerOrRootUid(uid)) return true; |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 62 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 63 | // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder) |
| 64 | // may open a record track on behalf of a client. Note that pid may be a tid. |
| 65 | // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE. |
| 66 | PermissionController permissionController; |
| 67 | const bool ok = permissionController.checkPermission(sAndroidPermissionRecordAudio, pid, uid); |
| 68 | if (!ok) { |
| 69 | ALOGE("Request requires %s", String8(sAndroidPermissionRecordAudio).c_str()); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 74 | permissionController, opPackageName, uid); |
| 75 | if (resolvedOpPackageName.size() == 0) { |
| 76 | return false; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 77 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 78 | |
| 79 | AppOpsManager appOps; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 80 | const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio); |
| 81 | if (start) { |
| 82 | if (appOps.startOpNoThrow(op, uid, resolvedOpPackageName, /*startIfModeDefault*/ false) |
| 83 | != AppOpsManager::MODE_ALLOWED) { |
| 84 | ALOGE("Request denied by app op: %d", op); |
| 85 | return false; |
| 86 | } |
| 87 | } else { |
Eric Laurent | 5c5c8a1 | 2019-02-15 17:36:00 -0800 | [diff] [blame] | 88 | if (appOps.checkOp(op, uid, resolvedOpPackageName) != AppOpsManager::MODE_ALLOWED) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 89 | ALOGE("Request denied by app op: %d", op); |
| 90 | return false; |
| 91 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 97 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 98 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ false); |
| 99 | } |
| 100 | |
| 101 | bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 102 | return checkRecordingInternal(opPackageName, pid, uid, /*start*/ true); |
| 103 | } |
| 104 | |
| 105 | void finishRecording(const String16& opPackageName, uid_t uid) { |
| 106 | // Okay to not track in app ops as audio server is us and if |
| 107 | // device is rooted security model is considered compromised. |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 108 | if (isAudioServerOrRootUid(uid)) return; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 109 | |
| 110 | PermissionController permissionController; |
| 111 | String16 resolvedOpPackageName = resolveCallingPackage( |
| 112 | permissionController, opPackageName, uid); |
| 113 | if (resolvedOpPackageName.size() == 0) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | AppOpsManager appOps; |
| 118 | const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio); |
| 119 | appOps.finishOp(op, uid, resolvedOpPackageName); |
| 120 | } |
| 121 | |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 122 | bool captureAudioOutputAllowed(pid_t pid, uid_t uid) { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 123 | if (isAudioServerOrRootUid(uid)) return true; |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 124 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 125 | bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 126 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
| 127 | return ok; |
| 128 | } |
| 129 | |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 130 | bool captureHotwordAllowed(pid_t pid, uid_t uid) { |
| 131 | // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission |
| 132 | bool ok = recordingAllowed(String16(""), pid, uid); |
| 133 | |
| 134 | if (ok) { |
| 135 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 136 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 137 | ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed); |
| 138 | } |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 139 | if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 140 | return ok; |
| 141 | } |
| 142 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 143 | bool settingsAllowed() { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 144 | // given this is a permission check, could this be isAudioServerOrRootUid()? |
| 145 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 146 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 147 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 148 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 149 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 150 | return ok; |
| 151 | } |
| 152 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 153 | bool modifyAudioRoutingAllowed() { |
| 154 | static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 155 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 156 | bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 157 | if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING"); |
| 158 | return ok; |
| 159 | } |
| 160 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 161 | bool modifyDefaultAudioEffectsAllowed() { |
| 162 | static const String16 sModifyDefaultAudioEffectsAllowed( |
| 163 | "android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 164 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 165 | bool ok = PermissionCache::checkCallingPermission(sModifyDefaultAudioEffectsAllowed); |
| 166 | |
| 167 | #ifdef TARGET_ANDROID_THINGS |
| 168 | if (!ok) { |
| 169 | // Use a secondary permission on Android Things to allow a more lenient level of protection. |
| 170 | static const String16 sModifyDefaultAudioEffectsAndroidThingsAllowed( |
| 171 | "com.google.android.things.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 172 | ok = PermissionCache::checkCallingPermission( |
| 173 | sModifyDefaultAudioEffectsAndroidThingsAllowed); |
| 174 | } |
| 175 | if (!ok) ALOGE("com.google.android.things.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 176 | #else |
| 177 | if (!ok) ALOGE("android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 178 | #endif |
| 179 | return ok; |
| 180 | } |
| 181 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 182 | bool dumpAllowed() { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 183 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 184 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 185 | bool ok = PermissionCache::checkCallingPermission(sDump); |
| 186 | // convention is for caller to dump an error message to fd instead of logging here |
| 187 | //if (!ok) ALOGE("Request requires android.permission.DUMP"); |
| 188 | return ok; |
| 189 | } |
| 190 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 191 | bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) { |
| 192 | static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 193 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 194 | if (!ok) ALOGE("Request requires android.permission.MODIFY_PHONE_STATE"); |
| 195 | return ok; |
| 196 | } |
| 197 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 198 | status_t checkIMemory(const sp<IMemory>& iMemory) |
| 199 | { |
| 200 | if (iMemory == 0) { |
| 201 | ALOGE("%s check failed: NULL IMemory pointer", __FUNCTION__); |
| 202 | return BAD_VALUE; |
| 203 | } |
| 204 | |
| 205 | sp<IMemoryHeap> heap = iMemory->getMemory(); |
| 206 | if (heap == 0) { |
| 207 | ALOGE("%s check failed: NULL heap pointer", __FUNCTION__); |
| 208 | return BAD_VALUE; |
| 209 | } |
| 210 | |
| 211 | off_t size = lseek(heap->getHeapID(), 0, SEEK_END); |
| 212 | lseek(heap->getHeapID(), 0, SEEK_SET); |
| 213 | |
| 214 | if (iMemory->pointer() == NULL || size < (off_t)iMemory->size()) { |
| 215 | ALOGE("%s check failed: pointer %p size %zu fd size %u", |
| 216 | __FUNCTION__, iMemory->pointer(), iMemory->size(), (uint32_t)size); |
| 217 | return BAD_VALUE; |
| 218 | } |
| 219 | |
| 220 | return NO_ERROR; |
| 221 | } |
| 222 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 223 | } // namespace android |