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 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_MEDIAUTILS_SERVICEUTILITIES_H |
| 18 | #define ANDROID_MEDIAUTILS_SERVICEUTILITIES_H |
| 19 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 22 | #include <android/content/pm/IPackageManagerNative.h> |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 23 | #include <binder/IMemory.h> |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 24 | #include <binder/PermissionController.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 25 | #include <cutils/multiuser.h> |
| 26 | #include <private/android_filesystem_config.h> |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 27 | #include <system/audio-hal-enums.h> |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 28 | #include <android/media/permission/Identity.h> |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 29 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 30 | #include <map> |
| 31 | #include <optional> |
| 32 | #include <string> |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 33 | #include <unordered_map> |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 38 | // Audio permission utilities |
| 39 | |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 40 | // Used for calls that should originate from system services. |
| 41 | // We allow that some services might have separate processes to |
| 42 | // handle multiple users, e.g. u10_system, u10_bluetooth, u10_radio. |
| 43 | static inline bool isServiceUid(uid_t uid) { |
| 44 | return multiuser_get_app_id(uid) < AID_APP_START; |
| 45 | } |
| 46 | |
| 47 | // Used for calls that should originate from audioserver. |
| 48 | static inline bool isAudioServerUid(uid_t uid) { |
| 49 | return uid == AID_AUDIOSERVER; |
| 50 | } |
| 51 | |
| 52 | // Used for some permission checks. |
| 53 | // AID_ROOT is OK for command-line tests. Native audioserver always OK. |
| 54 | static inline bool isAudioServerOrRootUid(uid_t uid) { |
| 55 | return uid == AID_AUDIOSERVER || uid == AID_ROOT; |
| 56 | } |
| 57 | |
| 58 | // Used for calls that should come from system server or internal. |
| 59 | // Note: system server is multiprocess for multiple users. audioserver is not. |
| 60 | static inline bool isAudioServerOrSystemServerUid(uid_t uid) { |
| 61 | return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER; |
| 62 | } |
| 63 | |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 64 | // used for calls that should come from system_server or audio_server or media server and |
Jeffrey Carlyle | 62cc92b | 2019-09-17 11:15:15 -0700 | [diff] [blame] | 65 | // include AID_ROOT for command-line tests. |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 66 | static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) { |
| 67 | return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER |
| 68 | || uid == AID_MEDIA || uid == AID_ROOT; |
Jeffrey Carlyle | 62cc92b | 2019-09-17 11:15:15 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 71 | // Mediaserver may forward the client PID and UID as part of a binder interface call; |
| 72 | // otherwise the calling UID must be equal to the client UID. |
| 73 | static inline bool isAudioServerOrMediaServerUid(uid_t uid) { |
| 74 | switch (uid) { |
| 75 | case AID_MEDIA: |
| 76 | case AID_AUDIOSERVER: |
| 77 | return true; |
| 78 | default: |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |
Eric Laurent | 45e16b9 | 2021-05-20 11:10:47 +0200 | [diff] [blame] | 83 | bool recordingAllowed(const media::permission::Identity& identity, |
| 84 | audio_source_t source = AUDIO_SOURCE_DEFAULT); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 85 | bool startRecording(const media::permission::Identity& identity, |
| 86 | const String16& msg, audio_source_t source); |
| 87 | void finishRecording(const media::permission::Identity& identity, audio_source_t source); |
| 88 | bool captureAudioOutputAllowed(const media::permission::Identity& identity); |
| 89 | bool captureMediaOutputAllowed(const media::permission::Identity& identity); |
| 90 | bool captureTunerAudioInputAllowed(const media::permission::Identity& identity); |
| 91 | bool captureVoiceCommunicationOutputAllowed(const media::permission::Identity& identity); |
| 92 | bool captureHotwordAllowed(const media::permission::Identity& identity); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 93 | bool settingsAllowed(); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 94 | bool modifyAudioRoutingAllowed(); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 95 | bool modifyAudioRoutingAllowed(const media::permission::Identity& identity); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 96 | bool modifyDefaultAudioEffectsAllowed(); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 97 | bool modifyDefaultAudioEffectsAllowed(const media::permission::Identity& identity); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 98 | bool dumpAllowed(); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 99 | bool modifyPhoneStateAllowed(const media::permission::Identity& identity); |
| 100 | bool bypassInterruptionPolicyAllowed(const media::permission::Identity& identity); |
Eric Laurent | 269acb4 | 2021-04-23 16:53:22 +0200 | [diff] [blame] | 101 | void purgePermissionCache(); |
Eric Laurent | 45e16b9 | 2021-05-20 11:10:47 +0200 | [diff] [blame] | 102 | int32_t getOpForSource(audio_source_t source); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 103 | |
| 104 | media::permission::Identity getCallingIdentity(); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 105 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 106 | status_t checkIMemory(const sp<IMemory>& iMemory); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 107 | |
| 108 | class MediaPackageManager { |
| 109 | public: |
| 110 | /** Query the PackageManager to check if all apps of an UID allow playback capture. */ |
| 111 | bool allowPlaybackCapture(uid_t uid) { |
| 112 | auto result = doIsAllowed(uid); |
| 113 | if (!result) { |
| 114 | mPackageManagerErrors++; |
| 115 | } |
| 116 | return result.value_or(false); |
| 117 | } |
| 118 | void dump(int fd, int spaces = 0) const; |
| 119 | private: |
| 120 | static constexpr const char* nativePackageManagerName = "package_native"; |
| 121 | std::optional<bool> doIsAllowed(uid_t uid); |
Oreste Salerno | 703ec26 | 2020-12-17 16:38:38 +0100 | [diff] [blame] | 122 | sp<content::pm::IPackageManagerNative> retrievePackageManager(); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 123 | sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest |
| 124 | uint_t mPackageManagerErrors = 0; |
| 125 | struct Package { |
| 126 | std::string name; |
| 127 | bool playbackCaptureAllowed = false; |
| 128 | }; |
| 129 | using Packages = std::vector<Package>; |
| 130 | std::map<uid_t, Packages> mDebugLog; |
| 131 | }; |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 132 | |
| 133 | namespace mediautils { |
| 134 | |
| 135 | /** |
| 136 | * This class is used to retrieve (and cache) package information |
| 137 | * for a given uid. |
| 138 | */ |
| 139 | class UidInfo { |
| 140 | public: |
| 141 | struct Info { |
| 142 | uid_t uid = -1; // uid used for lookup. |
| 143 | std::string package; // package name. |
| 144 | std::string installer; // installer for the package (e.g. preload, play store). |
| 145 | int64_t versionCode = 0; // reported version code. |
| 146 | int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch. |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Returns the package information for a UID. |
| 151 | * |
| 152 | * The package name will be the uid if we cannot find the associated name. |
| 153 | * |
| 154 | * \param uid is the uid of the app or service. |
| 155 | */ |
| 156 | Info getInfo(uid_t uid); |
| 157 | |
| 158 | private: |
| 159 | std::mutex mLock; |
| 160 | // TODO: use concurrent hashmap with striped lock. |
| 161 | std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock) |
| 162 | }; |
| 163 | |
| 164 | } // namespace mediautils |
| 165 | |
| 166 | } // namespace android |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 167 | |
| 168 | #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H |