blob: e7132b857f4c02888d0f2c2531426627ac9b6ba5 [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>
Philip P. Moltmannbda45752020-07-17 16:41:18 -070028#include <android/media/permission/Identity.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080029
Kevin Rocard8be94972019-02-22 13:26:25 -080030#include <map>
31#include <optional>
32#include <string>
Andy Hunga85efab2019-12-23 11:41:29 -080033#include <unordered_map>
Kevin Rocard8be94972019-02-22 13:26:25 -080034#include <vector>
35
Glenn Kasten44deb052012-02-05 18:09:08 -080036namespace android {
37
Andy Hungab7ef302018-05-15 19:35:29 -070038// Audio permission utilities
39
Andy Hung4ef19fa2018-05-15 19:35:29 -070040// 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.
43static 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.
48static 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.
54static 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.
60static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
61 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
62}
63
Eric Laurent58a0dd82019-10-24 12:42:17 -070064// used for calls that should come from system_server or audio_server or media server and
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070065// include AID_ROOT for command-line tests.
Eric Laurent58a0dd82019-10-24 12:42:17 -070066static 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 Carlyle62cc92b2019-09-17 11:15:15 -070069}
70
Andy Hung4ef19fa2018-05-15 19:35:29 -070071// 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.
73static 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 Laurent45e16b92021-05-20 11:10:47 +020083bool recordingAllowed(const media::permission::Identity& identity,
84 audio_source_t source = AUDIO_SOURCE_DEFAULT);
Philip P. Moltmannbda45752020-07-17 16:41:18 -070085bool startRecording(const media::permission::Identity& identity,
86 const String16& msg, audio_source_t source);
87void finishRecording(const media::permission::Identity& identity, audio_source_t source);
88bool captureAudioOutputAllowed(const media::permission::Identity& identity);
89bool captureMediaOutputAllowed(const media::permission::Identity& identity);
90bool captureTunerAudioInputAllowed(const media::permission::Identity& identity);
91bool captureVoiceCommunicationOutputAllowed(const media::permission::Identity& identity);
92bool captureHotwordAllowed(const media::permission::Identity& identity);
Glenn Kasten44deb052012-02-05 18:09:08 -080093bool settingsAllowed();
Eric Laurent5284ed52014-05-29 14:37:38 -070094bool modifyAudioRoutingAllowed();
Philip P. Moltmannbda45752020-07-17 16:41:18 -070095bool modifyAudioRoutingAllowed(const media::permission::Identity& identity);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070096bool modifyDefaultAudioEffectsAllowed();
Philip P. Moltmannbda45752020-07-17 16:41:18 -070097bool modifyDefaultAudioEffectsAllowed(const media::permission::Identity& identity);
Glenn Kasten44deb052012-02-05 18:09:08 -080098bool dumpAllowed();
Philip P. Moltmannbda45752020-07-17 16:41:18 -070099bool modifyPhoneStateAllowed(const media::permission::Identity& identity);
100bool bypassInterruptionPolicyAllowed(const media::permission::Identity& identity);
Eric Laurent269acb42021-04-23 16:53:22 +0200101void purgePermissionCache();
Eric Laurent45e16b92021-05-20 11:10:47 +0200102int32_t getOpForSource(audio_source_t source);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700103
104media::permission::Identity getCallingIdentity();
Eric Laurent42984412019-05-09 17:57:03 -0700105
Eric Laurent9b11c022018-06-06 19:19:22 -0700106status_t checkIMemory(const sp<IMemory>& iMemory);
Kevin Rocard8be94972019-02-22 13:26:25 -0800107
108class MediaPackageManager {
109public:
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;
119private:
120 static constexpr const char* nativePackageManagerName = "package_native";
121 std::optional<bool> doIsAllowed(uid_t uid);
Oreste Salerno703ec262020-12-17 16:38:38 +0100122 sp<content::pm::IPackageManagerNative> retrievePackageManager();
Kevin Rocard8be94972019-02-22 13:26:25 -0800123 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 Hunga85efab2019-12-23 11:41:29 -0800132
133namespace mediautils {
134
135/**
136 * This class is used to retrieve (and cache) package information
137 * for a given uid.
138 */
139class UidInfo {
140public:
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
158private:
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 Rocard8be94972019-02-22 13:26:25 -0800167
168#endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H