Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 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 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_MEDIAUTILS_SERVICEUTILITIES_H |
| 18 | #define ANDROID_MEDIAUTILS_SERVICEUTILITIES_H |
| 19 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 22 | #include <android/content/pm/IPackageManagerNative.h> |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 23 | #include <binder/IMemory.h> |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 24 | #include <binder/PermissionController.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 25 | #include <cutils/multiuser.h> |
| 26 | #include <private/android_filesystem_config.h> |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 27 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 28 | #include <map> |
| 29 | #include <optional> |
| 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 35 | // Audio permission utilities |
| 36 | |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 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. |
| 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. |
| 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. |
| 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. |
| 57 | static inline bool isAudioServerOrSystemServerUid(uid_t uid) { |
| 58 | return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER; |
| 59 | } |
| 60 | |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 61 | // used for calls that should come from system_server or audio_server or media server and |
Jeffrey Carlyle | 62cc92b | 2019-09-17 11:15:15 -0700 | [diff] [blame] | 62 | // include AID_ROOT for command-line tests. |
Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 63 | static inline bool isAudioServerOrMediaServerOrSystemServerOrRootUid(uid_t uid) { |
| 64 | return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER |
| 65 | || uid == AID_MEDIA || uid == AID_ROOT; |
Jeffrey Carlyle | 62cc92b | 2019-09-17 11:15:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 68 | // Mediaserver may forward the client PID and UID as part of a binder interface call; |
| 69 | // otherwise the calling UID must be equal to the client UID. |
| 70 | static inline bool isAudioServerOrMediaServerUid(uid_t uid) { |
| 71 | switch (uid) { |
| 72 | case AID_MEDIA: |
| 73 | case AID_AUDIOSERVER: |
| 74 | return true; |
| 75 | default: |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 80 | bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid); |
Svet Ganov | 5b81f55 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 81 | bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid); |
| 82 | void finishRecording(const String16& opPackageName, uid_t uid); |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 83 | bool captureAudioOutputAllowed(pid_t pid, uid_t uid); |
Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 84 | bool captureMediaOutputAllowed(pid_t pid, uid_t uid); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 85 | bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 86 | bool settingsAllowed(); |
Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 87 | bool modifyAudioRoutingAllowed(); |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame^] | 88 | bool modifyAudioRoutingAllowed(pid_t pid, uid_t uid); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 89 | bool modifyDefaultAudioEffectsAllowed(); |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 90 | bool dumpAllowed(); |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 91 | bool modifyPhoneStateAllowed(pid_t pid, uid_t uid); |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 92 | bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid); |
| 93 | |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 94 | status_t checkIMemory(const sp<IMemory>& iMemory); |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 95 | |
| 96 | class MediaPackageManager { |
| 97 | public: |
| 98 | /** Query the PackageManager to check if all apps of an UID allow playback capture. */ |
| 99 | bool allowPlaybackCapture(uid_t uid) { |
| 100 | auto result = doIsAllowed(uid); |
| 101 | if (!result) { |
| 102 | mPackageManagerErrors++; |
| 103 | } |
| 104 | return result.value_or(false); |
| 105 | } |
| 106 | void dump(int fd, int spaces = 0) const; |
| 107 | private: |
| 108 | static constexpr const char* nativePackageManagerName = "package_native"; |
| 109 | std::optional<bool> doIsAllowed(uid_t uid); |
| 110 | sp<content::pm::IPackageManagerNative> retreivePackageManager(); |
| 111 | sp<content::pm::IPackageManagerNative> mPackageManager; // To check apps manifest |
| 112 | uint_t mPackageManagerErrors = 0; |
| 113 | struct Package { |
| 114 | std::string name; |
| 115 | bool playbackCaptureAllowed = false; |
| 116 | }; |
| 117 | using Packages = std::vector<Package>; |
| 118 | std::map<uid_t, Packages> mDebugLog; |
| 119 | }; |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 120 | } |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 121 | |
| 122 | #endif // ANDROID_MEDIAUTILS_SERVICEUTILITIES_H |