unveil-symlinks.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. int main()
  11. {
  12. rmdir("/tmp/foo/1");
  13. rmdir("/tmp/foo");
  14. unlink("/tmp/bar");
  15. if (mkdir("/tmp/foo", 0755) < 0) {
  16. perror("mkdir");
  17. return 1;
  18. }
  19. if (mkdir("/tmp/foo/1", 0755) < 0) {
  20. perror("mkdir");
  21. return 1;
  22. }
  23. if (symlink("/tmp/foo", "/tmp/bar")) {
  24. perror("symlink");
  25. return 1;
  26. }
  27. if (unveil("/tmp/foo", "r") < 0) {
  28. perror("unveil");
  29. return 1;
  30. }
  31. if (unveil(nullptr, nullptr) < 0) {
  32. perror("unveil");
  33. return 1;
  34. }
  35. int fd = open("/tmp/foo/1", O_RDONLY);
  36. if (fd < 0) {
  37. perror("open");
  38. return 1;
  39. }
  40. close(fd);
  41. fd = open("/tmp/bar/1", O_RDONLY);
  42. if (fd >= 0) {
  43. fprintf(stderr, "FAIL, symlink was not unveiled\n");
  44. return 1;
  45. }
  46. if (chdir("/tmp")) {
  47. perror("chdir");
  48. return 1;
  49. }
  50. fd = open("./foo/1", O_RDONLY);
  51. if (fd < 0) {
  52. perror("open");
  53. return 1;
  54. }
  55. close(fd);
  56. fd = open("./bar/1", O_RDONLY);
  57. if (fd >= 0) {
  58. fprintf(stderr, "FAIL, symlink was not unveiled\n");
  59. return 1;
  60. }
  61. printf("PASS\n");
  62. return 0;
  63. }