Skip to content

Commit e2a2982

Browse files
committed
When listing another user, only apply NOPASSWD for "ALL" or "list"
For "sudo -U otheruser -l", we only want to apply the NOPASSWD tag if the matching command in sudoers is "ALL" or "list". Those are the only commands in sudoers that permit the use of the -U option. Thanks to Marc Schoolderman of the sudo-rs project for reporting this.
1 parent 4630f49 commit e2a2982

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

plugins/sudoers/lookup.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2004-2005, 2007-2024 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2004-2005, 2007-2025 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above
@@ -128,14 +128,6 @@ sudoers_lookup_pseudo(struct sudo_nss_list *snl, struct sudoers_context *ctx,
128128
int date_match = UNSPEC;
129129
int runas_match = UNSPEC;
130130

131-
if (pwcheck == any) {
132-
if (cs->tags.nopasswd == true || priv_nopass == true)
133-
nopass = true;
134-
} else if (pwcheck == all) {
135-
if (cs->tags.nopasswd != true && priv_nopass != true)
136-
nopass = false;
137-
}
138-
139131
if (cs->notbefore != UNSPEC) {
140132
date_match = now < cs->notbefore ? DENY : ALLOW;
141133
}
@@ -184,6 +176,22 @@ sudoers_lookup_pseudo(struct sudo_nss_list *snl, struct sudoers_context *ctx,
184176
break;
185177
}
186178
}
179+
180+
/*
181+
* Apply the NOPASSWD tag if the entry matched.
182+
* This is relevant for "sudo -U otheruser -l".
183+
*/
184+
if (cmnd_match == ALLOW && runas_match == ALLOW &&
185+
date_match != DENY) {
186+
if (pwcheck == any) {
187+
if (cs->tags.nopasswd == true || priv_nopass == true)
188+
nopass = true;
189+
} else if (pwcheck == all) {
190+
if (cs->tags.nopasswd != true && priv_nopass != true)
191+
nopass = false;
192+
}
193+
}
194+
187195
if (callback != NULL) {
188196
callback(nss->parse_tree, us, user_match, priv,
189197
host_match, cs, date_match, runas_match,

plugins/sudoers/regress/testsudoers/test31.out.ok

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,44 @@ ALL = NOPASSWD: list, !list
7474

7575
Command denied
7676

77+
'sudo -U root -l' with a matching ALL=ALL rule and non-matching NOPASSWD
78+
Parses OK
79+
80+
Entries for user admin:
81+
82+
ALL = PASSWD: ALL
83+
host allowed
84+
runas allowed
85+
cmnd allowed
86+
87+
ALL = NOPASSWD: /bin/ls
88+
host allowed
89+
runas allowed
90+
cmnd unmatched
91+
92+
Password required
93+
94+
Command allowed
95+
96+
'sudo -U root -l' with a matching list rule and non-matching NOPASSWD
97+
Parses OK
98+
99+
Entries for user admin:
100+
101+
ALL = PASSWD: list
102+
host allowed
103+
runas allowed
104+
cmnd allowed
105+
106+
ALL = NOPASSWD: /bin/ls
107+
host allowed
108+
runas allowed
109+
cmnd unmatched
110+
111+
Password required
112+
113+
Command allowed
114+
77115
'sudo -l command' with a matching command
78116
Parses OK
79117

plugins/sudoers/regress/testsudoers/test31.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ $TESTSUDOERS -p ${TESTDIR}/passwd -P ${TESTDIR}/group -L root admin <<'EOF'
4444
admin ALL = NOPASSWD: list, !list
4545
EOF
4646

47+
echo ""
48+
echo "'sudo -U root -l' with a matching ALL=ALL rule and non-matching NOPASSWD"
49+
$TESTSUDOERS -p ${TESTDIR}/passwd -P ${TESTDIR}/group -L root admin <<'EOF'
50+
admin ALL = PASSWD: ALL
51+
admin ALL = NOPASSWD: /bin/ls
52+
EOF
53+
54+
echo ""
55+
echo "'sudo -U root -l' with a matching list rule and non-matching NOPASSWD"
56+
$TESTSUDOERS -p ${TESTDIR}/passwd -P ${TESTDIR}/group -L root admin <<'EOF'
57+
admin ALL = PASSWD: list
58+
admin ALL = NOPASSWD: /bin/ls
59+
EOF
60+
4761
echo ""
4862
echo "'sudo -l command' with a matching command"
4963
$TESTSUDOERS -p ${TESTDIR}/passwd -P ${TESTDIR}/group -l admin /bin/ls <<'EOF'

0 commit comments

Comments
 (0)