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> |
| 21 | #include "ServiceUtilities.h" |
| 22 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 23 | /* When performing permission checks we do not use permission cache for |
| 24 | * runtime permissions (protection level dangerous) as they may change at |
| 25 | * runtime. All other permissions (protection level normal and dangerous) |
| 26 | * can be cached as they never change. Of course all permission checked |
| 27 | * here are platform defined. |
| 28 | */ |
| 29 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 32 | // Not valid until initialized by AudioFlinger constructor. It would have to be |
| 33 | // re-initialized if the process containing AudioFlinger service forks (which it doesn't). |
| 34 | pid_t getpid_cached; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 35 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 36 | bool recordingAllowed(const String16& opPackageName) { |
| 37 | // Note: We are getting the UID from the calling IPC thread state because all |
| 38 | // clients that perform recording create AudioRecord in their own processes |
| 39 | // and the system does not create AudioRecord objects on behalf of apps. This |
| 40 | // differs from playback where in some situations the system recreates AudioTrack |
| 41 | // instances associated with a client's MediaPlayer on behalf of this client. |
| 42 | // In the latter case we have to store the client UID and pass in along for |
| 43 | // security checks. |
| 44 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 45 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 46 | static const String16 sRecordAudio("android.permission.RECORD_AUDIO"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 47 | |
| 48 | // IMPORTANT: Don't use PermissionCache - a runtime permission and may change. |
| 49 | const bool ok = checkCallingPermission(sRecordAudio); |
| 50 | if (!ok) { |
| 51 | ALOGE("Request requires android.permission.RECORD_AUDIO"); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 56 | String16 checkedOpPackageName = opPackageName; |
| 57 | |
| 58 | // In some cases the calling code has no access to the package it runs under. |
| 59 | // For example, code using the wilhelm framework's OpenSL-ES APIs. In this |
| 60 | // case we will get the packages for the calling UID and pick the first one |
| 61 | // for attributing the app op. This will work correctly for runtime permissions |
| 62 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 63 | // The caveat is that the operation may be attributed to the wrong package and |
| 64 | // stats based on app ops may be slightly off. |
| 65 | if (checkedOpPackageName.size() <= 0) { |
| 66 | sp<IServiceManager> sm = defaultServiceManager(); |
| 67 | sp<IBinder> binder = sm->getService(String16("permission")); |
| 68 | if (binder == 0) { |
| 69 | ALOGE("Cannot get permission service"); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 74 | Vector<String16> packages; |
| 75 | |
| 76 | permCtrl->getPackagesForUid(uid, packages); |
| 77 | |
| 78 | if (packages.isEmpty()) { |
| 79 | ALOGE("No packages for calling UID"); |
| 80 | return false; |
| 81 | } |
| 82 | checkedOpPackageName = packages[0]; |
| 83 | } |
| 84 | |
| 85 | AppOpsManager appOps; |
Eric Laurent | 617575b | 2015-05-12 18:32:04 -0700 | [diff] [blame^] | 86 | if (appOps.noteOp(AppOpsManager::OP_RECORD_AUDIO, uid, checkedOpPackageName) |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 87 | != AppOpsManager::MODE_ALLOWED) { |
| 88 | ALOGE("Request denied by app op OP_RECORD_AUDIO"); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 95 | bool captureAudioOutputAllowed() { |
| 96 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 97 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 98 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 99 | bool ok = PermissionCache::checkCallingPermission(sCaptureAudioOutput); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 100 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
| 101 | return ok; |
| 102 | } |
| 103 | |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 104 | bool captureHotwordAllowed() { |
| 105 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 106 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 107 | bool ok = PermissionCache::checkCallingPermission(sCaptureHotwordAllowed); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 108 | if (!ok) ALOGE("android.permission.CAPTURE_AUDIO_HOTWORD"); |
| 109 | return ok; |
| 110 | } |
| 111 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 112 | bool settingsAllowed() { |
| 113 | if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; |
| 114 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 115 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 116 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 117 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 118 | return ok; |
| 119 | } |
| 120 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 121 | bool modifyAudioRoutingAllowed() { |
| 122 | static const String16 sModifyAudioRoutingAllowed("android.permission.MODIFY_AUDIO_ROUTING"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 123 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 124 | bool ok = PermissionCache::checkCallingPermission(sModifyAudioRoutingAllowed); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 125 | if (!ok) ALOGE("android.permission.MODIFY_AUDIO_ROUTING"); |
| 126 | return ok; |
| 127 | } |
| 128 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 129 | bool dumpAllowed() { |
| 130 | // don't optimize for same pid, since mediaserver never dumps itself |
| 131 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 132 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 133 | bool ok = PermissionCache::checkCallingPermission(sDump); |
| 134 | // convention is for caller to dump an error message to fd instead of logging here |
| 135 | //if (!ok) ALOGE("Request requires android.permission.DUMP"); |
| 136 | return ok; |
| 137 | } |
| 138 | |
| 139 | } // namespace android |