Skip to content

Commit db86da7

Browse files
committed
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>
1 parent 36476be commit db86da7

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

‎tools/testing/selftests/mount/unprivileged-remount-test.c‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,14 @@ static void die(char *fmt, ...)
5353
exit(EXIT_FAILURE);
5454
}
5555

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)
5757
{
5858
char buf[4096];
5959
int fd;
6060
ssize_t written;
6161
int buf_len;
62-
va_list ap;
6362

64-
va_start(ap, fmt);
6563
buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
66-
va_end(ap);
6764
if (buf_len < 0) {
6865
die("vsnprintf failed: %s\n",
6966
strerror(errno));
@@ -74,6 +71,8 @@ static void write_file(char *filename, char *fmt, ...)
7471

7572
fd = open(filename, O_WRONLY);
7673
if (fd < 0) {
74+
if ((errno == ENOENT) && enoent_ok)
75+
return;
7776
die("open of %s failed: %s\n",
7877
filename, strerror(errno));
7978
}
@@ -92,6 +91,26 @@ static void write_file(char *filename, char *fmt, ...)
9291
}
9392
}
9493

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+
95114
static int read_mnt_flags(const char *path)
96115
{
97116
int ret;
@@ -144,13 +163,10 @@ static void create_and_enter_userns(void)
144163
strerror(errno));
145164
}
146165

166+
maybe_write_file("/proc/self/setgroups", "deny");
147167
write_file("/proc/self/uid_map", "0 %d 1", uid);
148168
write_file("/proc/self/gid_map", "0 %d 1", gid);
149169

150-
if (setgroups(0, NULL) != 0) {
151-
die("setgroups failed: %s\n",
152-
strerror(errno));
153-
}
154170
if (setgid(0) != 0) {
155171
die ("setgid(0) failed %s\n",
156172
strerror(errno));

0 commit comments

Comments
 (0)