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
28 #include <map>
29 #include <optional>
30 #include <string>
31 #include <unordered_map>
32 #include <vector>
33
34 namespace android {
35
36 // Audio permission utilities
37
38 // Used for calls that should originate from system services.
39 // We allow that some services might have separate processes to
40 // handle multiple users, e.g. u10_system, u10_bluetooth, u10_radio.
isServiceUid(uid_t uid)41 static inline bool isServiceUid(uid_t uid) {
42 return multiuser_get_app_id(uid) < AID_APP_START;
43 }
44
45 // Used for calls that should originate from audioserver.
isAudioServerUid(uid_t uid)46 static inline bool isAudioServerUid(uid_t uid) {
47 return uid == AID_AUDIOSERVER;
48 }
49
50 // Used for some permission checks.
51 // AID_ROOT is OK for command-line tests. Native audioserver always OK.
isAudioServerOrRootUid(uid_t uid)52 static inline bool isAudioServerOrRootUid(uid_t uid) {
53 return uid == AID_AUDIOSERVER || uid == AID_ROOT;
54 }
55
56 // Used for calls that should come from system server or internal.
57 // Note: system server is multiprocess for multiple users. audioserver is not.
isAudioServerOrSystemServerUid(uid_t uid)58 static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
59 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
60 }
61
62 // used for calls that should come from system_server or audio_server or media server and
63 // include AID_ROOT for command-line tests.
isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid)64 static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) {
65 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER
66 || uid == AID_MEDIA || uid == AID_ROOT;
67 }
68
69 // Mediaserver may forward the client PID and UID as part of a binder interface call;
70 // otherwise the calling UID must be equal to the client UID.
isAudioServerOrMediaServerUid(uid_t uid)71 static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
72 switch (uid) {
73 case AID_MEDIA:
74 case AID_AUDIOSERVER:
75 return true;
76 default:
77 return false;
78 }
79 }
80
81 bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
82 bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid);
83 void finishRecording(const String16& opPackageName, uid_t uid);
84 bool captureAudioOutputAllowed(pid_t pid, uid_t uid);
85 bool captureMediaOutputAllowed(pid_t pid, uid_t uid);
86 bool captureVoiceCommunicationOutputAllowed(pid_t pid, uid_t uid);
87 bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
88 bool settingsAllowed();
89 bool modifyAudioRoutingAllowed();
90 bool modifyAudioRoutingAllowed(pid_t pid, uid_t uid);
91 bool modifyDefaultAudioEffectsAllowed();
92 bool modifyDefaultAudioEffectsAllowed(pid_t pid, uid_t uid);
93 bool dumpAllowed();
94 bool modifyPhoneStateAllowed(pid_t pid, uid_t uid);
95 bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid);
96
97 status_t checkIMemory(const sp<IMemory>& iMemory);
98
99 class MediaPackageManager {
100 public:
101 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
allowPlaybackCapture(uid_t uid)102 bool allowPlaybackCapture(uid_t uid) {
103 auto result = doIsAllowed(uid);
104 if (!result) {
105 mPackageManagerErrors++;
106 }
107 return result.value_or(false);
108 }
109 void dump(int fd, int spaces = 0) const;
110 private:
111 static constexpr const char* nativePackageManagerName = "package_native";
112 std::optional<bool> doIsAllowed(uid_t uid);
113 sp<content::pm::IPackageManagerNative> retreivePackageManager();
114 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
115 uint_t mPackageManagerErrors = 0;
116 struct Package {
117 std::string name;
118 bool playbackCaptureAllowed = false;
119 };
120 using Packages = std::vector<Package>;
121 std::map<uid_t, Packages> mDebugLog;
122 };
123
124 namespace mediautils {
125
126 /**
127 * This class is used to retrieve (and cache) package information
128 * for a given uid.
129 */
130 class UidInfo {
131 public:
132 struct Info {
133 uid_t uid = -1; // uid used for lookup.
134 std::string package; // package name.
135 std::string installer; // installer for the package (e.g. preload, play store).
136 int64_t versionCode = 0; // reported version code.
137 int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
138 };
139
140 /**
141 * Returns the package information for a UID.
142 *
143 * The package name will be the uid if we cannot find the associated name.
144 *
145 * \param uid is the uid of the app or service.
146 */
147 Info getInfo(uid_t uid);
148
149 private:
150 std::mutex mLock;
151 // TODO: use concurrent hashmap with striped lock.
152 std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock)
153 };
154
155 } // namespace mediautils
156
157 } // namespace android
158
159 #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
160