blob: 53e37d874e84bcc6b37bb3f19b119aa66fc83e04 [file] [log] [blame]
chaviw8ffc7b82020-08-18 11:25:37 -07001/*
2 * Copyright (C) 2020 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// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Ana Krulec13be8ad2018-08-21 02:43:56 +000021#include <gtest/gtest.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000022#include <gui/ISurfaceComposer.h>
23#include <gui/LayerDebugInfo.h>
24#include <gui/Surface.h>
25#include <gui/SurfaceComposerClient.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000026#include <private/android_filesystem_config.h>
27#include <private/gui/ComposerService.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010028#include <ui/DisplayMode.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000029#include <utils/String8.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080030#include <functional>
chaviw8ffc7b82020-08-18 11:25:37 -070031#include "utils/ScreenshotUtils.h"
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080032
Ana Krulec13be8ad2018-08-21 02:43:56 +000033namespace android {
34
35using Transaction = SurfaceComposerClient::Transaction;
Peiyong Lin4f3fddf2019-01-24 17:21:24 -080036using ui::ColorMode;
Ana Krulec13be8ad2018-08-21 02:43:56 +000037
38namespace {
39const String8 DISPLAY_NAME("Credentials Display Test");
40const String8 SURFACE_NAME("Test Surface Name");
Ana Krulec13be8ad2018-08-21 02:43:56 +000041} // namespace
42
43/**
44 * This class tests the CheckCredentials method in SurfaceFlinger.
45 * Methods like EnableVsyncInjections and InjectVsync are not tested since they do not
46 * return anything meaningful.
47 */
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080048
49// TODO(b/129481165): remove the #pragma below and fix conversion issues
50#pragma clang diagnostic push
51#pragma clang diagnostic ignored "-Wconversion"
Ana Krulec13be8ad2018-08-21 02:43:56 +000052class CredentialsTest : public ::testing::Test {
53protected:
54 void SetUp() override {
55 // Start the tests as root.
56 seteuid(AID_ROOT);
57
58 ASSERT_NO_FATAL_FAILURE(initClient());
59 }
60
61 void TearDown() override {
62 mComposerClient->dispose();
63 mBGSurfaceControl.clear();
64 mComposerClient.clear();
65 // Finish the tests as root.
66 seteuid(AID_ROOT);
67 }
68
69 sp<IBinder> mDisplay;
70 sp<IBinder> mVirtualDisplay;
71 sp<SurfaceComposerClient> mComposerClient;
72 sp<SurfaceControl> mBGSurfaceControl;
73 sp<SurfaceControl> mVirtualSurfaceControl;
74
75 void initClient() {
76 mComposerClient = new SurfaceComposerClient;
77 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
78 }
79
80 void setupBackgroundSurface() {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080081 mDisplay = SurfaceComposerClient::getInternalDisplayToken();
82 ASSERT_FALSE(mDisplay == nullptr);
83
Marin Shalamanova7fe3042021-01-29 21:02:08 +010084 ui::DisplayMode mode;
85 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplay, &mode));
Ana Krulec13be8ad2018-08-21 02:43:56 +000086
87 // Background surface
Marin Shalamanova7fe3042021-01-29 21:02:08 +010088 mBGSurfaceControl = mComposerClient->createSurface(SURFACE_NAME, mode.resolution.getWidth(),
89 mode.resolution.getHeight(),
90 PIXEL_FORMAT_RGBA_8888, 0);
Ana Krulec13be8ad2018-08-21 02:43:56 +000091 ASSERT_TRUE(mBGSurfaceControl != nullptr);
92 ASSERT_TRUE(mBGSurfaceControl->isValid());
93
94 Transaction t;
95 t.setDisplayLayerStack(mDisplay, 0);
96 ASSERT_EQ(NO_ERROR,
97 t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply());
98 }
99
Ana Krulec13be8ad2018-08-21 02:43:56 +0000100 /**
101 * Sets UID to imitate Graphic's process.
102 */
103 void setGraphicsUID() {
104 seteuid(AID_ROOT);
105 seteuid(AID_GRAPHICS);
106 }
107
108 /**
109 * Sets UID to imitate System's process.
110 */
111 void setSystemUID() {
112 seteuid(AID_ROOT);
113 seteuid(AID_SYSTEM);
114 }
115
116 /**
117 * Sets UID to imitate a process that doesn't have any special privileges in
118 * our code.
119 */
120 void setBinUID() {
121 seteuid(AID_ROOT);
122 seteuid(AID_BIN);
123 }
124
125 /**
126 * Template function the check a condition for different types of users: root
127 * graphics, system, and non-supported user. Root, graphics, and system should
128 * always equal privilegedValue, and non-supported user should equal unprivilegedValue.
129 */
130 template <typename T>
131 void checkWithPrivileges(std::function<T()> condition, T privilegedValue, T unprivilegedValue) {
132 // Check with root.
133 seteuid(AID_ROOT);
134 ASSERT_EQ(privilegedValue, condition());
135
136 // Check as a Graphics user.
137 setGraphicsUID();
138 ASSERT_EQ(privilegedValue, condition());
139
140 // Check as a system user.
141 setSystemUID();
142 ASSERT_EQ(privilegedValue, condition());
143
144 // Check as a non-supported user.
145 setBinUID();
146 ASSERT_EQ(unprivilegedValue, condition());
chaviwd4a61642020-09-01 14:53:46 -0700147
148 // Check as shell since shell has some additional permissions
149 seteuid(AID_SHELL);
150 ASSERT_EQ(unprivilegedValue, condition());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000151 }
152};
153
154TEST_F(CredentialsTest, ClientInitTest) {
155 // Root can init can init the client.
156 ASSERT_NO_FATAL_FAILURE(initClient());
157
158 // Graphics can init the client.
159 setGraphicsUID();
160 ASSERT_NO_FATAL_FAILURE(initClient());
161
162 // System can init the client.
163 setSystemUID();
164 ASSERT_NO_FATAL_FAILURE(initClient());
165
Robert Carrb89ea9d2018-12-10 13:01:14 -0800166 // Anyone else can init the client.
Ana Krulec13be8ad2018-08-21 02:43:56 +0000167 setBinUID();
168 mComposerClient = new SurfaceComposerClient;
Robert Carrb89ea9d2018-12-10 13:01:14 -0800169 ASSERT_NO_FATAL_FAILURE(initClient());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000170}
171
172TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800173 std::function<bool()> condition = [] {
174 return SurfaceComposerClient::getInternalDisplayToken() != nullptr;
Ana Krulec13be8ad2018-08-21 02:43:56 +0000175 };
176 // Anyone can access display information.
177 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
178}
179
180TEST_F(CredentialsTest, AllowedGetterMethodsTest) {
181 // The following methods are tested with a UID that is not root, graphics,
182 // or system, to show that anyone can access them.
183 setBinUID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800184 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000185 ASSERT_TRUE(display != nullptr);
186
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100187 ui::DisplayMode mode;
188 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(display, &mode));
Ana Krulec13be8ad2018-08-21 02:43:56 +0000189
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100190 Vector<ui::DisplayMode> modes;
191 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayModes(display, &modes));
Ana Krulec13be8ad2018-08-21 02:43:56 +0000192
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100193 ASSERT_TRUE(SurfaceComposerClient::getActiveDisplayModeId(display) >= 0);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000194
195 ASSERT_NE(static_cast<ui::ColorMode>(BAD_VALUE),
196 SurfaceComposerClient::getActiveColorMode(display));
197}
198
199TEST_F(CredentialsTest, GetDisplayColorModesTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800200 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000201 std::function<status_t()> condition = [=]() {
202 Vector<ui::ColorMode> outColorModes;
203 return SurfaceComposerClient::getDisplayColorModes(display, &outColorModes);
204 };
205 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
206}
207
Daniel Solomon42d04562019-01-20 21:03:19 -0800208TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800209 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Daniel Solomon42d04562019-01-20 21:03:19 -0800210 std::function<status_t()> condition = [=]() {
211 ui::DisplayPrimaries primaries;
212 return SurfaceComposerClient::getDisplayNativePrimaries(display, primaries);
213 };
214 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
215}
216
Steven Thomasa87ed452020-01-03 16:10:05 -0800217TEST_F(CredentialsTest, SetDesiredDisplayConfigsTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800218 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100219 size_t defaultMode;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200220 bool allowGroupSwitching;
Steven Thomasf734df42020-04-13 21:09:28 -0700221 float primaryFpsMin;
222 float primaryFpsMax;
223 float appRequestFpsMin;
224 float appRequestFpsMax;
225 status_t res =
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100226 SurfaceComposerClient::getDesiredDisplayModeSpecs(display, &defaultMode,
227 &allowGroupSwitching, &primaryFpsMin,
228 &primaryFpsMax, &appRequestFpsMin,
229 &appRequestFpsMax);
Steven Thomasa87ed452020-01-03 16:10:05 -0800230 ASSERT_EQ(res, NO_ERROR);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000231 std::function<status_t()> condition = [=]() {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100232 return SurfaceComposerClient::setDesiredDisplayModeSpecs(display, defaultMode,
233 allowGroupSwitching, primaryFpsMin,
234 primaryFpsMax, appRequestFpsMin,
235 appRequestFpsMax);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000236 };
237 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
238}
239
240TEST_F(CredentialsTest, SetActiveColorModeTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800241 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000242 std::function<status_t()> condition = [=]() {
243 return SurfaceComposerClient::setActiveColorMode(display, ui::ColorMode::NATIVE);
244 };
245 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
246}
247
Ana Krulec13be8ad2018-08-21 02:43:56 +0000248TEST_F(CredentialsTest, CreateDisplayTest) {
chaviwd4a61642020-09-01 14:53:46 -0700249 // Only graphics and system processes can create a secure display.
Ana Krulec13be8ad2018-08-21 02:43:56 +0000250 std::function<bool()> condition = [=]() {
251 sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
252 return testDisplay.get() != nullptr;
253 };
chaviwd4a61642020-09-01 14:53:46 -0700254
255 // Check with root.
256 seteuid(AID_ROOT);
257 ASSERT_FALSE(condition());
258
259 // Check as a Graphics user.
260 setGraphicsUID();
261 ASSERT_TRUE(condition());
262
263 // Check as a system user.
264 setSystemUID();
265 ASSERT_TRUE(condition());
266
267 // Check as a non-supported user.
268 setBinUID();
269 ASSERT_FALSE(condition());
270
271 // Check as shell since shell has some additional permissions
272 seteuid(AID_SHELL);
273 ASSERT_FALSE(condition());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000274
275 condition = [=]() {
276 sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false);
277 return testDisplay.get() != nullptr;
278 };
279 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
280}
281
Ana Krulec13be8ad2018-08-21 02:43:56 +0000282TEST_F(CredentialsTest, CaptureTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800283 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000284 std::function<status_t()> condition = [=]() {
285 sp<GraphicBuffer> outBuffer;
chaviw690db382020-07-27 16:46:46 -0700286 DisplayCaptureArgs captureArgs;
287 captureArgs.displayToken = display;
288 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -0700289 return ScreenCapture::captureDisplay(captureArgs, captureResults);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000290 };
291 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
292}
293
294TEST_F(CredentialsTest, CaptureLayersTest) {
295 setupBackgroundSurface();
296 sp<GraphicBuffer> outBuffer;
297 std::function<status_t()> condition = [=]() {
chaviw26c52482020-07-28 16:25:52 -0700298 LayerCaptureArgs captureArgs;
299 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
300 captureArgs.sourceCrop = {0, 0, 1, 1};
301
302 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -0700303 return ScreenCapture::captureLayers(captureArgs, captureResults);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000304 };
305 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
306}
307
308/**
309 * The following tests are for methods accessible directly through SurfaceFlinger.
310 */
311
312/**
313 * An app can pass a buffer queue to the media server and ask the media server to decode a DRM video
314 * to that buffer queue. The media server is the buffer producer in this case. Because the app may create
315 * its own buffer queue and act as the buffer consumer, the media server wants to be careful to avoid
316 * sending decoded video frames to the app. This is where authenticateSurfaceTexture call comes in, to check
317 * the consumer of a buffer queue is SurfaceFlinger.
318 */
319TEST_F(CredentialsTest, AuthenticateSurfaceTextureTest) {
320 setupBackgroundSurface();
321 sp<IGraphicBufferProducer> producer =
322 mBGSurfaceControl->getSurface()->getIGraphicBufferProducer();
323 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
324
325 std::function<bool()> condition = [=]() { return sf->authenticateSurfaceTexture(producer); };
326 // Anyone should be able to check if the consumer of the buffer queue is SF.
327 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
328}
329
330TEST_F(CredentialsTest, GetLayerDebugInfo) {
331 setupBackgroundSurface();
332 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
333
334 // Historically, only root and shell can access the getLayerDebugInfo which
335 // is called when we call dumpsys. I don't see a reason why we should change this.
336 std::vector<LayerDebugInfo> outLayers;
337 // Check with root.
338 seteuid(AID_ROOT);
339 ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers));
340
341 // Check as a shell.
342 seteuid(AID_SHELL);
343 ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers));
344
345 // Check as anyone else.
346 seteuid(AID_ROOT);
347 seteuid(AID_BIN);
348 ASSERT_EQ(PERMISSION_DENIED, sf->getLayerDebugInfo(&outLayers));
349}
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800350
351TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800352 const auto display = SurfaceComposerClient::getInternalDisplayToken();
353 ASSERT_FALSE(display == nullptr);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800354 bool result = false;
355 status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result);
356 ASSERT_EQ(NO_ERROR, error);
357 bool hasWideColorMode = false;
358 Vector<ColorMode> colorModes;
359 SurfaceComposerClient::getDisplayColorModes(display, &colorModes);
360 for (ColorMode colorMode : colorModes) {
361 switch (colorMode) {
362 case ColorMode::DISPLAY_P3:
363 case ColorMode::ADOBE_RGB:
364 case ColorMode::DCI_P3:
365 hasWideColorMode = true;
366 break;
367 default:
368 break;
369 }
370 }
371 ASSERT_EQ(hasWideColorMode, result);
372}
373
374TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800375 const auto display = SurfaceComposerClient::getInternalDisplayToken();
376 ASSERT_FALSE(display == nullptr);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800377 std::function<status_t()> condition = [=]() {
378 bool result = false;
379 return SurfaceComposerClient::isWideColorDisplay(display, &result);
380 };
381 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
382}
383
Peiyong Lind1fedb42019-03-11 17:48:41 -0700384TEST_F(CredentialsTest, GetActiveColorModeBasicCorrectness) {
385 const auto display = SurfaceComposerClient::getInternalDisplayToken();
386 ASSERT_FALSE(display == nullptr);
387 ColorMode colorMode = SurfaceComposerClient::getActiveColorMode(display);
388 ASSERT_NE(static_cast<ColorMode>(BAD_VALUE), colorMode);
389}
390
Ana Krulec13be8ad2018-08-21 02:43:56 +0000391} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800392
393// TODO(b/129481165): remove the #pragma below and fix conversion issues
394#pragma clang diagnostic pop // ignored "-Wconversion"