Skip to content

Commit 76dfc87

Browse files
committed
newgidmap: enforce setgroups=deny if self-mapping a group
This is necessary to match the kernel-side policy of "self-mapping in a user namespace is fine, but you cannot drop groups" -- a policy that was created in order to stop user namespaces from allowing trivial privilege escalation by dropping supplementary groups that were "blacklisted" from certain paths. This is the simplest fix for the underlying issue, and effectively makes it so that unless a user has a valid mapping set in /etc/subgid (which only administrators can modify) -- and they are currently trying to use that mapping -- then /proc/$pid/setgroups will be set to deny. This workaround is only partial, because ideally it should be possible to set an "allow_setgroups" or "deny_setgroups" flag in /etc/subgid to allow administrators to further restrict newgidmap(1). We also don't write anything in the "allow" case because "allow" is the default, and users may have already written "deny" even if they technically are allowed to use setgroups. And we don't write anything if the setgroups policy is already "deny". Ref: https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/1729357 Fixes: CVE-2018-7169 Reported-by: Craig Furman <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]>
1 parent c0f0c67 commit 76dfc87

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

src/newgidmap.c

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,37 @@
4646
*/
4747
const char *Prog;
4848

49-
static bool verify_range(struct passwd *pw, struct map_range *range)
49+
50+
static bool verify_range(struct passwd *pw, struct map_range *range, bool *allow_setgroups)
5051
{
5152
/* An empty range is invalid */
5253
if (range->count == 0)
5354
return false;
5455

55-
/* Test /etc/subgid */
56-
if (have_sub_gids(pw->pw_name, range->lower, range->count))
56+
/* Test /etc/subgid. If the mapping is valid then we allow setgroups. */
57+
if (have_sub_gids(pw->pw_name, range->lower, range->count)) {
58+
*allow_setgroups = true;
5759
return true;
60+
}
5861

59-
/* Allow a process to map its own gid */
60-
if ((range->count == 1) && (pw->pw_gid == range->lower))
62+
/* Allow a process to map its own gid. */
63+
if ((range->count == 1) && (pw->pw_gid == range->lower)) {
64+
/* noop -- if setgroups is enabled already we won't disable it. */
6165
return true;
66+
}
6267

6368
return false;
6469
}
6570

6671
static void verify_ranges(struct passwd *pw, int ranges,
67-
struct map_range *mappings)
72+
struct map_range *mappings, bool *allow_setgroups)
6873
{
6974
struct map_range *mapping;
7075
int idx;
7176

7277
mapping = mappings;
7378
for (idx = 0; idx < ranges; idx++, mapping++) {
74-
if (!verify_range(pw, mapping)) {
79+
if (!verify_range(pw, mapping, allow_setgroups)) {
7580
fprintf(stderr, _( "%s: gid range [%lu-%lu) -> [%lu-%lu) not allowed\n"),
7681
Prog,
7782
mapping->upper,
@@ -89,6 +94,69 @@ static void usage(void)
8994
exit(EXIT_FAILURE);
9095
}
9196

97+
void write_setgroups(int proc_dir_fd, bool allow_setgroups)
98+
{
99+
int setgroups_fd;
100+
char *policy, policy_buffer[4096];
101+
102+
/*
103+
* Default is "deny", and any "allow" will out-rank a "deny". We don't
104+
* forcefully write an "allow" here because the process we are writing
105+
* mappings for may have already set themselves to "deny" (and "allow"
106+
* is the default anyway). So allow_setgroups == true is a noop.
107+
*/
108+
policy = "deny\n";
109+
if (allow_setgroups)
110+
return;
111+
112+
setgroups_fd = openat(proc_dir_fd, "setgroups", O_RDWR|O_CLOEXEC);
113+
if (setgroups_fd < 0) {
114+
/*
115+
* If it's an ENOENT then we are on too old a kernel for the setgroups
116+
* code to exist. Emit a warning and bail on this.
117+
*/
118+
if (ENOENT == errno) {
119+
fprintf(stderr, _("%s: kernel doesn't support setgroups restrictions\n"), Prog);
120+
goto out;
121+
}
122+
fprintf(stderr, _("%s: couldn't open process setgroups: %s\n"),
123+
Prog,
124+
strerror(errno));
125+
exit(EXIT_FAILURE);
126+
}
127+
128+
/* Check whether the policy is already what we want. /proc/self/setgroups
129+
* is write-once, so attempting to write after it's already written to will
130+
* fail.
131+
*/
132+
if (read(setgroups_fd, policy_buffer, sizeof(policy_buffer)) < 0) {
133+
fprintf(stderr, _("%s: failed to read setgroups: %s\n"),
134+
Prog,
135+
strerror(errno));
136+
exit(EXIT_FAILURE);
137+
}
138+
if (!strncmp(policy_buffer, policy, strlen(policy)))
139+
goto out;
140+
141+
/* Write the policy. */
142+
if (lseek(setgroups_fd, 0, SEEK_SET) < 0) {
143+
fprintf(stderr, _("%s: failed to seek setgroups: %s\n"),
144+
Prog,
145+
strerror(errno));
146+
exit(EXIT_FAILURE);
147+
}
148+
if (dprintf(setgroups_fd, "%s", policy) < 0) {
149+
fprintf(stderr, _("%s: failed to setgroups %s policy: %s\n"),
150+
Prog,
151+
policy,
152+
strerror(errno));
153+
exit(EXIT_FAILURE);
154+
}
155+
156+
out:
157+
close(setgroups_fd);
158+
}
159+
92160
/*
93161
* newgidmap - Set the gid_map for the specified process
94162
*/
@@ -103,6 +171,7 @@ int main(int argc, char **argv)
103171
struct stat st;
104172
struct passwd *pw;
105173
int written;
174+
bool allow_setgroups = false;
106175

107176
Prog = Basename (argv[0]);
108177

@@ -145,7 +214,7 @@ int main(int argc, char **argv)
145214
(unsigned long) getuid ()));
146215
return EXIT_FAILURE;
147216
}
148-
217+
149218
/* Get the effective uid and effective gid of the target process */
150219
if (fstat(proc_dir_fd, &st) < 0) {
151220
fprintf(stderr, _("%s: Could not stat directory for target %u\n"),
@@ -177,8 +246,9 @@ int main(int argc, char **argv)
177246
if (!mappings)
178247
usage();
179248

180-
verify_ranges(pw, ranges, mappings);
249+
verify_ranges(pw, ranges, mappings, &allow_setgroups);
181250

251+
write_setgroups(proc_dir_fd, allow_setgroups);
182252
write_mapping(proc_dir_fd, ranges, mappings, "gid_map");
183253
sub_gid_close();
184254

0 commit comments

Comments
 (0)