sudoers Check · guide

sudoers mistakes explained: what each one hands out and the fix

Every rule on this page passes visudo -c. sudo is doing exactly what the file says; the file says something its author did not mean. This guide gives the sudo behaviour behind each finding the checker reports, so you can judge whether it applies to you. Behaviour is paraphrased from the sudoers(5) manual for sudo 1.9.15, with its section names in quotes. Which programs offer a shell escape is a long-known list; GTFOBins documents the exact escape for each.

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

Defaults that widen every rule

Rules that do not do what they say

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.

Back to the sudoers Check.