TestProcFSWrite.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <fcntl.h>
  8. #include <sys/prctl.h>
  9. #include <unistd.h>
  10. TEST_CASE(check_root)
  11. {
  12. auto uid = geteuid();
  13. // This test only makes sense as root.
  14. EXPECT_EQ(uid, 0u);
  15. // Before we make the process dumpable, become "fully" root, so that the user cannot tamper with our memory:
  16. EXPECT_EQ(setuid(0), 0);
  17. // If running as setuid, the process is automatically marked as non-dumpable, which bars access to /proc/self/.
  18. // However, that is the easiest guess for a /proc/$PID/ directory, so we'd like to use that.
  19. // In order to do so, mark this process as dumpable:
  20. EXPECT_EQ(prctl(PR_SET_DUMPABLE, 1, 0), 0);
  21. }
  22. TEST_CASE(root_writes_to_procfs)
  23. {
  24. int fd = open("/proc/self/unveil", O_RDWR | O_APPEND | O_CREAT, 0666); // = 6
  25. if (fd < 0) {
  26. perror("open");
  27. dbgln("fd was {}", fd);
  28. FAIL("open failed?! See debugout");
  29. return;
  30. }
  31. int rc = write(fd, "hello", 5);
  32. perror("write");
  33. dbgln("write rc = {}", rc);
  34. if (rc >= 0) {
  35. FAIL("Wrote successfully?!");
  36. }
  37. }