Commit fa0bad3f28a7c4a192769a128ede584c49a7e12f

Authored by Eric W. Biederman
Committed by Greg Kroah-Hartman
1 parent 80d4d8397a

mnt: Update unprivileged remount test

commit 4a44a19b470a886997d6647a77bb3e38dcbfa8c5 upstream.

- MNT_NODEV should be irrelevant except when reading back mount flags,
  no longer specify MNT_NODEV on remount.

- Test MNT_NODEV on devpts where it is meaningful even for unprivileged mounts.

- Add a test to verify that remount of a prexisting mount with the same flags
  is allowed and does not change those flags.

- Cleanup up the definitions of MS_REC, MS_RELATIME, MS_STRICTATIME that are used
  when the code is built in an environment without them.

- Correct the test error messages when tests fail.  There were not 5 tests
  that tested MS_RELATIME.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 1 changed file with 142 additions and 30 deletions Inline Diff

tools/testing/selftests/mount/unprivileged-remount-test.c
1 #define _GNU_SOURCE 1 #define _GNU_SOURCE
2 #include <sched.h> 2 #include <sched.h>
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <errno.h> 4 #include <errno.h>
5 #include <string.h> 5 #include <string.h>
6 #include <sys/types.h> 6 #include <sys/types.h>
7 #include <sys/mount.h> 7 #include <sys/mount.h>
8 #include <sys/wait.h> 8 #include <sys/wait.h>
9 #include <sys/vfs.h>
10 #include <sys/statvfs.h>
9 #include <stdlib.h> 11 #include <stdlib.h>
10 #include <unistd.h> 12 #include <unistd.h>
11 #include <fcntl.h> 13 #include <fcntl.h>
12 #include <grp.h> 14 #include <grp.h>
13 #include <stdbool.h> 15 #include <stdbool.h>
14 #include <stdarg.h> 16 #include <stdarg.h>
15 17
16 #ifndef CLONE_NEWNS 18 #ifndef CLONE_NEWNS
17 # define CLONE_NEWNS 0x00020000 19 # define CLONE_NEWNS 0x00020000
18 #endif 20 #endif
19 #ifndef CLONE_NEWUTS 21 #ifndef CLONE_NEWUTS
20 # define CLONE_NEWUTS 0x04000000 22 # define CLONE_NEWUTS 0x04000000
21 #endif 23 #endif
22 #ifndef CLONE_NEWIPC 24 #ifndef CLONE_NEWIPC
23 # define CLONE_NEWIPC 0x08000000 25 # define CLONE_NEWIPC 0x08000000
24 #endif 26 #endif
25 #ifndef CLONE_NEWNET 27 #ifndef CLONE_NEWNET
26 # define CLONE_NEWNET 0x40000000 28 # define CLONE_NEWNET 0x40000000
27 #endif 29 #endif
28 #ifndef CLONE_NEWUSER 30 #ifndef CLONE_NEWUSER
29 # define CLONE_NEWUSER 0x10000000 31 # define CLONE_NEWUSER 0x10000000
30 #endif 32 #endif
31 #ifndef CLONE_NEWPID 33 #ifndef CLONE_NEWPID
32 # define CLONE_NEWPID 0x20000000 34 # define CLONE_NEWPID 0x20000000
33 #endif 35 #endif
34 36
37 #ifndef MS_REC
38 # define MS_REC 16384
39 #endif
35 #ifndef MS_RELATIME 40 #ifndef MS_RELATIME
36 #define MS_RELATIME (1 << 21) 41 # define MS_RELATIME (1 << 21)
37 #endif 42 #endif
38 #ifndef MS_STRICTATIME 43 #ifndef MS_STRICTATIME
39 #define MS_STRICTATIME (1 << 24) 44 # define MS_STRICTATIME (1 << 24)
40 #endif 45 #endif
41 46
42 static void die(char *fmt, ...) 47 static void die(char *fmt, ...)
43 { 48 {
44 va_list ap; 49 va_list ap;
45 va_start(ap, fmt); 50 va_start(ap, fmt);
46 vfprintf(stderr, fmt, ap); 51 vfprintf(stderr, fmt, ap);
47 va_end(ap); 52 va_end(ap);
48 exit(EXIT_FAILURE); 53 exit(EXIT_FAILURE);
49 } 54 }
50 55
51 static void write_file(char *filename, char *fmt, ...) 56 static void write_file(char *filename, char *fmt, ...)
52 { 57 {
53 char buf[4096]; 58 char buf[4096];
54 int fd; 59 int fd;
55 ssize_t written; 60 ssize_t written;
56 int buf_len; 61 int buf_len;
57 va_list ap; 62 va_list ap;
58 63
59 va_start(ap, fmt); 64 va_start(ap, fmt);
60 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap); 65 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
61 va_end(ap); 66 va_end(ap);
62 if (buf_len < 0) { 67 if (buf_len < 0) {
63 die("vsnprintf failed: %s\n", 68 die("vsnprintf failed: %s\n",
64 strerror(errno)); 69 strerror(errno));
65 } 70 }
66 if (buf_len >= sizeof(buf)) { 71 if (buf_len >= sizeof(buf)) {
67 die("vsnprintf output truncated\n"); 72 die("vsnprintf output truncated\n");
68 } 73 }
69 74
70 fd = open(filename, O_WRONLY); 75 fd = open(filename, O_WRONLY);
71 if (fd < 0) { 76 if (fd < 0) {
72 die("open of %s failed: %s\n", 77 die("open of %s failed: %s\n",
73 filename, strerror(errno)); 78 filename, strerror(errno));
74 } 79 }
75 written = write(fd, buf, buf_len); 80 written = write(fd, buf, buf_len);
76 if (written != buf_len) { 81 if (written != buf_len) {
77 if (written >= 0) { 82 if (written >= 0) {
78 die("short write to %s\n", filename); 83 die("short write to %s\n", filename);
79 } else { 84 } else {
80 die("write to %s failed: %s\n", 85 die("write to %s failed: %s\n",
81 filename, strerror(errno)); 86 filename, strerror(errno));
82 } 87 }
83 } 88 }
84 if (close(fd) != 0) { 89 if (close(fd) != 0) {
85 die("close of %s failed: %s\n", 90 die("close of %s failed: %s\n",
86 filename, strerror(errno)); 91 filename, strerror(errno));
87 } 92 }
88 } 93 }
89 94
95 static int read_mnt_flags(const char *path)
96 {
97 int ret;
98 struct statvfs stat;
99 int mnt_flags;
100
101 ret = statvfs(path, &stat);
102 if (ret != 0) {
103 die("statvfs of %s failed: %s\n",
104 path, strerror(errno));
105 }
106 if (stat.f_flag & ~(ST_RDONLY | ST_NOSUID | ST_NODEV | \
107 ST_NOEXEC | ST_NOATIME | ST_NODIRATIME | ST_RELATIME | \
108 ST_SYNCHRONOUS | ST_MANDLOCK)) {
109 die("Unrecognized mount flags\n");
110 }
111 mnt_flags = 0;
112 if (stat.f_flag & ST_RDONLY)
113 mnt_flags |= MS_RDONLY;
114 if (stat.f_flag & ST_NOSUID)
115 mnt_flags |= MS_NOSUID;
116 if (stat.f_flag & ST_NODEV)
117 mnt_flags |= MS_NODEV;
118 if (stat.f_flag & ST_NOEXEC)
119 mnt_flags |= MS_NOEXEC;
120 if (stat.f_flag & ST_NOATIME)
121 mnt_flags |= MS_NOATIME;
122 if (stat.f_flag & ST_NODIRATIME)
123 mnt_flags |= MS_NODIRATIME;
124 if (stat.f_flag & ST_RELATIME)
125 mnt_flags |= MS_RELATIME;
126 if (stat.f_flag & ST_SYNCHRONOUS)
127 mnt_flags |= MS_SYNCHRONOUS;
128 if (stat.f_flag & ST_MANDLOCK)
129 mnt_flags |= ST_MANDLOCK;
130
131 return mnt_flags;
132 }
133
90 static void create_and_enter_userns(void) 134 static void create_and_enter_userns(void)
91 { 135 {
92 uid_t uid; 136 uid_t uid;
93 gid_t gid; 137 gid_t gid;
94 138
95 uid = getuid(); 139 uid = getuid();
96 gid = getgid(); 140 gid = getgid();
97 141
98 if (unshare(CLONE_NEWUSER) !=0) { 142 if (unshare(CLONE_NEWUSER) !=0) {
99 die("unshare(CLONE_NEWUSER) failed: %s\n", 143 die("unshare(CLONE_NEWUSER) failed: %s\n",
100 strerror(errno)); 144 strerror(errno));
101 } 145 }
102 146
103 write_file("/proc/self/uid_map", "0 %d 1", uid); 147 write_file("/proc/self/uid_map", "0 %d 1", uid);
104 write_file("/proc/self/gid_map", "0 %d 1", gid); 148 write_file("/proc/self/gid_map", "0 %d 1", gid);
105 149
106 if (setgroups(0, NULL) != 0) { 150 if (setgroups(0, NULL) != 0) {
107 die("setgroups failed: %s\n", 151 die("setgroups failed: %s\n",
108 strerror(errno)); 152 strerror(errno));
109 } 153 }
110 if (setgid(0) != 0) { 154 if (setgid(0) != 0) {
111 die ("setgid(0) failed %s\n", 155 die ("setgid(0) failed %s\n",
112 strerror(errno)); 156 strerror(errno));
113 } 157 }
114 if (setuid(0) != 0) { 158 if (setuid(0) != 0) {
115 die("setuid(0) failed %s\n", 159 die("setuid(0) failed %s\n",
116 strerror(errno)); 160 strerror(errno));
117 } 161 }
118 } 162 }
119 163
120 static 164 static
121 bool test_unpriv_remount(int mount_flags, int remount_flags, int invalid_flags) 165 bool test_unpriv_remount(const char *fstype, const char *mount_options,
166 int mount_flags, int remount_flags, int invalid_flags)
122 { 167 {
123 pid_t child; 168 pid_t child;
124 169
125 child = fork(); 170 child = fork();
126 if (child == -1) { 171 if (child == -1) {
127 die("fork failed: %s\n", 172 die("fork failed: %s\n",
128 strerror(errno)); 173 strerror(errno));
129 } 174 }
130 if (child != 0) { /* parent */ 175 if (child != 0) { /* parent */
131 pid_t pid; 176 pid_t pid;
132 int status; 177 int status;
133 pid = waitpid(child, &status, 0); 178 pid = waitpid(child, &status, 0);
134 if (pid == -1) { 179 if (pid == -1) {
135 die("waitpid failed: %s\n", 180 die("waitpid failed: %s\n",
136 strerror(errno)); 181 strerror(errno));
137 } 182 }
138 if (pid != child) { 183 if (pid != child) {
139 die("waited for %d got %d\n", 184 die("waited for %d got %d\n",
140 child, pid); 185 child, pid);
141 } 186 }
142 if (!WIFEXITED(status)) { 187 if (!WIFEXITED(status)) {
143 die("child did not terminate cleanly\n"); 188 die("child did not terminate cleanly\n");
144 } 189 }
145 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false; 190 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
146 } 191 }
147 192
148 create_and_enter_userns(); 193 create_and_enter_userns();
149 if (unshare(CLONE_NEWNS) != 0) { 194 if (unshare(CLONE_NEWNS) != 0) {
150 die("unshare(CLONE_NEWNS) failed: %s\n", 195 die("unshare(CLONE_NEWNS) failed: %s\n",
151 strerror(errno)); 196 strerror(errno));
152 } 197 }
153 198
154 if (mount("testing", "/tmp", "ramfs", mount_flags, NULL) != 0) { 199 if (mount("testing", "/tmp", fstype, mount_flags, mount_options) != 0) {
155 die("mount of /tmp failed: %s\n", 200 die("mount of %s with options '%s' on /tmp failed: %s\n",
156 strerror(errno)); 201 fstype,
202 mount_options? mount_options : "",
203 strerror(errno));
157 } 204 }
158 205
159 create_and_enter_userns(); 206 create_and_enter_userns();
160 207
161 if (unshare(CLONE_NEWNS) != 0) { 208 if (unshare(CLONE_NEWNS) != 0) {
162 die("unshare(CLONE_NEWNS) failed: %s\n", 209 die("unshare(CLONE_NEWNS) failed: %s\n",
163 strerror(errno)); 210 strerror(errno));
164 } 211 }
165 212
166 if (mount("/tmp", "/tmp", "none", 213 if (mount("/tmp", "/tmp", "none",
167 MS_REMOUNT | MS_BIND | remount_flags, NULL) != 0) { 214 MS_REMOUNT | MS_BIND | remount_flags, NULL) != 0) {
168 /* system("cat /proc/self/mounts"); */ 215 /* system("cat /proc/self/mounts"); */
169 die("remount of /tmp failed: %s\n", 216 die("remount of /tmp failed: %s\n",
170 strerror(errno)); 217 strerror(errno));
171 } 218 }
172 219
173 if (mount("/tmp", "/tmp", "none", 220 if (mount("/tmp", "/tmp", "none",
174 MS_REMOUNT | MS_BIND | invalid_flags, NULL) == 0) { 221 MS_REMOUNT | MS_BIND | invalid_flags, NULL) == 0) {
175 /* system("cat /proc/self/mounts"); */ 222 /* system("cat /proc/self/mounts"); */
176 die("remount of /tmp with invalid flags " 223 die("remount of /tmp with invalid flags "
177 "succeeded unexpectedly\n"); 224 "succeeded unexpectedly\n");
178 } 225 }
179 exit(EXIT_SUCCESS); 226 exit(EXIT_SUCCESS);
180 } 227 }
181 228
182 static bool test_unpriv_remount_simple(int mount_flags) 229 static bool test_unpriv_remount_simple(int mount_flags)
183 { 230 {
184 return test_unpriv_remount(mount_flags, mount_flags, 0); 231 return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags, 0);
185 } 232 }
186 233
187 static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags) 234 static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags)
188 { 235 {
189 return test_unpriv_remount(mount_flags, mount_flags, invalid_flags); 236 return test_unpriv_remount("ramfs", NULL, mount_flags, mount_flags,
237 invalid_flags);
190 } 238 }
191 239
240 static bool test_priv_mount_unpriv_remount(void)
241 {
242 pid_t child;
243 int ret;
244 const char *orig_path = "/dev";
245 const char *dest_path = "/tmp";
246 int orig_mnt_flags, remount_mnt_flags;
247
248 child = fork();
249 if (child == -1) {
250 die("fork failed: %s\n",
251 strerror(errno));
252 }
253 if (child != 0) { /* parent */
254 pid_t pid;
255 int status;
256 pid = waitpid(child, &status, 0);
257 if (pid == -1) {
258 die("waitpid failed: %s\n",
259 strerror(errno));
260 }
261 if (pid != child) {
262 die("waited for %d got %d\n",
263 child, pid);
264 }
265 if (!WIFEXITED(status)) {
266 die("child did not terminate cleanly\n");
267 }
268 return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
269 }
270
271 orig_mnt_flags = read_mnt_flags(orig_path);
272
273 create_and_enter_userns();
274 ret = unshare(CLONE_NEWNS);
275 if (ret != 0) {
276 die("unshare(CLONE_NEWNS) failed: %s\n",
277 strerror(errno));
278 }
279
280 ret = mount(orig_path, dest_path, "bind", MS_BIND | MS_REC, NULL);
281 if (ret != 0) {
282 die("recursive bind mount of %s onto %s failed: %s\n",
283 orig_path, dest_path, strerror(errno));
284 }
285
286 ret = mount(dest_path, dest_path, "none",
287 MS_REMOUNT | MS_BIND | orig_mnt_flags , NULL);
288 if (ret != 0) {
289 /* system("cat /proc/self/mounts"); */
290 die("remount of /tmp failed: %s\n",
291 strerror(errno));
292 }
293
294 remount_mnt_flags = read_mnt_flags(dest_path);
295 if (orig_mnt_flags != remount_mnt_flags) {
296 die("Mount flags unexpectedly changed during remount of %s originally mounted on %s\n",
297 dest_path, orig_path);
298 }
299 exit(EXIT_SUCCESS);
300 }
301
192 int main(int argc, char **argv) 302 int main(int argc, char **argv)
193 { 303 {
194 if (!test_unpriv_remount_simple(MS_RDONLY|MS_NODEV)) { 304 if (!test_unpriv_remount_simple(MS_RDONLY)) {
195 die("MS_RDONLY malfunctions\n"); 305 die("MS_RDONLY malfunctions\n");
196 } 306 }
197 if (!test_unpriv_remount_simple(MS_NODEV)) { 307 if (!test_unpriv_remount("devpts", "newinstance", MS_NODEV, MS_NODEV, 0)) {
198 die("MS_NODEV malfunctions\n"); 308 die("MS_NODEV malfunctions\n");
199 } 309 }
200 if (!test_unpriv_remount_simple(MS_NOSUID|MS_NODEV)) { 310 if (!test_unpriv_remount_simple(MS_NOSUID)) {
201 die("MS_NOSUID malfunctions\n"); 311 die("MS_NOSUID malfunctions\n");
202 } 312 }
203 if (!test_unpriv_remount_simple(MS_NOEXEC|MS_NODEV)) { 313 if (!test_unpriv_remount_simple(MS_NOEXEC)) {
204 die("MS_NOEXEC malfunctions\n"); 314 die("MS_NOEXEC malfunctions\n");
205 } 315 }
206 if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODEV, 316 if (!test_unpriv_remount_atime(MS_RELATIME,
207 MS_NOATIME|MS_NODEV)) 317 MS_NOATIME))
208 { 318 {
209 die("MS_RELATIME malfunctions\n"); 319 die("MS_RELATIME malfunctions\n");
210 } 320 }
211 if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODEV, 321 if (!test_unpriv_remount_atime(MS_STRICTATIME,
212 MS_NOATIME|MS_NODEV)) 322 MS_NOATIME))
213 { 323 {
214 die("MS_STRICTATIME malfunctions\n"); 324 die("MS_STRICTATIME malfunctions\n");
215 } 325 }
216 if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODEV, 326 if (!test_unpriv_remount_atime(MS_NOATIME,
217 MS_STRICTATIME|MS_NODEV)) 327 MS_STRICTATIME))
218 { 328 {
219 die("MS_RELATIME malfunctions\n"); 329 die("MS_NOATIME malfunctions\n");
220 } 330 }
221 if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME|MS_NODEV, 331 if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME,
222 MS_NOATIME|MS_NODEV)) 332 MS_NOATIME))
223 { 333 {
224 die("MS_RELATIME malfunctions\n"); 334 die("MS_RELATIME|MS_NODIRATIME malfunctions\n");
225 } 335 }
226 if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME|MS_NODEV, 336 if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME,
227 MS_NOATIME|MS_NODEV)) 337 MS_NOATIME))
228 { 338 {
229 die("MS_RELATIME malfunctions\n"); 339 die("MS_STRICTATIME|MS_NODIRATIME malfunctions\n");
230 } 340 }
231 if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME|MS_NODEV, 341 if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME,
232 MS_STRICTATIME|MS_NODEV)) 342 MS_STRICTATIME))
233 { 343 {
234 die("MS_RELATIME malfunctions\n"); 344 die("MS_NOATIME|MS_DIRATIME malfunctions\n");
235 } 345 }
236 if (!test_unpriv_remount(MS_STRICTATIME|MS_NODEV, MS_NODEV, 346 if (!test_unpriv_remount("ramfs", NULL, MS_STRICTATIME, 0, MS_NOATIME))
237 MS_NOATIME|MS_NODEV))
238 { 347 {
239 die("Default atime malfunctions\n"); 348 die("Default atime malfunctions\n");
349 }
350 if (!test_priv_mount_unpriv_remount()) {
351 die("Mount flags unexpectedly changed after remount\n");
240 } 352 }
241 return EXIT_SUCCESS; 353 return EXIT_SUCCESS;
242 } 354 }