blob: aa9e1201f3521c928d58d696a86245ca7880c980 [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);
Svet Ganov3e5f14f2021-05-13 22:51:08 +000094bool startRecording(const AttributionSourceState& attributionSource,
Philip P. Moltmannbda45752020-07-17 16:41:18 -070095 const String16& msg, audio_source_t source);
Svet Ganov3e5f14f2021-05-13 22:51:08 +000096void finishRecording(const AttributionSourceState& attributionSource, audio_source_t source);
97std::optional<AttributionSourceState> resolveAttributionSource(
98 const AttributionSourceState& callerAttributionSource);
99bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource);
100bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource);
101bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource);
102bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource);
Carter Hsua3abb402021-10-26 11:11:20 +0800103bool accessUltrasoundAllowed(const AttributionSourceState& attributionSource);
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000104bool captureHotwordAllowed(const AttributionSourceState& attributionSource);
Glenn Kasten44deb052012-02-05 18:09:08 -0800105bool settingsAllowed();
Eric Laurent5284ed52014-05-29 14:37:38 -0700106bool modifyAudioRoutingAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000107bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700108bool modifyDefaultAudioEffectsAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000109bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource);
Glenn Kasten44deb052012-02-05 18:09:08 -0800110bool dumpAllowed();
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000111bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource);
112bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource);
Eric Laurentb0eff0f2021-11-09 16:05:49 +0100113bool callAudioInterceptionAllowed(const AttributionSourceState& attributionSource);
Eric Laurent269acb42021-04-23 16:53:22 +0200114void purgePermissionCache();
Eric Laurent24df2832023-10-13 18:13:43 +0200115bool mustAnonymizeBluetoothAddress(
116 const AttributionSourceState& attributionSource, const String16& caller);
117void anonymizeBluetoothAddress(char *address);
118
Eric Laurent45e16b92021-05-20 11:10:47 +0200119int32_t getOpForSource(audio_source_t source);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700120
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000121AttributionSourceState getCallingAttributionSource();
Eric Laurent42984412019-05-09 17:57:03 -0700122
Eric Laurent9b11c022018-06-06 19:19:22 -0700123status_t checkIMemory(const sp<IMemory>& iMemory);
Kevin Rocard8be94972019-02-22 13:26:25 -0800124
125class MediaPackageManager {
126public:
127 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
128 bool allowPlaybackCapture(uid_t uid) {
129 auto result = doIsAllowed(uid);
130 if (!result) {
131 mPackageManagerErrors++;
132 }
133 return result.value_or(false);
134 }
135 void dump(int fd, int spaces = 0) const;
136private:
137 static constexpr const char* nativePackageManagerName = "package_native";
138 std::optional<bool> doIsAllowed(uid_t uid);
Oreste Salerno703ec262020-12-17 16:38:38 +0100139 sp<content::pm::IPackageManagerNative> retrievePackageManager();
Kevin Rocard8be94972019-02-22 13:26:25 -0800140 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
Atneya Nair7795f5f2022-05-23 13:44:00 -0400141 unsigned int mPackageManagerErrors = 0;
Kevin Rocard8be94972019-02-22 13:26:25 -0800142 struct Package {
143 std::string name;
144 bool playbackCaptureAllowed = false;
145 };
146 using Packages = std::vector<Package>;
147 std::map<uid_t, Packages> mDebugLog;
148};
Andy Hunga85efab2019-12-23 11:41:29 -0800149
150namespace mediautils {
151
152/**
153 * This class is used to retrieve (and cache) package information
154 * for a given uid.
155 */
156class UidInfo {
157public:
158 struct Info {
159 uid_t uid = -1; // uid used for lookup.
160 std::string package; // package name.
161 std::string installer; // installer for the package (e.g. preload, play store).
162 int64_t versionCode = 0; // reported version code.
163 int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
164 };
165
166 /**
167 * Returns the package information for a UID.
168 *
169 * The package name will be the uid if we cannot find the associated name.
170 *
171 * \param uid is the uid of the app or service.
172 */
173 Info getInfo(uid_t uid);
174
175private:
176 std::mutex mLock;
177 // TODO: use concurrent hashmap with striped lock.
178 std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock)
179};
180
181} // namespace mediautils
182
183} // namespace android
Kevin Rocard8be94972019-02-22 13:26:25 -0800184
185#endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H