Credentials_test.cpp 13 KB

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