blob: 857d73ecb02117f53fb2ee520bb8dd2be6a5e6e0 [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
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
Takeshi Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshi27ed8ad2010-07-29 10:12:27 +090018#define LOG_TAG "DrmManagerService(Native)"
19#include <utils/Log.h>
20
Takeshi Aimi34738462010-11-16 13:56:11 +090021#include <private/android_filesystem_config.h>
James Dong8635b7b2011-03-14 17:01:38 -070022#include <media/MemoryLeakTrackUtil.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090023
aimitakeshi27ed8ad2010-07-29 10:12:27 +090024#include <errno.h>
25#include <utils/threads.h>
26#include <binder/IServiceManager.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090027#include <binder/IPCThreadState.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090028#include <sys/stat.h>
29#include "DrmManagerService.h"
30#include "DrmManager.h"
31
Riley Spahnf785f492014-07-01 15:32:31 -070032#include <selinux/android.h>
33
aimitakeshi27ed8ad2010-07-29 10:12:27 +090034using namespace android;
35
Riley Spahnf785f492014-07-01 15:32:31 -070036static int selinux_enabled;
37static char *drmserver_context;
Takeshi Aimi34738462010-11-16 13:56:11 +090038static Vector<uid_t> trustedUids;
39
Riley Spahnf785f492014-07-01 15:32:31 -070040const char *const DrmManagerService::drm_perm_labels[] = {
41 "consumeRights",
42 "setPlaybackStatus",
43 "openDecryptSession",
44 "closeDecryptSession",
45 "initializeDecryptUnit",
46 "decrypt",
47 "finalizeDecryptUnit",
48 "pread"
49};
50
51const 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
62bool 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
83bool DrmManagerService::isProtectedCallAllowed(drm_perm_t perm) {
Jeff Tinker6868e982014-06-17 16:45:46 -070084 // 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();
Riley Spahnf785f492014-07-01 15:32:31 -070089 pid_t spid = ipcState->getCallingPid();
Jeff Tinker6868e982014-06-17 16:45:46 -070090
91 for (unsigned int i = 0; i < trustedUids.size(); ++i) {
92 if (trustedUids[i] == uid) {
Riley Spahnf785f492014-07-01 15:32:31 -070093 return selinuxIsProtectedCallAllowed(spid, perm);
Jeff Tinker6868e982014-06-17 16:45:46 -070094 }
95 }
96 return false;
Takeshi Aimi34738462010-11-16 13:56:11 +090097}
98
aimitakeshi27ed8ad2010-07-29 10:12:27 +090099void DrmManagerService::instantiate() {
Steve Block3856b092011-10-20 11:56:00 +0100100 ALOGV("instantiate");
Takeshi Aimie943f842010-10-08 23:05:49 +0900101 defaultServiceManager()->addService(String16("drm.drmManager"), new DrmManagerService());
Takeshi Aimi34738462010-11-16 13:56:11 +0900102
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 }
Riley Spahnf785f492014-07-01 15:32:31 -0700111
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);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900121}
122
Takeshi Aimie943f842010-10-08 23:05:49 +0900123DrmManagerService::DrmManagerService() :
124 mDrmManager(NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100125 ALOGV("created");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900126 mDrmManager = new DrmManager();
Takeshi Aimie943f842010-10-08 23:05:49 +0900127 mDrmManager->loadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900128}
129
130DrmManagerService::~DrmManagerService() {
Steve Block3856b092011-10-20 11:56:00 +0100131 ALOGV("Destroyed");
Takeshi Aimie943f842010-10-08 23:05:49 +0900132 mDrmManager->unloadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900133 delete mDrmManager; mDrmManager = NULL;
134}
135
Gloria Wang8f001512011-07-21 15:10:22 -0700136int DrmManagerService::addUniqueId(bool isNative) {
137 return mDrmManager->addUniqueId(isNative);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900138}
139
140void DrmManagerService::removeUniqueId(int uniqueId) {
141 mDrmManager->removeUniqueId(uniqueId);
142}
143
Takeshi Aimie943f842010-10-08 23:05:49 +0900144void DrmManagerService::addClient(int uniqueId) {
145 mDrmManager->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900146}
147
Takeshi Aimie943f842010-10-08 23:05:49 +0900148void DrmManagerService::removeClient(int uniqueId) {
149 mDrmManager->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900150}
151
152status_t DrmManagerService::setDrmServiceListener(
153 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Steve Block3856b092011-10-20 11:56:00 +0100154 ALOGV("Entering setDrmServiceListener");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900155 mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
156 return DRM_NO_ERROR;
157}
158
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900159DrmConstraints* DrmManagerService::getConstraints(
160 int uniqueId, const String8* path, const int action) {
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("Entering getConstraints from content");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900162 return mDrmManager->getConstraints(uniqueId, path, action);
163}
164
Takeshi Aimi34738462010-11-16 13:56:11 +0900165DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
Steve Block3856b092011-10-20 11:56:00 +0100166 ALOGV("Entering getMetadata from content");
Takeshi Aimi34738462010-11-16 13:56:11 +0900167 return mDrmManager->getMetadata(uniqueId, path);
168}
169
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900170bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100171 ALOGV("Entering canHandle");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900172 return mDrmManager->canHandle(uniqueId, path, mimeType);
173}
174
175DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100176 ALOGV("Entering processDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900177 return mDrmManager->processDrmInfo(uniqueId, drmInfo);
178}
179
180DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Steve Block3856b092011-10-20 11:56:00 +0100181 ALOGV("Entering acquireDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900182 return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
183}
184
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900185status_t DrmManagerService::saveRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900186 int uniqueId, const DrmRights& drmRights,
187 const String8& rightsPath, const String8& contentPath) {
Steve Block3856b092011-10-20 11:56:00 +0100188 ALOGV("Entering saveRights");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900189 return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
190}
191
James Dongbf5b3b22012-07-30 17:57:39 -0700192String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Steve Block3856b092011-10-20 11:56:00 +0100193 ALOGV("Entering getOriginalMimeType");
James Dongbf5b3b22012-07-30 17:57:39 -0700194 return mDrmManager->getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900195}
196
197int DrmManagerService::getDrmObjectType(
198 int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100199 ALOGV("Entering getDrmObjectType");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900200 return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
201}
202
203int DrmManagerService::checkRightsStatus(
204 int uniqueId, const String8& path, int action) {
Steve Block3856b092011-10-20 11:56:00 +0100205 ALOGV("Entering checkRightsStatus");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900206 return mDrmManager->checkRightsStatus(uniqueId, path, action);
207}
208
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900209status_t DrmManagerService::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900210 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Steve Block3856b092011-10-20 11:56:00 +0100211 ALOGV("Entering consumeRights");
Riley Spahnf785f492014-07-01 15:32:31 -0700212 if (!isProtectedCallAllowed(CONSUME_RIGHTS)) {
James Dong328745b2012-02-28 13:55:55 -0800213 return DRM_ERROR_NO_PERMISSION;
214 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900215 return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900216}
217
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900218status_t DrmManagerService::setPlaybackStatus(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800219 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("Entering setPlaybackStatus");
Riley Spahnf785f492014-07-01 15:32:31 -0700221 if (!isProtectedCallAllowed(SET_PLAYBACK_STATUS)) {
James Dong328745b2012-02-28 13:55:55 -0800222 return DRM_ERROR_NO_PERMISSION;
223 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900224 return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900225}
226
227bool DrmManagerService::validateAction(
228 int uniqueId, const String8& path,
229 int action, const ActionDescription& description) {
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("Entering validateAction");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900231 return mDrmManager->validateAction(uniqueId, path, action, description);
232}
233
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900234status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
Steve Block3856b092011-10-20 11:56:00 +0100235 ALOGV("Entering removeRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900236 return mDrmManager->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900237}
238
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900239status_t DrmManagerService::removeAllRights(int uniqueId) {
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("Entering removeAllRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900241 return mDrmManager->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900242}
243
244int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100245 ALOGV("Entering openConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900246 return mDrmManager->openConvertSession(uniqueId, mimeType);
247}
248
249DrmConvertedStatus* DrmManagerService::convertData(
250 int uniqueId, int convertId, const DrmBuffer* inputData) {
Steve Block3856b092011-10-20 11:56:00 +0100251 ALOGV("Entering convertData");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900252 return mDrmManager->convertData(uniqueId, convertId, inputData);
253}
254
255DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("Entering closeConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900257 return mDrmManager->closeConvertSession(uniqueId, convertId);
258}
259
260status_t DrmManagerService::getAllSupportInfo(
261 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("Entering getAllSupportInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900263 return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
264}
265
266DecryptHandle* DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800267 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("Entering DrmManagerService::openDecryptSession");
Riley Spahnf785f492014-07-01 15:32:31 -0700269 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
James Dong9d2f3862012-01-10 08:24:37 -0800270 return mDrmManager->openDecryptSession(uniqueId, fd, offset, length, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900271 }
272
273 return NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900274}
275
Takeshi Aimie943f842010-10-08 23:05:49 +0900276DecryptHandle* DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800277 int uniqueId, const char* uri, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100278 ALOGV("Entering DrmManagerService::openDecryptSession with uri");
Riley Spahnf785f492014-07-01 15:32:31 -0700279 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
James Dong9d2f3862012-01-10 08:24:37 -0800280 return mDrmManager->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900281 }
282
283 return NULL;
Takeshi Aimie943f842010-10-08 23:05:49 +0900284}
285
Kei Takahashicba7b322012-01-18 17:10:19 +0900286DecryptHandle* DrmManagerService::openDecryptSession(
287 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
288 ALOGV("Entering DrmManagerService::openDecryptSession for streaming");
Riley Spahnf785f492014-07-01 15:32:31 -0700289 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
Kei Takahashicba7b322012-01-18 17:10:19 +0900290 return mDrmManager->openDecryptSession(uniqueId, buf, mimeType);
291 }
292
293 return NULL;
294}
295
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900296status_t DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("Entering closeDecryptSession");
Riley Spahnf785f492014-07-01 15:32:31 -0700298 if (!isProtectedCallAllowed(CLOSE_DECRYPT_SESSION)) {
James Dong328745b2012-02-28 13:55:55 -0800299 return DRM_ERROR_NO_PERMISSION;
300 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900301 return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900302}
303
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900304status_t DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900305 int decryptUnitId, const DrmBuffer* headerInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100306 ALOGV("Entering initializeDecryptUnit");
Riley Spahnf785f492014-07-01 15:32:31 -0700307 if (!isProtectedCallAllowed(INITIALIZE_DECRYPT_UNIT)) {
James Dong328745b2012-02-28 13:55:55 -0800308 return DRM_ERROR_NO_PERMISSION;
309 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900310 return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900311}
312
313status_t DrmManagerService::decrypt(
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900314 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
315 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
Steve Block3856b092011-10-20 11:56:00 +0100316 ALOGV("Entering decrypt");
Riley Spahnf785f492014-07-01 15:32:31 -0700317 if (!isProtectedCallAllowed(DECRYPT)) {
James Dong328745b2012-02-28 13:55:55 -0800318 return DRM_ERROR_NO_PERMISSION;
319 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900320 return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900321}
322
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900323status_t DrmManagerService::finalizeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900324 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("Entering finalizeDecryptUnit");
Riley Spahnf785f492014-07-01 15:32:31 -0700326 if (!isProtectedCallAllowed(FINALIZE_DECRYPT_UNIT)) {
James Dong328745b2012-02-28 13:55:55 -0800327 return DRM_ERROR_NO_PERMISSION;
328 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900329 return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900330}
331
332ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800333 void* buffer, ssize_t numBytes, off64_t offset) {
Steve Block3856b092011-10-20 11:56:00 +0100334 ALOGV("Entering pread");
Riley Spahnf785f492014-07-01 15:32:31 -0700335 if (!isProtectedCallAllowed(PREAD)) {
James Dong328745b2012-02-28 13:55:55 -0800336 return DRM_ERROR_NO_PERMISSION;
337 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900338 return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
339}
340
James Dong8635b7b2011-03-14 17:01:38 -0700341status_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