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> |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 26 | #include <media/AidlConversion.h> |
| 27 | #include <media/AidlConversionUtil.h> |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 28 | #include <android/content/AttributionSourceState.h> |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 29 | |
Eric Laurent | 24df283 | 2023-10-13 18:13:43 +0200 | [diff] [blame^] | 30 | #include <com_android_media_audio_flags.h> |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 31 | #include <iterator> |
| 32 | #include <algorithm> |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 33 | #include <pwd.h> |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 34 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 35 | /* When performing permission checks we do not use permission cache for |
| 36 | * runtime permissions (protection level dangerous) as they may change at |
| 37 | * runtime. All other permissions (protection level normal and dangerous) |
| 38 | * can be cached as they never change. Of course all permission checked |
| 39 | * here are platform defined. |
| 40 | */ |
| 41 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 44 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 45 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 46 | static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO"); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 47 | static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE"); |
| 48 | static const String16 sModifyAudioRouting("android.permission.MODIFY_AUDIO_ROUTING"); |
Eric Laurent | b0eff0f | 2021-11-09 16:05:49 +0100 | [diff] [blame] | 49 | static const String16 sCallAudioInterception("android.permission.CALL_AUDIO_INTERCEPTION"); |
Eric Laurent | 24df283 | 2023-10-13 18:13:43 +0200 | [diff] [blame^] | 50 | static const String16 sAndroidPermissionBluetoothConnect("android.permission.BLUETOOTH_CONNECT"); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 51 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 52 | static String16 resolveCallingPackage(PermissionController& permissionController, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 53 | const std::optional<String16> opPackageName, uid_t uid) { |
| 54 | if (opPackageName.has_value() && opPackageName.value().size() > 0) { |
| 55 | return opPackageName.value(); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 56 | } |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 57 | // In some cases the calling code has no access to the package it runs under. |
| 58 | // For example, code using the wilhelm framework's OpenSL-ES APIs. In this |
| 59 | // case we will get the packages for the calling UID and pick the first one |
| 60 | // for attributing the app op. This will work correctly for runtime permissions |
| 61 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 62 | // The caveat is that the operation may be attributed to the wrong package and |
| 63 | // stats based on app ops may be slightly off. |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 64 | Vector<String16> packages; |
| 65 | permissionController.getPackagesForUid(uid, packages); |
| 66 | if (packages.isEmpty()) { |
| 67 | ALOGE("No packages for uid %d", uid); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 68 | return String16(); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 69 | } |
| 70 | return packages[0]; |
| 71 | } |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 72 | |
Eric Laurent | 45e16b9 | 2021-05-20 11:10:47 +0200 | [diff] [blame] | 73 | int32_t getOpForSource(audio_source_t source) { |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 74 | switch (source) { |
| 75 | case AUDIO_SOURCE_HOTWORD: |
| 76 | return AppOpsManager::OP_RECORD_AUDIO_HOTWORD; |
Mickey Keeley | 90d657d | 2021-10-20 18:06:38 -0700 | [diff] [blame] | 77 | case AUDIO_SOURCE_ECHO_REFERENCE: // fallthrough |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 78 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 79 | return AppOpsManager::OP_RECORD_AUDIO_OUTPUT; |
Nate Myren | f7c8835 | 2021-04-12 14:41:49 -0700 | [diff] [blame] | 80 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 81 | return AppOpsManager::OP_RECORD_INCOMING_PHONE_AUDIO; |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 82 | case AUDIO_SOURCE_DEFAULT: |
| 83 | default: |
| 84 | return AppOpsManager::OP_RECORD_AUDIO; |
| 85 | } |
| 86 | } |
| 87 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 88 | std::optional<AttributionSourceState> resolveAttributionSource( |
| 89 | const AttributionSourceState& callerAttributionSource) { |
| 90 | AttributionSourceState nextAttributionSource = callerAttributionSource; |
| 91 | |
| 92 | if (!nextAttributionSource.packageName.has_value()) { |
| 93 | nextAttributionSource = AttributionSourceState(nextAttributionSource); |
| 94 | PermissionController permissionController; |
| 95 | const uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(nextAttributionSource.uid)); |
| 96 | nextAttributionSource.packageName = VALUE_OR_FATAL(legacy2aidl_String16_string( |
| 97 | resolveCallingPackage(permissionController, VALUE_OR_FATAL( |
| 98 | aidl2legacy_string_view_String16(nextAttributionSource.packageName.value_or(""))), |
| 99 | uid))); |
| 100 | if (!nextAttributionSource.packageName.has_value()) { |
| 101 | return std::nullopt; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | AttributionSourceState myAttributionSource; |
| 106 | myAttributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid())); |
| 107 | myAttributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid())); |
Nate Myren | e84d691 | 2022-11-10 14:09:34 -0800 | [diff] [blame] | 108 | // Create a static token for audioserver requests, which identifies the |
| 109 | // audioserver to the app ops system |
| 110 | static sp<BBinder> appOpsToken = sp<BBinder>::make(); |
| 111 | myAttributionSource.token = appOpsToken; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 112 | myAttributionSource.next.push_back(nextAttributionSource); |
| 113 | |
| 114 | return std::optional<AttributionSourceState>{myAttributionSource}; |
| 115 | } |
| 116 | |
| 117 | static bool checkRecordingInternal(const AttributionSourceState& attributionSource, |
| 118 | const String16& msg, bool start, audio_source_t source) { |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 119 | // 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] | 120 | // device is rooted security model is considered compromised. |
Jeffrey Carlyle | 62cc92b | 2019-09-17 11:15:15 -0700 | [diff] [blame] | 121 | // system_server loses its RECORD_AUDIO permission when a secondary |
| 122 | // user is active, but it is a core system service so let it through. |
| 123 | // TODO(b/141210120): UserManager.DISALLOW_RECORD_AUDIO should not affect system user 0 |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 124 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 125 | if (isAudioServerOrMediaServerOrSystemServerOrRootUid(uid)) return true; |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 126 | |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 127 | // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder) |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 128 | // may open a record track on behalf of a client. Note that pid may be a tid. |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 129 | // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE. |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 130 | const std::optional<AttributionSourceState> resolvedAttributionSource = |
| 131 | resolveAttributionSource(attributionSource); |
| 132 | if (!resolvedAttributionSource.has_value()) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 136 | const int32_t attributedOpCode = getOpForSource(source); |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 137 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 138 | permission::PermissionChecker permissionChecker; |
| 139 | bool permitted = false; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 140 | if (start) { |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 141 | permitted = (permissionChecker.checkPermissionForStartDataDeliveryFromDatasource( |
| 142 | sAndroidPermissionRecordAudio, resolvedAttributionSource.value(), msg, |
| 143 | attributedOpCode) != permission::PermissionChecker::PERMISSION_HARD_DENIED); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 144 | } else { |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 145 | permitted = (permissionChecker.checkPermissionForPreflightFromDatasource( |
| 146 | sAndroidPermissionRecordAudio, resolvedAttributionSource.value(), msg, |
| 147 | attributedOpCode) != permission::PermissionChecker::PERMISSION_HARD_DENIED); |
Svetoslav Ganov | 599ec46 | 2018-03-01 22:19:55 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 150 | return permitted; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 153 | bool recordingAllowed(const AttributionSourceState& attributionSource, audio_source_t source) { |
| 154 | return checkRecordingInternal(attributionSource, String16(), /*start*/ false, source); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 157 | bool startRecording(const AttributionSourceState& attributionSource, const String16& msg, |
| 158 | audio_source_t source) { |
| 159 | return checkRecordingInternal(attributionSource, msg, /*start*/ true, source); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 162 | void finishRecording(const AttributionSourceState& attributionSource, audio_source_t source) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 163 | // Okay to not track in app ops as audio server is us and if |
| 164 | // device is rooted security model is considered compromised. |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 165 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 166 | if (isAudioServerOrMediaServerOrSystemServerOrRootUid(uid)) return; |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 167 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 168 | // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder) |
| 169 | // may open a record track on behalf of a client. Note that pid may be a tid. |
| 170 | // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE. |
| 171 | const std::optional<AttributionSourceState> resolvedAttributionSource = |
| 172 | resolveAttributionSource(attributionSource); |
| 173 | if (!resolvedAttributionSource.has_value()) { |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 177 | const int32_t attributedOpCode = getOpForSource(source); |
| 178 | permission::PermissionChecker permissionChecker; |
| 179 | permissionChecker.finishDataDeliveryFromDatasource(attributedOpCode, |
| 180 | resolvedAttributionSource.value()); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 183 | bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource) { |
| 184 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 185 | if (isAudioServerOrRootUid(uid)) return true; |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 186 | static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT"); |
lpeter | 20d2e03 | 2022-06-01 19:38:44 +0800 | [diff] [blame] | 187 | // Use PermissionChecker, which includes some logic for allowing the isolated |
| 188 | // HotwordDetectionService to hold certain permissions. |
| 189 | permission::PermissionChecker permissionChecker; |
| 190 | bool ok = (permissionChecker.checkPermissionForPreflight( |
| 191 | sCaptureAudioOutput, attributionSource, String16(), |
| 192 | AppOpsManager::OP_NONE) != permission::PermissionChecker::PERMISSION_HARD_DENIED); |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 193 | if (!ok) ALOGV("Request requires android.permission.CAPTURE_AUDIO_OUTPUT"); |
Jeff Brown | 893a564 | 2013-08-16 20:19:26 -0700 | [diff] [blame] | 194 | return ok; |
| 195 | } |
| 196 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 197 | bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource) { |
| 198 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 199 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 200 | if (isAudioServerOrRootUid(uid)) return true; |
| 201 | static const String16 sCaptureMediaOutput("android.permission.CAPTURE_MEDIA_OUTPUT"); |
| 202 | bool ok = PermissionCache::checkPermission(sCaptureMediaOutput, pid, uid); |
| 203 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_MEDIA_OUTPUT"); |
| 204 | return ok; |
| 205 | } |
| 206 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 207 | bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource) { |
| 208 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 209 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Hayden Gomes | b742992 | 2020-12-11 13:59:18 -0800 | [diff] [blame] | 210 | if (isAudioServerOrRootUid(uid)) return true; |
| 211 | static const String16 sCaptureTunerAudioInput("android.permission.CAPTURE_TUNER_AUDIO_INPUT"); |
| 212 | bool ok = PermissionCache::checkPermission(sCaptureTunerAudioInput, pid, uid); |
| 213 | if (!ok) ALOGV("Request requires android.permission.CAPTURE_TUNER_AUDIO_INPUT"); |
| 214 | return ok; |
| 215 | } |
| 216 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 217 | bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource) { |
| 218 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 219 | uid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 220 | if (isAudioServerOrRootUid(uid)) return true; |
| 221 | static const String16 sCaptureVoiceCommOutput( |
| 222 | "android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT"); |
| 223 | bool ok = PermissionCache::checkPermission(sCaptureVoiceCommOutput, pid, uid); |
| 224 | if (!ok) ALOGE("Request requires android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT"); |
| 225 | return ok; |
| 226 | } |
| 227 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 228 | bool accessUltrasoundAllowed(const AttributionSourceState& attributionSource) { |
| 229 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 230 | uid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
| 231 | if (isAudioServerOrRootUid(uid)) return true; |
| 232 | static const String16 sAccessUltrasound( |
| 233 | "android.permission.ACCESS_ULTRASOUND"); |
| 234 | bool ok = PermissionCache::checkPermission(sAccessUltrasound, pid, uid); |
| 235 | if (!ok) ALOGE("Request requires android.permission.ACCESS_ULTRASOUND"); |
| 236 | return ok; |
| 237 | } |
| 238 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 239 | bool captureHotwordAllowed(const AttributionSourceState& attributionSource) { |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 240 | // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 241 | bool ok = recordingAllowed(attributionSource); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 242 | |
| 243 | if (ok) { |
| 244 | static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD"); |
Ahaan Ugale | b18227c | 2021-06-07 11:52:54 -0700 | [diff] [blame] | 245 | // Use PermissionChecker, which includes some logic for allowing the isolated |
| 246 | // HotwordDetectionService to hold certain permissions. |
| 247 | permission::PermissionChecker permissionChecker; |
| 248 | ok = (permissionChecker.checkPermissionForPreflight( |
| 249 | sCaptureHotwordAllowed, attributionSource, String16(), |
| 250 | AppOpsManager::OP_NONE) != permission::PermissionChecker::PERMISSION_HARD_DENIED); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 251 | } |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 252 | if (!ok) ALOGV("android.permission.CAPTURE_AUDIO_HOTWORD"); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 253 | return ok; |
| 254 | } |
| 255 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 256 | bool settingsAllowed() { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 257 | // given this is a permission check, could this be isAudioServerOrRootUid()? |
| 258 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 259 | static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 260 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 261 | bool ok = PermissionCache::checkCallingPermission(sAudioSettings); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 262 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 263 | return ok; |
| 264 | } |
| 265 | |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 266 | bool modifyAudioRoutingAllowed() { |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 267 | return modifyAudioRoutingAllowed(getCallingAttributionSource()); |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 270 | bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource) { |
| 271 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 272 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 273 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 274 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 275 | bool ok = PermissionCache::checkPermission(sModifyAudioRouting, pid, uid); |
| 276 | if (!ok) ALOGE("%s(): android.permission.MODIFY_AUDIO_ROUTING denied for uid %d", |
| 277 | __func__, uid); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 278 | return ok; |
| 279 | } |
| 280 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 281 | bool modifyDefaultAudioEffectsAllowed() { |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 282 | return modifyDefaultAudioEffectsAllowed(getCallingAttributionSource()); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 285 | bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource) { |
| 286 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 287 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 288 | if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true; |
| 289 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 290 | static const String16 sModifyDefaultAudioEffectsAllowed( |
| 291 | "android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS"); |
| 292 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 293 | bool ok = PermissionCache::checkPermission(sModifyDefaultAudioEffectsAllowed, pid, uid); |
| 294 | ALOGE_IF(!ok, "%s(): android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS denied for uid %d", |
| 295 | __func__, uid); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 296 | return ok; |
| 297 | } |
| 298 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 299 | bool dumpAllowed() { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 300 | static const String16 sDump("android.permission.DUMP"); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 301 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 302 | bool ok = PermissionCache::checkCallingPermission(sDump); |
| 303 | // convention is for caller to dump an error message to fd instead of logging here |
| 304 | //if (!ok) ALOGE("Request requires android.permission.DUMP"); |
| 305 | return ok; |
| 306 | } |
| 307 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 308 | bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource) { |
| 309 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 310 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 311 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 312 | ALOGE_IF(!ok, "Request requires %s", String8(sModifyPhoneState).c_str()); |
| 313 | return ok; |
| 314 | } |
| 315 | |
| 316 | // privileged behavior needed by Dialer, Settings, SetupWizard and CellBroadcastReceiver |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 317 | bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource) { |
| 318 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 319 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 320 | static const String16 sWriteSecureSettings("android.permission.WRITE_SECURE_SETTINGS"); |
| 321 | bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid) |
| 322 | || PermissionCache::checkPermission(sWriteSecureSettings, pid, uid) |
| 323 | || PermissionCache::checkPermission(sModifyAudioRouting, pid, uid); |
| 324 | ALOGE_IF(!ok, "Request requires %s or %s", |
| 325 | String8(sModifyPhoneState).c_str(), String8(sWriteSecureSettings).c_str()); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 326 | return ok; |
| 327 | } |
| 328 | |
Eric Laurent | b0eff0f | 2021-11-09 16:05:49 +0100 | [diff] [blame] | 329 | bool callAudioInterceptionAllowed(const AttributionSourceState& attributionSource) { |
| 330 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 331 | pid_t pid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(attributionSource.pid)); |
| 332 | |
| 333 | // IMPORTANT: Use PermissionCache - not a runtime permission and may not change. |
| 334 | bool ok = PermissionCache::checkPermission(sCallAudioInterception, pid, uid); |
Eric Laurent | a6244a0 | 2021-12-14 14:05:25 +0100 | [diff] [blame] | 335 | if (!ok) ALOGV("%s(): android.permission.CALL_AUDIO_INTERCEPTION denied for uid %d", |
Eric Laurent | b0eff0f | 2021-11-09 16:05:49 +0100 | [diff] [blame] | 336 | __func__, uid); |
| 337 | return ok; |
| 338 | } |
| 339 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 340 | AttributionSourceState getCallingAttributionSource() { |
| 341 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 342 | attributionSource.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t( |
| 343 | IPCThreadState::self()->getCallingPid())); |
| 344 | attributionSource.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t( |
| 345 | IPCThreadState::self()->getCallingUid())); |
| 346 | attributionSource.token = sp<BBinder>::make(); |
| 347 | return attributionSource; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Eric Laurent | 269acb4 | 2021-04-23 16:53:22 +0200 | [diff] [blame] | 350 | void purgePermissionCache() { |
| 351 | PermissionCache::purgeCache(); |
| 352 | } |
| 353 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 354 | status_t checkIMemory(const sp<IMemory>& iMemory) |
| 355 | { |
| 356 | if (iMemory == 0) { |
| 357 | ALOGE("%s check failed: NULL IMemory pointer", __FUNCTION__); |
| 358 | return BAD_VALUE; |
| 359 | } |
| 360 | |
| 361 | sp<IMemoryHeap> heap = iMemory->getMemory(); |
| 362 | if (heap == 0) { |
| 363 | ALOGE("%s check failed: NULL heap pointer", __FUNCTION__); |
| 364 | return BAD_VALUE; |
| 365 | } |
| 366 | |
| 367 | off_t size = lseek(heap->getHeapID(), 0, SEEK_END); |
| 368 | lseek(heap->getHeapID(), 0, SEEK_SET); |
| 369 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 370 | if (iMemory->unsecurePointer() == NULL || size < (off_t)iMemory->size()) { |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 371 | ALOGE("%s check failed: pointer %p size %zu fd size %u", |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 372 | __FUNCTION__, iMemory->unsecurePointer(), iMemory->size(), (uint32_t)size); |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 373 | return BAD_VALUE; |
| 374 | } |
| 375 | |
| 376 | return NO_ERROR; |
| 377 | } |
| 378 | |
Eric Laurent | 24df283 | 2023-10-13 18:13:43 +0200 | [diff] [blame^] | 379 | /** |
| 380 | * Determines if the MAC address in Bluetooth device descriptors returned by APIs of |
| 381 | * a native audio service (audio flinger, audio policy) must be anonymized. |
| 382 | * MAC addresses returned to system server or apps with BLUETOOTH_CONNECT permission |
| 383 | * are not anonymized. |
| 384 | * |
| 385 | * @param attributionSource The attribution source of the calling app. |
| 386 | * @param caller string identifying the caller for logging. |
| 387 | * @return true if the MAC addresses must be anonymized, false otherwise. |
| 388 | */ |
| 389 | bool mustAnonymizeBluetoothAddress( |
| 390 | const AttributionSourceState& attributionSource, const String16& caller) { |
| 391 | if (!com::android::media::audio::flags::bluetooth_mac_address_anonymization()) { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
| 396 | if (isAudioServerOrSystemServerUid(uid)) { |
| 397 | return false; |
| 398 | } |
| 399 | const std::optional<AttributionSourceState> resolvedAttributionSource = |
| 400 | resolveAttributionSource(attributionSource); |
| 401 | if (!resolvedAttributionSource.has_value()) { |
| 402 | return true; |
| 403 | } |
| 404 | permission::PermissionChecker permissionChecker; |
| 405 | return permissionChecker.checkPermissionForPreflightFromDatasource( |
| 406 | sAndroidPermissionBluetoothConnect, resolvedAttributionSource.value(), caller, |
| 407 | AppOpsManager::OP_BLUETOOTH_CONNECT) |
| 408 | != permission::PermissionChecker::PERMISSION_GRANTED; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Modifies the passed MAC address string in place for consumption by unprivileged clients. |
| 413 | * the string is assumed to have a valid MAC address format. |
| 414 | * the anonymzation must be kept in sync with toAnonymizedAddress() in BluetoothUtils.java |
| 415 | * |
| 416 | * @param address input/output the char string contining the MAC address to anonymize. |
| 417 | */ |
| 418 | void anonymizeBluetoothAddress(char *address) { |
| 419 | if (address == nullptr || strlen(address) != strlen("AA:BB:CC:DD:EE:FF")) { |
| 420 | return; |
| 421 | } |
| 422 | memcpy(address, "XX:XX:XX:XX", strlen("XX:XX:XX:XX")); |
| 423 | } |
| 424 | |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 425 | sp<content::pm::IPackageManagerNative> MediaPackageManager::retrievePackageManager() { |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 426 | const sp<IServiceManager> sm = defaultServiceManager(); |
| 427 | if (sm == nullptr) { |
| 428 | ALOGW("%s: failed to retrieve defaultServiceManager", __func__); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 429 | return nullptr; |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 430 | } |
| 431 | sp<IBinder> packageManager = sm->checkService(String16(nativePackageManagerName)); |
| 432 | if (packageManager == nullptr) { |
| 433 | ALOGW("%s: failed to retrieve native package manager", __func__); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 434 | return nullptr; |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 435 | } |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 436 | return interface_cast<content::pm::IPackageManagerNative>(packageManager); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | std::optional<bool> MediaPackageManager::doIsAllowed(uid_t uid) { |
| 440 | if (mPackageManager == nullptr) { |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 441 | /** Can not fetch package manager at construction it may not yet be registered. */ |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 442 | mPackageManager = retrievePackageManager(); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 443 | if (mPackageManager == nullptr) { |
| 444 | ALOGW("%s: Playback capture is denied as package manager is not reachable", __func__); |
| 445 | return std::nullopt; |
| 446 | } |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 449 | // Retrieve package names for the UID and transform to a std::vector<std::string>. |
| 450 | Vector<String16> str16PackageNames; |
| 451 | PermissionController{}.getPackagesForUid(uid, str16PackageNames); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 452 | std::vector<std::string> packageNames; |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 453 | for (const auto& str16PackageName : str16PackageNames) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 454 | packageNames.emplace_back(String8(str16PackageName).c_str()); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 455 | } |
| 456 | if (packageNames.empty()) { |
| 457 | ALOGW("%s: Playback capture for uid %u is denied as no package name could be retrieved " |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 458 | "from the package manager.", __func__, uid); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 459 | return std::nullopt; |
| 460 | } |
| 461 | std::vector<bool> isAllowed; |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 462 | auto status = mPackageManager->isAudioPlaybackCaptureAllowed(packageNames, &isAllowed); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 463 | if (!status.isOk()) { |
| 464 | ALOGW("%s: Playback capture is denied for uid %u as the manifest property could not be " |
| 465 | "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str()); |
| 466 | return std::nullopt; |
| 467 | } |
| 468 | if (packageNames.size() != isAllowed.size()) { |
| 469 | ALOGW("%s: Playback capture is denied for uid %u as the package manager returned incoherent" |
| 470 | " response size: %zu != %zu", __func__, uid, packageNames.size(), isAllowed.size()); |
| 471 | return std::nullopt; |
| 472 | } |
| 473 | |
| 474 | // Zip together packageNames and isAllowed for debug logs |
| 475 | Packages& packages = mDebugLog[uid]; |
| 476 | packages.resize(packageNames.size()); // Reuse all objects |
| 477 | std::transform(begin(packageNames), end(packageNames), begin(isAllowed), |
| 478 | begin(packages), [] (auto& name, bool isAllowed) -> Package { |
| 479 | return {std::move(name), isAllowed}; |
| 480 | }); |
| 481 | |
| 482 | // Only allow playback record if all packages in this UID allow it |
| 483 | bool playbackCaptureAllowed = std::all_of(begin(isAllowed), end(isAllowed), |
| 484 | [](bool b) { return b; }); |
| 485 | |
| 486 | return playbackCaptureAllowed; |
| 487 | } |
| 488 | |
| 489 | void MediaPackageManager::dump(int fd, int spaces) const { |
| 490 | dprintf(fd, "%*sAllow playback capture log:\n", spaces, ""); |
| 491 | if (mPackageManager == nullptr) { |
| 492 | dprintf(fd, "%*sNo package manager\n", spaces + 2, ""); |
| 493 | } |
| 494 | dprintf(fd, "%*sPackage manager errors: %u\n", spaces + 2, "", mPackageManagerErrors); |
| 495 | |
| 496 | for (const auto& uidCache : mDebugLog) { |
| 497 | for (const auto& package : std::get<Packages>(uidCache)) { |
| 498 | dprintf(fd, "%*s- uid=%5u, allowPlaybackCapture=%s, packageName=%s\n", spaces + 2, "", |
| 499 | std::get<const uid_t>(uidCache), |
| 500 | package.playbackCaptureAllowed ? "true " : "false", |
| 501 | package.name.c_str()); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 506 | // How long we hold info before we re-fetch it (24 hours) if we found it previously. |
| 507 | static constexpr nsecs_t INFO_EXPIRATION_NS = 24 * 60 * 60 * NANOS_PER_SECOND; |
| 508 | // Maximum info records we retain before clearing everything. |
| 509 | static constexpr size_t INFO_CACHE_MAX = 1000; |
| 510 | |
| 511 | // The original code is from MediaMetricsService.cpp. |
| 512 | mediautils::UidInfo::Info mediautils::UidInfo::getInfo(uid_t uid) |
| 513 | { |
| 514 | const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 515 | struct mediautils::UidInfo::Info info; |
| 516 | { |
| 517 | std::lock_guard _l(mLock); |
| 518 | auto it = mInfoMap.find(uid); |
| 519 | if (it != mInfoMap.end()) { |
| 520 | info = it->second; |
| 521 | ALOGV("%s: uid %d expiration %lld now %lld", |
| 522 | __func__, uid, (long long)info.expirationNs, (long long)now); |
| 523 | if (info.expirationNs <= now) { |
| 524 | // purge the stale entry and fall into re-fetching |
| 525 | ALOGV("%s: entry for uid %d expired, now %lld", |
| 526 | __func__, uid, (long long)now); |
| 527 | mInfoMap.erase(it); |
| 528 | info.uid = (uid_t)-1; // this is always fully overwritten |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // if we did not find it in our map, look it up |
| 534 | if (info.uid == (uid_t)(-1)) { |
| 535 | sp<IServiceManager> sm = defaultServiceManager(); |
| 536 | sp<content::pm::IPackageManagerNative> package_mgr; |
| 537 | if (sm.get() == nullptr) { |
| 538 | ALOGE("%s: Cannot find service manager", __func__); |
| 539 | } else { |
| 540 | sp<IBinder> binder = sm->getService(String16("package_native")); |
| 541 | if (binder.get() == nullptr) { |
| 542 | ALOGE("%s: Cannot find package_native", __func__); |
| 543 | } else { |
| 544 | package_mgr = interface_cast<content::pm::IPackageManagerNative>(binder); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // find package name |
| 549 | std::string pkg; |
| 550 | if (package_mgr != nullptr) { |
| 551 | std::vector<std::string> names; |
| 552 | binder::Status status = package_mgr->getNamesForUids({(int)uid}, &names); |
| 553 | if (!status.isOk()) { |
| 554 | ALOGE("%s: getNamesForUids failed: %s", |
| 555 | __func__, status.exceptionMessage().c_str()); |
| 556 | } else { |
| 557 | if (!names[0].empty()) { |
| 558 | pkg = names[0].c_str(); |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (pkg.empty()) { |
| 564 | struct passwd pw{}, *result; |
| 565 | char buf[8192]; // extra buffer space - should exceed what is |
| 566 | // required in struct passwd_pw (tested), |
| 567 | // and even then this is only used in backup |
| 568 | // when the package manager is unavailable. |
| 569 | if (getpwuid_r(uid, &pw, buf, sizeof(buf), &result) == 0 |
| 570 | && result != nullptr |
| 571 | && result->pw_name != nullptr) { |
| 572 | pkg = result->pw_name; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // strip any leading "shared:" strings that came back |
| 577 | if (pkg.compare(0, 7, "shared:") == 0) { |
| 578 | pkg.erase(0, 7); |
| 579 | } |
| 580 | |
| 581 | // determine how pkg was installed and the versionCode |
| 582 | std::string installer; |
| 583 | int64_t versionCode = 0; |
| 584 | bool notFound = false; |
| 585 | if (pkg.empty()) { |
| 586 | pkg = std::to_string(uid); // not found |
| 587 | notFound = true; |
| 588 | } else if (strchr(pkg.c_str(), '.') == nullptr) { |
| 589 | // not of form 'com.whatever...'; assume internal |
| 590 | // so we don't need to look it up in package manager. |
Andy Hung | e6a65ac | 2020-01-08 16:56:17 -0800 | [diff] [blame] | 591 | } else if (strncmp(pkg.c_str(), "android.", 8) == 0) { |
| 592 | // android.* packages are assumed fine |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 593 | } else if (package_mgr.get() != nullptr) { |
| 594 | String16 pkgName16(pkg.c_str()); |
| 595 | binder::Status status = package_mgr->getInstallerForPackage(pkgName16, &installer); |
| 596 | if (!status.isOk()) { |
| 597 | ALOGE("%s: getInstallerForPackage failed: %s", |
| 598 | __func__, status.exceptionMessage().c_str()); |
| 599 | } |
| 600 | |
| 601 | // skip if we didn't get an installer |
| 602 | if (status.isOk()) { |
| 603 | status = package_mgr->getVersionCodeForPackage(pkgName16, &versionCode); |
| 604 | if (!status.isOk()) { |
| 605 | ALOGE("%s: getVersionCodeForPackage failed: %s", |
| 606 | __func__, status.exceptionMessage().c_str()); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | ALOGV("%s: package '%s' installed by '%s' versioncode %lld", |
| 611 | __func__, pkg.c_str(), installer.c_str(), (long long)versionCode); |
| 612 | } |
| 613 | |
| 614 | // add it to the map, to save a subsequent lookup |
| 615 | std::lock_guard _l(mLock); |
| 616 | // first clear if we have too many cached elements. This would be rare. |
| 617 | if (mInfoMap.size() >= INFO_CACHE_MAX) mInfoMap.clear(); |
| 618 | |
| 619 | // always overwrite |
| 620 | info.uid = uid; |
| 621 | info.package = std::move(pkg); |
| 622 | info.installer = std::move(installer); |
| 623 | info.versionCode = versionCode; |
| 624 | info.expirationNs = now + (notFound ? 0 : INFO_EXPIRATION_NS); |
| 625 | ALOGV("%s: adding uid %d package '%s' expirationNs: %lld", |
| 626 | __func__, uid, info.package.c_str(), (long long)info.expirationNs); |
| 627 | mInfoMap[uid] = info; |
| 628 | } |
| 629 | return info; |
| 630 | } |
| 631 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 632 | } // namespace android |