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