]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mount-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <sys/statvfs.h>
6 #include "alloc-util.h"
7 #include "capability-util.h"
11 #include "missing_mount.h"
13 #include "mount-util.h"
14 #include "mountpoint-util.h"
15 #include "namespace-util.h"
16 #include "path-util.h"
17 #include "process-util.h"
19 #include "string-util.h"
22 #include "tmpfile-util.h"
24 TEST(mount_option_mangle
) {
28 assert_se(mount_option_mangle(NULL
, MS_RDONLY
|MS_NOSUID
, &f
, &opts
) == 0);
29 assert_se(f
== (MS_RDONLY
|MS_NOSUID
));
30 assert_se(opts
== NULL
);
32 assert_se(mount_option_mangle("", MS_RDONLY
|MS_NOSUID
, &f
, &opts
) == 0);
33 assert_se(f
== (MS_RDONLY
|MS_NOSUID
));
34 assert_se(opts
== NULL
);
36 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec", 0, &f
, &opts
) == 0);
37 assert_se(f
== (MS_RDONLY
|MS_NOSUID
|MS_NODEV
|MS_NOEXEC
));
38 assert_se(opts
== NULL
);
40 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec,mode=0755", 0, &f
, &opts
) == 0);
41 assert_se(f
== (MS_RDONLY
|MS_NOSUID
|MS_NODEV
|MS_NOEXEC
));
42 assert_se(streq(opts
, "mode=0755"));
45 assert_se(mount_option_mangle("rw,nosuid,foo,hogehoge,nodev,mode=0755", 0, &f
, &opts
) == 0);
46 assert_se(f
== (MS_NOSUID
|MS_NODEV
));
47 assert_se(streq(opts
, "foo,hogehoge,mode=0755"));
50 assert_se(mount_option_mangle("rw,nosuid,nodev,noexec,relatime,net_cls,net_prio", MS_RDONLY
, &f
, &opts
) == 0);
51 assert_se(f
== (MS_NOSUID
|MS_NODEV
|MS_NOEXEC
|MS_RELATIME
));
52 assert_se(streq(opts
, "net_cls,net_prio"));
55 assert_se(mount_option_mangle("rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000", MS_RDONLY
, &f
, &opts
) == 0);
56 assert_se(f
== (MS_NOSUID
|MS_NODEV
|MS_RELATIME
));
57 assert_se(streq(opts
, "size=1630748k,mode=0700,uid=1000,gid=1000"));
60 assert_se(mount_option_mangle("size=1630748k,rw,gid=1000,,,nodev,relatime,,mode=0700,nosuid,uid=1000", MS_RDONLY
, &f
, &opts
) == 0);
61 assert_se(f
== (MS_NOSUID
|MS_NODEV
|MS_RELATIME
));
62 assert_se(streq(opts
, "size=1630748k,gid=1000,mode=0700,uid=1000"));
65 assert_se(mount_option_mangle("rw,exec,size=8143984k,nr_inodes=2035996,mode=0755", MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, &f
, &opts
) == 0);
66 assert_se(f
== (MS_NOSUID
|MS_NODEV
));
67 assert_se(streq(opts
, "size=8143984k,nr_inodes=2035996,mode=0755"));
70 assert_se(mount_option_mangle("rw,relatime,fmask=0022,,,dmask=0022", MS_RDONLY
, &f
, &opts
) == 0);
71 assert_se(f
== MS_RELATIME
);
72 assert_se(streq(opts
, "fmask=0022,dmask=0022"));
75 assert_se(mount_option_mangle("rw,relatime,fmask=0022,dmask=0022,\"hogehoge", MS_RDONLY
, &f
, &opts
) < 0);
77 assert_se(mount_option_mangle("mode=01777,size=10%,nr_inodes=400k,uid=496107520,gid=496107520,context=\"system_u:object_r:svirt_sandbox_file_t:s0:c0,c1\"", 0, &f
, &opts
) == 0);
79 assert_se(streq(opts
, "mode=01777,size=10%,nr_inodes=400k,uid=496107520,gid=496107520,context=\"system_u:object_r:svirt_sandbox_file_t:s0:c0,c1\""));
83 static void test_mount_flags_to_string_one(unsigned long flags
, const char *expected
) {
84 _cleanup_free_
char *x
= NULL
;
87 r
= mount_flags_to_string(flags
, &x
);
88 log_info("flags: %#lX → %d/\"%s\"", flags
, r
, strnull(x
));
90 assert_se(streq(x
, expected
));
93 TEST(mount_flags_to_string
) {
94 test_mount_flags_to_string_one(0, "0");
95 test_mount_flags_to_string_one(MS_RDONLY
, "MS_RDONLY");
96 test_mount_flags_to_string_one(MS_NOSUID
, "MS_NOSUID");
97 test_mount_flags_to_string_one(MS_NODEV
, "MS_NODEV");
98 test_mount_flags_to_string_one(MS_NOEXEC
, "MS_NOEXEC");
99 test_mount_flags_to_string_one(MS_SYNCHRONOUS
, "MS_SYNCHRONOUS");
100 test_mount_flags_to_string_one(MS_REMOUNT
, "MS_REMOUNT");
101 test_mount_flags_to_string_one(MS_MANDLOCK
, "MS_MANDLOCK");
102 test_mount_flags_to_string_one(MS_DIRSYNC
, "MS_DIRSYNC");
103 test_mount_flags_to_string_one(MS_NOSYMFOLLOW
, "MS_NOSYMFOLLOW");
104 test_mount_flags_to_string_one(MS_NOATIME
, "MS_NOATIME");
105 test_mount_flags_to_string_one(MS_NODIRATIME
, "MS_NODIRATIME");
106 test_mount_flags_to_string_one(MS_BIND
, "MS_BIND");
107 test_mount_flags_to_string_one(MS_MOVE
, "MS_MOVE");
108 test_mount_flags_to_string_one(MS_REC
, "MS_REC");
109 test_mount_flags_to_string_one(MS_SILENT
, "MS_SILENT");
110 test_mount_flags_to_string_one(MS_POSIXACL
, "MS_POSIXACL");
111 test_mount_flags_to_string_one(MS_UNBINDABLE
, "MS_UNBINDABLE");
112 test_mount_flags_to_string_one(MS_PRIVATE
, "MS_PRIVATE");
113 test_mount_flags_to_string_one(MS_SLAVE
, "MS_SLAVE");
114 test_mount_flags_to_string_one(MS_SHARED
, "MS_SHARED");
115 test_mount_flags_to_string_one(MS_RELATIME
, "MS_RELATIME");
116 test_mount_flags_to_string_one(MS_KERNMOUNT
, "MS_KERNMOUNT");
117 test_mount_flags_to_string_one(MS_I_VERSION
, "MS_I_VERSION");
118 test_mount_flags_to_string_one(MS_STRICTATIME
, "MS_STRICTATIME");
119 test_mount_flags_to_string_one(MS_LAZYTIME
, "MS_LAZYTIME");
120 test_mount_flags_to_string_one(MS_LAZYTIME
|MS_STRICTATIME
, "MS_STRICTATIME|MS_LAZYTIME");
121 test_mount_flags_to_string_one(UINT_MAX
,
122 "MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS|MS_REMOUNT|"
123 "MS_MANDLOCK|MS_DIRSYNC|MS_NOSYMFOLLOW|MS_NOATIME|MS_NODIRATIME|"
124 "MS_BIND|MS_MOVE|MS_REC|MS_SILENT|MS_POSIXACL|MS_UNBINDABLE|"
125 "MS_PRIVATE|MS_SLAVE|MS_SHARED|MS_RELATIME|MS_KERNMOUNT|"
126 "MS_I_VERSION|MS_STRICTATIME|MS_LAZYTIME|fc000200");
129 TEST(bind_remount_recursive
) {
130 _cleanup_(rm_rf_physical_and_freep
) char *tmp
= NULL
;
131 _cleanup_free_
char *subdir
= NULL
;
133 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN
) <= 0) {
134 (void) log_tests_skipped("not running privileged");
138 assert_se(mkdtemp_malloc("/tmp/XXXXXX", &tmp
) >= 0);
139 subdir
= path_join(tmp
, "subdir");
141 assert_se(mkdir(subdir
, 0755) >= 0);
143 FOREACH_STRING(p
, "/usr", "/sys", "/", tmp
) {
152 assert_se(detach_mount_namespace() >= 0);
154 /* Check that the subdir is writable (it must be because it's in /tmp) */
155 assert_se(statvfs(subdir
, &svfs
) >= 0);
156 assert_se(!FLAGS_SET(svfs
.f_flag
, ST_RDONLY
));
158 /* Make the subdir a bind mount */
159 assert_se(mount_nofollow(subdir
, subdir
, NULL
, MS_BIND
|MS_REC
, NULL
) >= 0);
161 /* Ensure it's still writable */
162 assert_se(statvfs(subdir
, &svfs
) >= 0);
163 assert_se(!FLAGS_SET(svfs
.f_flag
, ST_RDONLY
));
165 /* Now mark the path we currently run for read-only */
166 assert_se(bind_remount_recursive(p
, MS_RDONLY
, MS_RDONLY
, path_equal(p
, "/sys") ? STRV_MAKE("/sys/kernel") : NULL
) >= 0);
168 /* Ensure that this worked on the top-level */
169 assert_se(statvfs(p
, &svfs
) >= 0);
170 assert_se(FLAGS_SET(svfs
.f_flag
, ST_RDONLY
));
172 /* And ensure this had an effect on the subdir exactly if we are talking about a path above the subdir */
173 assert_se(statvfs(subdir
, &svfs
) >= 0);
174 assert_se(FLAGS_SET(svfs
.f_flag
, ST_RDONLY
) == !!path_startswith(subdir
, p
));
179 assert_se(wait_for_terminate_and_check("test-remount-rec", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
183 TEST(bind_remount_one
) {
186 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN
) <= 0) {
187 (void) log_tests_skipped("not running privileged");
197 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
199 assert_se(detach_mount_namespace() >= 0);
201 assert_se(fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo
) >= 0);
203 assert_se(bind_remount_one_with_mountinfo("/run", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) >= 0);
204 assert_se(bind_remount_one_with_mountinfo("/run", MS_NOEXEC
, MS_RDONLY
|MS_NOEXEC
, proc_self_mountinfo
) >= 0);
205 assert_se(bind_remount_one_with_mountinfo("/proc/idontexist", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) == -ENOENT
);
206 assert_se(bind_remount_one_with_mountinfo("/proc/self", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) == -EINVAL
);
207 assert_se(bind_remount_one_with_mountinfo("/", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) >= 0);
212 assert_se(wait_for_terminate_and_check("test-remount-one", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
215 TEST(make_mount_point_inode
) {
216 _cleanup_(rm_rf_physical_and_freep
) char *d
= NULL
;
217 const char *src_file
, *src_dir
, *dst_file
, *dst_dir
;
220 assert_se(mkdtemp_malloc(NULL
, &d
) >= 0);
222 src_file
= strjoina(d
, "/src/file");
223 src_dir
= strjoina(d
, "/src/dir");
224 dst_file
= strjoina(d
, "/dst/file");
225 dst_dir
= strjoina(d
, "/dst/dir");
227 assert_se(mkdir_p(src_dir
, 0755) >= 0);
228 assert_se(mkdir_parents(dst_file
, 0755) >= 0);
229 assert_se(touch(src_file
) >= 0);
231 assert_se(make_mount_point_inode_from_path(src_file
, dst_file
, 0755) >= 0);
232 assert_se(make_mount_point_inode_from_path(src_dir
, dst_dir
, 0755) >= 0);
234 assert_se(stat(dst_dir
, &st
) == 0);
235 assert_se(S_ISDIR(st
.st_mode
));
236 assert_se(stat(dst_file
, &st
) == 0);
237 assert_se(S_ISREG(st
.st_mode
));
238 assert_se(!(S_IXUSR
& st
.st_mode
));
239 assert_se(!(S_IXGRP
& st
.st_mode
));
240 assert_se(!(S_IXOTH
& st
.st_mode
));
242 assert_se(unlink(dst_file
) == 0);
243 assert_se(rmdir(dst_dir
) == 0);
245 assert_se(stat(src_file
, &st
) == 0);
246 assert_se(make_mount_point_inode_from_stat(&st
, dst_file
, 0755) >= 0);
247 assert_se(stat(src_dir
, &st
) == 0);
248 assert_se(make_mount_point_inode_from_stat(&st
, dst_dir
, 0755) >= 0);
250 assert_se(stat(dst_dir
, &st
) == 0);
251 assert_se(S_ISDIR(st
.st_mode
));
252 assert_se(stat(dst_file
, &st
) == 0);
253 assert_se(S_ISREG(st
.st_mode
));
254 assert_se(!(S_IXUSR
& st
.st_mode
));
255 assert_se(!(S_IXGRP
& st
.st_mode
));
256 assert_se(!(S_IXOTH
& st
.st_mode
));
259 DEFINE_TEST_MAIN(LOG_DEBUG
);