TestKernelUnveil.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. TEST_CASE(test_argument_validation)
  10. {
  11. auto res = unveil("/etc", "aaaaaaaaaaaa");
  12. EXPECT_EQ(res, -1);
  13. EXPECT_EQ(errno, EINVAL);
  14. res = unveil(nullptr, "r");
  15. EXPECT_EQ(res, -1);
  16. EXPECT_EQ(errno, EINVAL);
  17. res = unveil("/etc", nullptr);
  18. EXPECT_EQ(res, -1);
  19. EXPECT_EQ(errno, EINVAL);
  20. res = unveil("", "r");
  21. EXPECT_EQ(res, -1);
  22. EXPECT_EQ(errno, EINVAL);
  23. res = unveil("test", "r");
  24. EXPECT_EQ(res, -1);
  25. EXPECT_EQ(errno, EINVAL);
  26. res = unveil("/etc", "f");
  27. EXPECT_EQ(res, -1);
  28. EXPECT_EQ(errno, EINVAL);
  29. }
  30. TEST_CASE(test_failures)
  31. {
  32. auto res = unveil("/etc", "r");
  33. if (res < 0)
  34. FAIL("unveil read only failed");
  35. res = unveil("/etc", "w");
  36. if (res >= 0)
  37. FAIL("unveil write permitted after unveil read only");
  38. res = unveil("/etc", "x");
  39. if (res >= 0)
  40. FAIL("unveil execute permitted after unveil read only");
  41. res = unveil("/etc", "c");
  42. if (res >= 0)
  43. FAIL("unveil create permitted after unveil read only");
  44. res = unveil("/tmp/doesnotexist", "c");
  45. if (res < 0)
  46. FAIL("unveil create on non-existent path failed");
  47. res = unveil("/home", "b");
  48. if (res < 0)
  49. FAIL("unveil browse failed");
  50. res = unveil("/home", "w");
  51. if (res >= 0)
  52. FAIL("unveil write permitted after unveil browse only");
  53. res = unveil("/home", "x");
  54. if (res >= 0)
  55. FAIL("unveil execute permitted after unveil browse only");
  56. res = unveil("/home", "c");
  57. if (res >= 0)
  58. FAIL("unveil create permitted after unveil browse only");
  59. res = unveil(nullptr, nullptr);
  60. if (res < 0)
  61. FAIL("unveil state lock failed");
  62. res = unveil("/bin", "w");
  63. if (res >= 0)
  64. FAIL("unveil permitted after unveil state locked");
  65. res = access("/bin/id", F_OK);
  66. if (res == 0)
  67. FAIL("access(..., F_OK) permitted after locked veil without relevant unveil");
  68. }