]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mount-util.c
Merge pull request #19064 from yuwata/resolve-fix-cache-19049
[thirdparty/systemd.git] / src / test / test-mount-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mount.h>
4 #include <sys/statvfs.h>
5
6 #include "alloc-util.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "mount-util.h"
10 #include "namespace-util.h"
11 #include "path-util.h"
12 #include "process-util.h"
13 #include "rm-rf.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "tests.h"
17 #include "tmpfile-util.h"
18
19 static void test_mount_option_mangle(void) {
20 char *opts = NULL;
21 unsigned long f;
22
23 assert_se(mount_option_mangle(NULL, MS_RDONLY|MS_NOSUID, &f, &opts) == 0);
24 assert_se(f == (MS_RDONLY|MS_NOSUID));
25 assert_se(opts == NULL);
26
27 assert_se(mount_option_mangle("", MS_RDONLY|MS_NOSUID, &f, &opts) == 0);
28 assert_se(f == (MS_RDONLY|MS_NOSUID));
29 assert_se(opts == NULL);
30
31 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec", 0, &f, &opts) == 0);
32 assert_se(f == (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC));
33 assert_se(opts == NULL);
34
35 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec,mode=755", 0, &f, &opts) == 0);
36 assert_se(f == (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC));
37 assert_se(streq(opts, "mode=755"));
38 opts = mfree(opts);
39
40 assert_se(mount_option_mangle("rw,nosuid,foo,hogehoge,nodev,mode=755", 0, &f, &opts) == 0);
41 assert_se(f == (MS_NOSUID|MS_NODEV));
42 assert_se(streq(opts, "foo,hogehoge,mode=755"));
43 opts = mfree(opts);
44
45 assert_se(mount_option_mangle("rw,nosuid,nodev,noexec,relatime,net_cls,net_prio", MS_RDONLY, &f, &opts) == 0);
46 assert_se(f == (MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_RELATIME));
47 assert_se(streq(opts, "net_cls,net_prio"));
48 opts = mfree(opts);
49
50 assert_se(mount_option_mangle("rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000", MS_RDONLY, &f, &opts) == 0);
51 assert_se(f == (MS_NOSUID|MS_NODEV|MS_RELATIME));
52 assert_se(streq(opts, "size=1630748k,mode=700,uid=1000,gid=1000"));
53 opts = mfree(opts);
54
55 assert_se(mount_option_mangle("size=1630748k,rw,gid=1000,,,nodev,relatime,,mode=700,nosuid,uid=1000", MS_RDONLY, &f, &opts) == 0);
56 assert_se(f == (MS_NOSUID|MS_NODEV|MS_RELATIME));
57 assert_se(streq(opts, "size=1630748k,gid=1000,mode=700,uid=1000"));
58 opts = mfree(opts);
59
60 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);
61 assert_se(f == (MS_NOSUID|MS_NODEV));
62 assert_se(streq(opts, "size=8143984k,nr_inodes=2035996,mode=755"));
63 opts = mfree(opts);
64
65 assert_se(mount_option_mangle("rw,relatime,fmask=0022,,,dmask=0022", MS_RDONLY, &f, &opts) == 0);
66 assert_se(f == MS_RELATIME);
67 assert_se(streq(opts, "fmask=0022,dmask=0022"));
68 opts = mfree(opts);
69
70 assert_se(mount_option_mangle("rw,relatime,fmask=0022,dmask=0022,\"hogehoge", MS_RDONLY, &f, &opts) < 0);
71 }
72
73 static void test_bind_remount_recursive(void) {
74 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
75 _cleanup_free_ char *subdir = NULL;
76 const char *p;
77
78 if (geteuid() != 0) {
79 (void) log_tests_skipped("not running as root");
80 return;
81 }
82
83 assert_se(mkdtemp_malloc("/tmp/XXXXXX", &tmp) >= 0);
84 subdir = path_join(tmp, "subdir");
85 assert_se(subdir);
86 assert_se(mkdir(subdir, 0755) >= 0);
87
88 FOREACH_STRING(p, "/usr", "/sys", "/", tmp) {
89 pid_t pid;
90
91 pid = fork();
92 assert_se(pid >= 0);
93
94 if (pid == 0) {
95 struct statvfs svfs;
96 /* child */
97 assert_se(detach_mount_namespace() >= 0);
98
99 /* Check that the subdir is writable (it must be because it's in /tmp) */
100 assert_se(statvfs(subdir, &svfs) >= 0);
101 assert_se(!FLAGS_SET(svfs.f_flag, ST_RDONLY));
102
103 /* Make the subdir a bind mount */
104 assert_se(mount_nofollow(subdir, subdir, NULL, MS_BIND|MS_REC, NULL) >= 0);
105
106 /* Ensure it's still writable */
107 assert_se(statvfs(subdir, &svfs) >= 0);
108 assert_se(!FLAGS_SET(svfs.f_flag, ST_RDONLY));
109
110 /* Now mark the path we currently run for read-only */
111 assert_se(bind_remount_recursive(p, MS_RDONLY, MS_RDONLY, STRV_MAKE("/sys/kernel")) >= 0);
112
113 /* Ensure that this worked on the top-level */
114 assert_se(statvfs(p, &svfs) >= 0);
115 assert_se(FLAGS_SET(svfs.f_flag, ST_RDONLY));
116
117 /* And ensure this had an effect on the subdir exactly if we are talking about a path above the subdir */
118 assert_se(statvfs(subdir, &svfs) >= 0);
119 assert_se(FLAGS_SET(svfs.f_flag, ST_RDONLY) == !!path_startswith(subdir, p));
120
121 _exit(EXIT_SUCCESS);
122 }
123
124 assert_se(wait_for_terminate_and_check("test-remount-rec", pid, WAIT_LOG) == EXIT_SUCCESS);
125 }
126 }
127
128 static void test_bind_remount_one(void) {
129 pid_t pid;
130
131 if (geteuid() != 0) {
132 (void) log_tests_skipped("not running as root");
133 return;
134 }
135
136 pid = fork();
137 assert_se(pid >= 0);
138
139 if (pid == 0) {
140 /* child */
141
142 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
143
144 assert_se(detach_mount_namespace() >= 0);
145
146 assert_se(fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo) >= 0);
147
148 assert_se(bind_remount_one_with_mountinfo("/run", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) >= 0);
149 assert_se(bind_remount_one_with_mountinfo("/proc/idontexist", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) == -ENOENT);
150 assert_se(bind_remount_one_with_mountinfo("/proc/self", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) == -EINVAL);
151 assert_se(bind_remount_one_with_mountinfo("/", MS_RDONLY, MS_RDONLY, proc_self_mountinfo) >= 0);
152
153 _exit(EXIT_SUCCESS);
154 }
155
156 assert_se(wait_for_terminate_and_check("test-remount-one", pid, WAIT_LOG) == EXIT_SUCCESS);
157 }
158
159 int main(int argc, char *argv[]) {
160 test_setup_logging(LOG_DEBUG);
161
162 test_mount_option_mangle();
163 test_bind_remount_recursive();
164 test_bind_remount_one();
165
166 return 0;
167 }