]>
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"
10 #include "missing_mount.h"
11 #include "mount-util.h"
12 #include "namespace-util.h"
13 #include "path-util.h"
14 #include "process-util.h"
16 #include "string-util.h"
19 #include "tmpfile-util.h"
21 static void test_mount_option_mangle(void) {
25 log_info("/* %s */", __func__
);
27 assert_se(mount_option_mangle(NULL
, MS_RDONLY
|MS_NOSUID
, &f
, &opts
) == 0);
28 assert_se(f
== (MS_RDONLY
|MS_NOSUID
));
29 assert_se(opts
== NULL
);
31 assert_se(mount_option_mangle("", MS_RDONLY
|MS_NOSUID
, &f
, &opts
) == 0);
32 assert_se(f
== (MS_RDONLY
|MS_NOSUID
));
33 assert_se(opts
== NULL
);
35 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec", 0, &f
, &opts
) == 0);
36 assert_se(f
== (MS_RDONLY
|MS_NOSUID
|MS_NODEV
|MS_NOEXEC
));
37 assert_se(opts
== NULL
);
39 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec,mode=755", 0, &f
, &opts
) == 0);
40 assert_se(f
== (MS_RDONLY
|MS_NOSUID
|MS_NODEV
|MS_NOEXEC
));
41 assert_se(streq(opts
, "mode=755"));
44 assert_se(mount_option_mangle("rw,nosuid,foo,hogehoge,nodev,mode=755", 0, &f
, &opts
) == 0);
45 assert_se(f
== (MS_NOSUID
|MS_NODEV
));
46 assert_se(streq(opts
, "foo,hogehoge,mode=755"));
49 assert_se(mount_option_mangle("rw,nosuid,nodev,noexec,relatime,net_cls,net_prio", MS_RDONLY
, &f
, &opts
) == 0);
50 assert_se(f
== (MS_NOSUID
|MS_NODEV
|MS_NOEXEC
|MS_RELATIME
));
51 assert_se(streq(opts
, "net_cls,net_prio"));
54 assert_se(mount_option_mangle("rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000", MS_RDONLY
, &f
, &opts
) == 0);
55 assert_se(f
== (MS_NOSUID
|MS_NODEV
|MS_RELATIME
));
56 assert_se(streq(opts
, "size=1630748k,mode=700,uid=1000,gid=1000"));
59 assert_se(mount_option_mangle("size=1630748k,rw,gid=1000,,,nodev,relatime,,mode=700,nosuid,uid=1000", MS_RDONLY
, &f
, &opts
) == 0);
60 assert_se(f
== (MS_NOSUID
|MS_NODEV
|MS_RELATIME
));
61 assert_se(streq(opts
, "size=1630748k,gid=1000,mode=700,uid=1000"));
64 assert_se(mount_option_mangle("rw,exec,size=8143984k,nr_inodes=2035996,mode=755", MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, &f
, &opts
) == 0);
65 assert_se(f
== (MS_NOSUID
|MS_NODEV
));
66 assert_se(streq(opts
, "size=8143984k,nr_inodes=2035996,mode=755"));
69 assert_se(mount_option_mangle("rw,relatime,fmask=0022,,,dmask=0022", MS_RDONLY
, &f
, &opts
) == 0);
70 assert_se(f
== MS_RELATIME
);
71 assert_se(streq(opts
, "fmask=0022,dmask=0022"));
74 assert_se(mount_option_mangle("rw,relatime,fmask=0022,dmask=0022,\"hogehoge", MS_RDONLY
, &f
, &opts
) < 0);
76 assert_se(mount_option_mangle("mode=1777,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);
78 assert_se(streq(opts
, "mode=1777,size=10%,nr_inodes=400k,uid=496107520,gid=496107520,context=\"system_u:object_r:svirt_sandbox_file_t:s0:c0,c1\""));
82 static void test_mount_flags_to_string_one(unsigned long flags
, const char *expected
) {
83 _cleanup_free_
char *x
= NULL
;
86 r
= mount_flags_to_string(flags
, &x
);
87 log_info("flags: %#lX → %d/\"%s\"", flags
, r
, strnull(x
));
89 assert_se(streq(x
, expected
));
92 static void test_mount_flags_to_string(void) {
93 log_info("/* %s */", __func__
);
95 test_mount_flags_to_string_one(0, "0");
96 test_mount_flags_to_string_one(MS_RDONLY
, "MS_RDONLY");
97 test_mount_flags_to_string_one(MS_NOSUID
, "MS_NOSUID");
98 test_mount_flags_to_string_one(MS_NODEV
, "MS_NODEV");
99 test_mount_flags_to_string_one(MS_NOEXEC
, "MS_NOEXEC");
100 test_mount_flags_to_string_one(MS_SYNCHRONOUS
, "MS_SYNCHRONOUS");
101 test_mount_flags_to_string_one(MS_REMOUNT
, "MS_REMOUNT");
102 test_mount_flags_to_string_one(MS_MANDLOCK
, "MS_MANDLOCK");
103 test_mount_flags_to_string_one(MS_DIRSYNC
, "MS_DIRSYNC");
104 test_mount_flags_to_string_one(MS_NOSYMFOLLOW
, "MS_NOSYMFOLLOW");
105 test_mount_flags_to_string_one(MS_NOATIME
, "MS_NOATIME");
106 test_mount_flags_to_string_one(MS_NODIRATIME
, "MS_NODIRATIME");
107 test_mount_flags_to_string_one(MS_BIND
, "MS_BIND");
108 test_mount_flags_to_string_one(MS_MOVE
, "MS_MOVE");
109 test_mount_flags_to_string_one(MS_REC
, "MS_REC");
110 test_mount_flags_to_string_one(MS_SILENT
, "MS_SILENT");
111 test_mount_flags_to_string_one(MS_POSIXACL
, "MS_POSIXACL");
112 test_mount_flags_to_string_one(MS_UNBINDABLE
, "MS_UNBINDABLE");
113 test_mount_flags_to_string_one(MS_PRIVATE
, "MS_PRIVATE");
114 test_mount_flags_to_string_one(MS_SLAVE
, "MS_SLAVE");
115 test_mount_flags_to_string_one(MS_SHARED
, "MS_SHARED");
116 test_mount_flags_to_string_one(MS_RELATIME
, "MS_RELATIME");
117 test_mount_flags_to_string_one(MS_KERNMOUNT
, "MS_KERNMOUNT");
118 test_mount_flags_to_string_one(MS_I_VERSION
, "MS_I_VERSION");
119 test_mount_flags_to_string_one(MS_STRICTATIME
, "MS_STRICTATIME");
120 test_mount_flags_to_string_one(MS_LAZYTIME
, "MS_LAZYTIME");
121 test_mount_flags_to_string_one(MS_LAZYTIME
|MS_STRICTATIME
, "MS_STRICTATIME|MS_LAZYTIME");
122 test_mount_flags_to_string_one(UINT_MAX
,
123 "MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS|MS_REMOUNT|"
124 "MS_MANDLOCK|MS_DIRSYNC|MS_NOSYMFOLLOW|MS_NOATIME|MS_NODIRATIME|"
125 "MS_BIND|MS_MOVE|MS_REC|MS_SILENT|MS_POSIXACL|MS_UNBINDABLE|"
126 "MS_PRIVATE|MS_SLAVE|MS_SHARED|MS_RELATIME|MS_KERNMOUNT|"
127 "MS_I_VERSION|MS_STRICTATIME|MS_LAZYTIME|fc000200");
130 static void test_bind_remount_recursive(void) {
131 _cleanup_(rm_rf_physical_and_freep
) char *tmp
= NULL
;
132 _cleanup_free_
char *subdir
= NULL
;
135 log_info("/* %s */", __func__
);
137 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN
) <= 0) {
138 (void) log_tests_skipped("not running privileged");
142 assert_se(mkdtemp_malloc("/tmp/XXXXXX", &tmp
) >= 0);
143 subdir
= path_join(tmp
, "subdir");
145 assert_se(mkdir(subdir
, 0755) >= 0);
147 FOREACH_STRING(p
, "/usr", "/sys", "/", tmp
) {
156 assert_se(detach_mount_namespace() >= 0);
158 /* Check that the subdir is writable (it must be because it's in /tmp) */
159 assert_se(statvfs(subdir
, &svfs
) >= 0);
160 assert_se(!FLAGS_SET(svfs
.f_flag
, ST_RDONLY
));
162 /* Make the subdir a bind mount */
163 assert_se(mount_nofollow(subdir
, subdir
, NULL
, MS_BIND
|MS_REC
, NULL
) >= 0);
165 /* Ensure it's still writable */
166 assert_se(statvfs(subdir
, &svfs
) >= 0);
167 assert_se(!FLAGS_SET(svfs
.f_flag
, ST_RDONLY
));
169 /* Now mark the path we currently run for read-only */
170 assert_se(bind_remount_recursive(p
, MS_RDONLY
, MS_RDONLY
, STRV_MAKE("/sys/kernel")) >= 0);
172 /* Ensure that this worked on the top-level */
173 assert_se(statvfs(p
, &svfs
) >= 0);
174 assert_se(FLAGS_SET(svfs
.f_flag
, ST_RDONLY
));
176 /* And ensure this had an effect on the subdir exactly if we are talking about a path above the subdir */
177 assert_se(statvfs(subdir
, &svfs
) >= 0);
178 assert_se(FLAGS_SET(svfs
.f_flag
, ST_RDONLY
) == !!path_startswith(subdir
, p
));
183 assert_se(wait_for_terminate_and_check("test-remount-rec", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
187 static void test_bind_remount_one(void) {
190 log_info("/* %s */", __func__
);
192 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN
) <= 0) {
193 (void) log_tests_skipped("not running privileged");
203 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
205 assert_se(detach_mount_namespace() >= 0);
207 assert_se(fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo
) >= 0);
209 assert_se(bind_remount_one_with_mountinfo("/run", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) >= 0);
210 assert_se(bind_remount_one_with_mountinfo("/proc/idontexist", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) == -ENOENT
);
211 assert_se(bind_remount_one_with_mountinfo("/proc/self", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) == -EINVAL
);
212 assert_se(bind_remount_one_with_mountinfo("/", MS_RDONLY
, MS_RDONLY
, proc_self_mountinfo
) >= 0);
217 assert_se(wait_for_terminate_and_check("test-remount-one", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
220 int main(int argc
, char *argv
[]) {
221 test_setup_logging(LOG_DEBUG
);
223 test_mount_option_mangle();
224 test_mount_flags_to_string();
225 test_bind_remount_recursive();
226 test_bind_remount_one();