• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
19 #include <MediaTranscodingService.h>
20 #include <android/binder_manager.h>
21 #include <android/binder_process.h>
22 #include <private/android_filesystem_config.h>
23 #include <utils/Log.h>
24 #include <utils/Vector.h>
25 
26 namespace android {
27 
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.
isTrustedCallingUid(uid_t 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 
MediaTranscodingService()48 MediaTranscodingService::MediaTranscodingService()
49       : mTranscodingClientManager(TranscodingClientManager::getInstance()) {
50     ALOGV("MediaTranscodingService is created");
51 }
52 
~MediaTranscodingService()53 MediaTranscodingService::~MediaTranscodingService() {
54     ALOGE("Should not be in ~MediaTranscodingService");
55 }
56 
dump(int fd,const char **,uint32_t)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;
67     mTranscodingClientManager.dumpAllClients(fd, args);
68     return OK;
69 }
70 
71 //static
instantiate()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 
registerClient(const std::shared_ptr<ITranscodingServiceClient> & in_client,const std::string & in_opPackageName,int32_t in_clientUid,int32_t in_clientPid,int32_t * _aidl_return)82 Status MediaTranscodingService::registerClient(
83         const std::shared_ptr<ITranscodingServiceClient>& in_client,
84         const std::string& in_opPackageName, int32_t in_clientUid, int32_t in_clientPid,
85         int32_t* _aidl_return) {
86     if (in_client == nullptr) {
87         ALOGE("Client can not be null");
88         *_aidl_return = kInvalidJobId;
89         return Status::fromServiceSpecificError(ERROR_ILLEGAL_ARGUMENT);
90     }
91 
92     int32_t callingPid = AIBinder_getCallingPid();
93     int32_t callingUid = AIBinder_getCallingUid();
94 
95     // Check if we can trust clientUid. Only privilege caller could forward the uid on app client's behalf.
96     if (in_clientUid == USE_CALLING_UID) {
97         in_clientUid = callingUid;
98     } else if (!isTrustedCallingUid(callingUid)) {
99         ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
100               "rejected "
101               "(don't trust clientUid %d)",
102               in_clientPid, in_clientUid, in_clientUid);
103         return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
104                                 "Untrusted caller (calling PID %d, UID %d) trying to "
105                                 "register client",
106                                 in_clientPid, in_clientUid);
107     }
108 
109     // Check if we can trust clientPid. Only privilege caller could forward the pid on app client's behalf.
110     if (in_clientPid == USE_CALLING_PID) {
111         in_clientPid = callingPid;
112     } else if (!isTrustedCallingUid(callingUid)) {
113         ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
114               "%d) rejected "
115               "(don't trust clientPid %d)",
116               in_clientPid, in_clientUid, in_clientPid);
117         return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
118                                 "Untrusted caller (calling PID %d, UID %d) trying to "
119                                 "register client",
120                                 in_clientPid, in_clientUid);
121     }
122 
123     // We know the clientId must be equal to its pid as we assigned client's pid as its clientId.
124     int32_t clientId = in_clientPid;
125 
126     // Checks if the client already registers.
127     if (mTranscodingClientManager.isClientIdRegistered(clientId)) {
128         return Status::fromServiceSpecificError(ERROR_ALREADY_EXISTS);
129     }
130 
131     // Creates the client and uses its process id as client id.
132     std::unique_ptr<TranscodingClientManager::ClientInfo> newClient =
133             std::make_unique<TranscodingClientManager::ClientInfo>(
134                     in_client, clientId, in_clientPid, in_clientUid, in_opPackageName);
135     status_t err = mTranscodingClientManager.addClient(std::move(newClient));
136     if (err != OK) {
137         *_aidl_return = kInvalidClientId;
138         return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
139     }
140 
141     ALOGD("Assign client: %s pid: %d, uid: %d with id: %d", in_opPackageName.c_str(), in_clientPid,
142           in_clientUid, clientId);
143 
144     *_aidl_return = clientId;
145     return Status::ok();
146 }
147 
unregisterClient(int32_t clientId,bool * _aidl_return)148 Status MediaTranscodingService::unregisterClient(int32_t clientId, bool* _aidl_return) {
149     ALOGD("unregisterClient id: %d", clientId);
150     int32_t callingUid = AIBinder_getCallingUid();
151     int32_t callingPid = AIBinder_getCallingPid();
152 
153     // Only the client with clientId or the trusted caller could unregister the client.
154     if (callingPid != clientId) {
155         if (!isTrustedCallingUid(callingUid)) {
156             ALOGE("Untrusted caller (calling PID %d, UID %d) trying to "
157                   "unregister client with id: %d",
158                   callingUid, callingPid, clientId);
159             *_aidl_return = true;
160             return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
161                                     "Untrusted caller (calling PID %d, UID %d) trying to "
162                                     "unregister client with id: %d",
163                                     callingUid, callingPid, clientId);
164         }
165     }
166 
167     *_aidl_return = (mTranscodingClientManager.removeClient(clientId) == OK);
168     return Status::ok();
169 }
170 
getNumOfClients(int32_t * _aidl_return)171 Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
172     ALOGD("MediaTranscodingService::getNumOfClients");
173     *_aidl_return = mTranscodingClientManager.getNumOfClients();
174     return Status::ok();
175 }
176 
submitRequest(int32_t,const TranscodingRequestParcel &,TranscodingJobParcel *,int32_t *)177 Status MediaTranscodingService::submitRequest(int32_t /*clientId*/,
178                                               const TranscodingRequestParcel& /*request*/,
179                                               TranscodingJobParcel* /*job*/,
180                                               int32_t* /*_aidl_return*/) {
181     // TODO(hkuang): Add implementation.
182     return Status::ok();
183 }
184 
cancelJob(int32_t,int32_t,bool *)185 Status MediaTranscodingService::cancelJob(int32_t /*in_clientId*/, int32_t /*in_jobId*/,
186                                           bool* /*_aidl_return*/) {
187     // TODO(hkuang): Add implementation.
188     return Status::ok();
189 }
190 
getJobWithId(int32_t,TranscodingJobParcel *,bool *)191 Status MediaTranscodingService::getJobWithId(int32_t /*in_jobId*/,
192                                              TranscodingJobParcel* /*out_job*/,
193                                              bool* /*_aidl_return*/) {
194     // TODO(hkuang): Add implementation.
195     return Status::ok();
196 }
197 
198 }  // namespace android
199