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