• 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 #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 
91 bool recordingAllowed(const AttributionSourceState &attributionSource,
92                       uint32_t virtualDeviceId,
93                       audio_source_t source);
94 bool startRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
95                     const String16& msg, audio_source_t source);
96 void finishRecording(const AttributionSourceState& attributionSource, uint32_t virtualDeviceId,
97                      audio_source_t source);
98 std::optional<AttributionSourceState> resolveAttributionSource(
99     const AttributionSourceState& callerAttributionSource, uint32_t virtualDeviceId);
100 bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource);
101 bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource);
102 bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource);
103 bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource);
104 bool accessUltrasoundAllowed(const AttributionSourceState& attributionSource);
105 bool captureHotwordAllowed(const AttributionSourceState& attributionSource);
106 bool settingsAllowed();
107 bool modifyAudioRoutingAllowed();
108 bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource);
109 bool modifyDefaultAudioEffectsAllowed();
110 bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource);
111 bool dumpAllowed();
112 bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource);
113 bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource);
114 bool callAudioInterceptionAllowed(const AttributionSourceState& attributionSource);
115 void purgePermissionCache();
116 int32_t getOpForSource(audio_source_t source);
117 
118 AttributionSourceState getCallingAttributionSource();
119 
120 status_t checkIMemory(const sp<IMemory>& iMemory);
121 
122 class MediaPackageManager {
123 public:
124     /** Query the PackageManager to check if all apps of an UID allow playback capture. */
allowPlaybackCapture(uid_t uid)125     bool allowPlaybackCapture(uid_t uid) {
126         auto result = doIsAllowed(uid);
127         if (!result) {
128             mPackageManagerErrors++;
129         }
130         return result.value_or(false);
131     }
132     void dump(int fd, int spaces = 0) const;
133 private:
134     static constexpr const char* nativePackageManagerName = "package_native";
135     std::optional<bool> doIsAllowed(uid_t uid);
136     sp<content::pm::IPackageManagerNative> retrievePackageManager();
137     sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
138     unsigned int mPackageManagerErrors = 0;
139     struct Package {
140         std::string name;
141         bool playbackCaptureAllowed = false;
142     };
143     using Packages = std::vector<Package>;
144     std::map<uid_t, Packages> mDebugLog;
145 };
146 
147 namespace mediautils {
148 
149 /**
150  * This class is used to retrieve (and cache) package information
151  * for a given uid.
152  */
153 class UidInfo {
154 public:
155     struct Info {
156         uid_t uid = -1;           // uid used for lookup.
157         std::string package;      // package name.
158         std::string installer;    // installer for the package (e.g. preload, play store).
159         int64_t versionCode = 0;  // reported version code.
160         int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
161     };
162 
163     /**
164      * Returns the package information for a UID.
165      *
166      * The package name will be the uid if we cannot find the associated name.
167      *
168      * \param uid is the uid of the app or service.
169      */
170     Info getInfo(uid_t uid);
171 
172 private:
173     std::mutex mLock;
174     // TODO: use concurrent hashmap with striped lock.
175     std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock)
176 };
177 
178 } // namespace mediautils
179 
180 } // namespace android
181 
182 #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
183