A worked example
A small team runs one app. ops may restart it without a password, read its logs and copy a build into place; everyone in %sudo is an admin:
Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
root ALL=(ALL:ALL) ALL
ops ALL=(root) NOPASSWD: /usr/bin/systemctl restart app
%sudo ALL=(ALL:ALL) ALL
ops ALL=(root) NOPASSWD: /usr/bin/less /var/log/app/*
ops ALL=(root) NOPASSWD: /bin/cp /srv/app/build/* /opt/app/
Each ops line looks narrow. node check.mjs sudoers says otherwise:
sudoers:7: [high] shell-escape-command
ops may run /usr/bin/less /var/log/app/* as root without a password. A pager or man page viewer starts a shell with !sh and opens other files with :e, whatever the arguments.
fix: For logs, give read access through a group (adm, systemd-journal) instead of sudo; otherwise add NOEXEC: and set SYSTEMD_PAGER= or --no-pager in a wrapper.
sudoers:8: [high] writes-any-file
ops may run /bin/cp /srv/app/build/* /opt/app/ as root without a password with arguments matched by a wildcard: cp can then overwrite /etc/sudoers, /etc/passwd or a root-owned script, which becomes root access.
fix: Pin the exact arguments, or use a root-owned wrapper script that writes only the one file.
sudoers:5: [low] nopasswd-overridden
NOPASSWD on /usr/bin/systemctl restart app for ops does not take effect if ops is in %sudo: sudoers:6 (%sudo ALL=(ALL:ALL) ALL) also matches and comes later, and sudo uses the last matching rule, which asks for a password.
fix: Move this rule below sudoers:6, or into a file in /etc/sudoers.d, which sudo reads where the @includedir line is (by default the last line of /etc/sudoers).
who can become root:
root: any command (ALL), with a password (sudoers:4)
%sudo: any command (ALL), with a password (sudoers:6)
ops: /usr/bin/less /var/log/app/* (shell-escape-command), no password (sudoers:7)
ops: /bin/cp /srv/app/build/* /opt/app/ (writes-any-file), no password (sudoers:8)
3 findings: 2 high, 1 low. (1 file read)
Line 7: in less, typing !sh opens a root shell, and the wildcard is not needed for that. Line 8: sudo matches arguments as one string, so * also matches ../../../tmp/sudoers -t /etc; GNU cp takes the -t option after its operands and copies the file over /etc/sudoers. Line 5 is the reverse problem: if ops is in %sudo, line 6 matches the same command later, and sudo applies the last match, so the password prompt ops was meant to skip still appears.
Rules that are root in disguise
- nopasswd-all:
NOPASSWD: ALLas root means anything that runs as that account (a leaked SSH key, a web app bug, a malicious package in a build) is root without a prompt. Cloud images add one for the default user on purpose; it is a trade-off worth revisiting on a long-lived or shared machine. - all-users-full-root:
ALL ALL=(ALL) ALLgives every account root with its own password. Some distributions ship it together withDefaults targetpw, which asks for the target's (root's) password instead; withtargetpw,rootpworrunaspwset, the finding is low. - negated-command-bypass: "Limitations of the '!' operator": subtracting commands from
ALLis not effective, because the user can copy the program to another name or start it from a shell or editor. TreatALL, !/usr/bin/suas full root. - shell-escape-command: editors (
vi,vim,nano,emacs,ed) write any file and start shells; pagers and viewers (less,more,man,journalctl) run!sh; interactive clients (psql,mysql,sqlite3,ftp,gdb,screen,tmux) have their own escape. Pinned arguments do not help. TheNOEXECtag ("Preventing shell escapes") blocks the shell for dynamically linked programs, so a pager drops to medium, but an editor can still save over/etc/sudoersand stays high. Usesudoeditto edit files: it runs the editor as the user on a copy. - runs-any-command: shells,
env,nice,xargsand similar run their arguments; interpreters (python3,perl,node,awk) run code; andfind(-exec),tar(--checkpoint-action),systemctl(edit, a unit with anyExecStart),docker(run -v /:/host),git,rsync,apt-get,pipand others run commands through options. With no arguments in the rule, or a wildcard, the user picks them. With pinned arguments (systemctl restart nginx)sudoallows only that exact command line. - writes-any-file:
tee,cp,mv,dd,chown,chmod,ln,curl,wgetand similar, with open arguments, can overwrite/etc/sudoers,/etc/passwdor a script root runs. Account tools count too:passwdas root resets another account's password, andusermodorgpasswdcan put an account in the sudo group.suwith open arguments switches to any account the pattern allows, without its password, which is reported as runs-any-command. - writable-command-path: a program or script under
/home,/tmp,/var/tmp,/dev/shm,/run/user,/var/wwwor/srvis usually writable by the user or the web app; whoever can edit it decides what runs as root. Paths are normalised first, so/usr/local/../../tmp/xcounts. A digest (sha256:... /home/op/run) makessudorefuse an edited file, so that case is low; the manual's own example uses one for exactly this reason. - directory-or-glob-command: a command ending in
/allows every file in that directory, and a pattern such as/usr/sbin/*every match, including any shell or interpreter there now or later. - setenv-tag: "SETENV and NOSETENV": variables set on the command line skip
env_check,env_deleteandenv_keep, soLD_PRELOAD=/tmp/x.so sudo backuploads the user's library as root.ALLimpliesSETENV, which is one more reasonALLis full root.
Defaults that widen every rule
- env-reset-off:
!env_resetkeeps the caller's environment except whatenv_deleteremoves, and the manual notes that no such list can block every dangerous variable. - env-keep-dangerous:
env_keepofLD_PRELOAD,LD_LIBRARY_PATH,PYTHONPATH,PERL5OPT,NODE_OPTIONS,BASH_ENVand similar lets the caller load their own code into any command.PATHis medium, and only reported withoutsecure_path, which replaces it.EDITOR,PAGERandHOMEare low. - authenticate-off:
!authenticateworks likeNOPASSWDon every rule it covers (aPASSWD:tag still wins). - defaults-setenv:
Defaults setenvisSETENVon every command. - credential-cache-unbounded: a negative
timestamp_timeoutkeeps a password entry valid until reboot (medium); over an hour is low. - credential-cache-shared:
!tty_ticketsortimestamp_type=globalshares one cached entry across all the user's terminals and background jobs. - use-pty-off: without a separate pseudo-terminal, a program run with
sudocan inject input into the user's terminal;use_ptyis on by default from sudo 1.9.14. - visiblepw-on:
sudowill ask for a password even where it cannot hide it. - pwfeedback-on: shows the password's length as it is typed; in sudo before 1.8.31 it also enabled CVE-2019-18634.
- secure-path-missing: reported only when the main
sudoersfile is pasted. Debian and Ubuntu setsecure_path; without it,sudo foosearches the caller'sPATH.
Rules that do not do what they say
- nopasswd-overridden: "where there are multiple matches, the last match is used (which is not necessarily the most specific match)". A
NOPASSWDrule above a group rule grantingALLwith a password does nothing for members of that group. The checker cannot see group membership, so it says "if alice is in %sudo". - wildcard-arguments: "Wildcards in command arguments": arguments are matched as one string, so
*matches spaces, extra options and../. A regular-expression argument (^...$, sudo 1.9.10+) can match exactly what you mean.sudoeditpaths are the exception: there a wildcard does not match/. - reads-any-file:
cat,head,grepand similar with open arguments read/etc/shadowand every key on the machine. - includedir-file-ignored: files in an
@includedirdirectory whose names contain a.or end in~are skipped, "to avoid causing problems with package manager or editor temporary/backup files". A drop-in nameddeploy.confnever applies. Files are also read in lexical order, so1_whoopscomes after10_second.
A rule whose target is not root ((postgres), (www-data)) drops one severity level: it hands out that account, not the machine. A rule with only a group, (:adm), runs as the user and is not reported.
How the parser was checked
sudo ships cvtsudoers, which converts a sudoers file to JSON with its own parser. The checker's reading of 41 grammar corner cases (tag and runas inheritance, : Host = groups, escaped commas, digests, option specs, nested and negated aliases, continuation lines, quoted names, user IDs) matches cvtsudoers -e -f json from sudo 1.9.15p5 on every one; the comparison runs in the tool's test suite. It also matches on a whole real file, the manual's own EXAMPLES section (49 rule entries). The comparison found two bugs in the checker before release: LOG_INPUT and MAIL tags were not cleared by their opposites, and a sha224: digest inside a Cmnd_Alias was read as the start of another alias. One more case is left out because that cvtsudoers prints CWD= as JSON that does not parse.
What the checker cannot see
It reads files, so it does not know group memberships, who can write to a program a rule names, your sudo version, rules held in LDAP or SSSD, or files you did not paste (an @include that matches nothing is reported as a note). Its list of programs with shell escapes is not complete. A clean scan means these patterns were not found, not that the system is secure. Run visudo -c as well, and keep a root shell open while you change sudoers.