Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 71567037 authored by android-build-team Robot's avatar android-build-team Robot
Browse files

Snap for 6029552 from 88468e16 to rvc-release

Change-Id: I9e377f6f9c29dbe06ff96105fa4ec108d4d430b2
parents 11a5020f 88468e16
Loading
Loading
Loading
Loading
+19 −53
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ using ::android::hardware::hidl_handle;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::HidlMemory;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
@@ -255,10 +256,7 @@ bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
 * size.  Once the heap base is established, shared memory buffers
 * are sent by providing an offset into the heap and a buffer size.
 */
int32_t CryptoHal::setHeapBase(const sp<IMemoryHeap>& heap) {
    using ::android::hardware::fromHeap;
    using ::android::hardware::HidlMemory;

int32_t CryptoHal::setHeapBase(const sp<HidlMemory>& heap) {
    if (heap == NULL || mHeapSeqNum < 0) {
        ALOGE("setHeapBase(): heap %p mHeapSeqNum %d", heap.get(), mHeapSeqNum);
        return -1;
@@ -268,9 +266,8 @@ int32_t CryptoHal::setHeapBase(const sp<IMemoryHeap>& heap) {

    int32_t seqNum = mHeapSeqNum++;
    uint32_t bufferId = static_cast<uint32_t>(seqNum);
    sp<HidlMemory> hidlMemory = fromHeap(heap);
    mHeapBases.add(seqNum, HeapBase(bufferId, heap->getSize()));
    Return<void> hResult = mPlugin->setSharedBufferBase(*hidlMemory, bufferId);
    mHeapSizes.add(seqNum, heap->size());
    Return<void> hResult = mPlugin->setSharedBufferBase(*heap, bufferId);
    ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
    return seqNum;
}
@@ -285,64 +282,40 @@ void CryptoHal::clearHeapBase(int32_t seqNum) {
     * TODO: Add a releaseSharedBuffer method in a future DRM HAL
     * API version to make this explicit.
     */
    ssize_t index = mHeapBases.indexOfKey(seqNum);
    ssize_t index = mHeapSizes.indexOfKey(seqNum);
    if (index >= 0) {
        if (mPlugin != NULL) {
            uint32_t bufferId = mHeapBases[index].getBufferId();
            uint32_t bufferId = static_cast<uint32_t>(seqNum);
            Return<void> hResult = mPlugin->setSharedBufferBase(hidl_memory(), bufferId);
            ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
        }
        mHeapBases.removeItem(seqNum);
        mHeapSizes.removeItem(seqNum);
    }
}

status_t CryptoHal::toSharedBuffer(const sp<IMemory>& memory, int32_t seqNum, ::SharedBuffer* buffer) {
    ssize_t offset;
    size_t size;

    if (memory == NULL || buffer == NULL) {
        return UNEXPECTED_NULL;
    }

    sp<IMemoryHeap> heap = memory->getMemory(&offset, &size);
    if (heap == NULL) {
        return UNEXPECTED_NULL;
    }

status_t CryptoHal::checkSharedBuffer(const ::SharedBuffer &buffer) {
    int32_t seqNum = static_cast<int32_t>(buffer.bufferId);
    // memory must be in one of the heaps that have been set
    if (mHeapBases.indexOfKey(seqNum) < 0) {
        return UNKNOWN_ERROR;
    }

    // heap must be the same size as the one that was set in setHeapBase
    if (mHeapBases.valueFor(seqNum).getSize() != heap->getSize()) {
        android_errorWriteLog(0x534e4554, "76221123");
    if (mHeapSizes.indexOfKey(seqNum) < 0) {
        return UNKNOWN_ERROR;
    }

    // memory must be within the address space of the heap
    // TODO: Using unsecurePointer() has some associated security pitfalls
    //       (see declaration for details).
    //       Either document why it is safe in this case or address the
    //       issue (e.g. by copying).
    if (memory->unsecurePointer() != static_cast<uint8_t *>(heap->getBase()) + memory->offset()  ||
            heap->getSize() < memory->offset() + memory->size() ||
            SIZE_MAX - memory->offset() < memory->size()) {
    size_t heapSize = mHeapSizes.valueFor(seqNum);
    if (heapSize < buffer.offset + buffer.size ||
            SIZE_MAX - buffer.offset < buffer.size) {
        android_errorWriteLog(0x534e4554, "76221123");
        return UNKNOWN_ERROR;
    }

    buffer->bufferId = mHeapBases.valueFor(seqNum).getBufferId();
    buffer->offset = offset >= 0 ? offset : 0;
    buffer->size = size;
    return OK;
}

ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
        CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
        const ICrypto::SourceBuffer &source, size_t offset,
        const ::SharedBuffer &hSource, size_t offset,
        const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
        const ICrypto::DestinationBuffer &destination, AString *errorDetailMsg) {
        const ::DestinationBuffer &hDestination, AString *errorDetailMsg) {
    Mutex::Autolock autoLock(mLock);

    if (mInitCheck != OK) {
@@ -380,28 +353,21 @@ ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
    }
    auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);

    int32_t heapSeqNum = source.mHeapSeqNum;
    bool secure;
    ::DestinationBuffer hDestination;
    if (destination.mType == kDestinationTypeSharedMemory) {
        hDestination.type = BufferType::SHARED_MEMORY;
        status_t status = toSharedBuffer(destination.mSharedMemory, heapSeqNum,
                &hDestination.nonsecureMemory);
    if (hDestination.type == BufferType::SHARED_MEMORY) {
        status_t status = checkSharedBuffer(hDestination.nonsecureMemory);
        if (status != OK) {
            return status;
        }
        secure = false;
    } else if (destination.mType == kDestinationTypeNativeHandle) {
        hDestination.type = BufferType::NATIVE_HANDLE;
        hDestination.secureMemory = hidl_handle(destination.mHandle);
    } else if (hDestination.type == BufferType::NATIVE_HANDLE) {
        secure = true;
    } else {
        android_errorWriteLog(0x534e4554, "70526702");
        return UNKNOWN_ERROR;
    }

    ::SharedBuffer hSource;
    status_t status = toSharedBuffer(source.mSharedMemory, heapSeqNum, &hSource);
    status_t status = checkSharedBuffer(hSource);
    if (status != OK) {
        return status;
    }
+4 −20
Original line number Diff line number Diff line
@@ -20,8 +20,7 @@

#include <utils/Log.h>

#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <android/binder_manager.h>

#include <aidl/android/media/BnResourceManagerClient.h>
#include <android/hardware/drm/1.2/types.h>
@@ -99,17 +98,6 @@ namespace android {

#define INIT_CHECK() {if (mInitCheck != OK) return mInitCheck;}

static inline int getCallingPid() {
    return IPCThreadState::self()->getCallingPid();
}

static bool checkPermission(const char* permissionString) {
    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
    bool ok = checkCallingPermission(String16(permissionString));
    if (!ok) ALOGE("Request requires %s", permissionString);
    return ok;
}

static const Vector<uint8_t> toVector(const hidl_vec<uint8_t> &vec) {
    Vector<uint8_t> vector;
    vector.appendArray(vec.data(), vec.size());
@@ -454,7 +442,7 @@ sp<IDrmPlugin> DrmHal::makeDrmPlugin(const sp<IDrmFactory>& factory,
        const uint8_t uuid[16], const String8& appPackageName) {
    mAppPackageName = appPackageName;
    mMetrics.SetAppPackageName(appPackageName);
    mMetrics.SetAppUid(IPCThreadState::self()->getCallingUid());
    mMetrics.SetAppUid(AIBinder_getCallingUid());

    sp<IDrmPlugin> plugin;
    Return<void> hResult = factory->createPlugin(uuid, appPackageName.string(),
@@ -751,7 +739,7 @@ status_t DrmHal::openSession(DrmPlugin::SecurityLevel level,
            // reclaimSession may call back to closeSession, since mLock is
            // shared between Drm instances, we should unlock here to avoid
            // deadlock.
            retry = DrmSessionManager::Instance()->reclaimSession(getCallingPid());
            retry = DrmSessionManager::Instance()->reclaimSession(AIBinder_getCallingPid());
            mLock.lock();
        } else {
            retry = false;
@@ -760,7 +748,7 @@ status_t DrmHal::openSession(DrmPlugin::SecurityLevel level,

    if (err == OK) {
        std::shared_ptr<DrmSessionClient> client(new DrmSessionClient(this, sessionId));
        DrmSessionManager::Instance()->addSession(getCallingPid(),
        DrmSessionManager::Instance()->addSession(AIBinder_getCallingPid(),
                std::static_pointer_cast<IResourceManagerClient>(client), sessionId);
        mOpenSessions.push_back(client);
        mMetrics.SetSessionStart(sessionId);
@@ -1543,10 +1531,6 @@ status_t DrmHal::signRSA(Vector<uint8_t> const &sessionId,
    Mutex::Autolock autoLock(mLock);
    INIT_CHECK();

    if (!checkPermission("android.permission.ACCESS_DRM_CERTIFICATES")) {
        return -EPERM;
    }

    DrmSessionManager::Instance()->useSession(sessionId);

    status_t err = UNKNOWN_ERROR;
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <binder/PersistableBundle.h>
#include <mediadrm/IDrmMetricsConsumer.h>
#include <utils/Errors.h>

#ifndef ANDROID_BUNDLEMETRICSCONSUMER_H_

#define ANDROID_BUNDLEMETRICSCONSUMER_H_

namespace android {

/**
 * IDrmMetricsConsumer which saves IDrm/ICrypto metrics into a PersistableBundle.
 *
 * Example usage:
 *
 *   PersistableBundle bundle;
 *   BundleDrmMetricsConsumer consumer(&bundle);
 *   drm->exportMetrics(&consumer);
 *   crypto->exportMetrics(&consumer);
 *   // bundle now contains metrics from drm/crypto.
 *
 */
struct BundleDrmMetricsConsumer : public IDrmMetricsConsumer {
    BundleDrmMetricsConsumer(os::PersistableBundle*) {}

    status_t consumeFrameworkMetrics(const MediaDrmMetrics &) override {
        return OK;
    }

    status_t consumeHidlMetrics(
            const String8 &/*vendor*/,
            const hidl_vec<DrmMetricGroup> &/*pluginMetrics*/) override {
        return OK;
    }

private:
    DISALLOW_EVIL_CONSTRUCTORS(BundleDrmMetricsConsumer);
};

}  // namespace android

#endif // ANDROID_BUNDLEMETRICSCONSUMER_H_
+9 −19
Original line number Diff line number Diff line
@@ -31,6 +31,9 @@ namespace drm = ::android::hardware::drm;
using drm::V1_0::ICryptoFactory;
using drm::V1_0::ICryptoPlugin;
using drm::V1_0::SharedBuffer;
using drm::V1_0::DestinationBuffer;

using ::android::hardware::HidlMemory;

class IMemoryHeap;

@@ -58,12 +61,12 @@ struct CryptoHal : public ICrypto {

    virtual ssize_t decrypt(const uint8_t key[16], const uint8_t iv[16],
            CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
            const ICrypto::SourceBuffer &source, size_t offset,
            const ::SharedBuffer &source, size_t offset,
            const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
            const ICrypto::DestinationBuffer &destination,
            const ::DestinationBuffer &destination,
            AString *errorDetailMsg);

    virtual int32_t setHeap(const sp<IMemoryHeap>& heap) {
    virtual int32_t setHeap(const sp<HidlMemory>& heap) {
        return setHeapBase(heap);
    }
    virtual void unsetHeap(int32_t seqNum) { clearHeapBase(seqNum); }
@@ -83,30 +86,17 @@ private:
     */
    status_t mInitCheck;

    struct HeapBase {
        HeapBase() : mBufferId(0), mSize(0) {}
        HeapBase(uint32_t bufferId, size_t size) :
            mBufferId(bufferId), mSize(size) {}

        uint32_t getBufferId() const {return mBufferId;}
        size_t getSize() const {return mSize;}

    private:
        uint32_t mBufferId;
        size_t mSize;
    };

    KeyedVector<int32_t, HeapBase> mHeapBases;
    KeyedVector<int32_t, size_t> mHeapSizes;
    int32_t mHeapSeqNum;

    Vector<sp<ICryptoFactory>> makeCryptoFactories();
    sp<ICryptoPlugin> makeCryptoPlugin(const sp<ICryptoFactory>& factory,
            const uint8_t uuid[16], const void *initData, size_t size);

    int32_t setHeapBase(const sp<IMemoryHeap>& heap);
    int32_t setHeapBase(const sp<HidlMemory>& heap);
    void clearHeapBase(int32_t seqNum);

    status_t toSharedBuffer(const sp<IMemory>& memory, int32_t seqNum, ::SharedBuffer* buffer);
    status_t checkSharedBuffer(const ::SharedBuffer& buffer);

    DISALLOW_EVIL_CONSTRUCTORS(CryptoHal);
};
+87 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android/hardware/drm/1.1/types.h>
#include <hidl/HidlSupport.h>
#include <media/stagefright/foundation/ABase.h>

#ifndef ANDROID_IDRMMETRICSCONSUMER_H_

#define ANDROID_IDRMMETRICSCONSUMER_H_

using ::android::hardware::hidl_vec;
using ::android::hardware::drm::V1_1::DrmMetricGroup;

namespace android {

class MediaDrmMetrics;
class String8;

/**
 * Interface to consume metrics produced by the IDrm/ICrypto
 *
 * To use with IDrm:
 *   drm->exportMetrics(&consumer);
 *
 * IDrmMetricsConsumer::consumeFrameworkMetrics &
 * IDrmMetricsConsumer::consumeHidlMetrics implementations
 * would each be invoked once per call to IDrm::exportMetrics.
 * |consumeFrameworkMetrics| would be called for plugin-agnostic
 * framework metrics; |consumeHidlMetrics| would be called for
 * plugin specific metrics.
 *
 * ----------------------------------------
 *
 * To use with ICrypto:
 *   crypto->exportMetrics(&consumer);
 *
 * IDrmMetricsConsumer::consumeHidlMetrics implementation
 * would each be invoked once per call to ICrypto::exportMetrics.
 * ICrypto metrics are plugin agnostic.
 *
 * ----------------------------------------
 *
 * For an example implementation of IDrmMetricsConsumer, please
 * see BundleDrmMetricsConsumer. BundleDrmMetricsConsumer consumes
 * IDrm/ICrypto metrics and saves the metrics to a PersistableBundle.
 *
 */
struct IDrmMetricsConsumer : public RefBase {

    virtual ~IDrmMetricsConsumer() {}

    /**
     * Consume framework (plugin agnostic) MediaDrmMetrics
     */
    virtual status_t consumeFrameworkMetrics(const MediaDrmMetrics &) = 0;

    /**
     * Consume list of DrmMetricGroup with optional Drm vendor name
     */
    virtual status_t consumeHidlMetrics(
            const String8 &vendor,
            const hidl_vec<DrmMetricGroup> &pluginMetrics) = 0;

protected:
    IDrmMetricsConsumer() {}

private:
    DISALLOW_EVIL_CONSTRUCTORS(IDrmMetricsConsumer);
};

}  // namespace android

#endif // ANDROID_IDRMMETRICSCONSUMER_H_
Loading