blob: 276b471257aa353c878ab4e75537b22bdd40d42e [file] [log] [blame]
Glenn Kasten44deb052012-02-05 18:09:08 -08001/*
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
Kevin Rocard8be94972019-02-22 13:26:25 -080017#ifndef ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
18#define ANDROID_MEDIAUTILS_SERVICEUTILITIES_H
19
Glenn Kasten44deb052012-02-05 18:09:08 -080020#include <unistd.h>
21
Kevin Rocard8be94972019-02-22 13:26:25 -080022#include <android/content/pm/IPackageManagerNative.h>
Eric Laurent9b11c022018-06-06 19:19:22 -070023#include <binder/IMemory.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080024#include <binder/PermissionController.h>
Andy Hung4ef19fa2018-05-15 19:35:29 -070025#include <cutils/multiuser.h>
26#include <private/android_filesystem_config.h>
Nate Myrene69cada2020-12-10 10:00:36 -080027#include <system/audio-hal-enums.h>
Svet Ganov5b81f552018-03-02 09:21:30 -080028
Kevin Rocard8be94972019-02-22 13:26:25 -080029#include <map>
30#include <optional>
31#include <string>
Andy Hunga85efab2019-12-23 11:41:29 -080032#include <unordered_map>
Kevin Rocard8be94972019-02-22 13:26:25 -080033#include <vector>
34
Glenn Kasten44deb052012-02-05 18:09:08 -080035namespace android {
36
Andy Hungab7ef302018-05-15 19:35:29 -070037// Audio permission utilities
38
Andy Hung4ef19fa2018-05-15 19:35:29 -070039// Used for calls that should originate from system services.
40// We allow that some services might have separate processes to
41// handle multiple users, e.g. u10_system, u10_bluetooth, u10_radio.
42static inline bool isServiceUid(uid_t uid) {
43 return multiuser_get_app_id(uid) < AID_APP_START;
44}
45
46// Used for calls that should originate from audioserver.
47static inline bool isAudioServerUid(uid_t uid) {
48 return uid == AID_AUDIOSERVER;
49}
50
51// Used for some permission checks.
52// AID_ROOT is OK for command-line tests. Native audioserver always OK.
53static inline bool isAudioServerOrRootUid(uid_t uid) {
54 return uid == AID_AUDIOSERVER || uid == AID_ROOT;
55}
56
57// Used for calls that should come from system server or internal.
58// Note: system server is multiprocess for multiple users. audioserver is not.
59static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
60 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
61}
62
Eric Laurent58a0dd82019-10-24 12:42:17 -070063// used for calls that should come from system_server or audio_server or media server and
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070064// include AID_ROOT for command-line tests.
Eric Laurent58a0dd82019-10-24 12:42:17 -070065static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) {
66 return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER
67 || uid == AID_MEDIA || uid == AID_ROOT;
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070068}
69
Andy Hung4ef19fa2018-05-15 19:35:29 -070070// Mediaserver may forward the client PID and UID as part of a binder interface call;
71// otherwise the calling UID must be equal to the client UID.
72static inline bool isAudioServerOrMediaServerUid(uid_t uid) {
73 switch (uid) {
74 case AID_MEDIA:
75 case AID_AUDIOSERVER:
76 return true;
77 default:
78 return false;
79 }
80}
81
Marco Nelissendcb346b2015-09-09 10:47:29 -070082bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
Nate Myrene69cada2020-12-10 10:00:36 -080083bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid, audio_source_t source);
84void finishRecording(const String16& opPackageName, uid_t uid, audio_source_t source);
Eric Laurentb2379ba2016-05-23 17:42:12 -070085bool captureAudioOutputAllowed(pid_t pid, uid_t uid);
Kevin Rocard36b17552019-03-07 18:48:07 -080086bool captureMediaOutputAllowed(pid_t pid, uid_t uid);
Hayden Gomesb7429922020-12-11 13:59:18 -080087bool captureTunerAudioInputAllowed(pid_t pid, uid_t uid);
Nadav Bardbf0a2e2020-01-16 23:09:25 +020088bool captureVoiceCommunicationOutputAllowed(pid_t pid, uid_t uid);
jiabin68e0df72019-03-18 17:55:35 -070089bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
Glenn Kasten44deb052012-02-05 18:09:08 -080090bool settingsAllowed();
Eric Laurent5284ed52014-05-29 14:37:38 -070091bool modifyAudioRoutingAllowed();
Eric Laurent8a1095a2019-11-08 14:44:16 -080092bool modifyAudioRoutingAllowed(pid_t pid, uid_t uid);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070093bool modifyDefaultAudioEffectsAllowed();
Eric Laurent3f75a5b2019-11-12 15:55:51 -080094bool modifyDefaultAudioEffectsAllowed(pid_t pid, uid_t uid);
Glenn Kasten44deb052012-02-05 18:09:08 -080095bool dumpAllowed();
Nadav Bar766fb022018-01-07 12:18:03 +020096bool modifyPhoneStateAllowed(pid_t pid, uid_t uid);
Eric Laurent42984412019-05-09 17:57:03 -070097bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid);
98
Eric Laurent9b11c022018-06-06 19:19:22 -070099status_t checkIMemory(const sp<IMemory>& iMemory);
Kevin Rocard8be94972019-02-22 13:26:25 -0800100
101class MediaPackageManager {
102public:
103 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
104 bool allowPlaybackCapture(uid_t uid) {
105 auto result = doIsAllowed(uid);
106 if (!result) {
107 mPackageManagerErrors++;
108 }
109 return result.value_or(false);
110 }
111 void dump(int fd, int spaces = 0) const;
112private:
113 static constexpr const char* nativePackageManagerName = "package_native";
114 std::optional<bool> doIsAllowed(uid_t uid);
Ricardo Correa57a37692020-03-23 17:27:25 -0700115 sp<content::pm::IPackageManagerNative> retreivePackageManager();
Kevin Rocard8be94972019-02-22 13:26:25 -0800116 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
117 uint_t mPackageManagerErrors = 0;
118 struct Package {
119 std::string name;
120 bool playbackCaptureAllowed = false;
121 };
122 using Packages = std::vector<Package>;
123 std::map<uid_t, Packages> mDebugLog;
124};
Andy Hunga85efab2019-12-23 11:41:29 -0800125
126namespace mediautils {
127
128/**
129 * This class is used to retrieve (and cache) package information
130 * for a given uid.
131 */
132class UidInfo {
133public:
134 struct Info {
135 uid_t uid = -1; // uid used for lookup.
136 std::string package; // package name.
137 std::string installer; // installer for the package (e.g. preload, play store).
138 int64_t versionCode = 0; // reported version code.
139 int64_t expirationNs = 0; // after this time in SYSTEM_TIME_REALTIME we refetch.
140 };
141
142 /**
143 * Returns the package information for a UID.
144 *
145 * The package name will be the uid if we cannot find the associated name.
146 *
147 * \param uid is the uid of the app or service.
148 */
149 Info getInfo(uid_t uid);
150
151private:
152 std::mutex mLock;
153 // TODO: use concurrent hashmap with striped lock.
154 std::unordered_map<uid_t, Info> mInfoMap; // GUARDED_BY(mLock)
155};
156
157} // namespace mediautils
158
159} // namespace android
Kevin Rocard8be94972019-02-22 13:26:25 -0800160
161#endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H