dev_in_maps.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. static int sys_fsopen(const char *fsname, unsigned int flags)
  18. {
  19. return syscall(__NR_fsopen, fsname, flags);
  20. }
  21. static int sys_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux)
  22. {
  23. return syscall(__NR_fsconfig, fd, cmd, key, value, aux);
  24. }
  25. static int sys_fsmount(int fd, unsigned int flags, unsigned int attr_flags)
  26. {
  27. return syscall(__NR_fsmount, fd, flags, attr_flags);
  28. }
  29. static int sys_mount(const char *src, const char *tgt, const char *fst,
  30. unsigned long flags, const void *data)
  31. {
  32. return syscall(__NR_mount, src, tgt, fst, flags, data);
  33. }
  34. static int sys_move_mount(int from_dfd, const char *from_pathname,
  35. int to_dfd, const char *to_pathname,
  36. unsigned int flags)
  37. {
  38. return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, to_pathname, flags);
  39. }
  40. static long get_file_dev_and_inode(void *addr, struct statx *stx)
  41. {
  42. char buf[4096];
  43. FILE *mapf;
  44. mapf = fopen("/proc/self/maps", "r");
  45. if (mapf == NULL)
  46. return pr_perror("fopen(/proc/self/maps)");
  47. while (fgets(buf, sizeof(buf), mapf)) {
  48. unsigned long start, end;
  49. uint32_t maj, min;
  50. __u64 ino;
  51. if (sscanf(buf, "%lx-%lx %*s %*s %x:%x %llu",
  52. &start, &end, &maj, &min, &ino) != 5)
  53. return pr_perror("unable to parse: %s", buf);
  54. if (start == (unsigned long)addr) {
  55. stx->stx_dev_major = maj;
  56. stx->stx_dev_minor = min;
  57. stx->stx_ino = ino;
  58. return 0;
  59. }
  60. }
  61. return pr_err("unable to find the mapping");
  62. }
  63. static int ovl_mount(void)
  64. {
  65. int tmpfs, fsfd, ovl;
  66. fsfd = sys_fsopen("tmpfs", 0);
  67. if (fsfd == -1)
  68. return pr_perror("fsopen(tmpfs)");
  69. if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)
  70. return pr_perror("FSCONFIG_CMD_CREATE");
  71. tmpfs = sys_fsmount(fsfd, 0, 0);
  72. if (tmpfs == -1)
  73. return pr_perror("fsmount");
  74. close(fsfd);
  75. /* overlayfs can't be constructed on top of a detached mount. */
  76. if (sys_move_mount(tmpfs, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_PATH))
  77. return pr_perror("move_mount");
  78. close(tmpfs);
  79. if (mkdir("/tmp/w", 0755) == -1 ||
  80. mkdir("/tmp/u", 0755) == -1 ||
  81. mkdir("/tmp/l", 0755) == -1)
  82. return pr_perror("mkdir");
  83. fsfd = sys_fsopen("overlay", 0);
  84. if (fsfd == -1)
  85. return pr_perror("fsopen(overlay)");
  86. if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 ||
  87. sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 ||
  88. sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 ||
  89. sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1)
  90. return pr_perror("fsconfig");
  91. if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)
  92. return pr_perror("fsconfig");
  93. ovl = sys_fsmount(fsfd, 0, 0);
  94. if (ovl == -1)
  95. return pr_perror("fsmount");
  96. return ovl;
  97. }
  98. /*
  99. * Check that the file device and inode shown in /proc/pid/maps match values
  100. * returned by stat(2).
  101. */
  102. static int test(void)
  103. {
  104. struct statx stx, mstx;
  105. int ovl, fd;
  106. void *addr;
  107. ovl = ovl_mount();
  108. if (ovl == -1)
  109. return -1;
  110. fd = openat(ovl, "test", O_RDWR | O_CREAT, 0644);
  111. if (fd == -1)
  112. return pr_perror("openat");
  113. addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
  114. if (addr == MAP_FAILED)
  115. return pr_perror("mmap");
  116. if (get_file_dev_and_inode(addr, &mstx))
  117. return -1;
  118. if (statx(fd, "", AT_EMPTY_PATH | AT_STATX_SYNC_AS_STAT, STATX_INO, &stx))
  119. return pr_perror("statx");
  120. if (stx.stx_dev_major != mstx.stx_dev_major ||
  121. stx.stx_dev_minor != mstx.stx_dev_minor ||
  122. stx.stx_ino != mstx.stx_ino)
  123. return pr_fail("unmatched dev:ino %x:%x:%llx (expected %x:%x:%llx)\n",
  124. mstx.stx_dev_major, mstx.stx_dev_minor, mstx.stx_ino,
  125. stx.stx_dev_major, stx.stx_dev_minor, stx.stx_ino);
  126. ksft_test_result_pass("devices are matched\n");
  127. return 0;
  128. }
  129. int main(int argc, char **argv)
  130. {
  131. int fsfd;
  132. fsfd = sys_fsopen("overlay", 0);
  133. if (fsfd == -1) {
  134. ksft_test_result_skip("unable to create overlay mount\n");
  135. return 1;
  136. }
  137. close(fsfd);
  138. /* Create a new mount namespace to not care about cleaning test mounts. */
  139. if (unshare(CLONE_NEWNS) == -1) {
  140. ksft_test_result_skip("unable to create a new mount namespace\n");
  141. return 1;
  142. }
  143. if (sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {
  144. pr_perror("mount");
  145. return 1;
  146. }
  147. ksft_set_plan(1);
  148. if (test())
  149. return 1;
  150. ksft_exit_pass();
  151. return 0;
  152. }