• 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.
isAudioServerOrSystemServerUid(uid_t uid)64 static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
65     return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
66 }
67 
68 // used for calls that should come from system_server or audio_server or media server and
69 // include AID_ROOT for command-line tests.
isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid)70 static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) {
71     return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER
72               || uid == AID_MEDIA || uid == AID_ROOT;
73 }
74 
75 // Mediaserver may forward the client PID and UID as part of a binder interface call;
76 // otherwise the calling UID must be equal to the client UID.
isAudioServerOrMediaServerUid(uid_t uid)77 static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
78     switch (uid) {
79     case AID_MEDIA:
80     case AID_AUDIOSERVER:
81         return true;
82     default:
83         return false;
84     }
85 }
86 
87 bool recordingAllowed(const AttributionSourceState& attributionSource,
88         audio_source_t source = AUDIO_SOURCE_DEFAULT);
89 bool startRecording(const AttributionSourceState& attributionSource,
90     const String16& msg, audio_source_t source);
91 void finishRecording(const AttributionSourceState& attributionSource, audio_source_t source);
92 std::optional<AttributionSourceState> resolveAttributionSource(
93     const AttributionSourceState& callerAttributionSource);
94 bool captureAudioOutputAllowed(const AttributionSourceState& attributionSource);
95 bool captureMediaOutputAllowed(const AttributionSourceState& attributionSource);
96 bool captureTunerAudioInputAllowed(const AttributionSourceState& attributionSource);
97 bool captureVoiceCommunicationOutputAllowed(const AttributionSourceState& attributionSource);
98 bool captureHotwordAllowed(const AttributionSourceState& attributionSource);
99 bool settingsAllowed();
100 bool modifyAudioRoutingAllowed();
101 bool modifyAudioRoutingAllowed(const AttributionSourceState& attributionSource);
102 bool modifyDefaultAudioEffectsAllowed();
103 bool modifyDefaultAudioEffectsAllowed(const AttributionSourceState& attributionSource);
104 bool dumpAllowed();
105 bool modifyPhoneStateAllowed(const AttributionSourceState& attributionSource);
106 bool bypassInterruptionPolicyAllowed(const AttributionSourceState& attributionSource);
107 void purgePermissionCache();
108 int32_t getOpForSource(audio_source_t source);
109 
110 AttributionSourceState getCallingAttributionSource();
111 
112 status_t checkIMemory(const sp<IMemory>& iMemory);
113 
114 class MediaPackageManager {
115 public:
116     /** Query the PackageManager to check if all apps of an UID allow playback capture. */
allowPlaybackCapture(uid_t uid)117     bool allowPlaybackCapture(uid_t uid) {
118         auto result = doIsAllowed(uid);
119         if (!result) {
120             mPackageManagerErrors++;
121         }
122         return result.value_or(false);
123     }
124     void dump(int fd, int spaces = 0) const;
125 private:
126     static constexpr const char* nativePackageManagerName = "package_native";
127     std::optional<bool> doIsAllowed(uid_t uid);
128     sp<content::pm::IPackageManagerNative> retrievePackageManager();
129     sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
130     uint_t mPackageManagerErrors = 0;
131     struct Package {
132         std::string name;
133         bool playbackCaptureAllowed = false;
134     };
135     using Packages = std::vector<Package>;
136     std::map<uid_t, Packages> mDebugLog;
137 };
138 
139 namespace mediautils {
140 
141 /**
142  * This class is used to retrieve (and cache) package information
143  * for a given uid.
144  */
145 class UidInfo {
146 public:
147     struct Info {
148         uid_t uid = -1;           // uid used for lookup.
149         std::string package;      // package name.
150         std::string installer;    // installer for the package (e.g. preload, play store).
151         int64_t versionCode = 0;  // reported version code.
152         int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
153     };
154 
155     /**
156      * Returns the package information for a UID.
157      *
158      * The package name will be the uid if we cannot find the associated name.
159      *
160      * \param uid is the uid of the app or service.
161      */
162     Info getInfo(uid_t uid);
163 
164 private:
165     std::mutex mLock;
166     // TODO: use concurrent hashmap with striped lock.
167     std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock)
168 };
169 
170 } // namespace mediautils
171 
172 } // namespace android
173 
174 #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
175