blob: 9c02cd41f5bcf6c53cef51ed58336a582a3a22da [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>
Eric Laurent9b11c022018-06-06 19:19:22 -070023#include <binder/IMemory.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080024#include <binder/PermissionController.h>
Andy Hung4ef19fa2018-05-15 19:35:29 -070025#include <cutils/multiuser.h>
26#include <private/android_filesystem_config.h>
Nate Myrene69cada2020-12-10 10:00:36 -080027#include <system/audio-hal-enums.h>
Svet Ganov3e5f14f2021-05-13 22:51:08 +000028#include <android/content/AttributionSourceState.h>
29#include <binder/PermissionController.h>
30#include <android/permission/PermissionChecker.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080031
Kevin Rocard8be94972019-02-22 13:26:25 -080032#include <map>
33#include <optional>
34#include <string>
Andy Hunga85efab2019-12-23 11:41:29 -080035#include <unordered_map>
Kevin Rocard8be94972019-02-22 13:26:25 -080036#include <vector>
37
Glenn Kasten44deb052012-02-05 18:09:08 -080038namespace android {
39
Svet Ganov3e5f14f2021-05-13 22:51:08 +000040using content::AttributionSourceState;
41
Andy Hungab7ef302018-05-15 19:35:29 -070042// Audio permission utilities
43
Andy Hung4ef19fa2018-05-15 19:35:29 -070044// Used for calls that should originate from system services.
45// We allow that some services might have separate processes to
46// handle multiple users, e.g. u10_system, u10_bluetooth, u10_radio.
47static inline bool isServiceUid(uid_t uid) {
48 return multiuser_get_app_id(uid) < AID_APP_START;
49}
50
51// Used for calls that should originate from audioserver.
52static inline bool isAudioServerUid(uid_t uid) {
53 return uid == AID_AUDIOSERVER;
54}
55
56// Used for some permission checks.
57// AID_ROOT is OK for command-line tests. Native audioserver always OK.
58static inline bool isAudioServerOrRootUid(uid_t uid) {
59 return uid == AID_AUDIOSERVER || uid == AID_ROOT;
60}
61
62// Used for calls that should come from system server or internal.
63// Note: system server is multiprocess for multiple users. audioserver is not.
Eric Laurentffa034d2022-01-13 16:52:44 +010064// Note: if this method is modified, also update the same method in SensorService.h.
Andy Hung4ef19fa2018-05-15 19:35:29 -070065static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
66 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
67}
68
Eric Laurent58a0dd82019-10-24 12:42:17 -070069// used for calls that should come from system_server or audio_server or media server and
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070070// include AID_ROOT for command-line tests.
Eric Laurent58a0dd82019-10-24 12:42:17 -070071static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) {
72 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER
73 || uid == AID_MEDIA || uid == AID_ROOT;
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070074}
75
Andy Hung4ef19fa2018-05-15 19:35:29 -070076// Mediaserver may forward the client PID and UID as part of a binder interface call;
77// otherwise the calling UID must be equal to the client UID.
78static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
79 switch (uid) {
80 case AID_MEDIA:
81 case AID_AUDIOSERVER:
82 return true;
83 default:
84 return false;
85 }
86}
87
Svet Ganov3e5f14f2021-05-13 22:51:08 +000088bool recordingAllowed(const AttributionSourceState& attributionSource,
Eric Laurent45e16b92021-05-20 11:10:47 +020089 audio_source_t source = AUDIO_SOURCE_DEFAULT);
Marvin Ramine5a122d2023-12-07 13:57:59 +010090
91bool recordingAllowed(const AttributionSourceState &attributionSource,
92 uint32_t virtualDeviceId,
93 audio_source_t source);
Marvin Raminb03b49f2024-04-04 16:25:31 +020094bool startRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
95 const String16& msg, audio_source_t source);
96void finishRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
97 audio_source_t source);
Svet Ganov3e5f14f2021-05-13 22:51:08 +000098std::optional<AttributionSourceState> resolveAttributionSource(
Marvin Raminb03b49f2024-04-04 16:25:31 +020099 const AttributionSourceState& callerAttributionSource, uint32_t virtualDeviceId);
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000100bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource);
101bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource);
102bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource);
103bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource);
Carter Hsua3abb402021-10-26 11:11:20 +0800104bool accessUltrasoundAllowed(const AttributionSourceState& attributionSource);
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000105bool captureHotwordAllowed(const AttributionSourceState& attributionSource);
Glenn Kasten44deb052012-02-05 18:09:08 -0800106bool settingsAllowed();
Eric Laurent5284ed52014-05-29 14:37:38 -0700107bool modifyAudioRoutingAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000108bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700109bool modifyDefaultAudioEffectsAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000110bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource);
Glenn Kasten44deb052012-02-05 18:09:08 -0800111bool dumpAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000112bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource);
113bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource);
Eric Laurentb0eff0f2021-11-09 16:05:49 +0100114bool callAudioInterceptionAllowed(const AttributionSourceState& attributionSource);
Eric Laurent269acb42021-04-23 16:53:22 +0200115void purgePermissionCache();
Charlie Boutier62496752024-05-31 15:52:27 +0000116bool mustAnonymizeBluetoothAddress(
117 const AttributionSourceState& attributionSource, const String16& caller);
118void anonymizeBluetoothAddress(char *address);
119
Eric Laurent45e16b92021-05-20 11:10:47 +0200120int32_t getOpForSource(audio_source_t source);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700121
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000122AttributionSourceState getCallingAttributionSource();
Eric Laurent42984412019-05-09 17:57:03 -0700123
Eric Laurent9b11c022018-06-06 19:19:22 -0700124status_t checkIMemory(const sp<IMemory>& iMemory);
Kevin Rocard8be94972019-02-22 13:26:25 -0800125
126class MediaPackageManager {
127public:
128 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
129 bool allowPlaybackCapture(uid_t uid) {
130 auto result = doIsAllowed(uid);
131 if (!result) {
132 mPackageManagerErrors++;
133 }
134 return result.value_or(false);
135 }
136 void dump(int fd, int spaces = 0) const;
137private:
138 static constexpr const char* nativePackageManagerName = "package_native";
139 std::optional<bool> doIsAllowed(uid_t uid);
Oreste Salerno703ec262020-12-17 16:38:38 +0100140 sp<content::pm::IPackageManagerNative> retrievePackageManager();
Kevin Rocard8be94972019-02-22 13:26:25 -0800141 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
Atneya Nair7795f5f2022-05-23 13:44:00 -0400142 unsigned int mPackageManagerErrors = 0;
Kevin Rocard8be94972019-02-22 13:26:25 -0800143 struct Package {
144 std::string name;
145 bool playbackCaptureAllowed = false;
146 };
147 using Packages = std::vector<Package>;
148 std::map<uid_t, Packages> mDebugLog;
149};
Andy Hunga85efab2019-12-23 11:41:29 -0800150
151namespace mediautils {
152
153/**
154 * This class is used to retrieve (and cache) package information
155 * for a given uid.
156 */
157class UidInfo {
158public:
159 struct Info {
160 uid_t uid = -1; // uid used for lookup.
161 std::string package; // package name.
162 std::string installer; // installer for the package (e.g. preload, play store).
163 int64_t versionCode = 0; // reported version code.
164 int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
165 };
166
167 /**
168 * Returns the package information for a UID.
169 *
170 * The package name will be the uid if we cannot find the associated name.
171 *
172 * \param uid is the uid of the app or service.
173 */
174 Info getInfo(uid_t uid);
175
176private:
177 std::mutex mLock;
178 // TODO: use concurrent hashmap with striped lock.
179 std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock)
180};
181
182} // namespace mediautils
183
184} // namespace android
Kevin Rocard8be94972019-02-22 13:26:25 -0800185
186#endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H