Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 1 | #include <algorithm> |
| 2 | #include <functional> |
| 3 | #include <limits> |
| 4 | #include <ostream> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | #include <gui/ISurfaceComposer.h> |
| 9 | #include <gui/LayerDebugInfo.h> |
| 10 | #include <gui/Surface.h> |
| 11 | #include <gui/SurfaceComposerClient.h> |
| 12 | |
| 13 | #include <private/android_filesystem_config.h> |
| 14 | #include <private/gui/ComposerService.h> |
| 15 | |
| 16 | #include <ui/DisplayInfo.h> |
| 17 | #include <utils/String8.h> |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | using Transaction = SurfaceComposerClient::Transaction; |
| 22 | |
| 23 | namespace { |
| 24 | const String8 DISPLAY_NAME("Credentials Display Test"); |
| 25 | const String8 SURFACE_NAME("Test Surface Name"); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 26 | const uint32_t ROTATION = 0; |
| 27 | const float FRAME_SCALE = 1.0f; |
| 28 | } // namespace |
| 29 | |
| 30 | /** |
| 31 | * This class tests the CheckCredentials method in SurfaceFlinger. |
| 32 | * Methods like EnableVsyncInjections and InjectVsync are not tested since they do not |
| 33 | * return anything meaningful. |
| 34 | */ |
| 35 | class CredentialsTest : public ::testing::Test { |
| 36 | protected: |
| 37 | void SetUp() override { |
| 38 | // Start the tests as root. |
| 39 | seteuid(AID_ROOT); |
| 40 | |
| 41 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 42 | } |
| 43 | |
| 44 | void TearDown() override { |
| 45 | mComposerClient->dispose(); |
| 46 | mBGSurfaceControl.clear(); |
| 47 | mComposerClient.clear(); |
| 48 | // Finish the tests as root. |
| 49 | seteuid(AID_ROOT); |
| 50 | } |
| 51 | |
| 52 | sp<IBinder> mDisplay; |
| 53 | sp<IBinder> mVirtualDisplay; |
| 54 | sp<SurfaceComposerClient> mComposerClient; |
| 55 | sp<SurfaceControl> mBGSurfaceControl; |
| 56 | sp<SurfaceControl> mVirtualSurfaceControl; |
| 57 | |
| 58 | void initClient() { |
| 59 | mComposerClient = new SurfaceComposerClient; |
| 60 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| 61 | } |
| 62 | |
| 63 | void setupBackgroundSurface() { |
| 64 | mDisplay = SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain); |
| 65 | DisplayInfo info; |
| 66 | SurfaceComposerClient::getDisplayInfo(mDisplay, &info); |
| 67 | const ssize_t displayWidth = info.w; |
| 68 | const ssize_t displayHeight = info.h; |
| 69 | |
| 70 | // Background surface |
| 71 | mBGSurfaceControl = |
| 72 | mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight, |
| 73 | PIXEL_FORMAT_RGBA_8888, 0); |
| 74 | ASSERT_TRUE(mBGSurfaceControl != nullptr); |
| 75 | ASSERT_TRUE(mBGSurfaceControl->isValid()); |
| 76 | |
| 77 | Transaction t; |
| 78 | t.setDisplayLayerStack(mDisplay, 0); |
| 79 | ASSERT_EQ(NO_ERROR, |
| 80 | t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply()); |
| 81 | } |
| 82 | |
| 83 | void setupVirtualDisplay() { |
| 84 | mVirtualDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true); |
| 85 | const ssize_t displayWidth = 100; |
| 86 | const ssize_t displayHeight = 100; |
| 87 | |
| 88 | // Background surface |
| 89 | mVirtualSurfaceControl = |
| 90 | mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight, |
| 91 | PIXEL_FORMAT_RGBA_8888, 0); |
| 92 | ASSERT_TRUE(mVirtualSurfaceControl != nullptr); |
| 93 | ASSERT_TRUE(mVirtualSurfaceControl->isValid()); |
| 94 | |
| 95 | Transaction t; |
| 96 | t.setDisplayLayerStack(mVirtualDisplay, 0); |
| 97 | ASSERT_EQ(NO_ERROR, |
| 98 | t.setLayer(mVirtualSurfaceControl, INT_MAX - 3) |
| 99 | .show(mVirtualSurfaceControl) |
| 100 | .apply()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets UID to imitate Graphic's process. |
| 105 | */ |
| 106 | void setGraphicsUID() { |
| 107 | seteuid(AID_ROOT); |
| 108 | seteuid(AID_GRAPHICS); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets UID to imitate System's process. |
| 113 | */ |
| 114 | void setSystemUID() { |
| 115 | seteuid(AID_ROOT); |
| 116 | seteuid(AID_SYSTEM); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets UID to imitate a process that doesn't have any special privileges in |
| 121 | * our code. |
| 122 | */ |
| 123 | void setBinUID() { |
| 124 | seteuid(AID_ROOT); |
| 125 | seteuid(AID_BIN); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Template function the check a condition for different types of users: root |
| 130 | * graphics, system, and non-supported user. Root, graphics, and system should |
| 131 | * always equal privilegedValue, and non-supported user should equal unprivilegedValue. |
| 132 | */ |
| 133 | template <typename T> |
| 134 | void checkWithPrivileges(std::function<T()> condition, T privilegedValue, T unprivilegedValue) { |
| 135 | // Check with root. |
| 136 | seteuid(AID_ROOT); |
| 137 | ASSERT_EQ(privilegedValue, condition()); |
| 138 | |
| 139 | // Check as a Graphics user. |
| 140 | setGraphicsUID(); |
| 141 | ASSERT_EQ(privilegedValue, condition()); |
| 142 | |
| 143 | // Check as a system user. |
| 144 | setSystemUID(); |
| 145 | ASSERT_EQ(privilegedValue, condition()); |
| 146 | |
| 147 | // Check as a non-supported user. |
| 148 | setBinUID(); |
| 149 | ASSERT_EQ(unprivilegedValue, condition()); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | TEST_F(CredentialsTest, ClientInitTest) { |
| 154 | // Root can init can init the client. |
| 155 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 156 | |
| 157 | // Graphics can init the client. |
| 158 | setGraphicsUID(); |
| 159 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 160 | |
| 161 | // System can init the client. |
| 162 | setSystemUID(); |
| 163 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 164 | |
| 165 | // No one else can init the client. |
| 166 | setBinUID(); |
| 167 | mComposerClient = new SurfaceComposerClient; |
| 168 | ASSERT_EQ(NO_INIT, mComposerClient->initCheck()); |
| 169 | } |
| 170 | |
| 171 | TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) { |
| 172 | std::function<bool()> condition = [=]() { |
| 173 | sp<IBinder> display( |
| 174 | SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 175 | return (display != nullptr); |
| 176 | }; |
| 177 | // Anyone can access display information. |
| 178 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true)); |
| 179 | } |
| 180 | |
| 181 | TEST_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(); |
| 185 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 186 | ASSERT_TRUE(display != nullptr); |
| 187 | |
| 188 | DisplayInfo info; |
| 189 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info)); |
| 190 | |
| 191 | Vector<DisplayInfo> configs; |
| 192 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs)); |
| 193 | |
| 194 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveConfig(display)); |
| 195 | |
| 196 | ASSERT_NE(static_cast<ui::ColorMode>(BAD_VALUE), |
| 197 | SurfaceComposerClient::getActiveColorMode(display)); |
| 198 | } |
| 199 | |
| 200 | TEST_F(CredentialsTest, GetDisplayColorModesTest) { |
| 201 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 202 | 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 | |
| 209 | TEST_F(CredentialsTest, SetActiveConfigTest) { |
| 210 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 211 | std::function<status_t()> condition = [=]() { |
| 212 | return SurfaceComposerClient::setActiveConfig(display, 0); |
| 213 | }; |
| 214 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 215 | } |
| 216 | |
| 217 | TEST_F(CredentialsTest, SetActiveColorModeTest) { |
| 218 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 219 | std::function<status_t()> condition = [=]() { |
| 220 | return SurfaceComposerClient::setActiveColorMode(display, ui::ColorMode::NATIVE); |
| 221 | }; |
| 222 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 223 | } |
| 224 | |
| 225 | TEST_F(CredentialsTest, CreateSurfaceTest) { |
| 226 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 227 | DisplayInfo info; |
| 228 | SurfaceComposerClient::getDisplayInfo(display, &info); |
| 229 | const ssize_t displayWidth = info.w; |
| 230 | const ssize_t displayHeight = info.h; |
| 231 | |
| 232 | std::function<bool()> condition = [=]() { |
| 233 | mBGSurfaceControl = |
| 234 | mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight, |
| 235 | PIXEL_FORMAT_RGBA_8888, 0); |
| 236 | return mBGSurfaceControl != nullptr && mBGSurfaceControl->isValid(); |
| 237 | }; |
| 238 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
| 239 | } |
| 240 | |
| 241 | TEST_F(CredentialsTest, CreateDisplayTest) { |
| 242 | std::function<bool()> condition = [=]() { |
| 243 | sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true); |
| 244 | return testDisplay.get() != nullptr; |
| 245 | }; |
| 246 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
| 247 | |
| 248 | condition = [=]() { |
| 249 | sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false); |
| 250 | return testDisplay.get() != nullptr; |
| 251 | }; |
| 252 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
| 253 | } |
| 254 | |
| 255 | TEST_F(CredentialsTest, DISABLED_DestroyDisplayTest) { |
| 256 | setupVirtualDisplay(); |
| 257 | |
| 258 | DisplayInfo info; |
| 259 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info)); |
| 260 | SurfaceComposerClient::destroyDisplay(mVirtualDisplay); |
| 261 | // This test currently fails. TODO(b/112002626): Find a way to properly create |
| 262 | // a display in the test environment, so that destroy display can remove it. |
| 263 | ASSERT_EQ(NAME_NOT_FOUND, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info)); |
| 264 | } |
| 265 | |
| 266 | TEST_F(CredentialsTest, CaptureTest) { |
| 267 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 268 | std::function<status_t()> condition = [=]() { |
| 269 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 0e003c9 | 2018-09-17 11:09:51 -0700 | [diff] [blame] | 270 | return ScreenshotClient::capture(display, ui::Dataspace::V0_SRGB, |
| 271 | ui::PixelFormat::RGBA_8888, Rect(), 0 /*reqWidth*/, |
| 272 | 0 /*reqHeight*/, false, ROTATION, &outBuffer); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 273 | }; |
| 274 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 275 | } |
| 276 | |
| 277 | TEST_F(CredentialsTest, CaptureLayersTest) { |
| 278 | setupBackgroundSurface(); |
| 279 | sp<GraphicBuffer> outBuffer; |
| 280 | std::function<status_t()> condition = [=]() { |
| 281 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 0e003c9 | 2018-09-17 11:09:51 -0700 | [diff] [blame] | 282 | return ScreenshotClient::captureLayers(mBGSurfaceControl->getHandle(), |
| 283 | ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, |
| 284 | Rect(), FRAME_SCALE, &outBuffer); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 285 | }; |
| 286 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * The following tests are for methods accessible directly through SurfaceFlinger. |
| 291 | */ |
| 292 | |
| 293 | /** |
| 294 | * An app can pass a buffer queue to the media server and ask the media server to decode a DRM video |
| 295 | * to that buffer queue. The media server is the buffer producer in this case. Because the app may create |
| 296 | * its own buffer queue and act as the buffer consumer, the media server wants to be careful to avoid |
| 297 | * sending decoded video frames to the app. This is where authenticateSurfaceTexture call comes in, to check |
| 298 | * the consumer of a buffer queue is SurfaceFlinger. |
| 299 | */ |
| 300 | TEST_F(CredentialsTest, AuthenticateSurfaceTextureTest) { |
| 301 | setupBackgroundSurface(); |
| 302 | sp<IGraphicBufferProducer> producer = |
| 303 | mBGSurfaceControl->getSurface()->getIGraphicBufferProducer(); |
| 304 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 305 | |
| 306 | std::function<bool()> condition = [=]() { return sf->authenticateSurfaceTexture(producer); }; |
| 307 | // Anyone should be able to check if the consumer of the buffer queue is SF. |
| 308 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true)); |
| 309 | } |
| 310 | |
| 311 | TEST_F(CredentialsTest, GetLayerDebugInfo) { |
| 312 | setupBackgroundSurface(); |
| 313 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 314 | |
| 315 | // Historically, only root and shell can access the getLayerDebugInfo which |
| 316 | // is called when we call dumpsys. I don't see a reason why we should change this. |
| 317 | std::vector<LayerDebugInfo> outLayers; |
| 318 | // Check with root. |
| 319 | seteuid(AID_ROOT); |
| 320 | ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers)); |
| 321 | |
| 322 | // Check as a shell. |
| 323 | seteuid(AID_SHELL); |
| 324 | ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers)); |
| 325 | |
| 326 | // Check as anyone else. |
| 327 | seteuid(AID_ROOT); |
| 328 | seteuid(AID_BIN); |
| 329 | ASSERT_EQ(PERMISSION_DENIED, sf->getLayerDebugInfo(&outLayers)); |
| 330 | } |
| 331 | } // namespace android |