10 #include <sys/statvfs.h>
19 # define CLONE_NEWNS 0x00020000
22 # define CLONE_NEWUTS 0x04000000
25 # define CLONE_NEWIPC 0x08000000
28 # define CLONE_NEWNET 0x40000000
31 # define CLONE_NEWUSER 0x10000000
34 # define CLONE_NEWPID 0x20000000
41 # define MS_RELATIME (1 << 21)
43 #ifndef MS_STRICTATIME
44 # define MS_STRICTATIME (1 << 24)
47 static void die(char *fmt, ...)
51 vfprintf(stderr, fmt, ap);
56 static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
63 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
65 die("vsnprintf failed: %s\n",
68 if (buf_len >= sizeof(buf)) {
69 die("vsnprintf output truncated\n");
72 fd = open(filename, O_WRONLY);
74 if ((errno == ENOENT) && enoent_ok)
76 die("open of %s failed: %s\n",
77 filename, strerror(errno));
79 written = write(fd, buf, buf_len);
80 if (written != buf_len) {
82 die("short write to %s\n", filename);
84 die("write to %s failed: %s\n",
85 filename, strerror(errno));
89 die("close of %s failed: %s\n",
90 filename, strerror(errno));
94 static void maybe_write_file(char *filename, char *fmt, ...)
99 vmaybe_write_file(true, filename, fmt, ap);
104 static void write_file(char *filename, char *fmt, ...)
109 vmaybe_write_file(false, filename, fmt, ap);
114 static int read_mnt_flags(const char *path)
120 ret = statvfs(path, &stat);
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");
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;
153 static void create_and_enter_userns(void)
161 if (unshare(CLONE_NEWUSER) !=0) {
162 die("unshare(CLONE_NEWUSER) failed: %s\n",
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",
174 if (setuid(0) != 0) {
175 die("setuid(0) failed %s\n",
181 bool test_unpriv_remount(const char *fstype, const char *mount_options,
182 int mount_flags, int remount_flags, int invalid_flags)
188 die("fork failed: %s\n",
191 if (child != 0) { /* parent */
194 pid = waitpid(child, &status, 0);
196 die("waitpid failed: %s\n",
200 die("waited for %d got %d\n",
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",
215 if (mount("testing", "/tmp", fstype, mount_flags, mount_options) != 0) {
216 die("mount of %s with options '%s' on /tmp failed: %s\n",
218 mount_options? mount_options : "",
222 create_and_enter_userns();
224 if (unshare(CLONE_NEWNS) != 0) {
225 die("unshare(CLONE_NEWNS) failed: %s\n",
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",
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");
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,
256 static bool test_priv_mount_unpriv_remount(void)
260 const char *orig_path = "/dev";
261 const char *dest_path = "/tmp";
262 int orig_mnt_flags, remount_mnt_flags;
266 die("fork failed: %s\n",
269 if (child != 0) { /* parent */
272 pid = waitpid(child, &status, 0);
274 die("waitpid failed: %s\n",
278 die("waited for %d got %d\n",
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);
292 die("unshare(CLONE_NEWNS) failed: %s\n",
296 ret = mount(orig_path, dest_path, "bind", MS_BIND | MS_REC, NULL);
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);
305 /* system("cat /proc/self/mounts"); */
306 die("remount of /tmp failed: %s\n",
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);
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,
335 die("MS_RELATIME malfunctions\n");
337 if (!test_unpriv_remount_atime(MS_STRICTATIME,
340 die("MS_STRICTATIME malfunctions\n");
342 if (!test_unpriv_remount_atime(MS_NOATIME,
345 die("MS_NOATIME malfunctions\n");
347 if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME,
350 die("MS_RELATIME|MS_NODIRATIME malfunctions\n");
352 if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME,
355 die("MS_STRICTATIME|MS_NODIRATIME malfunctions\n");
357 if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME,
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");