• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Mediaserver may forward the client PID and UID as part of a binder interface call;
62 // otherwise the calling UID must be equal to the client UID.
isAudioServerOrMediaServerUid(uid_t uid)63 static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
64     switch (uid) {
65     case AID_MEDIA:
66     case AID_AUDIOSERVER:
67         return true;
68     default:
69         return false;
70     }
71 }
72 
73 bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
74 bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid);
75 void finishRecording(const String16& opPackageName, uid_t uid);
76 bool captureAudioOutputAllowed(pid_t pid, uid_t uid);
77 bool captureMediaOutputAllowed(pid_t pid, uid_t uid);
78 bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
79 bool settingsAllowed();
80 bool modifyAudioRoutingAllowed();
81 bool modifyDefaultAudioEffectsAllowed();
82 bool dumpAllowed();
83 bool modifyPhoneStateAllowed(pid_t pid, uid_t uid);
84 bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid);
85 
86 status_t checkIMemory(const sp<IMemory>& iMemory);
87 
88 class MediaPackageManager {
89 public:
90     /** Query the PackageManager to check if all apps of an UID allow playback capture. */
allowPlaybackCapture(uid_t uid)91     bool allowPlaybackCapture(uid_t uid) {
92         auto result = doIsAllowed(uid);
93         if (!result) {
94             mPackageManagerErrors++;
95         }
96         return result.value_or(false);
97     }
98     void dump(int fd, int spaces = 0) const;
99 private:
100     static constexpr const char* nativePackageManagerName = "package_native";
101     std::optional<bool> doIsAllowed(uid_t uid);
102     sp<content::pm::IPackageManagerNative> retreivePackageManager();
103     sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
104     uint_t mPackageManagerErrors = 0;
105     struct Package {
106         std::string name;
107         bool playbackCaptureAllowed = false;
108     };
109     using Packages = std::vector<Package>;
110     std::map<uid_t, Packages> mDebugLog;
111 };
112 }
113 
114 #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
115