diff options
author | Eric W. Biederman <ebiederm@xmission.com> | 2014-12-02 14:56:30 -0500 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2014-12-11 19:07:07 -0500 |
commit | db86da7cb76f797a1a8b445166a15cb922c6ff85 (patch) | |
tree | 555f131be82199d97ed63118d8f00d3a768f5f96 | |
parent | 36476beac4f8ca9dc7722790b2e8ef0e8e51034e (diff) |
userns: Unbreak the unprivileged remount tests
A security fix in caused the way the unprivileged remount tests were
using user namespaces to break. Tweak the way user namespaces are
being used so the test works again.
Cc: stable@vger.kernel.org
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
-rw-r--r-- | tools/testing/selftests/mount/unprivileged-remount-test.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/tools/testing/selftests/mount/unprivileged-remount-test.c b/tools/testing/selftests/mount/unprivileged-remount-test.c index 9669d375625a..517785052f1c 100644 --- a/tools/testing/selftests/mount/unprivileged-remount-test.c +++ b/tools/testing/selftests/mount/unprivileged-remount-test.c | |||
@@ -53,17 +53,14 @@ static void die(char *fmt, ...) | |||
53 | exit(EXIT_FAILURE); | 53 | exit(EXIT_FAILURE); |
54 | } | 54 | } |
55 | 55 | ||
56 | static void write_file(char *filename, char *fmt, ...) | 56 | static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap) |
57 | { | 57 | { |
58 | char buf[4096]; | 58 | char buf[4096]; |
59 | int fd; | 59 | int fd; |
60 | ssize_t written; | 60 | ssize_t written; |
61 | int buf_len; | 61 | int buf_len; |
62 | va_list ap; | ||
63 | 62 | ||
64 | va_start(ap, fmt); | ||
65 | buf_len = vsnprintf(buf, sizeof(buf), fmt, ap); | 63 | buf_len = vsnprintf(buf, sizeof(buf), fmt, ap); |
66 | va_end(ap); | ||
67 | if (buf_len < 0) { | 64 | if (buf_len < 0) { |
68 | die("vsnprintf failed: %s\n", | 65 | die("vsnprintf failed: %s\n", |
69 | strerror(errno)); | 66 | strerror(errno)); |
@@ -74,6 +71,8 @@ static void write_file(char *filename, char *fmt, ...) | |||
74 | 71 | ||
75 | fd = open(filename, O_WRONLY); | 72 | fd = open(filename, O_WRONLY); |
76 | if (fd < 0) { | 73 | if (fd < 0) { |
74 | if ((errno == ENOENT) && enoent_ok) | ||
75 | return; | ||
77 | die("open of %s failed: %s\n", | 76 | die("open of %s failed: %s\n", |
78 | filename, strerror(errno)); | 77 | filename, strerror(errno)); |
79 | } | 78 | } |
@@ -92,6 +91,26 @@ static void write_file(char *filename, char *fmt, ...) | |||
92 | } | 91 | } |
93 | } | 92 | } |
94 | 93 | ||
94 | static void maybe_write_file(char *filename, char *fmt, ...) | ||
95 | { | ||
96 | va_list ap; | ||
97 | |||
98 | va_start(ap, fmt); | ||
99 | vmaybe_write_file(true, filename, fmt, ap); | ||
100 | va_end(ap); | ||
101 | |||
102 | } | ||
103 | |||
104 | static void write_file(char *filename, char *fmt, ...) | ||
105 | { | ||
106 | va_list ap; | ||
107 | |||
108 | va_start(ap, fmt); | ||
109 | vmaybe_write_file(false, filename, fmt, ap); | ||
110 | va_end(ap); | ||
111 | |||
112 | } | ||
113 | |||
95 | static int read_mnt_flags(const char *path) | 114 | static int read_mnt_flags(const char *path) |
96 | { | 115 | { |
97 | int ret; | 116 | int ret; |
@@ -144,13 +163,10 @@ static void create_and_enter_userns(void) | |||
144 | strerror(errno)); | 163 | strerror(errno)); |
145 | } | 164 | } |
146 | 165 | ||
166 | maybe_write_file("/proc/self/setgroups", "deny"); | ||
147 | write_file("/proc/self/uid_map", "0 %d 1", uid); | 167 | write_file("/proc/self/uid_map", "0 %d 1", uid); |
148 | write_file("/proc/self/gid_map", "0 %d 1", gid); | 168 | write_file("/proc/self/gid_map", "0 %d 1", gid); |
149 | 169 | ||
150 | if (setgroups(0, NULL) != 0) { | ||
151 | die("setgroups failed: %s\n", | ||
152 | strerror(errno)); | ||
153 | } | ||
154 | if (setgid(0) != 0) { | 170 | if (setgid(0) != 0) { |
155 | die ("setgid(0) failed %s\n", | 171 | die ("setgid(0) failed %s\n", |
156 | strerror(errno)); | 172 | strerror(errno)); |