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 <vector>
32
33 namespace android {
34
35 // Audio permission utilities
36
37 // Used for calls that should originate from system services.
38 // We allow that some services might have separate processes to
39 // handle multiple users, e.g. u10_system, u10_bluetooth, u10_radio.
isServiceUid(uid_t uid)40 static inline bool isServiceUid(uid_t uid) {
41 return multiuser_get_app_id(uid) < AID_APP_START;
42 }
43
44 // Used for calls that should originate from audioserver.
isAudioServerUid(uid_t uid)45 static inline bool isAudioServerUid(uid_t uid) {
46 return uid == AID_AUDIOSERVER;
47 }
48
49 // Used for some permission checks.
50 // AID_ROOT is OK for command-line tests. Native audioserver always OK.
isAudioServerOrRootUid(uid_t uid)51 static inline bool isAudioServerOrRootUid(uid_t uid) {
52 return uid == AID_AUDIOSERVER || uid == AID_ROOT;
53 }
54
55 // Used for calls that should come from system server or internal.
56 // Note: system server is multiprocess for multiple users. audioserver is not.
isAudioServerOrSystemServerUid(uid_t uid)57 static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
58 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
59 }
60
61 // used for calls that should come from system_server or audio_server and
62 // include AID_ROOT for command-line tests.
isAudioServerOrSystemServerOrRootUid(uid_t uid)63 static inline bool isAudioServerOrSystemServerOrRootUid(uid_t uid) {
64 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER || uid == AID_ROOT;
65 }
66
67 // Mediaserver may forward the client PID and UID as part of a binder interface call;
68 // otherwise the calling UID must be equal to the client UID.
isAudioServerOrMediaServerUid(uid_t uid)69 static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
70 switch (uid) {
71 case AID_MEDIA:
72 case AID_AUDIOSERVER:
73 return true;
74 default:
75 return false;
76 }
77 }
78
79 bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
80 bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid);
81 void finishRecording(const String16& opPackageName, uid_t uid);
82 bool captureAudioOutputAllowed(pid_t pid, uid_t uid);
83 bool captureMediaOutputAllowed(pid_t pid, uid_t uid);
84 bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
85 bool settingsAllowed();
86 bool modifyAudioRoutingAllowed();
87 bool modifyDefaultAudioEffectsAllowed();
88 bool modifyDefaultAudioEffectsAllowed(pid_t pid, uid_t uid);
89 bool dumpAllowed();
90 bool modifyPhoneStateAllowed(pid_t pid, uid_t uid);
91 bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid);
92
93 status_t checkIMemory(const sp<IMemory>& iMemory);
94
95 class MediaPackageManager {
96 public:
97 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
allowPlaybackCapture(uid_t uid)98 bool allowPlaybackCapture(uid_t uid) {
99 auto result = doIsAllowed(uid);
100 if (!result) {
101 mPackageManagerErrors++;
102 }
103 return result.value_or(false);
104 }
105 void dump(int fd, int spaces = 0) const;
106 private:
107 static constexpr const char* nativePackageManagerName = "package_native";
108 std::optional<bool> doIsAllowed(uid_t uid);
109 sp<content::pm::IPackageManagerNative> retreivePackageManager();
110 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
111 uint_t mPackageManagerErrors = 0;
112 struct Package {
113 std::string name;
114 bool playbackCaptureAllowed = false;
115 };
116 using Packages = std::vector<Package>;
117 std::map<uid_t, Packages> mDebugLog;
118 };
119 }
120
121 #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
122