/* * Copyright 2017 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 #include #include #include #include namespace android { namespace test { class ProxyBQP : public BnGraphicBufferProducer { public: explicit ProxyBQP(const sp& producer) : mProducer(producer) {} // Pass through calls to mProducer status_t requestBuffer(int slot, sp* buf) override { return mProducer->requestBuffer(slot, buf); } status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) override { return mProducer->setMaxDequeuedBufferCount(maxDequeuedBuffers); } status_t setAsyncMode(bool async) override { return mProducer->setAsyncMode(async); } status_t dequeueBuffer(int* slot, sp* fence, uint32_t w, uint32_t h, PixelFormat format, uint64_t usage, uint64_t* outBufferAge, FrameEventHistoryDelta* outTimestamps) override { return mProducer->dequeueBuffer(slot, fence, w, h, format, usage, outBufferAge, outTimestamps); } status_t detachBuffer(int slot) override { return mProducer->detachBuffer(slot); } status_t detachNextBuffer(sp* outBuffer, sp* outFence) override { return mProducer->detachNextBuffer(outBuffer, outFence); } status_t attachBuffer(int* outSlot, const sp& buffer) override { return mProducer->attachBuffer(outSlot, buffer); } status_t queueBuffer(int slot, const QueueBufferInput& input, QueueBufferOutput* output) override { return mProducer->queueBuffer(slot, input, output); } status_t cancelBuffer(int slot, const sp& fence) override { return mProducer->cancelBuffer(slot, fence); } int query(int what, int* value) override { return mProducer->query(what, value); } status_t connect(const sp& listener, int api, bool producerControlledByApp, QueueBufferOutput* output) override { return mProducer->connect(listener, api, producerControlledByApp, output); } status_t disconnect(int api, DisconnectMode mode) override { return mProducer->disconnect(api, mode); } status_t setSidebandStream(const sp& stream) override { return mProducer->setSidebandStream(stream); } void allocateBuffers(uint32_t width, uint32_t height, PixelFormat format, uint64_t usage) override { mProducer->allocateBuffers(width, height, format, usage); } status_t allowAllocation(bool allow) override { return mProducer->allowAllocation(allow); } status_t setGenerationNumber(uint32_t generationNumber) override { return mProducer->setGenerationNumber(generationNumber); } String8 getConsumerName() const override { return mProducer->getConsumerName(); } status_t setSharedBufferMode(bool sharedBufferMode) override { return mProducer->setSharedBufferMode(sharedBufferMode); } status_t setAutoRefresh(bool autoRefresh) override { return mProducer->setAutoRefresh(autoRefresh); } status_t setDequeueTimeout(nsecs_t timeout) override { return mProducer->setDequeueTimeout(timeout); } status_t getLastQueuedBuffer(sp* outBuffer, sp* outFence, float outTransformMatrix[16]) override { return mProducer->getLastQueuedBuffer(outBuffer, outFence, outTransformMatrix); } void getFrameTimestamps(FrameEventHistoryDelta*) override {} status_t getUniqueId(uint64_t* outId) const override { return mProducer->getUniqueId(outId); } status_t getConsumerUsage(uint64_t* outUsage) const override { return mProducer->getConsumerUsage(outUsage); } protected: sp mProducer; }; class MaliciousBQP : public ProxyBQP { public: explicit MaliciousBQP(const sp& producer) : ProxyBQP(producer) {} void beMalicious(int32_t value) { mMaliciousValue = value; } void setExpectedSlot(int32_t slot) { mExpectedSlot = slot; } // Override dequeueBuffer, optionally corrupting the returned slot number status_t dequeueBuffer(int* buf, sp* fence, uint32_t width, uint32_t height, PixelFormat format, uint64_t usage, uint64_t* outBufferAge, FrameEventHistoryDelta* outTimestamps) override { EXPECT_EQ(BUFFER_NEEDS_REALLOCATION, mProducer->dequeueBuffer(buf, fence, width, height, format, usage, outBufferAge, outTimestamps)); EXPECT_EQ(mExpectedSlot, *buf); if (mMaliciousValue != 0) { *buf = mMaliciousValue; return NO_ERROR; } else { return BUFFER_NEEDS_REALLOCATION; } } private: int32_t mMaliciousValue = 0; int32_t mExpectedSlot = 0; }; class FakeListener : public BnConsumerListener { public: void onFrameAvailable(const BufferItem&) override {} void onBuffersReleased() override {} void onSidebandStreamChanged() override {} }; sp getMaliciousBQP() { sp producer; sp consumer; BufferQueue::createBufferQueue(&producer, &consumer); sp listener = new FakeListener; consumer->consumerConnect(listener, false); sp malicious = new MaliciousBQP(producer); return malicious; } TEST(Malicious, Bug36991414Max) { sp malicious = getMaliciousBQP(); sp surface = new Surface(malicious); ASSERT_EQ(NO_ERROR, surface->connect(NATIVE_WINDOW_API_CPU, nullptr, false)); ANativeWindow_Buffer buffer; ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr)); ASSERT_EQ(NO_ERROR, surface->unlockAndPost()); malicious->setExpectedSlot(1); malicious->beMalicious(std::numeric_limits::max()); ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr)); } TEST(Malicious, Bug36991414Min) { sp malicious = getMaliciousBQP(); sp surface = new Surface(malicious); ASSERT_EQ(NO_ERROR, surface->connect(NATIVE_WINDOW_API_CPU, nullptr, false)); ANativeWindow_Buffer buffer; ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr)); ASSERT_EQ(NO_ERROR, surface->unlockAndPost()); malicious->setExpectedSlot(1); malicious->beMalicious(std::numeric_limits::min()); ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr)); } TEST(Malicious, Bug36991414NegativeOne) { sp malicious = getMaliciousBQP(); sp surface = new Surface(malicious); ASSERT_EQ(NO_ERROR, surface->connect(NATIVE_WINDOW_API_CPU, nullptr, false)); ANativeWindow_Buffer buffer; ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr)); ASSERT_EQ(NO_ERROR, surface->unlockAndPost()); malicious->setExpectedSlot(1); malicious->beMalicious(-1); ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr)); } TEST(Malicious, Bug36991414NumSlots) { sp malicious = getMaliciousBQP(); sp surface = new Surface(malicious); ASSERT_EQ(NO_ERROR, surface->connect(NATIVE_WINDOW_API_CPU, nullptr, false)); ANativeWindow_Buffer buffer; ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr)); ASSERT_EQ(NO_ERROR, surface->unlockAndPost()); malicious->setExpectedSlot(1); malicious->beMalicious(BufferQueueDefs::NUM_BUFFER_SLOTS); ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr)); } } // namespace test } // namespace android