Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / testing / selftests / mount / unprivileged-remount-test.c
blob584dc6bc3b06679e8afd2a48ab52cd0e47e1ec56
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <sched.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/mount.h>
9 #include <sys/wait.h>
10 #include <sys/vfs.h>
11 #include <sys/statvfs.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <grp.h>
16 #include <stdbool.h>
17 #include <stdarg.h>
19 #ifndef CLONE_NEWNS
20 # define CLONE_NEWNS 0x00020000
21 #endif
22 #ifndef CLONE_NEWUTS
23 # define CLONE_NEWUTS 0x04000000
24 #endif
25 #ifndef CLONE_NEWIPC
26 # define CLONE_NEWIPC 0x08000000
27 #endif
28 #ifndef CLONE_NEWNET
29 # define CLONE_NEWNET 0x40000000
30 #endif
31 #ifndef CLONE_NEWUSER
32 # define CLONE_NEWUSER 0x10000000
33 #endif
34 #ifndef CLONE_NEWPID
35 # define CLONE_NEWPID 0x20000000
36 #endif
38 #ifndef MS_REC
39 # define MS_REC 16384
40 #endif
41 #ifndef MS_RELATIME
42 # define MS_RELATIME (1 << 21)
43 #endif
44 #ifndef MS_STRICTATIME
45 # define MS_STRICTATIME (1 << 24)
46 #endif
48 static void die(char *fmt, ...)
50 va_list ap;
51 va_start(ap, fmt);
52 vfprintf(stderr, fmt, ap);
53 va_end(ap);
54 exit(EXIT_FAILURE);
57 static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
59 char buf[4096];
60 int fd;
61 ssize_t written;
62 int buf_len;
64 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
65 if (buf_len < 0) {
66 die("vsnprintf failed: %s\n",
67 strerror(errno));
69 if (buf_len >= sizeof(buf)) {
70 die("vsnprintf output truncated\n");
73 fd = open(filename, O_WRONLY);
74 if (fd < 0) {
75 if ((errno == ENOENT) && enoent_ok)
76 return;
77 die("open of %s failed: %s\n",
78 filename, strerror(errno));
80 written = write(fd, buf, buf_len);
81 if (written != buf_len) {
82 if (written >= 0) {
83 die("short write to %s\n", filename);
84 } else {
85 die("write to %s failed: %s\n",
86 filename, strerror(errno));
89 if (close(fd) != 0) {
90 die("close of %s failed: %s\n",
91 filename, strerror(errno));
95 static void maybe_write_file(char *filename, char *fmt, ...)
97 va_list ap;
99 va_start(ap, fmt);
100 vmaybe_write_file(true, filename, fmt, ap);
101 va_end(ap);
105 static void write_file(char *filename, char *fmt, ...)
107 va_list ap;
109 va_start(ap, fmt);
110 vmaybe_write_file(false, filename, fmt, ap);
111 va_end(ap);
115 static int read_mnt_flags(const char *path)
117 int ret;
118 struct statvfs stat;
119 int mnt_flags;
121 ret = statvfs(path, &stat);
122 if (ret != 0) {
123 die("statvfs of %s failed: %s\n",
124 path, strerror(errno));
126 if (stat.f_flag & ~(ST_RDONLY | ST_NOSUID | ST_NODEV | \
127 ST_NOEXEC | ST_NOATIME | ST_NODIRATIME | ST_RELATIME | \
128 ST_SYNCHRONOUS | ST_MANDLOCK)) {
129 die("Unrecognized mount flags\n");
131 mnt_flags = 0;
132 if (stat.f_flag & ST_RDONLY)
133 mnt_flags |= MS_RDONLY;
134 if (stat.f_flag & ST_NOSUID)
135 mnt_flags |= MS_NOSUID;
136 if (stat.f_flag & ST_NODEV)
137 mnt_flags |= MS_NODEV;
138 if (stat.f_flag & ST_NOEXEC)
139 mnt_flags |= MS_NOEXEC;
140 if (stat.f_flag & ST_NOATIME)
141 mnt_flags |= MS_NOATIME;
142 if (stat.f_flag & ST_NODIRATIME)
143 mnt_flags |= MS_NODIRATIME;
144 if (stat.f_flag & ST_RELATIME)
145 mnt_flags |= MS_RELATIME;
146 if (stat.f_flag & ST_SYNCHRONOUS)
147 mnt_flags |= MS_SYNCHRONOUS;
148 if (stat.f_flag & ST_MANDLOCK)
149 mnt_flags |= ST_MANDLOCK;
151 return mnt_flags;
154 static void create_and_enter_userns(void)
156 uid_t uid;
157 gid_t gid;
159 uid = getuid();
160 gid = getgid();
162 if (unshare(CLONE_NEWUSER) !=0) {
163 die("unshare(CLONE_NEWUSER) failed: %s\n",
164 strerror(errno));
167 maybe_write_file("/proc/self/setgroups", "deny");
168 write_file("/proc/self/uid_map", "0 %d 1", uid);
169 write_file("/proc/self/gid_map", "0 %d 1", gid);
171 if (setgid(0) != 0) {
172 die ("setgid(0) failed %s\n",
173 strerror(errno));
175 if (setuid(0) != 0) {
176 die("setuid(0) failed %s\n",
177 strerror(errno));
181 static
182 bool test_unpriv_remount(const char *fstype, const char *mount_options,
183 int mount_flags, int remount_flags, int invalid_flags)
185 pid_t child;
187 child = fork();
188 if (child == -1) {
189 die("fork failed: %s\n",
190 strerror(errno));
192 if (child != 0) { /* parent */
193 pid_t pid;
194 int status;
195 pid = waitpid(child, &status, 0);
196 if (pid == -1) {
197 die("waitpid failed: %s\n",
198 strerror(errno));
200 if (pid != child) {
201 die("waited for %d got %d\n",
202 child, pid);
204 if (!WIFEXITED(status)) {
205 die("child did not terminate cleanly\n");
207 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
210 create_and_enter_userns();
211 if (unshare(CLONE_NEWNS) != 0) {
212 die("unshare(CLONE_NEWNS) failed: %s\n",
213 strerror(errno));
216 if (mount("testing", "/tmp", fstype, mount_flags, mount_options) != 0) {
217 die("mount of %s with options '%s' on /tmp failed: %s\n",
218 fstype,
219 mount_options? mount_options : "",
220 strerror(errno));
223 create_and_enter_userns();
225 if (unshare(CLONE_NEWNS) != 0) {
226 die("unshare(CLONE_NEWNS) failed: %s\n",
227 strerror(errno));
230 if (mount("/tmp", "/tmp", "none",
231 MS_REMOUNT | MS_BIND | remount_flags, NULL) != 0) {
232 /* system("cat /proc/self/mounts"); */
233 die("remount of /tmp failed: %s\n",
234 strerror(errno));
237 if (mount("/tmp", "/tmp", "none",
238 MS_REMOUNT | MS_BIND | invalid_flags, NULL) == 0) {
239 /* system("cat /proc/self/mounts"); */
240 die("remount of /tmp with invalid flags "
241 "succeeded unexpectedly\n");
243 exit(EXIT_SUCCESS);
246 static bool test_unpriv_remount_simple(int mount_flags)
248 return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags, 0);
251 static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags)
253 return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags,
254 invalid_flags);
257 static bool test_priv_mount_unpriv_remount(void)
259 pid_t child;
260 int ret;
261 const char *orig_path = "/dev";
262 const char *dest_path = "/tmp";
263 int orig_mnt_flags, remount_mnt_flags;
265 child = fork();
266 if (child == -1) {
267 die("fork failed: %s\n",
268 strerror(errno));
270 if (child != 0) { /* parent */
271 pid_t pid;
272 int status;
273 pid = waitpid(child, &status, 0);
274 if (pid == -1) {
275 die("waitpid failed: %s\n",
276 strerror(errno));
278 if (pid != child) {
279 die("waited for %d got %d\n",
280 child, pid);
282 if (!WIFEXITED(status)) {
283 die("child did not terminate cleanly\n");
285 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
288 orig_mnt_flags = read_mnt_flags(orig_path);
290 create_and_enter_userns();
291 ret = unshare(CLONE_NEWNS);
292 if (ret != 0) {
293 die("unshare(CLONE_NEWNS) failed: %s\n",
294 strerror(errno));
297 ret = mount(orig_path, dest_path, "bind", MS_BIND | MS_REC, NULL);
298 if (ret != 0) {
299 die("recursive bind mount of %s onto %s failed: %s\n",
300 orig_path, dest_path, strerror(errno));
303 ret = mount(dest_path, dest_path, "none",
304 MS_REMOUNT | MS_BIND | orig_mnt_flags , NULL);
305 if (ret != 0) {
306 /* system("cat /proc/self/mounts"); */
307 die("remount of /tmp failed: %s\n",
308 strerror(errno));
311 remount_mnt_flags = read_mnt_flags(dest_path);
312 if (orig_mnt_flags != remount_mnt_flags) {
313 die("Mount flags unexpectedly changed during remount of %s originally mounted on %s\n",
314 dest_path, orig_path);
316 exit(EXIT_SUCCESS);
319 int main(int argc, char **argv)
321 if (!test_unpriv_remount_simple(MS_RDONLY)) {
322 die("MS_RDONLY malfunctions\n");
324 if (!test_unpriv_remount("devpts", "newinstance", MS_NODEV, MS_NODEV, 0)) {
325 die("MS_NODEV malfunctions\n");
327 if (!test_unpriv_remount_simple(MS_NOSUID)) {
328 die("MS_NOSUID malfunctions\n");
330 if (!test_unpriv_remount_simple(MS_NOEXEC)) {
331 die("MS_NOEXEC malfunctions\n");
333 if (!test_unpriv_remount_atime(MS_RELATIME,
334 MS_NOATIME))
336 die("MS_RELATIME malfunctions\n");
338 if (!test_unpriv_remount_atime(MS_STRICTATIME,
339 MS_NOATIME))
341 die("MS_STRICTATIME malfunctions\n");
343 if (!test_unpriv_remount_atime(MS_NOATIME,
344 MS_STRICTATIME))
346 die("MS_NOATIME malfunctions\n");
348 if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME,
349 MS_NOATIME))
351 die("MS_RELATIME|MS_NODIRATIME malfunctions\n");
353 if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME,
354 MS_NOATIME))
356 die("MS_STRICTATIME|MS_NODIRATIME malfunctions\n");
358 if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME,
359 MS_STRICTATIME))
361 die("MS_NOATIME|MS_DIRATIME malfunctions\n");
363 if (!test_unpriv_remount("ramfs", NULL, MS_STRICTATIME, 0, MS_NOATIME))
365 die("Default atime malfunctions\n");
367 if (!test_priv_mount_unpriv_remount()) {
368 die("Mount flags unexpectedly changed after remount\n");
370 return EXIT_SUCCESS;