Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MediaTranscodingService" |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 19 | #include <MediaTranscodingService.h> |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 20 | #include <android/binder_manager.h> |
| 21 | #include <android/binder_process.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 22 | #include <private/android_filesystem_config.h> |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | #include <utils/Vector.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 28 | // Convenience methods for constructing binder::Status objects for error returns |
| 29 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 30 | Status::fromServiceSpecificErrorWithMessage( \ |
| 31 | errorCode, \ |
| 32 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__)) |
| 33 | |
| 34 | // Can MediaTranscoding service trust the caller based on the calling UID? |
| 35 | // TODO(hkuang): Add MediaProvider's UID. |
| 36 | static bool isTrustedCallingUid(uid_t uid) { |
| 37 | switch (uid) { |
| 38 | case AID_ROOT: // root user |
| 39 | case AID_SYSTEM: |
| 40 | case AID_SHELL: |
| 41 | case AID_MEDIA: // mediaserver |
| 42 | return true; |
| 43 | default: |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
hkuang | 5172cab | 2020-01-31 12:40:28 -0800 | [diff] [blame] | 48 | MediaTranscodingService::MediaTranscodingService() |
| 49 | : mTranscodingClientManager(TranscodingClientManager::getInstance()) { |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 50 | ALOGV("MediaTranscodingService is created"); |
| 51 | } |
| 52 | |
| 53 | MediaTranscodingService::~MediaTranscodingService() { |
| 54 | ALOGE("Should not be in ~MediaTranscodingService"); |
| 55 | } |
| 56 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 57 | binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
| 58 | String8 result; |
| 59 | const size_t SIZE = 256; |
| 60 | char buffer[SIZE]; |
| 61 | |
| 62 | snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this); |
| 63 | result.append(buffer); |
| 64 | write(fd, result.string(), result.size()); |
| 65 | |
| 66 | Vector<String16> args; |
hkuang | 5172cab | 2020-01-31 12:40:28 -0800 | [diff] [blame] | 67 | mTranscodingClientManager.dumpAllClients(fd, args); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 68 | return OK; |
| 69 | } |
| 70 | |
| 71 | //static |
| 72 | void MediaTranscodingService::instantiate() { |
| 73 | std::shared_ptr<MediaTranscodingService> service = |
| 74 | ::ndk::SharedRefBase::make<MediaTranscodingService>(); |
| 75 | binder_status_t status = |
| 76 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 77 | if (status != STATUS_OK) { |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | Status MediaTranscodingService::registerClient( |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 83 | const std::shared_ptr<ITranscodingClientListener>& in_listener, |
| 84 | const std::string& in_clientName, |
| 85 | const std::string& in_opPackageName, |
| 86 | int32_t in_clientUid, int32_t in_clientPid, |
| 87 | std::shared_ptr<ITranscodingClient>* _aidl_return) { |
| 88 | if (in_listener == nullptr) { |
| 89 | ALOGE("Client listener can not be null"); |
| 90 | *_aidl_return = nullptr; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 91 | return Status::fromServiceSpecificError(ERROR_ILLEGAL_ARGUMENT); |
| 92 | } |
| 93 | |
| 94 | int32_t callingPid = AIBinder_getCallingPid(); |
| 95 | int32_t callingUid = AIBinder_getCallingUid(); |
| 96 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 97 | // Check if we can trust clientUid. Only privilege caller could forward the |
| 98 | // uid on app client's behalf. |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 99 | if (in_clientUid == USE_CALLING_UID) { |
| 100 | in_clientUid = callingUid; |
| 101 | } else if (!isTrustedCallingUid(callingUid)) { |
| 102 | ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) " |
| 103 | "rejected " |
| 104 | "(don't trust clientUid %d)", |
| 105 | in_clientPid, in_clientUid, in_clientUid); |
| 106 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 107 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 108 | "register client", |
| 109 | in_clientPid, in_clientUid); |
| 110 | } |
| 111 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 112 | // Check if we can trust clientPid. Only privilege caller could forward the |
| 113 | // pid on app client's behalf. |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 114 | if (in_clientPid == USE_CALLING_PID) { |
| 115 | in_clientPid = callingPid; |
| 116 | } else if (!isTrustedCallingUid(callingUid)) { |
| 117 | ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID " |
| 118 | "%d) rejected " |
| 119 | "(don't trust clientPid %d)", |
| 120 | in_clientPid, in_clientUid, in_clientPid); |
| 121 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 122 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 123 | "register client", |
| 124 | in_clientPid, in_clientUid); |
| 125 | } |
| 126 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 127 | // Creates the client and uses its process id as client id. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 128 | std::shared_ptr<ITranscodingClient> newClient; |
| 129 | |
| 130 | status_t err = mTranscodingClientManager.addClient(in_listener, |
| 131 | in_clientPid, in_clientUid, in_clientName, in_opPackageName, &newClient); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 132 | if (err != OK) { |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 133 | *_aidl_return = nullptr; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 134 | return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager"); |
| 135 | } |
| 136 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 137 | *_aidl_return = newClient; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 138 | return Status::ok(); |
| 139 | } |
| 140 | |
| 141 | Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) { |
| 142 | ALOGD("MediaTranscodingService::getNumOfClients"); |
hkuang | 5172cab | 2020-01-31 12:40:28 -0800 | [diff] [blame] | 143 | *_aidl_return = mTranscodingClientManager.getNumOfClients(); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 144 | return Status::ok(); |
| 145 | } |
| 146 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 147 | } // namespace android |