1 From db181ce011e3c033328608299cd6fac06ea50130 Mon Sep 17 00:00:00 2001
2 From: "Eric W. Biederman" <ebiederm@xmission.com>
3 Date: Tue, 29 Jul 2014 15:50:44 -0700
4 Subject: mnt: Add tests for unprivileged remount cases that have found to be faulty
6 From: "Eric W. Biederman" <ebiederm@xmission.com>
8 commit db181ce011e3c033328608299cd6fac06ea50130 upstream.
10 Kenton Varda <kenton@sandstorm.io> discovered that by remounting a
11 read-only bind mount read-only in a user namespace the
12 MNT_LOCK_READONLY bit would be cleared, allowing an unprivileged user
13 to the remount a read-only mount read-write.
15 Upon review of the code in remount it was discovered that the code allowed
16 nosuid, noexec, and nodev to be cleared. It was also discovered that
17 the code was allowing the per mount atime flags to be changed.
19 The first naive patch to fix these issues contained the flaw that using
20 default atime settings when remounting a filesystem could be disallowed.
22 To avoid this problems in the future add tests to ensure unprivileged
23 remounts are succeeding and failing at the appropriate times.
25 Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
26 Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
27 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
30 tools/testing/selftests/Makefile | 1
31 tools/testing/selftests/mount/Makefile | 17
32 tools/testing/selftests/mount/unprivileged-remount-test.c | 242 ++++++++++++++
33 3 files changed, 260 insertions(+)
35 --- a/tools/testing/selftests/Makefile
36 +++ b/tools/testing/selftests/Makefile
37 @@ -4,6 +4,7 @@ TARGETS += efivarfs
39 TARGETS += memory-hotplug
46 +++ b/tools/testing/selftests/mount/Makefile
48 +# Makefile for mount selftests.
50 +all: unprivileged-remount-test
52 +unprivileged-remount-test: unprivileged-remount-test.c
53 + gcc -Wall -O2 unprivileged-remount-test.c -o unprivileged-remount-test
55 +# Allow specific tests to be selected.
56 +test_unprivileged_remount: unprivileged-remount-test
57 + @if [ -f /proc/self/uid_map ] ; then ./unprivileged-remount-test ; fi
59 +run_tests: all test_unprivileged_remount
62 + rm -f unprivileged-remount-test
64 +.PHONY: all test_unprivileged_remount
66 +++ b/tools/testing/selftests/mount/unprivileged-remount-test.c
73 +#include <sys/types.h>
74 +#include <sys/mount.h>
75 +#include <sys/wait.h>
84 +# define CLONE_NEWNS 0x00020000
87 +# define CLONE_NEWUTS 0x04000000
90 +# define CLONE_NEWIPC 0x08000000
93 +# define CLONE_NEWNET 0x40000000
95 +#ifndef CLONE_NEWUSER
96 +# define CLONE_NEWUSER 0x10000000
99 +# define CLONE_NEWPID 0x20000000
103 +#define MS_RELATIME (1 << 21)
105 +#ifndef MS_STRICTATIME
106 +#define MS_STRICTATIME (1 << 24)
109 +static void die(char *fmt, ...)
113 + vfprintf(stderr, fmt, ap);
115 + exit(EXIT_FAILURE);
118 +static void write_file(char *filename, char *fmt, ...)
127 + buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
130 + die("vsnprintf failed: %s\n",
133 + if (buf_len >= sizeof(buf)) {
134 + die("vsnprintf output truncated\n");
137 + fd = open(filename, O_WRONLY);
139 + die("open of %s failed: %s\n",
140 + filename, strerror(errno));
142 + written = write(fd, buf, buf_len);
143 + if (written != buf_len) {
144 + if (written >= 0) {
145 + die("short write to %s\n", filename);
147 + die("write to %s failed: %s\n",
148 + filename, strerror(errno));
151 + if (close(fd) != 0) {
152 + die("close of %s failed: %s\n",
153 + filename, strerror(errno));
157 +static void create_and_enter_userns(void)
165 + if (unshare(CLONE_NEWUSER) !=0) {
166 + die("unshare(CLONE_NEWUSER) failed: %s\n",
170 + write_file("/proc/self/uid_map", "0 %d 1", uid);
171 + write_file("/proc/self/gid_map", "0 %d 1", gid);
173 + if (setgroups(0, NULL) != 0) {
174 + die("setgroups failed: %s\n",
177 + if (setgid(0) != 0) {
178 + die ("setgid(0) failed %s\n",
181 + if (setuid(0) != 0) {
182 + die("setuid(0) failed %s\n",
188 +bool test_unpriv_remount(int mount_flags, int remount_flags, int invalid_flags)
194 + die("fork failed: %s\n",
197 + if (child != 0) { /* parent */
200 + pid = waitpid(child, &status, 0);
202 + die("waitpid failed: %s\n",
205 + if (pid != child) {
206 + die("waited for %d got %d\n",
209 + if (!WIFEXITED(status)) {
210 + die("child did not terminate cleanly\n");
212 + return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
215 + create_and_enter_userns();
216 + if (unshare(CLONE_NEWNS) != 0) {
217 + die("unshare(CLONE_NEWNS) failed: %s\n",
221 + if (mount("testing", "/tmp", "ramfs", mount_flags, NULL) != 0) {
222 + die("mount of /tmp failed: %s\n",
226 + create_and_enter_userns();
228 + if (unshare(CLONE_NEWNS) != 0) {
229 + die("unshare(CLONE_NEWNS) failed: %s\n",
233 + if (mount("/tmp", "/tmp", "none",
234 + MS_REMOUNT | MS_BIND | remount_flags, NULL) != 0) {
235 + /* system("cat /proc/self/mounts"); */
236 + die("remount of /tmp failed: %s\n",
240 + if (mount("/tmp", "/tmp", "none",
241 + MS_REMOUNT | MS_BIND | invalid_flags, NULL) == 0) {
242 + /* system("cat /proc/self/mounts"); */
243 + die("remount of /tmp with invalid flags "
244 + "succeeded unexpectedly\n");
246 + exit(EXIT_SUCCESS);
249 +static bool test_unpriv_remount_simple(int mount_flags)
251 + return test_unpriv_remount(mount_flags, mount_flags, 0);
254 +static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags)
256 + return test_unpriv_remount(mount_flags, mount_flags, invalid_flags);
259 +int main(int argc, char **argv)
261 + if (!test_unpriv_remount_simple(MS_RDONLY|MS_NODEV)) {
262 + die("MS_RDONLY malfunctions\n");
264 + if (!test_unpriv_remount_simple(MS_NODEV)) {
265 + die("MS_NODEV malfunctions\n");
267 + if (!test_unpriv_remount_simple(MS_NOSUID|MS_NODEV)) {
268 + die("MS_NOSUID malfunctions\n");
270 + if (!test_unpriv_remount_simple(MS_NOEXEC|MS_NODEV)) {
271 + die("MS_NOEXEC malfunctions\n");
273 + if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODEV,
274 + MS_NOATIME|MS_NODEV))
276 + die("MS_RELATIME malfunctions\n");
278 + if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODEV,
279 + MS_NOATIME|MS_NODEV))
281 + die("MS_STRICTATIME malfunctions\n");
283 + if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODEV,
284 + MS_STRICTATIME|MS_NODEV))
286 + die("MS_RELATIME malfunctions\n");
288 + if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME|MS_NODEV,
289 + MS_NOATIME|MS_NODEV))
291 + die("MS_RELATIME malfunctions\n");
293 + if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME|MS_NODEV,
294 + MS_NOATIME|MS_NODEV))
296 + die("MS_RELATIME malfunctions\n");
298 + if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME|MS_NODEV,
299 + MS_STRICTATIME|MS_NODEV))
301 + die("MS_RELATIME malfunctions\n");
303 + if (!test_unpriv_remount(MS_STRICTATIME|MS_NODEV, MS_NODEV,
304 + MS_NOATIME|MS_NODEV))
306 + die("Default atime malfunctions\n");
308 + return EXIT_SUCCESS;