Skip to content

Commit f275a08

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). Ref: https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/1729357 Reported-by: Craig Furman <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 3f1f999 commit f275a08

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ a lot of mail...
4444

4545
Adam Rudnicki <[email protected]>
4646
Alan Curry <[email protected]>
47+
Aleksa Sarai <[email protected]>
4748
Alexander O. Yuriev <[email protected]>
4849
Algis Rudys <[email protected]>
4950
Andreas Jaeger <[email protected]>

src/newgidmap.c

Lines changed: 46 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,36 @@ static void usage(void)
8994
exit(EXIT_FAILURE);
9095
}
9196

97+
void write_setgroups(int proc_dir_fd, unsigned int allow_setgroups)
98+
{
99+
int setgroups_fd = openat(proc_dir_fd, "setgroups", O_WRONLY);
100+
if (setgroups_fd < 0) {
101+
/*
102+
* If it's an ENOENT then we are on too old a kernel for the setgroups
103+
* code to exist. Emit a warning and bail on this.
104+
*/
105+
if (ENOENT == errno) {
106+
fprintf(stderr, _("%s: kernel doesn't support setgroups restrictions\n"), Prog);
107+
return;
108+
}
109+
fprintf(stderr, _("%s: couldn't open process setgroups: %m\n"), Prog);
110+
exit(EXIT_FAILURE);
111+
}
112+
113+
/* Default is deny, and any allow will out-rank a deny. */
114+
char *policy = "deny";
115+
if (allow_setgroups)
116+
policy = "allow";
117+
118+
/* Write to setgroups. */
119+
if (dprintf(setgroups_fd, "%s\n", policy) < 0) {
120+
fprintf(stderr, _("%s: failed to set setgroups %s policy: %m"),
121+
Prog,
122+
policy);
123+
exit(EXIT_FAILURE);
124+
}
125+
}
126+
92127
/*
93128
* newgidmap - Set the gid_map for the specified process
94129
*/
@@ -103,6 +138,7 @@ int main(int argc, char **argv)
103138
struct stat st;
104139
struct passwd *pw;
105140
int written;
141+
bool allow_setgroups = false;
106142

107143
Prog = Basename (argv[0]);
108144

@@ -145,7 +181,7 @@ int main(int argc, char **argv)
145181
(unsigned long) getuid ()));
146182
return EXIT_FAILURE;
147183
}
148-
184+
149185
/* Get the effective uid and effective gid of the target process */
150186
if (fstat(proc_dir_fd, &st) < 0) {
151187
fprintf(stderr, _("%s: Could not stat directory for target %u\n"),
@@ -177,8 +213,9 @@ int main(int argc, char **argv)
177213
if (!mappings)
178214
usage();
179215

180-
verify_ranges(pw, ranges, mappings);
216+
verify_ranges(pw, ranges, mappings, &allow_setgroups);
181217

218+
write_setgroups(proc_dir_fd, allow_setgroups);
182219
write_mapping(proc_dir_fd, ranges, mappings, "gid_map");
183220
sub_gid_close();
184221

0 commit comments

Comments
 (0)