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