blob: 9377ff31f159c22eb36b97f2484802226604ae5e [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>
Svet Ganov5b81f552018-03-02 09:21:30 -080027
Kevin Rocard8be94972019-02-22 13:26:25 -080028#include <map>
29#include <optional>
30#include <string>
31#include <vector>
32
Glenn Kasten44deb052012-02-05 18:09:08 -080033namespace android {
34
Andy Hungab7ef302018-05-15 19:35:29 -070035// Audio permission utilities
36
Andy Hung4ef19fa2018-05-15 19:35:29 -070037// 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.
40static 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.
45static 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.
51static 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.
57static 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.
63static 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
Marco Nelissendcb346b2015-09-09 10:47:29 -070073bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid);
Svet Ganov5b81f552018-03-02 09:21:30 -080074bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid);
75void finishRecording(const String16& opPackageName, uid_t uid);
Eric Laurentb2379ba2016-05-23 17:42:12 -070076bool captureAudioOutputAllowed(pid_t pid, uid_t uid);
Kevin Rocard36b17552019-03-07 18:48:07 -080077bool captureMediaOutputAllowed(pid_t pid, uid_t uid);
Eric Laurent7504b9e2017-08-15 18:17:26 -070078bool captureHotwordAllowed(pid_t pid, uid_t uid);
Glenn Kasten44deb052012-02-05 18:09:08 -080079bool settingsAllowed();
Eric Laurent5284ed52014-05-29 14:37:38 -070080bool modifyAudioRoutingAllowed();
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070081bool modifyDefaultAudioEffectsAllowed();
Glenn Kasten44deb052012-02-05 18:09:08 -080082bool dumpAllowed();
Nadav Bar766fb022018-01-07 12:18:03 +020083bool modifyPhoneStateAllowed(pid_t pid, uid_t uid);
Eric Laurent9b11c022018-06-06 19:19:22 -070084status_t checkIMemory(const sp<IMemory>& iMemory);
Kevin Rocard8be94972019-02-22 13:26:25 -080085
86class MediaPackageManager {
87public:
88 /** Query the PackageManager to check if all apps of an UID allow playback capture. */
89 bool allowPlaybackCapture(uid_t uid) {
90 auto result = doIsAllowed(uid);
91 if (!result) {
92 mPackageManagerErrors++;
93 }
94 return result.value_or(false);
95 }
96 void dump(int fd, int spaces = 0) const;
97private:
98 static constexpr const char* nativePackageManagerName = "package_native";
99 std::optional<bool> doIsAllowed(uid_t uid);
100 sp<content::pm::IPackageManagerNative> retreivePackageManager();
101 sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest
102 uint_t mPackageManagerErrors = 0;
103 struct Package {
104 std::string name;
105 bool playbackCaptureAllowed = false;
106 };
107 using Packages = std::vector<Package>;
108 std::map<uid_t, Packages> mDebugLog;
109};
Glenn Kasten44deb052012-02-05 18:09:08 -0800110}
Kevin Rocard8be94972019-02-22 13:26:25 -0800111
112#endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H