1 /*
2  * Copyright (C) 2017 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 "android.hardware.cas@1.0-DescramblerImpl"
19 
20 #include <hidlmemory/mapping.h>
21 #include <inttypes.h>
22 #include <media/cas/DescramblerAPI.h>
23 #include <media/hardware/CryptoAPI.h>
24 #include <media/stagefright/foundation/AString.h>
25 #include <media/stagefright/foundation/AUtils.h>
26 #include <utils/Log.h>
27 
28 #include "DescramblerImpl.h"
29 #include "SharedLibrary.h"
30 #include "TypeConvert.h"
31 
32 namespace android {
33 using hidl::memory::V1_0::IMemory;
34 
35 namespace hardware {
36 namespace cas {
37 namespace V1_0 {
38 namespace implementation {
39 
40 #define CHECK_SUBSAMPLE_DEF(type) \
41 static_assert(sizeof(SubSample) == sizeof(type::SubSample), \
42         "SubSample: size doesn't match"); \
43 static_assert(offsetof(SubSample, numBytesOfClearData) \
44         == offsetof(type::SubSample, mNumBytesOfClearData), \
45         "SubSample: numBytesOfClearData offset doesn't match"); \
46 static_assert(offsetof(SubSample, numBytesOfEncryptedData) \
47         == offsetof(type::SubSample, mNumBytesOfEncryptedData), \
48         "SubSample: numBytesOfEncryptedData offset doesn't match")
49 
50 CHECK_SUBSAMPLE_DEF(DescramblerPlugin);
51 CHECK_SUBSAMPLE_DEF(CryptoPlugin);
52 
DescramblerImpl(const sp<SharedLibrary> & library,DescramblerPlugin * plugin)53 DescramblerImpl::DescramblerImpl(
54         const sp<SharedLibrary>& library, DescramblerPlugin *plugin) :
55         mLibrary(library), mPluginHolder(plugin) {
56     ALOGV("CTOR: plugin=%p", mPluginHolder.get());
57 }
58 
~DescramblerImpl()59 DescramblerImpl::~DescramblerImpl() {
60     ALOGV("DTOR: plugin=%p", mPluginHolder.get());
61     release();
62 }
63 
setMediaCasSession(const HidlCasSessionId & sessionId)64 Return<Status> DescramblerImpl::setMediaCasSession(const HidlCasSessionId& sessionId) {
65     ALOGV("%s: sessionId=%s", __FUNCTION__,
66             sessionIdToString(sessionId).string());
67 
68     std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
69     if (holder.get() == nullptr) {
70         return toStatus(INVALID_OPERATION);
71     }
72 
73     return toStatus(holder->setMediaCasSession(sessionId));
74 }
75 
requiresSecureDecoderComponent(const hidl_string & mime)76 Return<bool> DescramblerImpl::requiresSecureDecoderComponent(
77         const hidl_string& mime) {
78     std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
79     if (holder.get() == nullptr) {
80         return false;
81     }
82 
83     return holder->requiresSecureDecoderComponent(String8(mime.c_str()));
84 }
85 
validateRangeForSize(uint64_t offset,uint64_t length,uint64_t size)86 static inline bool validateRangeForSize(
87         uint64_t offset, uint64_t length, uint64_t size) {
88     return isInRange<uint64_t, uint64_t>(0, size, offset, length);
89 }
90 
descramble(ScramblingControl scramblingControl,const hidl_vec<SubSample> & subSamples,const SharedBuffer & srcBuffer,uint64_t srcOffset,const DestinationBuffer & dstBuffer,uint64_t dstOffset,descramble_cb _hidl_cb)91 Return<void> DescramblerImpl::descramble(
92         ScramblingControl scramblingControl,
93         const hidl_vec<SubSample>& subSamples,
94         const SharedBuffer& srcBuffer,
95         uint64_t srcOffset,
96         const DestinationBuffer& dstBuffer,
97         uint64_t dstOffset,
98         descramble_cb _hidl_cb) {
99     ALOGV("%s", __FUNCTION__);
100 
101     // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
102     // size in size_t. If size is over SIZE_MAX, mapMemory mapMemory could succeed
103     // but the mapped memory's actual size will be smaller than the reported size.
104     if (srcBuffer.heapBase.size() > SIZE_MAX) {
105         ALOGE("Invalid hidl_memory size: %" PRIu64 "", srcBuffer.heapBase.size());
106         android_errorWriteLog(0x534e4554, "79376389");
107         _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
108         return Void();
109     }
110 
111     sp<IMemory> srcMem = mapMemory(srcBuffer.heapBase);
112 
113     // Validate if the offset and size in the SharedBuffer is consistent with the
114     // mapped ashmem, since the offset and size is controlled by client.
115     if (srcMem == NULL) {
116         ALOGE("Failed to map src buffer.");
117         _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
118         return Void();
119     }
120     if (!validateRangeForSize(
121             srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize())) {
122         ALOGE("Invalid src buffer range: offset %" PRIu64 ", size %" PRIu64 ", srcMem"
123               "size %" PRIu64 "", srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize());
124         android_errorWriteLog(0x534e4554, "67962232");
125         _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
126         return Void();
127     }
128 
129     // use 64-bit here to catch bad subsample size that might be overflowing.
130     uint64_t totalBytesInSubSamples = 0;
131     for (size_t i = 0; i < subSamples.size(); i++) {
132         totalBytesInSubSamples += (uint64_t)subSamples[i].numBytesOfClearData +
133                 subSamples[i].numBytesOfEncryptedData;
134     }
135     // Further validate if the specified srcOffset and requested total subsample size
136     // is consistent with the source shared buffer size.
137     if (!validateRangeForSize(srcOffset, totalBytesInSubSamples, srcBuffer.size)) {
138         ALOGE("Invalid srcOffset and subsample size: "
139                 "srcOffset %" PRIu64 ", totalBytesInSubSamples %" PRIu64 ", srcBuffer"
140                 "size %" PRIu64 "", srcOffset, totalBytesInSubSamples, srcBuffer.size);
141         android_errorWriteLog(0x534e4554, "67962232");
142         _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
143         return Void();
144     }
145 
146     void *srcPtr = (uint8_t *)(void *)srcMem->getPointer() + srcBuffer.offset;
147     void *dstPtr = NULL;
148     if (dstBuffer.type == BufferType::SHARED_MEMORY) {
149         // When using shared memory, src buffer is also used as dst,
150         // we don't map it again here.
151         dstPtr = srcPtr;
152 
153         // In this case the dst and src would be the same buffer, need to validate
154         // dstOffset against the buffer size too.
155         if (!validateRangeForSize(dstOffset, totalBytesInSubSamples, srcBuffer.size)) {
156             ALOGE("Invalid dstOffset and subsample size: "
157                     "dstOffset %" PRIu64 ", totalBytesInSubSamples %" PRIu64 ", srcBuffer"
158                     "size %" PRIu64 "", dstOffset, totalBytesInSubSamples, srcBuffer.size);
159             android_errorWriteLog(0x534e4554, "67962232");
160             _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
161             return Void();
162         }
163     } else {
164         native_handle_t *handle = const_cast<native_handle_t *>(
165                 dstBuffer.secureMemory.getNativeHandle());
166         dstPtr = static_cast<void *>(handle);
167     }
168 
169     // Get a local copy of the shared_ptr for the plugin. Note that before
170     // calling the HIDL callback, this shared_ptr must be manually reset,
171     // since the client side could proceed as soon as the callback is called
172     // without waiting for this method to go out of scope.
173     std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
174     if (holder.get() == nullptr) {
175         _hidl_cb(toStatus(INVALID_OPERATION), 0, NULL);
176         return Void();
177     }
178 
179     // Casting hidl SubSample to DescramblerPlugin::SubSample, but need
180     // to ensure structs are actually idential
181 
182     AString detailedError;
183     int32_t result = holder->descramble(
184             dstBuffer.type != BufferType::SHARED_MEMORY,
185             (DescramblerPlugin::ScramblingControl)scramblingControl,
186             subSamples.size(),
187             (DescramblerPlugin::SubSample*)subSamples.data(),
188             srcPtr,
189             srcOffset,
190             dstPtr,
191             dstOffset,
192             &detailedError);
193 
194     holder.reset();
195     _hidl_cb(toStatus(result >= 0 ? OK : result), result, detailedError.c_str());
196     return Void();
197 }
198 
release()199 Return<Status> DescramblerImpl::release() {
200     ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
201 
202     std::shared_ptr<DescramblerPlugin> holder(nullptr);
203     std::atomic_store(&mPluginHolder, holder);
204 
205     return Status::OK;
206 }
207 
208 } // namespace implementation
209 } // namespace V1_0
210 } // namespace cas
211 } // namespace hardware
212 } // namespace android
213