Skip to content

Commit 12724d1

Browse files
committed
runaslist_matches: restore ability of user to only specify a group
The removal of the special case in 8c1559e and the subsequent reworking of runaslist_matches() broke support for "sudo -g group" and "sudo -u myname -g group" when the user portion of the Runas_Spec is non-empty and the specified group matches the Runas_Spec. Thanks to Marc Schoolderman of the sudo-rs project for reporting this.
1 parent eb778da commit 12724d1

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

plugins/sudoers/match.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ runas_userlist_matches(const struct sudoers_parse_tree *parse_tree,
223223
*/
224224
static int
225225
runas_grouplist_matches(const struct sudoers_parse_tree *parse_tree,
226-
const struct member_list *group_list)
226+
const struct member_list *group_list, int user_matched)
227227
{
228228
const struct sudoers_context *ctx = parse_tree->ctx;
229229
int group_matched = UNSPEC;
@@ -241,7 +241,7 @@ runas_grouplist_matches(const struct sudoers_parse_tree *parse_tree,
241241
a = alias_get(parse_tree, m->name, RUNASALIAS);
242242
if (a != NULL) {
243243
const int rc = runas_grouplist_matches(parse_tree,
244-
&a->members);
244+
&a->members, user_matched);
245245
if (SPECIFIED(rc)) {
246246
if (m->negated) {
247247
group_matched = rc == ALLOW ? DENY : ALLOW;
@@ -260,11 +260,11 @@ runas_grouplist_matches(const struct sudoers_parse_tree *parse_tree,
260260
}
261261
}
262262
}
263-
if (!SPECIFIED(group_matched)) {
263+
if (!SPECIFIED(group_matched) && user_matched == ALLOW) {
264264
struct gid_list *runas_groups;
265265
/*
266266
* The runas group was not explicitly allowed by sudoers.
267-
* Check whether it is one of the target user's groups.
267+
* If the runas user matched, check its group list too.
268268
*/
269269
if (ctx->runas.pw->pw_gid == ctx->runas.gr->gr_gid) {
270270
group_matched = ALLOW; /* runas group matches passwd db */
@@ -312,7 +312,16 @@ runaslist_matches(const struct sudoers_parse_tree *parse_tree,
312312

313313
user_matched = runas_userlist_matches(parse_tree, user_list);
314314
if (ISSET(ctx->settings.flags, RUNAS_GROUP_SPECIFIED)) {
315-
group_matched = runas_grouplist_matches(parse_tree, group_list);
315+
group_matched = runas_grouplist_matches(parse_tree, group_list,
316+
user_matched);
317+
/*
318+
* Allow "sudo -g group" or "sudo -u myname -g group"
319+
* if the runas group matches.
320+
*/
321+
if (group_matched == ALLOW && user_matched == UNSPEC) {
322+
if (strcmp(ctx->user.name, ctx->runas.pw->pw_name) == 0)
323+
user_matched = ALLOW;
324+
}
316325
}
317326

318327
if (user_matched == DENY || group_matched == DENY)

0 commit comments

Comments
 (0)