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 | |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 33 | // Not valid until initialized by AudioFlinger constructor. It would have to be |
| 34 | // 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^] | 35 | // This is often used to validate binder interface calls within audioserver |
| 36 | // (e.g. AudioPolicyManager to AudioFlinger). |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 37 | pid_t getpid_cached; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 38 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame^] | 39 | // A trusted calling UID may specify the client UID as part of a binder interface call. |
| 40 | // otherwise the calling UID must be equal to the client UID. |
| 41 | bool isTrustedCallingUid(uid_t uid) { |
| 42 | switch (uid) { |
| 43 | case AID_MEDIA: |
| 44 | case AID_AUDIOSERVER: |
| 45 | return true; |
| 46 | default: |
| 47 | return false; |
| 48 | } |
| 49 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 50 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame^] | 51 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) { |
| 52 | // we're always OK. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 53 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame^] | 54 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 55 | static const String16 sRecordAudio("android.permission.RECORD_AUDIO"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 56 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame^] | 57 | // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder) |
| 58 | // may open a record track on behalf of a client. Note that pid may be a tid. |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 59 | // IMPORTANT: Don't use PermissionCache - a runtime permission and may change. |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame^] | 60 | const bool ok = checkPermission(sRecordAudio, pid, uid); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 61 | if (!ok) { |
| 62 | ALOGE("Request requires android.permission.RECORD_AUDIO"); |
| 63 | return false; |
| 64 | } |
| 65 | |
Glenn Kasten | de07e37 | 2015-06-02 12:34:11 -0700 | [diff] [blame] | 66 | // To permit command-line native tests |
| 67 | if (uid == AID_ROOT) return true; |
| 68 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 69 | String16 checkedOpPackageName = opPackageName; |
| 70 | |
| 71 | // In some cases the calling code has no access to the package it runs under. |
| 72 | // For example, code using the wilhelm framework's OpenSL-ES APIs. In this |
| 73 | // case we will get the packages for the calling UID and pick the first one |
| 74 | // for attributing the app op. This will work correctly for runtime permissions |
| 75 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 76 | // The caveat is that the operation may be attributed to the wrong package and |
| 77 | // stats based on app ops may be slightly off. |
| 78 | if (checkedOpPackageName.size() <= 0) { |
| 79 | sp<IServiceManager> sm = defaultServiceManager(); |
| 80 | sp<IBinder> binder = sm->getService(String16("permission")); |
| 81 | if (binder == 0) { |
| 82 | ALOGE("Cannot get permission service"); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 87 | Vector<String16> packages; |
| 88 | |
| 89 | permCtrl->getPackagesForUid(uid, packages); |
| 90 | |
| 91 | if (packages.isEmpty()) { |
| 92 | ALOGE("No packages for calling UID"); |
| 93 | return false; |
| 94 | } |
| 95 | checkedOpPackageName = packages[0]; |
| 96 | } |
| 97 | |
| 98 | AppOpsManager appOps; |
Eric Laurent | 617575b | 2015-05-12 18:32:04 -0700 | [diff] [blame] | 99 | if (appOps.noteOp(AppOpsManager::OP_RECORD_AUDIO, uid, checkedOpPackageName) |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 100 | != AppOpsManager::MODE_ALLOWED) { |
| 101 | ALOGE("Request denied by app op OP_RECORD_AUDIO"); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 108 | bool captureAudioOutputAllowed() { |
| 109 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 110 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 111 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 112 | bool ok = PermissionCache::checkCallingPermission(sCaptureAudioOutput); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 113 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
| 114 | return ok; |
| 115 | } |
| 116 | |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 117 | bool captureHotwordAllowed() { |
| 118 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 119 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 120 | bool ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 121 | if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 122 | return ok; |
| 123 | } |
| 124 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 125 | bool settingsAllowed() { |
| 126 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 127 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 128 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 129 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 130 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 131 | return ok; |
| 132 | } |
| 133 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 134 | bool modifyAudioRoutingAllowed() { |
| 135 | static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 136 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 137 | bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 138 | if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING"); |
| 139 | return ok; |
| 140 | } |
| 141 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 142 | bool dumpAllowed() { |
| 143 | // don't optimize for same pid, since mediaserver never dumps itself |
| 144 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 145 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 146 | bool ok = PermissionCache::checkCallingPermission(sDump); |
| 147 | // convention is for caller to dump an error message to fd instead of logging here |
| 148 | //if (!ok) ALOGE("Request requires android.permission.DUMP"); |
| 149 | return ok; |
| 150 | } |
| 151 | |
| 152 | } // namespace android |