mm: rename alloc_pages_exact_node() to __alloc_pages_node()
[linux/fpc-iii.git] / tools / testing / selftests / mount / unprivileged-remount-test.c
blob517785052f1c37a4c368810c5c88d35119c9cab0
1 #define _GNU_SOURCE
2 #include <sched.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/mount.h>
8 #include <sys/wait.h>
9 #include <sys/vfs.h>
10 #include <sys/statvfs.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <grp.h>
15 #include <stdbool.h>
16 #include <stdarg.h>
18 #ifndef CLONE_NEWNS
19 # define CLONE_NEWNS 0x00020000
20 #endif
21 #ifndef CLONE_NEWUTS
22 # define CLONE_NEWUTS 0x04000000
23 #endif
24 #ifndef CLONE_NEWIPC
25 # define CLONE_NEWIPC 0x08000000
26 #endif
27 #ifndef CLONE_NEWNET
28 # define CLONE_NEWNET 0x40000000
29 #endif
30 #ifndef CLONE_NEWUSER
31 # define CLONE_NEWUSER 0x10000000
32 #endif
33 #ifndef CLONE_NEWPID
34 # define CLONE_NEWPID 0x20000000
35 #endif
37 #ifndef MS_REC
38 # define MS_REC 16384
39 #endif
40 #ifndef MS_RELATIME
41 # define MS_RELATIME (1 << 21)
42 #endif
43 #ifndef MS_STRICTATIME
44 # define MS_STRICTATIME (1 << 24)
45 #endif
47 static void die(char *fmt, ...)
49 va_list ap;
50 va_start(ap, fmt);
51 vfprintf(stderr, fmt, ap);
52 va_end(ap);
53 exit(EXIT_FAILURE);
56 static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
58 char buf[4096];
59 int fd;
60 ssize_t written;
61 int buf_len;
63 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
64 if (buf_len < 0) {
65 die("vsnprintf failed: %s\n",
66 strerror(errno));
68 if (buf_len >= sizeof(buf)) {
69 die("vsnprintf output truncated\n");
72 fd = open(filename, O_WRONLY);
73 if (fd < 0) {
74 if ((errno == ENOENT) && enoent_ok)
75 return;
76 die("open of %s failed: %s\n",
77 filename, strerror(errno));
79 written = write(fd, buf, buf_len);
80 if (written != buf_len) {
81 if (written >= 0) {
82 die("short write to %s\n", filename);
83 } else {
84 die("write to %s failed: %s\n",
85 filename, strerror(errno));
88 if (close(fd) != 0) {
89 die("close of %s failed: %s\n",
90 filename, strerror(errno));
94 static void maybe_write_file(char *filename, char *fmt, ...)
96 va_list ap;
98 va_start(ap, fmt);
99 vmaybe_write_file(true, filename, fmt, ap);
100 va_end(ap);
104 static void write_file(char *filename, char *fmt, ...)
106 va_list ap;
108 va_start(ap, fmt);
109 vmaybe_write_file(false, filename, fmt, ap);
110 va_end(ap);
114 static int read_mnt_flags(const char *path)
116 int ret;
117 struct statvfs stat;
118 int mnt_flags;
120 ret = statvfs(path, &stat);
121 if (ret != 0) {
122 die("statvfs of %s failed: %s\n",
123 path, strerror(errno));
125 if (stat.f_flag & ~(ST_RDONLY | ST_NOSUID | ST_NODEV | \
126 ST_NOEXEC | ST_NOATIME | ST_NODIRATIME | ST_RELATIME | \
127 ST_SYNCHRONOUS | ST_MANDLOCK)) {
128 die("Unrecognized mount flags\n");
130 mnt_flags = 0;
131 if (stat.f_flag & ST_RDONLY)
132 mnt_flags |= MS_RDONLY;
133 if (stat.f_flag & ST_NOSUID)
134 mnt_flags |= MS_NOSUID;
135 if (stat.f_flag & ST_NODEV)
136 mnt_flags |= MS_NODEV;
137 if (stat.f_flag & ST_NOEXEC)
138 mnt_flags |= MS_NOEXEC;
139 if (stat.f_flag & ST_NOATIME)
140 mnt_flags |= MS_NOATIME;
141 if (stat.f_flag & ST_NODIRATIME)
142 mnt_flags |= MS_NODIRATIME;
143 if (stat.f_flag & ST_RELATIME)
144 mnt_flags |= MS_RELATIME;
145 if (stat.f_flag & ST_SYNCHRONOUS)
146 mnt_flags |= MS_SYNCHRONOUS;
147 if (stat.f_flag & ST_MANDLOCK)
148 mnt_flags |= ST_MANDLOCK;
150 return mnt_flags;
153 static void create_and_enter_userns(void)
155 uid_t uid;
156 gid_t gid;
158 uid = getuid();
159 gid = getgid();
161 if (unshare(CLONE_NEWUSER) !=0) {
162 die("unshare(CLONE_NEWUSER) failed: %s\n",
163 strerror(errno));
166 maybe_write_file("/proc/self/setgroups", "deny");
167 write_file("/proc/self/uid_map", "0 %d 1", uid);
168 write_file("/proc/self/gid_map", "0 %d 1", gid);
170 if (setgid(0) != 0) {
171 die ("setgid(0) failed %s\n",
172 strerror(errno));
174 if (setuid(0) != 0) {
175 die("setuid(0) failed %s\n",
176 strerror(errno));
180 static
181 bool test_unpriv_remount(const char *fstype, const char *mount_options,
182 int mount_flags, int remount_flags, int invalid_flags)
184 pid_t child;
186 child = fork();
187 if (child == -1) {
188 die("fork failed: %s\n",
189 strerror(errno));
191 if (child != 0) { /* parent */
192 pid_t pid;
193 int status;
194 pid = waitpid(child, &status, 0);
195 if (pid == -1) {
196 die("waitpid failed: %s\n",
197 strerror(errno));
199 if (pid != child) {
200 die("waited for %d got %d\n",
201 child, pid);
203 if (!WIFEXITED(status)) {
204 die("child did not terminate cleanly\n");
206 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
209 create_and_enter_userns();
210 if (unshare(CLONE_NEWNS) != 0) {
211 die("unshare(CLONE_NEWNS) failed: %s\n",
212 strerror(errno));
215 if (mount("testing", "/tmp", fstype, mount_flags, mount_options) != 0) {
216 die("mount of %s with options '%s' on /tmp failed: %s\n",
217 fstype,
218 mount_options? mount_options : "",
219 strerror(errno));
222 create_and_enter_userns();
224 if (unshare(CLONE_NEWNS) != 0) {
225 die("unshare(CLONE_NEWNS) failed: %s\n",
226 strerror(errno));
229 if (mount("/tmp", "/tmp", "none",
230 MS_REMOUNT | MS_BIND | remount_flags, NULL) != 0) {
231 /* system("cat /proc/self/mounts"); */
232 die("remount of /tmp failed: %s\n",
233 strerror(errno));
236 if (mount("/tmp", "/tmp", "none",
237 MS_REMOUNT | MS_BIND | invalid_flags, NULL) == 0) {
238 /* system("cat /proc/self/mounts"); */
239 die("remount of /tmp with invalid flags "
240 "succeeded unexpectedly\n");
242 exit(EXIT_SUCCESS);
245 static bool test_unpriv_remount_simple(int mount_flags)
247 return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags, 0);
250 static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags)
252 return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags,
253 invalid_flags);
256 static bool test_priv_mount_unpriv_remount(void)
258 pid_t child;
259 int ret;
260 const char *orig_path = "/dev";
261 const char *dest_path = "/tmp";
262 int orig_mnt_flags, remount_mnt_flags;
264 child = fork();
265 if (child == -1) {
266 die("fork failed: %s\n",
267 strerror(errno));
269 if (child != 0) { /* parent */
270 pid_t pid;
271 int status;
272 pid = waitpid(child, &status, 0);
273 if (pid == -1) {
274 die("waitpid failed: %s\n",
275 strerror(errno));
277 if (pid != child) {
278 die("waited for %d got %d\n",
279 child, pid);
281 if (!WIFEXITED(status)) {
282 die("child did not terminate cleanly\n");
284 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
287 orig_mnt_flags = read_mnt_flags(orig_path);
289 create_and_enter_userns();
290 ret = unshare(CLONE_NEWNS);
291 if (ret != 0) {
292 die("unshare(CLONE_NEWNS) failed: %s\n",
293 strerror(errno));
296 ret = mount(orig_path, dest_path, "bind", MS_BIND | MS_REC, NULL);
297 if (ret != 0) {
298 die("recursive bind mount of %s onto %s failed: %s\n",
299 orig_path, dest_path, strerror(errno));
302 ret = mount(dest_path, dest_path, "none",
303 MS_REMOUNT | MS_BIND | orig_mnt_flags , NULL);
304 if (ret != 0) {
305 /* system("cat /proc/self/mounts"); */
306 die("remount of /tmp failed: %s\n",
307 strerror(errno));
310 remount_mnt_flags = read_mnt_flags(dest_path);
311 if (orig_mnt_flags != remount_mnt_flags) {
312 die("Mount flags unexpectedly changed during remount of %s originally mounted on %s\n",
313 dest_path, orig_path);
315 exit(EXIT_SUCCESS);
318 int main(int argc, char **argv)
320 if (!test_unpriv_remount_simple(MS_RDONLY)) {
321 die("MS_RDONLY malfunctions\n");
323 if (!test_unpriv_remount("devpts", "newinstance", MS_NODEV, MS_NODEV, 0)) {
324 die("MS_NODEV malfunctions\n");
326 if (!test_unpriv_remount_simple(MS_NOSUID)) {
327 die("MS_NOSUID malfunctions\n");
329 if (!test_unpriv_remount_simple(MS_NOEXEC)) {
330 die("MS_NOEXEC malfunctions\n");
332 if (!test_unpriv_remount_atime(MS_RELATIME,
333 MS_NOATIME))
335 die("MS_RELATIME malfunctions\n");
337 if (!test_unpriv_remount_atime(MS_STRICTATIME,
338 MS_NOATIME))
340 die("MS_STRICTATIME malfunctions\n");
342 if (!test_unpriv_remount_atime(MS_NOATIME,
343 MS_STRICTATIME))
345 die("MS_NOATIME malfunctions\n");
347 if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME,
348 MS_NOATIME))
350 die("MS_RELATIME|MS_NODIRATIME malfunctions\n");
352 if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME,
353 MS_NOATIME))
355 die("MS_STRICTATIME|MS_NODIRATIME malfunctions\n");
357 if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME,
358 MS_STRICTATIME))
360 die("MS_NOATIME|MS_DIRATIME malfunctions\n");
362 if (!test_unpriv_remount("ramfs", NULL, MS_STRICTATIME, 0, MS_NOATIME))
364 die("Default atime malfunctions\n");
366 if (!test_priv_mount_unpriv_remount()) {
367 die("Mount flags unexpectedly changed after remount\n");
369 return EXIT_SUCCESS;