1 /*
2 * Copyright (C) 2010 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 "DrmManagerService(Native)"
19 #include <utils/Log.h>
20
21 #include <private/android_filesystem_config.h>
22 #include <media/MemoryLeakTrackUtil.h>
23
24 #include <errno.h>
25 #include <utils/threads.h>
26 #include <binder/IServiceManager.h>
27 #include <binder/IPCThreadState.h>
28 #include <sys/stat.h>
29 #include "DrmManagerService.h"
30 #include "DrmManager.h"
31
32 #include <selinux/android.h>
33
34 using namespace android;
35
36 static int selinux_enabled;
37 static char *drmserver_context;
38 static Vector<uid_t> trustedUids;
39
40 const char *const DrmManagerService::drm_perm_labels[] = {
41 "consumeRights",
42 "setPlaybackStatus",
43 "openDecryptSession",
44 "closeDecryptSession",
45 "initializeDecryptUnit",
46 "decrypt",
47 "finalizeDecryptUnit",
48 "pread"
49 };
50
get_perm_label(drm_perm_t perm)51 const char *DrmManagerService::get_perm_label(drm_perm_t perm) {
52 unsigned int index = perm;
53
54 if (index < 0 ||
55 index >= (sizeof(drm_perm_labels) / sizeof(drm_perm_labels[0]))) {
56 ALOGE("SELinux: Failed to retrieve permission label(perm=%d).\n", perm);
57 abort();
58 }
59 return drm_perm_labels[index];
60 }
61
selinuxIsProtectedCallAllowed(pid_t spid,drm_perm_t perm)62 bool DrmManagerService::selinuxIsProtectedCallAllowed(pid_t spid, drm_perm_t perm) {
63 if (selinux_enabled <= 0) {
64 return true;
65 }
66
67 char *sctx;
68 const char *selinux_class = "drmservice";
69 const char *str_perm = get_perm_label(perm);
70
71 if (getpidcon(spid, &sctx) != 0) {
72 ALOGE("SELinux: getpidcon(pid=%d) failed.\n", spid);
73 return false;
74 }
75
76 bool allowed = (selinux_check_access(sctx, drmserver_context, selinux_class,
77 str_perm, NULL) == 0);
78 freecon(sctx);
79
80 return allowed;
81 }
82
isProtectedCallAllowed(drm_perm_t perm)83 bool DrmManagerService::isProtectedCallAllowed(drm_perm_t perm) {
84 // TODO
85 // Following implementation is just for reference.
86 // Each OEM manufacturer should implement/replace with their own solutions.
87 IPCThreadState* ipcState = IPCThreadState::self();
88 uid_t uid = ipcState->getCallingUid();
89 pid_t spid = ipcState->getCallingPid();
90
91 for (unsigned int i = 0; i < trustedUids.size(); ++i) {
92 if (trustedUids[i] == uid) {
93 return selinuxIsProtectedCallAllowed(spid, perm);
94 }
95 }
96 return false;
97 }
98
instantiate()99 void DrmManagerService::instantiate() {
100 ALOGV("instantiate");
101 defaultServiceManager()->addService(String16("drm.drmManager"), new DrmManagerService());
102
103 if (0 >= trustedUids.size()) {
104 // TODO
105 // Following implementation is just for reference.
106 // Each OEM manufacturer should implement/replace with their own solutions.
107
108 // Add trusted uids here
109 trustedUids.push(AID_MEDIA);
110 }
111
112 selinux_enabled = is_selinux_enabled();
113 if (selinux_enabled > 0 && getcon(&drmserver_context) != 0) {
114 ALOGE("SELinux: DrmManagerService failed to get context for DrmManagerService. Aborting.\n");
115 abort();
116 }
117
118 union selinux_callback cb;
119 cb.func_log = selinux_log_callback;
120 selinux_set_callback(SELINUX_CB_LOG, cb);
121 }
122
DrmManagerService()123 DrmManagerService::DrmManagerService() :
124 mDrmManager(NULL) {
125 ALOGV("created");
126 mDrmManager = new DrmManager();
127 mDrmManager->loadPlugIns();
128 }
129
~DrmManagerService()130 DrmManagerService::~DrmManagerService() {
131 ALOGV("Destroyed");
132 mDrmManager->unloadPlugIns();
133 delete mDrmManager; mDrmManager = NULL;
134 }
135
addUniqueId(bool isNative)136 int DrmManagerService::addUniqueId(bool isNative) {
137 return mDrmManager->addUniqueId(isNative);
138 }
139
removeUniqueId(int uniqueId)140 void DrmManagerService::removeUniqueId(int uniqueId) {
141 mDrmManager->removeUniqueId(uniqueId);
142 }
143
addClient(int uniqueId)144 void DrmManagerService::addClient(int uniqueId) {
145 mDrmManager->addClient(uniqueId);
146 }
147
removeClient(int uniqueId)148 void DrmManagerService::removeClient(int uniqueId) {
149 mDrmManager->removeClient(uniqueId);
150 }
151
setDrmServiceListener(int uniqueId,const sp<IDrmServiceListener> & drmServiceListener)152 status_t DrmManagerService::setDrmServiceListener(
153 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
154 ALOGV("Entering setDrmServiceListener");
155 mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
156 return DRM_NO_ERROR;
157 }
158
getConstraints(int uniqueId,const String8 * path,const int action)159 DrmConstraints* DrmManagerService::getConstraints(
160 int uniqueId, const String8* path, const int action) {
161 ALOGV("Entering getConstraints from content");
162 return mDrmManager->getConstraints(uniqueId, path, action);
163 }
164
getMetadata(int uniqueId,const String8 * path)165 DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
166 ALOGV("Entering getMetadata from content");
167 return mDrmManager->getMetadata(uniqueId, path);
168 }
169
canHandle(int uniqueId,const String8 & path,const String8 & mimeType)170 bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
171 ALOGV("Entering canHandle");
172 return mDrmManager->canHandle(uniqueId, path, mimeType);
173 }
174
processDrmInfo(int uniqueId,const DrmInfo * drmInfo)175 DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
176 ALOGV("Entering processDrmInfo");
177 return mDrmManager->processDrmInfo(uniqueId, drmInfo);
178 }
179
acquireDrmInfo(int uniqueId,const DrmInfoRequest * drmInfoRequest)180 DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
181 ALOGV("Entering acquireDrmInfo");
182 return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
183 }
184
saveRights(int uniqueId,const DrmRights & drmRights,const String8 & rightsPath,const String8 & contentPath)185 status_t DrmManagerService::saveRights(
186 int uniqueId, const DrmRights& drmRights,
187 const String8& rightsPath, const String8& contentPath) {
188 ALOGV("Entering saveRights");
189 return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
190 }
191
getOriginalMimeType(int uniqueId,const String8 & path,int fd)192 String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
193 ALOGV("Entering getOriginalMimeType");
194 return mDrmManager->getOriginalMimeType(uniqueId, path, fd);
195 }
196
getDrmObjectType(int uniqueId,const String8 & path,const String8 & mimeType)197 int DrmManagerService::getDrmObjectType(
198 int uniqueId, const String8& path, const String8& mimeType) {
199 ALOGV("Entering getDrmObjectType");
200 return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
201 }
202
checkRightsStatus(int uniqueId,const String8 & path,int action)203 int DrmManagerService::checkRightsStatus(
204 int uniqueId, const String8& path, int action) {
205 ALOGV("Entering checkRightsStatus");
206 return mDrmManager->checkRightsStatus(uniqueId, path, action);
207 }
208
consumeRights(int uniqueId,DecryptHandle * decryptHandle,int action,bool reserve)209 status_t DrmManagerService::consumeRights(
210 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
211 ALOGV("Entering consumeRights");
212 if (!isProtectedCallAllowed(CONSUME_RIGHTS)) {
213 return DRM_ERROR_NO_PERMISSION;
214 }
215 return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
216 }
217
setPlaybackStatus(int uniqueId,DecryptHandle * decryptHandle,int playbackStatus,int64_t position)218 status_t DrmManagerService::setPlaybackStatus(
219 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
220 ALOGV("Entering setPlaybackStatus");
221 if (!isProtectedCallAllowed(SET_PLAYBACK_STATUS)) {
222 return DRM_ERROR_NO_PERMISSION;
223 }
224 return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
225 }
226
validateAction(int uniqueId,const String8 & path,int action,const ActionDescription & description)227 bool DrmManagerService::validateAction(
228 int uniqueId, const String8& path,
229 int action, const ActionDescription& description) {
230 ALOGV("Entering validateAction");
231 return mDrmManager->validateAction(uniqueId, path, action, description);
232 }
233
removeRights(int uniqueId,const String8 & path)234 status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
235 ALOGV("Entering removeRights");
236 return mDrmManager->removeRights(uniqueId, path);
237 }
238
removeAllRights(int uniqueId)239 status_t DrmManagerService::removeAllRights(int uniqueId) {
240 ALOGV("Entering removeAllRights");
241 return mDrmManager->removeAllRights(uniqueId);
242 }
243
openConvertSession(int uniqueId,const String8 & mimeType)244 int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
245 ALOGV("Entering openConvertSession");
246 return mDrmManager->openConvertSession(uniqueId, mimeType);
247 }
248
convertData(int uniqueId,int convertId,const DrmBuffer * inputData)249 DrmConvertedStatus* DrmManagerService::convertData(
250 int uniqueId, int convertId, const DrmBuffer* inputData) {
251 ALOGV("Entering convertData");
252 return mDrmManager->convertData(uniqueId, convertId, inputData);
253 }
254
closeConvertSession(int uniqueId,int convertId)255 DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
256 ALOGV("Entering closeConvertSession");
257 return mDrmManager->closeConvertSession(uniqueId, convertId);
258 }
259
getAllSupportInfo(int uniqueId,int * length,DrmSupportInfo ** drmSupportInfoArray)260 status_t DrmManagerService::getAllSupportInfo(
261 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
262 ALOGV("Entering getAllSupportInfo");
263 return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
264 }
265
openDecryptSession(int uniqueId,int fd,off64_t offset,off64_t length,const char * mime)266 DecryptHandle* DrmManagerService::openDecryptSession(
267 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
268 ALOGV("Entering DrmManagerService::openDecryptSession");
269 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
270 return mDrmManager->openDecryptSession(uniqueId, fd, offset, length, mime);
271 }
272
273 return NULL;
274 }
275
openDecryptSession(int uniqueId,const char * uri,const char * mime)276 DecryptHandle* DrmManagerService::openDecryptSession(
277 int uniqueId, const char* uri, const char* mime) {
278 ALOGV("Entering DrmManagerService::openDecryptSession with uri");
279 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
280 return mDrmManager->openDecryptSession(uniqueId, uri, mime);
281 }
282
283 return NULL;
284 }
285
openDecryptSession(int uniqueId,const DrmBuffer & buf,const String8 & mimeType)286 DecryptHandle* DrmManagerService::openDecryptSession(
287 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
288 ALOGV("Entering DrmManagerService::openDecryptSession for streaming");
289 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
290 return mDrmManager->openDecryptSession(uniqueId, buf, mimeType);
291 }
292
293 return NULL;
294 }
295
closeDecryptSession(int uniqueId,DecryptHandle * decryptHandle)296 status_t DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
297 ALOGV("Entering closeDecryptSession");
298 if (!isProtectedCallAllowed(CLOSE_DECRYPT_SESSION)) {
299 return DRM_ERROR_NO_PERMISSION;
300 }
301 return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
302 }
303
initializeDecryptUnit(int uniqueId,DecryptHandle * decryptHandle,int decryptUnitId,const DrmBuffer * headerInfo)304 status_t DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
305 int decryptUnitId, const DrmBuffer* headerInfo) {
306 ALOGV("Entering initializeDecryptUnit");
307 if (!isProtectedCallAllowed(INITIALIZE_DECRYPT_UNIT)) {
308 return DRM_ERROR_NO_PERMISSION;
309 }
310 return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
311 }
312
decrypt(int uniqueId,DecryptHandle * decryptHandle,int decryptUnitId,const DrmBuffer * encBuffer,DrmBuffer ** decBuffer,DrmBuffer * IV)313 status_t DrmManagerService::decrypt(
314 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
315 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
316 ALOGV("Entering decrypt");
317 if (!isProtectedCallAllowed(DECRYPT)) {
318 return DRM_ERROR_NO_PERMISSION;
319 }
320 return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
321 }
322
finalizeDecryptUnit(int uniqueId,DecryptHandle * decryptHandle,int decryptUnitId)323 status_t DrmManagerService::finalizeDecryptUnit(
324 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
325 ALOGV("Entering finalizeDecryptUnit");
326 if (!isProtectedCallAllowed(FINALIZE_DECRYPT_UNIT)) {
327 return DRM_ERROR_NO_PERMISSION;
328 }
329 return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
330 }
331
pread(int uniqueId,DecryptHandle * decryptHandle,void * buffer,ssize_t numBytes,off64_t offset)332 ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
333 void* buffer, ssize_t numBytes, off64_t offset) {
334 ALOGV("Entering pread");
335 if (!isProtectedCallAllowed(PREAD)) {
336 return DRM_ERROR_NO_PERMISSION;
337 }
338 return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
339 }
340
dump(int fd,const Vector<String16> & args)341 status_t DrmManagerService::dump(int fd, const Vector<String16>& args)
342 {
343 const size_t SIZE = 256;
344 char buffer[SIZE];
345 String8 result;
346 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
347 snprintf(buffer, SIZE, "Permission Denial: "
348 "can't dump DrmManagerService from pid=%d, uid=%d\n",
349 IPCThreadState::self()->getCallingPid(),
350 IPCThreadState::self()->getCallingUid());
351 result.append(buffer);
352 } else {
353 #if DRM_MEMORY_LEAK_TRACK
354 bool dumpMem = false;
355 for (size_t i = 0; i < args.size(); i++) {
356 if (args[i] == String16("-m")) {
357 dumpMem = true;
358 }
359 }
360 if (dumpMem) {
361 dumpMemoryAddresses(fd);
362 }
363 #endif
364 }
365 write(fd, result.string(), result.size());
366 return NO_ERROR;
367 }
368
369