blob: 7f541e2467ddc79ee3b4736c5b762ab01b33c3b5 [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>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080028#include <ui/DisplayConfig.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
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080084 DisplayConfig config;
85 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplay, &config));
Ana Krulec13be8ad2018-08-21 02:43:56 +000086
87 // Background surface
88 mBGSurfaceControl =
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080089 mComposerClient->createSurface(SURFACE_NAME, config.resolution.getWidth(),
90 config.resolution.getHeight(),
Ana Krulec13be8ad2018-08-21 02:43:56 +000091 PIXEL_FORMAT_RGBA_8888, 0);
92 ASSERT_TRUE(mBGSurfaceControl != nullptr);
93 ASSERT_TRUE(mBGSurfaceControl->isValid());
94
95 Transaction t;
96 t.setDisplayLayerStack(mDisplay, 0);
97 ASSERT_EQ(NO_ERROR,
98 t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply());
99 }
100
Ana Krulec13be8ad2018-08-21 02:43:56 +0000101 /**
102 * Sets UID to imitate Graphic's process.
103 */
104 void setGraphicsUID() {
105 seteuid(AID_ROOT);
106 seteuid(AID_GRAPHICS);
107 }
108
109 /**
110 * Sets UID to imitate System's process.
111 */
112 void setSystemUID() {
113 seteuid(AID_ROOT);
114 seteuid(AID_SYSTEM);
115 }
116
117 /**
118 * Sets UID to imitate a process that doesn't have any special privileges in
119 * our code.
120 */
121 void setBinUID() {
122 seteuid(AID_ROOT);
123 seteuid(AID_BIN);
124 }
125
126 /**
127 * Template function the check a condition for different types of users: root
128 * graphics, system, and non-supported user. Root, graphics, and system should
129 * always equal privilegedValue, and non-supported user should equal unprivilegedValue.
130 */
131 template <typename T>
132 void checkWithPrivileges(std::function<T()> condition, T privilegedValue, T unprivilegedValue) {
133 // Check with root.
134 seteuid(AID_ROOT);
135 ASSERT_EQ(privilegedValue, condition());
136
137 // Check as a Graphics user.
138 setGraphicsUID();
139 ASSERT_EQ(privilegedValue, condition());
140
141 // Check as a system user.
142 setSystemUID();
143 ASSERT_EQ(privilegedValue, condition());
144
145 // Check as a non-supported user.
146 setBinUID();
147 ASSERT_EQ(unprivilegedValue, condition());
chaviwd4a61642020-09-01 14:53:46 -0700148
149 // Check as shell since shell has some additional permissions
150 seteuid(AID_SHELL);
151 ASSERT_EQ(unprivilegedValue, condition());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000152 }
153};
154
155TEST_F(CredentialsTest, ClientInitTest) {
156 // Root can init can init the client.
157 ASSERT_NO_FATAL_FAILURE(initClient());
158
159 // Graphics can init the client.
160 setGraphicsUID();
161 ASSERT_NO_FATAL_FAILURE(initClient());
162
163 // System can init the client.
164 setSystemUID();
165 ASSERT_NO_FATAL_FAILURE(initClient());
166
Robert Carrb89ea9d2018-12-10 13:01:14 -0800167 // Anyone else can init the client.
Ana Krulec13be8ad2018-08-21 02:43:56 +0000168 setBinUID();
169 mComposerClient = new SurfaceComposerClient;
Robert Carrb89ea9d2018-12-10 13:01:14 -0800170 ASSERT_NO_FATAL_FAILURE(initClient());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000171}
172
173TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800174 std::function<bool()> condition = [] {
175 return SurfaceComposerClient::getInternalDisplayToken() != nullptr;
Ana Krulec13be8ad2018-08-21 02:43:56 +0000176 };
177 // Anyone can access display information.
178 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
179}
180
181TEST_F(CredentialsTest, AllowedGetterMethodsTest) {
182 // The following methods are tested with a UID that is not root, graphics,
183 // or system, to show that anyone can access them.
184 setBinUID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800185 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000186 ASSERT_TRUE(display != nullptr);
187
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800188 DisplayConfig config;
189 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
Ana Krulec13be8ad2018-08-21 02:43:56 +0000190
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800191 Vector<DisplayConfig> configs;
Ana Krulec13be8ad2018-08-21 02:43:56 +0000192 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs));
193
arthurhung79e81aa2020-08-28 00:09:19 +0800194 ASSERT_TRUE(SurfaceComposerClient::getActiveConfig(display) >= 0);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000195
196 ASSERT_NE(static_cast<ui::ColorMode>(BAD_VALUE),
197 SurfaceComposerClient::getActiveColorMode(display));
198}
199
200TEST_F(CredentialsTest, GetDisplayColorModesTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800201 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000202 std::function<status_t()> condition = [=]() {
203 Vector<ui::ColorMode> outColorModes;
204 return SurfaceComposerClient::getDisplayColorModes(display, &outColorModes);
205 };
206 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
207}
208
Daniel Solomon42d04562019-01-20 21:03:19 -0800209TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800210 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Daniel Solomon42d04562019-01-20 21:03:19 -0800211 std::function<status_t()> condition = [=]() {
212 ui::DisplayPrimaries primaries;
213 return SurfaceComposerClient::getDisplayNativePrimaries(display, primaries);
214 };
215 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
216}
217
Steven Thomasa87ed452020-01-03 16:10:05 -0800218TEST_F(CredentialsTest, SetDesiredDisplayConfigsTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800219 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Steven Thomasa87ed452020-01-03 16:10:05 -0800220 int32_t defaultConfig;
Steven Thomasf734df42020-04-13 21:09:28 -0700221 float primaryFpsMin;
222 float primaryFpsMax;
223 float appRequestFpsMin;
224 float appRequestFpsMax;
225 status_t res =
226 SurfaceComposerClient::getDesiredDisplayConfigSpecs(display, &defaultConfig,
227 &primaryFpsMin, &primaryFpsMax,
228 &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 = [=]() {
Steven Thomasf734df42020-04-13 21:09:28 -0700232 return SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, defaultConfig,
233 primaryFpsMin, primaryFpsMax,
234 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"