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