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" |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 19 | #include "MediaTranscodingService.h" |
| 20 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
Linus Nilsson | 06fab72 | 2020-07-16 15:53:14 -0700 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
| 25 | #include <media/TranscoderWrapper.h> |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 26 | #include <media/TranscodingClientManager.h> |
| 27 | #include <media/TranscodingJobScheduler.h> |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 28 | #include <media/TranscodingResourcePolicy.h> |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 29 | #include <media/TranscodingUidPolicy.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 30 | #include <private/android_filesystem_config.h> |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 31 | #include <utils/Log.h> |
| 32 | #include <utils/Vector.h> |
| 33 | |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 34 | #include "SimulatedTranscoder.h" |
| 35 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 38 | // Convenience methods for constructing binder::Status objects for error returns |
| 39 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 40 | Status::fromServiceSpecificErrorWithMessage( \ |
| 41 | errorCode, \ |
| 42 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__)) |
| 43 | |
| 44 | // Can MediaTranscoding service trust the caller based on the calling UID? |
| 45 | // TODO(hkuang): Add MediaProvider's UID. |
| 46 | static bool isTrustedCallingUid(uid_t uid) { |
| 47 | switch (uid) { |
| 48 | case AID_ROOT: // root user |
| 49 | case AID_SYSTEM: |
| 50 | case AID_SHELL: |
| 51 | case AID_MEDIA: // mediaserver |
| 52 | return true; |
| 53 | default: |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 58 | MediaTranscodingService::MediaTranscodingService( |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 59 | const std::shared_ptr<TranscoderInterface>& transcoder) |
| 60 | : mUidPolicy(new TranscodingUidPolicy()), |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 61 | mResourcePolicy(new TranscodingResourcePolicy()), |
| 62 | mJobScheduler(new TranscodingJobScheduler(transcoder, mUidPolicy, mResourcePolicy)), |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 63 | mClientManager(new TranscodingClientManager(mJobScheduler)) { |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 64 | ALOGV("MediaTranscodingService is created"); |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 65 | transcoder->setCallback(mJobScheduler); |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 66 | mUidPolicy->setCallback(mJobScheduler); |
Chong Zhang | f907751 | 2020-09-21 21:02:06 -0700 | [diff] [blame] | 67 | mResourcePolicy->setCallback(mJobScheduler); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | MediaTranscodingService::~MediaTranscodingService() { |
| 71 | ALOGE("Should not be in ~MediaTranscodingService"); |
| 72 | } |
| 73 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 74 | binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
| 75 | String8 result; |
Linus Nilsson | 06fab72 | 2020-07-16 15:53:14 -0700 | [diff] [blame] | 76 | |
Linus Nilsson | b65fbaf | 2020-07-17 12:05:48 -0700 | [diff] [blame] | 77 | // TODO(b/161549994): Remove libbinder dependencies for mainline. |
Linus Nilsson | 06fab72 | 2020-07-16 15:53:14 -0700 | [diff] [blame] | 78 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 79 | result.format( |
| 80 | "Permission Denial: " |
| 81 | "can't dump MediaTranscodingService from pid=%d, uid=%d\n", |
| 82 | AIBinder_getCallingPid(), AIBinder_getCallingUid()); |
| 83 | write(fd, result.string(), result.size()); |
| 84 | return PERMISSION_DENIED; |
| 85 | } |
| 86 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 87 | const size_t SIZE = 256; |
| 88 | char buffer[SIZE]; |
| 89 | |
| 90 | snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this); |
| 91 | result.append(buffer); |
| 92 | write(fd, result.string(), result.size()); |
| 93 | |
| 94 | Vector<String16> args; |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 95 | mClientManager->dumpAllClients(fd, args); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 96 | return OK; |
| 97 | } |
| 98 | |
| 99 | //static |
| 100 | void MediaTranscodingService::instantiate() { |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 101 | std::shared_ptr<TranscoderInterface> transcoder; |
Chong Zhang | 17495a9 | 2020-06-25 10:39:05 -0700 | [diff] [blame] | 102 | if (property_get_bool("debug.transcoding.simulated_transcoder", false)) { |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 103 | transcoder = std::make_shared<SimulatedTranscoder>(); |
| 104 | } else { |
| 105 | transcoder = std::make_shared<TranscoderWrapper>(); |
| 106 | } |
| 107 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 108 | std::shared_ptr<MediaTranscodingService> service = |
Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 109 | ::ndk::SharedRefBase::make<MediaTranscodingService>(transcoder); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 110 | binder_status_t status = |
| 111 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 112 | if (status != STATUS_OK) { |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | Status MediaTranscodingService::registerClient( |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 118 | const std::shared_ptr<ITranscodingClientCallback>& in_callback, |
| 119 | const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid, |
| 120 | int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) { |
| 121 | if (in_callback == nullptr) { |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 122 | *_aidl_return = nullptr; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 123 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!"); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | int32_t callingPid = AIBinder_getCallingPid(); |
| 127 | int32_t callingUid = AIBinder_getCallingUid(); |
| 128 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 129 | // Check if we can trust clientUid. Only privilege caller could forward the |
| 130 | // uid on app client's behalf. |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 131 | if (in_clientUid == USE_CALLING_UID) { |
| 132 | in_clientUid = callingUid; |
| 133 | } else if (!isTrustedCallingUid(callingUid)) { |
| 134 | ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) " |
| 135 | "rejected " |
| 136 | "(don't trust clientUid %d)", |
| 137 | in_clientPid, in_clientUid, in_clientUid); |
| 138 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 139 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 140 | "register client", |
| 141 | in_clientPid, in_clientUid); |
| 142 | } |
| 143 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 144 | // Check if we can trust clientPid. Only privilege caller could forward the |
| 145 | // pid on app client's behalf. |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 146 | if (in_clientPid == USE_CALLING_PID) { |
| 147 | in_clientPid = callingPid; |
| 148 | } else if (!isTrustedCallingUid(callingUid)) { |
| 149 | ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID " |
| 150 | "%d) rejected " |
| 151 | "(don't trust clientPid %d)", |
| 152 | in_clientPid, in_clientUid, in_clientPid); |
| 153 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 154 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 155 | "register client", |
| 156 | in_clientPid, in_clientUid); |
| 157 | } |
| 158 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 159 | // Creates the client and uses its process id as client id. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 160 | std::shared_ptr<ITranscodingClient> newClient; |
| 161 | |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 162 | status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName, |
| 163 | in_opPackageName, &newClient); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 164 | if (err != OK) { |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 165 | *_aidl_return = nullptr; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 166 | return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager"); |
| 167 | } |
| 168 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 169 | *_aidl_return = newClient; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 170 | return Status::ok(); |
| 171 | } |
| 172 | |
| 173 | Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) { |
| 174 | ALOGD("MediaTranscodingService::getNumOfClients"); |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 175 | *_aidl_return = mClientManager->getNumOfClients(); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 176 | return Status::ok(); |
| 177 | } |
| 178 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 179 | } // namespace android |