1 // SPDX-License-Identifier: GPL-2.0
11 #include <sys/statvfs.h>
20 # define CLONE_NEWNS 0x00020000
23 # define CLONE_NEWUTS 0x04000000
26 # define CLONE_NEWIPC 0x08000000
29 # define CLONE_NEWNET 0x40000000
32 # define CLONE_NEWUSER 0x10000000
35 # define CLONE_NEWPID 0x20000000
42 # define MS_RELATIME (1 << 21)
44 #ifndef MS_STRICTATIME
45 # define MS_STRICTATIME (1 << 24)
48 static void die(char *fmt
, ...)
52 vfprintf(stderr
, fmt
, ap
);
57 static void vmaybe_write_file(bool enoent_ok
, char *filename
, char *fmt
, va_list ap
)
64 buf_len
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
66 die("vsnprintf failed: %s\n",
69 if (buf_len
>= sizeof(buf
)) {
70 die("vsnprintf output truncated\n");
73 fd
= open(filename
, O_WRONLY
);
75 if ((errno
== ENOENT
) && enoent_ok
)
77 die("open of %s failed: %s\n",
78 filename
, strerror(errno
));
80 written
= write(fd
, buf
, buf_len
);
81 if (written
!= buf_len
) {
83 die("short write to %s\n", filename
);
85 die("write to %s failed: %s\n",
86 filename
, strerror(errno
));
90 die("close of %s failed: %s\n",
91 filename
, strerror(errno
));
95 static void maybe_write_file(char *filename
, char *fmt
, ...)
100 vmaybe_write_file(true, filename
, fmt
, ap
);
105 static void write_file(char *filename
, char *fmt
, ...)
110 vmaybe_write_file(false, filename
, fmt
, ap
);
115 static int read_mnt_flags(const char *path
)
121 ret
= statvfs(path
, &stat
);
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");
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
;
154 static void create_and_enter_userns(void)
162 if (unshare(CLONE_NEWUSER
) !=0) {
163 die("unshare(CLONE_NEWUSER) failed: %s\n",
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",
175 if (setuid(0) != 0) {
176 die("setuid(0) failed %s\n",
182 bool test_unpriv_remount(const char *fstype
, const char *mount_options
,
183 int mount_flags
, int remount_flags
, int invalid_flags
)
189 die("fork failed: %s\n",
192 if (child
!= 0) { /* parent */
195 pid
= waitpid(child
, &status
, 0);
197 die("waitpid failed: %s\n",
201 die("waited for %d got %d\n",
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",
216 if (mount("testing", "/tmp", fstype
, mount_flags
, mount_options
) != 0) {
217 die("mount of %s with options '%s' on /tmp failed: %s\n",
219 mount_options
? mount_options
: "",
223 create_and_enter_userns();
225 if (unshare(CLONE_NEWNS
) != 0) {
226 die("unshare(CLONE_NEWNS) failed: %s\n",
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",
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");
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
,
257 static bool test_priv_mount_unpriv_remount(void)
261 const char *orig_path
= "/dev";
262 const char *dest_path
= "/tmp";
263 int orig_mnt_flags
, remount_mnt_flags
;
267 die("fork failed: %s\n",
270 if (child
!= 0) { /* parent */
273 pid
= waitpid(child
, &status
, 0);
275 die("waitpid failed: %s\n",
279 die("waited for %d got %d\n",
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
);
293 die("unshare(CLONE_NEWNS) failed: %s\n",
297 ret
= mount(orig_path
, dest_path
, "bind", MS_BIND
| MS_REC
, NULL
);
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
);
306 /* system("cat /proc/self/mounts"); */
307 die("remount of /tmp failed: %s\n",
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
);
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
,
336 die("MS_RELATIME malfunctions\n");
338 if (!test_unpriv_remount_atime(MS_STRICTATIME
,
341 die("MS_STRICTATIME malfunctions\n");
343 if (!test_unpriv_remount_atime(MS_NOATIME
,
346 die("MS_NOATIME malfunctions\n");
348 if (!test_unpriv_remount_atime(MS_RELATIME
|MS_NODIRATIME
,
351 die("MS_RELATIME|MS_NODIRATIME malfunctions\n");
353 if (!test_unpriv_remount_atime(MS_STRICTATIME
|MS_NODIRATIME
,
356 die("MS_STRICTATIME|MS_NODIRATIME malfunctions\n");
358 if (!test_unpriv_remount_atime(MS_NOATIME
|MS_NODIRATIME
,
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");