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