xref: /aosp_15_r20/frameworks/native/libs/gui/tests/Malicious.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2017 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <gui/BufferQueue.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <gui/IProducerListener.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <gui/Surface.h>
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include <android/native_window.h>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker namespace android {
26*38e8c45fSAndroid Build Coastguard Worker namespace test {
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker class ProxyBQP : public BnGraphicBufferProducer {
29*38e8c45fSAndroid Build Coastguard Worker public:
ProxyBQP(const sp<IGraphicBufferProducer> & producer)30*38e8c45fSAndroid Build Coastguard Worker     explicit ProxyBQP(const sp<IGraphicBufferProducer>& producer) : mProducer(producer) {}
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker     // Pass through calls to mProducer
requestBuffer(int slot,sp<GraphicBuffer> * buf)33*38e8c45fSAndroid Build Coastguard Worker     status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) override {
34*38e8c45fSAndroid Build Coastguard Worker         return mProducer->requestBuffer(slot, buf);
35*38e8c45fSAndroid Build Coastguard Worker     }
setMaxDequeuedBufferCount(int maxDequeuedBuffers)36*38e8c45fSAndroid Build Coastguard Worker     status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) override {
37*38e8c45fSAndroid Build Coastguard Worker         return mProducer->setMaxDequeuedBufferCount(maxDequeuedBuffers);
38*38e8c45fSAndroid Build Coastguard Worker     }
setAsyncMode(bool async)39*38e8c45fSAndroid Build Coastguard Worker     status_t setAsyncMode(bool async) override { return mProducer->setAsyncMode(async); }
dequeueBuffer(int * slot,sp<Fence> * fence,uint32_t w,uint32_t h,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)40*38e8c45fSAndroid Build Coastguard Worker     status_t dequeueBuffer(int* slot, sp<Fence>* fence, uint32_t w, uint32_t h, PixelFormat format,
41*38e8c45fSAndroid Build Coastguard Worker                            uint64_t usage, uint64_t* outBufferAge,
42*38e8c45fSAndroid Build Coastguard Worker                            FrameEventHistoryDelta* outTimestamps) override {
43*38e8c45fSAndroid Build Coastguard Worker         return mProducer->dequeueBuffer(slot, fence, w, h, format, usage, outBufferAge,
44*38e8c45fSAndroid Build Coastguard Worker                                         outTimestamps);
45*38e8c45fSAndroid Build Coastguard Worker     }
detachBuffer(int slot)46*38e8c45fSAndroid Build Coastguard Worker     status_t detachBuffer(int slot) override { return mProducer->detachBuffer(slot); }
detachNextBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)47*38e8c45fSAndroid Build Coastguard Worker     status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) override {
48*38e8c45fSAndroid Build Coastguard Worker         return mProducer->detachNextBuffer(outBuffer, outFence);
49*38e8c45fSAndroid Build Coastguard Worker     }
attachBuffer(int * outSlot,const sp<GraphicBuffer> & buffer)50*38e8c45fSAndroid Build Coastguard Worker     status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer) override {
51*38e8c45fSAndroid Build Coastguard Worker         return mProducer->attachBuffer(outSlot, buffer);
52*38e8c45fSAndroid Build Coastguard Worker     }
queueBuffer(int slot,const QueueBufferInput & input,QueueBufferOutput * output)53*38e8c45fSAndroid Build Coastguard Worker     status_t queueBuffer(int slot, const QueueBufferInput& input,
54*38e8c45fSAndroid Build Coastguard Worker                          QueueBufferOutput* output) override {
55*38e8c45fSAndroid Build Coastguard Worker         return mProducer->queueBuffer(slot, input, output);
56*38e8c45fSAndroid Build Coastguard Worker     }
cancelBuffer(int slot,const sp<Fence> & fence)57*38e8c45fSAndroid Build Coastguard Worker     status_t cancelBuffer(int slot, const sp<Fence>& fence) override {
58*38e8c45fSAndroid Build Coastguard Worker         return mProducer->cancelBuffer(slot, fence);
59*38e8c45fSAndroid Build Coastguard Worker     }
query(int what,int * value)60*38e8c45fSAndroid Build Coastguard Worker     int query(int what, int* value) override { return mProducer->query(what, value); }
connect(const sp<IProducerListener> & listener,int api,bool producerControlledByApp,QueueBufferOutput * output)61*38e8c45fSAndroid Build Coastguard Worker     status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
62*38e8c45fSAndroid Build Coastguard Worker                      QueueBufferOutput* output) override {
63*38e8c45fSAndroid Build Coastguard Worker         return mProducer->connect(listener, api, producerControlledByApp, output);
64*38e8c45fSAndroid Build Coastguard Worker     }
disconnect(int api,DisconnectMode mode)65*38e8c45fSAndroid Build Coastguard Worker     status_t disconnect(int api, DisconnectMode mode) override {
66*38e8c45fSAndroid Build Coastguard Worker         return mProducer->disconnect(api, mode);
67*38e8c45fSAndroid Build Coastguard Worker     }
setSidebandStream(const sp<NativeHandle> & stream)68*38e8c45fSAndroid Build Coastguard Worker     status_t setSidebandStream(const sp<NativeHandle>& stream) override {
69*38e8c45fSAndroid Build Coastguard Worker         return mProducer->setSidebandStream(stream);
70*38e8c45fSAndroid Build Coastguard Worker     }
allocateBuffers(uint32_t width,uint32_t height,PixelFormat format,uint64_t usage)71*38e8c45fSAndroid Build Coastguard Worker     void allocateBuffers(uint32_t width, uint32_t height, PixelFormat format,
72*38e8c45fSAndroid Build Coastguard Worker                          uint64_t usage) override {
73*38e8c45fSAndroid Build Coastguard Worker         mProducer->allocateBuffers(width, height, format, usage);
74*38e8c45fSAndroid Build Coastguard Worker     }
allowAllocation(bool allow)75*38e8c45fSAndroid Build Coastguard Worker     status_t allowAllocation(bool allow) override { return mProducer->allowAllocation(allow); }
setGenerationNumber(uint32_t generationNumber)76*38e8c45fSAndroid Build Coastguard Worker     status_t setGenerationNumber(uint32_t generationNumber) override {
77*38e8c45fSAndroid Build Coastguard Worker         return mProducer->setGenerationNumber(generationNumber);
78*38e8c45fSAndroid Build Coastguard Worker     }
getConsumerName() const79*38e8c45fSAndroid Build Coastguard Worker     String8 getConsumerName() const override { return mProducer->getConsumerName(); }
setSharedBufferMode(bool sharedBufferMode)80*38e8c45fSAndroid Build Coastguard Worker     status_t setSharedBufferMode(bool sharedBufferMode) override {
81*38e8c45fSAndroid Build Coastguard Worker         return mProducer->setSharedBufferMode(sharedBufferMode);
82*38e8c45fSAndroid Build Coastguard Worker     }
setAutoRefresh(bool autoRefresh)83*38e8c45fSAndroid Build Coastguard Worker     status_t setAutoRefresh(bool autoRefresh) override {
84*38e8c45fSAndroid Build Coastguard Worker         return mProducer->setAutoRefresh(autoRefresh);
85*38e8c45fSAndroid Build Coastguard Worker     }
setDequeueTimeout(nsecs_t timeout)86*38e8c45fSAndroid Build Coastguard Worker     status_t setDequeueTimeout(nsecs_t timeout) override {
87*38e8c45fSAndroid Build Coastguard Worker         return mProducer->setDequeueTimeout(timeout);
88*38e8c45fSAndroid Build Coastguard Worker     }
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,float outTransformMatrix[16])89*38e8c45fSAndroid Build Coastguard Worker     status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
90*38e8c45fSAndroid Build Coastguard Worker                                  float outTransformMatrix[16]) override {
91*38e8c45fSAndroid Build Coastguard Worker         return mProducer->getLastQueuedBuffer(outBuffer, outFence, outTransformMatrix);
92*38e8c45fSAndroid Build Coastguard Worker     }
getFrameTimestamps(FrameEventHistoryDelta *)93*38e8c45fSAndroid Build Coastguard Worker     void getFrameTimestamps(FrameEventHistoryDelta*) override {}
getUniqueId(uint64_t * outId) const94*38e8c45fSAndroid Build Coastguard Worker     status_t getUniqueId(uint64_t* outId) const override { return mProducer->getUniqueId(outId); }
getConsumerUsage(uint64_t * outUsage) const95*38e8c45fSAndroid Build Coastguard Worker     status_t getConsumerUsage(uint64_t* outUsage) const override {
96*38e8c45fSAndroid Build Coastguard Worker         return mProducer->getConsumerUsage(outUsage);
97*38e8c45fSAndroid Build Coastguard Worker     }
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker protected:
100*38e8c45fSAndroid Build Coastguard Worker     sp<IGraphicBufferProducer> mProducer;
101*38e8c45fSAndroid Build Coastguard Worker };
102*38e8c45fSAndroid Build Coastguard Worker 
103*38e8c45fSAndroid Build Coastguard Worker class MaliciousBQP : public ProxyBQP {
104*38e8c45fSAndroid Build Coastguard Worker public:
MaliciousBQP(const sp<IGraphicBufferProducer> & producer)105*38e8c45fSAndroid Build Coastguard Worker     explicit MaliciousBQP(const sp<IGraphicBufferProducer>& producer) : ProxyBQP(producer) {}
106*38e8c45fSAndroid Build Coastguard Worker 
beMalicious(int32_t value)107*38e8c45fSAndroid Build Coastguard Worker     void beMalicious(int32_t value) { mMaliciousValue = value; }
108*38e8c45fSAndroid Build Coastguard Worker 
setExpectedSlot(int32_t slot)109*38e8c45fSAndroid Build Coastguard Worker     void setExpectedSlot(int32_t slot) { mExpectedSlot = slot; }
110*38e8c45fSAndroid Build Coastguard Worker 
111*38e8c45fSAndroid Build Coastguard Worker     // Override dequeueBuffer, optionally corrupting the returned slot number
dequeueBuffer(int * buf,sp<Fence> * fence,uint32_t width,uint32_t height,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)112*38e8c45fSAndroid Build Coastguard Worker     status_t dequeueBuffer(int* buf, sp<Fence>* fence, uint32_t width, uint32_t height,
113*38e8c45fSAndroid Build Coastguard Worker                            PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
114*38e8c45fSAndroid Build Coastguard Worker                            FrameEventHistoryDelta* outTimestamps) override {
115*38e8c45fSAndroid Build Coastguard Worker         EXPECT_EQ(BUFFER_NEEDS_REALLOCATION,
116*38e8c45fSAndroid Build Coastguard Worker                   mProducer->dequeueBuffer(buf, fence, width, height, format, usage, outBufferAge,
117*38e8c45fSAndroid Build Coastguard Worker                                            outTimestamps));
118*38e8c45fSAndroid Build Coastguard Worker         EXPECT_EQ(mExpectedSlot, *buf);
119*38e8c45fSAndroid Build Coastguard Worker         if (mMaliciousValue != 0) {
120*38e8c45fSAndroid Build Coastguard Worker             *buf = mMaliciousValue;
121*38e8c45fSAndroid Build Coastguard Worker             return NO_ERROR;
122*38e8c45fSAndroid Build Coastguard Worker         } else {
123*38e8c45fSAndroid Build Coastguard Worker             return BUFFER_NEEDS_REALLOCATION;
124*38e8c45fSAndroid Build Coastguard Worker         }
125*38e8c45fSAndroid Build Coastguard Worker     }
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker private:
128*38e8c45fSAndroid Build Coastguard Worker     int32_t mMaliciousValue = 0;
129*38e8c45fSAndroid Build Coastguard Worker     int32_t mExpectedSlot = 0;
130*38e8c45fSAndroid Build Coastguard Worker };
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker class FakeListener : public BnConsumerListener {
133*38e8c45fSAndroid Build Coastguard Worker public:
onFrameAvailable(const BufferItem &)134*38e8c45fSAndroid Build Coastguard Worker     void onFrameAvailable(const BufferItem&) override {}
onBuffersReleased()135*38e8c45fSAndroid Build Coastguard Worker     void onBuffersReleased() override {}
onSidebandStreamChanged()136*38e8c45fSAndroid Build Coastguard Worker     void onSidebandStreamChanged() override {}
137*38e8c45fSAndroid Build Coastguard Worker };
138*38e8c45fSAndroid Build Coastguard Worker 
getMaliciousBQP()139*38e8c45fSAndroid Build Coastguard Worker sp<MaliciousBQP> getMaliciousBQP() {
140*38e8c45fSAndroid Build Coastguard Worker     sp<IGraphicBufferProducer> producer;
141*38e8c45fSAndroid Build Coastguard Worker     sp<IGraphicBufferConsumer> consumer;
142*38e8c45fSAndroid Build Coastguard Worker     BufferQueue::createBufferQueue(&producer, &consumer);
143*38e8c45fSAndroid Build Coastguard Worker     sp<IConsumerListener> listener = new FakeListener;
144*38e8c45fSAndroid Build Coastguard Worker     consumer->consumerConnect(listener, false);
145*38e8c45fSAndroid Build Coastguard Worker 
146*38e8c45fSAndroid Build Coastguard Worker     sp<MaliciousBQP> malicious = new MaliciousBQP(producer);
147*38e8c45fSAndroid Build Coastguard Worker     return malicious;
148*38e8c45fSAndroid Build Coastguard Worker }
149*38e8c45fSAndroid Build Coastguard Worker 
TEST(Malicious,Bug36991414Max)150*38e8c45fSAndroid Build Coastguard Worker TEST(Malicious, Bug36991414Max) {
151*38e8c45fSAndroid Build Coastguard Worker     sp<MaliciousBQP> malicious = getMaliciousBQP();
152*38e8c45fSAndroid Build Coastguard Worker     sp<Surface> surface = new Surface(malicious);
153*38e8c45fSAndroid Build Coastguard Worker 
154*38e8c45fSAndroid Build Coastguard Worker     ANativeWindow_Buffer buffer;
155*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr));
156*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->unlockAndPost());
157*38e8c45fSAndroid Build Coastguard Worker 
158*38e8c45fSAndroid Build Coastguard Worker     malicious->setExpectedSlot(1);
159*38e8c45fSAndroid Build Coastguard Worker     malicious->beMalicious(std::numeric_limits<int32_t>::max());
160*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr));
161*38e8c45fSAndroid Build Coastguard Worker }
162*38e8c45fSAndroid Build Coastguard Worker 
TEST(Malicious,Bug36991414Min)163*38e8c45fSAndroid Build Coastguard Worker TEST(Malicious, Bug36991414Min) {
164*38e8c45fSAndroid Build Coastguard Worker     sp<MaliciousBQP> malicious = getMaliciousBQP();
165*38e8c45fSAndroid Build Coastguard Worker     sp<Surface> surface = new Surface(malicious);
166*38e8c45fSAndroid Build Coastguard Worker 
167*38e8c45fSAndroid Build Coastguard Worker     ANativeWindow_Buffer buffer;
168*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr));
169*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->unlockAndPost());
170*38e8c45fSAndroid Build Coastguard Worker 
171*38e8c45fSAndroid Build Coastguard Worker     malicious->setExpectedSlot(1);
172*38e8c45fSAndroid Build Coastguard Worker     malicious->beMalicious(std::numeric_limits<int32_t>::min());
173*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr));
174*38e8c45fSAndroid Build Coastguard Worker }
175*38e8c45fSAndroid Build Coastguard Worker 
TEST(Malicious,Bug36991414NegativeOne)176*38e8c45fSAndroid Build Coastguard Worker TEST(Malicious, Bug36991414NegativeOne) {
177*38e8c45fSAndroid Build Coastguard Worker     sp<MaliciousBQP> malicious = getMaliciousBQP();
178*38e8c45fSAndroid Build Coastguard Worker     sp<Surface> surface = new Surface(malicious);
179*38e8c45fSAndroid Build Coastguard Worker 
180*38e8c45fSAndroid Build Coastguard Worker     ANativeWindow_Buffer buffer;
181*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr));
182*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->unlockAndPost());
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     malicious->setExpectedSlot(1);
185*38e8c45fSAndroid Build Coastguard Worker     malicious->beMalicious(-1);
186*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr));
187*38e8c45fSAndroid Build Coastguard Worker }
188*38e8c45fSAndroid Build Coastguard Worker 
TEST(Malicious,Bug36991414NumSlots)189*38e8c45fSAndroid Build Coastguard Worker TEST(Malicious, Bug36991414NumSlots) {
190*38e8c45fSAndroid Build Coastguard Worker     sp<MaliciousBQP> malicious = getMaliciousBQP();
191*38e8c45fSAndroid Build Coastguard Worker     sp<Surface> surface = new Surface(malicious);
192*38e8c45fSAndroid Build Coastguard Worker 
193*38e8c45fSAndroid Build Coastguard Worker     ANativeWindow_Buffer buffer;
194*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->lock(&buffer, nullptr));
195*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(NO_ERROR, surface->unlockAndPost());
196*38e8c45fSAndroid Build Coastguard Worker 
197*38e8c45fSAndroid Build Coastguard Worker     malicious->setExpectedSlot(1);
198*38e8c45fSAndroid Build Coastguard Worker     malicious->beMalicious(BufferQueueDefs::NUM_BUFFER_SLOTS);
199*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(FAILED_TRANSACTION, surface->lock(&buffer, nullptr));
200*38e8c45fSAndroid Build Coastguard Worker }
201*38e8c45fSAndroid Build Coastguard Worker 
202*38e8c45fSAndroid Build Coastguard Worker } // namespace test
203*38e8c45fSAndroid Build Coastguard Worker } // namespace android
204