dev_in_maps.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #define __SANE_USERSPACE_TYPES__ // Use ll64
  4. #include <inttypes.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <linux/unistd.h>
  8. #include <linux/types.h>
  9. #include <linux/mount.h>
  10. #include <sys/syscall.h>
  11. #include <sys/stat.h>
  12. #include <sys/mman.h>
  13. #include <sched.h>
  14. #include <fcntl.h>
  15. #include "kselftest.h"
  16. #include "log.h"
  17. #include "../wrappers.h"
  18. static long get_file_dev_and_inode(void *addr, struct statx *stx)
  19. {
  20. char buf[4096];
  21. FILE *mapf;
  22. mapf = fopen("/proc/self/maps", "r");
  23. if (mapf == NULL)
  24. return pr_perror("fopen(/proc/self/maps)");
  25. while (fgets(buf, sizeof(buf), mapf)) {
  26. unsigned long start, end;
  27. uint32_t maj, min;
  28. __u64 ino;
  29. if (sscanf(buf, "%lx-%lx %*s %*s %x:%x %llu",
  30. &start, &end, &maj, &min, &ino) != 5)
  31. return pr_perror("unable to parse: %s", buf);
  32. if (start == (unsigned long)addr) {
  33. stx->stx_dev_major = maj;
  34. stx->stx_dev_minor = min;
  35. stx->stx_ino = ino;
  36. return 0;
  37. }
  38. }
  39. return pr_err("unable to find the mapping");
  40. }
  41. static int ovl_mount(void)
  42. {
  43. int tmpfs, fsfd, ovl;
  44. fsfd = sys_fsopen("tmpfs", 0);
  45. if (fsfd == -1)
  46. return pr_perror("fsopen(tmpfs)");
  47. if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)
  48. return pr_perror("FSCONFIG_CMD_CREATE");
  49. tmpfs = sys_fsmount(fsfd, 0, 0);
  50. if (tmpfs == -1)
  51. return pr_perror("fsmount");
  52. close(fsfd);
  53. /* overlayfs can't be constructed on top of a detached mount. */
  54. if (sys_move_mount(tmpfs, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_PATH))
  55. return pr_perror("move_mount");
  56. close(tmpfs);
  57. if (mkdir("/tmp/w", 0755) == -1 ||
  58. mkdir("/tmp/u", 0755) == -1 ||
  59. mkdir("/tmp/l", 0755) == -1)
  60. return pr_perror("mkdir");
  61. fsfd = sys_fsopen("overlay", 0);
  62. if (fsfd == -1)
  63. return pr_perror("fsopen(overlay)");
  64. if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 ||
  65. sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 ||
  66. sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 ||
  67. sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1)
  68. return pr_perror("fsconfig");
  69. if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)
  70. return pr_perror("fsconfig");
  71. ovl = sys_fsmount(fsfd, 0, 0);
  72. if (ovl == -1)
  73. return pr_perror("fsmount");
  74. return ovl;
  75. }
  76. /*
  77. * Check that the file device and inode shown in /proc/pid/maps match values
  78. * returned by stat(2).
  79. */
  80. static int test(void)
  81. {
  82. struct statx stx, mstx;
  83. int ovl, fd;
  84. void *addr;
  85. ovl = ovl_mount();
  86. if (ovl == -1)
  87. return -1;
  88. fd = openat(ovl, "test", O_RDWR | O_CREAT, 0644);
  89. if (fd == -1)
  90. return pr_perror("openat");
  91. addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
  92. if (addr == MAP_FAILED)
  93. return pr_perror("mmap");
  94. if (get_file_dev_and_inode(addr, &mstx))
  95. return -1;
  96. if (statx(fd, "", AT_EMPTY_PATH | AT_STATX_SYNC_AS_STAT, STATX_INO, &stx))
  97. return pr_perror("statx");
  98. if (stx.stx_dev_major != mstx.stx_dev_major ||
  99. stx.stx_dev_minor != mstx.stx_dev_minor ||
  100. stx.stx_ino != mstx.stx_ino)
  101. return pr_fail("unmatched dev:ino %x:%x:%llx (expected %x:%x:%llx)\n",
  102. mstx.stx_dev_major, mstx.stx_dev_minor, mstx.stx_ino,
  103. stx.stx_dev_major, stx.stx_dev_minor, stx.stx_ino);
  104. ksft_test_result_pass("devices are matched\n");
  105. return 0;
  106. }
  107. int main(int argc, char **argv)
  108. {
  109. int fsfd;
  110. fsfd = sys_fsopen("overlay", 0);
  111. if (fsfd == -1) {
  112. ksft_test_result_skip("unable to create overlay mount\n");
  113. return 1;
  114. }
  115. close(fsfd);
  116. /* Create a new mount namespace to not care about cleaning test mounts. */
  117. if (unshare(CLONE_NEWNS) == -1) {
  118. ksft_test_result_skip("unable to create a new mount namespace\n");
  119. return 1;
  120. }
  121. if (sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {
  122. pr_perror("mount");
  123. return 1;
  124. }
  125. ksft_set_plan(1);
  126. if (test())
  127. return 1;
  128. ksft_exit_pass();
  129. return 0;
  130. }