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
17 #ifndef ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
18 #define ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
19
20 #include <unistd.h>
21
22 #include <android/content/pm/IPackageManagerNative.h>
23 #include <android-base/thread_annotations.h>
24 #include <binder/IMemory.h>
25 #include <binder/PermissionController.h>
26 #include <cutils/multiuser.h>
27 #include <private/android_filesystem_config.h>
28 #include <system/audio-hal-enums.h>
29 #include <android/content/AttributionSourceState.h>
30 #include <binder/PermissionController.h>
31 #include <android/permission/PermissionChecker.h>
32
33 #include <map>
34 #include <optional>
35 #include <string>
36 #include <unordered_map>
37 #include <vector>
38
39 namespace android {
40
41 using content::AttributionSourceState;
42
43 // Audio permission utilities
44
45 // 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.
isServiceUid(uid_t uid)48 static 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.
isAudioServerUid(uid_t uid)53 static 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.
isAudioServerOrRootUid(uid_t uid)59 static 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.
65 // Note: if this method is modified, also update the same method in SensorService.h.
isAudioServerOrSystemServerUid(uid_t uid)66 static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
67 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
68 }
69
70 // used for calls that should come from system_server or audio_server or media server and
71 // include AID_ROOT for command-line tests.
isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid)72 static 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;
75 }
76
77 // 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.
isAudioServerOrMediaServerUid(uid_t uid)79 static 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
89 bool recordingAllowed(const AttributionSourceState& attributionSource,
90 audio_source_t source = AUDIO_SOURCE_DEFAULT);
91
92 bool recordingAllowed(const AttributionSourceState &attributionSource,
93 uint32_t virtualDeviceId,
94 audio_source_t source);
95 int startRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
96 const String16& msg, audio_source_t source);
97 void finishRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
98 audio_source_t source);
99 std::optional<AttributionSourceState> resolveAttributionSource(
100 const AttributionSourceState& callerAttributionSource, uint32_t virtualDeviceId);
101 bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource);
102 bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource);
103 bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource);
104 bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource);
105 bool bypassConcurrentPolicyAllowed(const AttributionSourceState& attributionSource) ;
106 bool accessUltrasoundAllowed(const AttributionSourceState& attributionSource);
107 bool captureHotwordAllowed(const AttributionSourceState& attributionSource);
108 bool settingsAllowed();
109 bool modifyAudioRoutingAllowed();
110 bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource);
111 bool modifyDefaultAudioEffectsAllowed();
112 bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource);
113 bool modifyAudioSettingsPrivilegedAllowed(const AttributionSourceState& attributionSource);
114 bool dumpAllowed();
115 bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource);
116 bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource);
117 bool callAudioInterceptionAllowed(const AttributionSourceState& attributionSource);
118 void purgePermissionCache();
119 bool mustAnonymizeBluetoothAddressLegacy(
120 const AttributionSourceState& attributionSource, const String16& caller);
121 void anonymizeBluetoothAddress(char *address);
122
123 bool isRecordOpRequired(audio_source_t source);
124 int32_t getOpForSource(audio_source_t source);
125
126 AttributionSourceState getCallingAttributionSource();
127
128 status_t checkIMemory(const sp<IMemory>& iMemory);
129
130 class MediaPackageManager {
131 public:
132 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
allowPlaybackCapture(uid_t uid)133 bool allowPlaybackCapture(uid_t uid) {
134 auto result = doIsAllowed(uid);
135 if (!result) {
136 mPackageManagerErrors++;
137 }
138 return result.value_or(false);
139 }
140 void dump(int fd, int spaces = 0) const;
141 private:
142 static constexpr const char* nativePackageManagerName = "package_native";
143 std::optional<bool> doIsAllowed(uid_t uid);
144 sp<content::pm::IPackageManagerNative> retrievePackageManager();
145 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
146 unsigned int mPackageManagerErrors = 0;
147 struct Package {
148 std::string name;
149 bool playbackCaptureAllowed = false;
150 };
151 using Packages = std::vector<Package>;
152 std::map<uid_t, Packages> mDebugLog;
153 };
154
155 namespace mediautils {
156
157 /**
158 * This class is used to retrieve (and cache) package information
159 * for a given uid.
160 */
161 class UidInfo {
162 public:
163 struct Info {
164 uid_t uid = -1; // uid used for lookup.
165 std::string package; // package name.
166 std::string installer; // installer for the package (e.g. preload, play store).
167 int64_t versionCode = 0; // reported version code.
168 int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
169 };
170
171 /**
172 * Returns the package information for a UID.
173 *
174 * The package name will be the uid if we cannot find the associated name.
175 *
176 * \param uid is the uid of the app or service.
177 */
178 std::shared_ptr<const Info> getCachedInfo(uid_t uid);
179
180 /* return a singleton */
181 static UidInfo& getUidInfo();
182
183 /* returns a non-null pointer to a const Info struct */
184 static std::shared_ptr<const Info> getInfo(uid_t uid);
185
186 private:
187 std::mutex mLock;
188 // TODO: use concurrent hashmap with striped lock.
189 std::unordered_map<uid_t, std::shared_ptr<const Info>> mInfoMap GUARDED_BY(mLock);
190 };
191
192 } // namespace mediautils
193
194 } // namespace android
195
196 #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
197