blob: b365648cf5edcdd43e504b4763d16be78b4c3556 [file] [log] [blame]
Glenn Kasten44deb052012-02-05 18:09:08 -08001/*
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 Rocard8be94972019-02-22 13:26:25 -080017#ifndef ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
18#define ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
19
Glenn Kasten44deb052012-02-05 18:09:08 -080020#include <unistd.h>
21
Kevin Rocard8be94972019-02-22 13:26:25 -080022#include <android/content/pm/IPackageManagerNative.h>
Andy Hungb26a1c82024-08-30 11:54:40 -070023#include <android-base/thread_annotations.h>
Eric Laurent9b11c022018-06-06 19:19:22 -070024#include <binder/IMemory.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080025#include <binder/PermissionController.h>
Andy Hung4ef19fa2018-05-15 19:35:29 -070026#include <cutils/multiuser.h>
27#include <private/android_filesystem_config.h>
Nate Myrene69cada2020-12-10 10:00:36 -080028#include <system/audio-hal-enums.h>
Svet Ganov3e5f14f2021-05-13 22:51:08 +000029#include <android/content/AttributionSourceState.h>
30#include <binder/PermissionController.h>
31#include <android/permission/PermissionChecker.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080032
Kevin Rocard8be94972019-02-22 13:26:25 -080033#include <map>
34#include <optional>
35#include <string>
Andy Hunga85efab2019-12-23 11:41:29 -080036#include <unordered_map>
Kevin Rocard8be94972019-02-22 13:26:25 -080037#include <vector>
38
Glenn Kasten44deb052012-02-05 18:09:08 -080039namespace android {
40
Svet Ganov3e5f14f2021-05-13 22:51:08 +000041using content::AttributionSourceState;
42
Andy Hungab7ef302018-05-15 19:35:29 -070043// Audio permission utilities
44
Andy Hung4ef19fa2018-05-15 19:35:29 -070045// Used for calls that should originate from system services.
46// We allow that some services might have separate processes to
47// handle multiple users, e.g. u10_system, u10_bluetooth, u10_radio.
48static inline bool isServiceUid(uid_t uid) {
49 return multiuser_get_app_id(uid) < AID_APP_START;
50}
51
52// Used for calls that should originate from audioserver.
53static inline bool isAudioServerUid(uid_t uid) {
54 return uid == AID_AUDIOSERVER;
55}
56
57// Used for some permission checks.
58// AID_ROOT is OK for command-line tests. Native audioserver always OK.
59static inline bool isAudioServerOrRootUid(uid_t uid) {
60 return uid == AID_AUDIOSERVER || uid == AID_ROOT;
61}
62
63// Used for calls that should come from system server or internal.
64// Note: system server is multiprocess for multiple users. audioserver is not.
Eric Laurentffa034d2022-01-13 16:52:44 +010065// Note: if this method is modified, also update the same method in SensorService.h.
Andy Hung4ef19fa2018-05-15 19:35:29 -070066static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
67 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
68}
69
Eric Laurent58a0dd82019-10-24 12:42:17 -070070// used for calls that should come from system_server or audio_server or media server and
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070071// include AID_ROOT for command-line tests.
Eric Laurent58a0dd82019-10-24 12:42:17 -070072static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) {
73 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER
74 || uid == AID_MEDIA || uid == AID_ROOT;
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070075}
76
Andy Hung4ef19fa2018-05-15 19:35:29 -070077// Mediaserver may forward the client PID and UID as part of a binder interface call;
78// otherwise the calling UID must be equal to the client UID.
79static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
80 switch (uid) {
81 case AID_MEDIA:
82 case AID_AUDIOSERVER:
83 return true;
84 default:
85 return false;
86 }
87}
88
Svet Ganov3e5f14f2021-05-13 22:51:08 +000089bool recordingAllowed(const AttributionSourceState& attributionSource,
Eric Laurent45e16b92021-05-20 11:10:47 +020090 audio_source_t source = AUDIO_SOURCE_DEFAULT);
Marvin Ramine5a122d2023-12-07 13:57:59 +010091
92bool recordingAllowed(const AttributionSourceState &attributionSource,
93 uint32_t virtualDeviceId,
94 audio_source_t source);
Marvin Raminb03b49f2024-04-04 16:25:31 +020095bool startRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
96 const String16& msg, audio_source_t source);
97void finishRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
98 audio_source_t source);
Svet Ganov3e5f14f2021-05-13 22:51:08 +000099std::optional<AttributionSourceState> resolveAttributionSource(
Marvin Raminb03b49f2024-04-04 16:25:31 +0200100 const AttributionSourceState& callerAttributionSource, uint32_t virtualDeviceId);
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000101bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource);
102bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource);
103bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource);
104bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource);
Carter Hsua3abb402021-10-26 11:11:20 +0800105bool accessUltrasoundAllowed(const AttributionSourceState& attributionSource);
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000106bool captureHotwordAllowed(const AttributionSourceState& attributionSource);
Glenn Kasten44deb052012-02-05 18:09:08 -0800107bool settingsAllowed();
Eric Laurent5284ed52014-05-29 14:37:38 -0700108bool modifyAudioRoutingAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000109bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700110bool modifyDefaultAudioEffectsAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000111bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource);
Glenn Kasten44deb052012-02-05 18:09:08 -0800112bool dumpAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000113bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource);
114bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource);
Eric Laurentb0eff0f2021-11-09 16:05:49 +0100115bool callAudioInterceptionAllowed(const AttributionSourceState& attributionSource);
Eric Laurent269acb42021-04-23 16:53:22 +0200116void purgePermissionCache();
Eric Laurent45e16b92021-05-20 11:10:47 +0200117int32_t getOpForSource(audio_source_t source);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700118
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000119AttributionSourceState getCallingAttributionSource();
Eric Laurent42984412019-05-09 17:57:03 -0700120
Eric Laurent9b11c022018-06-06 19:19:22 -0700121status_t checkIMemory(const sp<IMemory>& iMemory);
Kevin Rocard8be94972019-02-22 13:26:25 -0800122
123class MediaPackageManager {
124public:
125 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
126 bool allowPlaybackCapture(uid_t uid) {
127 auto result = doIsAllowed(uid);
128 if (!result) {
129 mPackageManagerErrors++;
130 }
131 return result.value_or(false);
132 }
133 void dump(int fd, int spaces = 0) const;
134private:
135 static constexpr const char* nativePackageManagerName = "package_native";
136 std::optional<bool> doIsAllowed(uid_t uid);
Oreste Salerno703ec262020-12-17 16:38:38 +0100137 sp<content::pm::IPackageManagerNative> retrievePackageManager();
Kevin Rocard8be94972019-02-22 13:26:25 -0800138 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
Atneya Nair7795f5f2022-05-23 13:44:00 -0400139 unsigned int mPackageManagerErrors = 0;
Kevin Rocard8be94972019-02-22 13:26:25 -0800140 struct Package {
141 std::string name;
142 bool playbackCaptureAllowed = false;
143 };
144 using Packages = std::vector<Package>;
145 std::map<uid_t, Packages> mDebugLog;
146};
Andy Hunga85efab2019-12-23 11:41:29 -0800147
148namespace mediautils {
149
150/**
151 * This class is used to retrieve (and cache) package information
152 * for a given uid.
153 */
154class UidInfo {
155public:
156 struct Info {
157 uid_t uid = -1; // uid used for lookup.
158 std::string package; // package name.
159 std::string installer; // installer for the package (e.g. preload, play store).
160 int64_t versionCode = 0; // reported version code.
161 int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
162 };
163
164 /**
165 * Returns the package information for a UID.
166 *
167 * The package name will be the uid if we cannot find the associated name.
168 *
169 * \param uid is the uid of the app or service.
170 */
Andy Hungb26a1c82024-08-30 11:54:40 -0700171 std::shared_ptr<const Info> getCachedInfo(uid_t uid);
172
173 /* return a singleton */
174 static UidInfo& getUidInfo();
175
176 /* returns a non-null pointer to a const Info struct */
177 static std::shared_ptr<const Info> getInfo(uid_t uid);
Andy Hunga85efab2019-12-23 11:41:29 -0800178
179private:
180 std::mutex mLock;
181 // TODO: use concurrent hashmap with striped lock.
Andy Hungb26a1c82024-08-30 11:54:40 -0700182 std::unordered_map<uid_t, std::shared_ptr<const Info>> mInfoMap GUARDED_BY(mLock);
Andy Hunga85efab2019-12-23 11:41:29 -0800183};
184
185} // namespace mediautils
186
187} // namespace android
Kevin Rocard8be94972019-02-22 13:26:25 -0800188
189#endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H