#Why this matters
Linux isolates normal accounts from root (UID 0) so that a compromised service cannot read /etc/shadow or alter system binaries. Real workloads still need bounded elevated actions, such as changing passwords, sending ICMP probes, or restarting a daemon.
To bridge that gap, Linux delegates privilege through sudo rules, SUID/SGID binaries, and file capabilities. When any of those grants more power than intended, a local user can step outside the intended task and gain administrative control. Auditing these three mechanisms lets you spot and close unintended escalation paths.
#How Linux delegates privilege
A child process inherits its parent's Real UID (RUID) and Effective UID (EUID). The kernel checks the EUID and capability sets for permission decisions. Privilege delegation raises the EUID or adds specific capabilities for one program:
| Mechanism | Where configured | Granularity | Typical misconfiguration |
|---|---|---|---|
sudo |
/etc/sudoers, /etc/sudoers.d/ |
Per user, target, command | NOPASSWD on editors, pagers, or wildcards |
| SUID / SGID | File mode (4755 / 2755) |
Entire binary runs as owner/group | Custom wrappers calling relative paths |
| Capabilities | Extended attributes (setcap) |
Specific kernel privilege bits | cap_setuid or cap_dac_override on interpreters |
#Auditing sudo rules
The sudo utility reads /etc/sudoers and /etc/sudoers.d/ to determine what a user can run. Always start by checking allowed commands with sudo -l:
$ id
uid=1001(student) gid=1001(student) groups=1001(student)
$ sudo -l
Matching Defaults entries for student on lab.local:
env_reset, mail_badpass, secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
User student may run the following commands on lab.local:
(root) NOPASSWD: /usr/bin/findHere, env_reset and secure_path sanitize environment variables, yet student can run /usr/bin/find as root with NOPASSWD and unrestricted arguments. Many Unix utilities break containment when granted via sudo:
- Shell-escaping tools (
find,vim,less,man,awk,tar,git): built-in flags or pager commands can launch subcommands under the target EUID. - Interpreters and package tools (
python3,perl,env,pip,apt): inline execution flags run arbitrary commands asroot. - Wildcards and
env_keep: unvalidated wildcards allow path traversal or flag injection, while preservingLD_PRELOADwithSETENVenables shared-library injection.
In a lab, an auditor verifies impact non-destructively with find . -maxdepth 0 -exec id \; to confirm euid=0(root) without altering system state.
#Finding and evaluating SUID and SGID binaries
An SUID (4000) executable displays s in the owner execute bit (-rwsr-xr-x), while SGID (2000) shows s in the group execute bit (-rwxr-sr-x). Legitimate OS utilities such as /usr/bin/passwd and /usr/bin/sudo require SUID root. Enumerate these files with find:
$ find / -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null
/usr/bin/sudo
/usr/bin/passwd
/usr/local/bin/diag-toolFocus on custom paths like /usr/local/bin or /opt. Modern Linux kernels ignore SUID/SGID bits on interpreted #! scripts, so compiled C/C++ wrappers are the main risk: if one calls system("ps aux") without a full path or loads user-writable config files, a caller can hijack execution.
#Inspecting Linux capabilities
Capabilities split traditional root authority into more than 40 discrete privileges so a binary holds only what it requires. File capabilities live in extended attributes and are inspected with getcap:
$ getcap -r / 2>/dev/null
/usr/bin/ping cap_net_raw=ep
/usr/local/bin/python3.11 cap_setuid=epThe =ep suffix marks the capability as effective and permitted. High-risk capabilities on shared binaries break isolation:
CAP_SETUID: lets a process callsetuid(0)and becomeroot; onpython3ortarit equals full SUIDroot.CAP_DAC_OVERRIDE: bypasses file read, write, and execute permission checks.CAP_DAC_READ_SEARCH: bypasses read and directory search checks, exposing/etc/shadowor private keys.CAP_CHOWN: allows changing ownership of arbitrary files.
#Defending and common mistakes
- Edit sudoers with
visudo: Syntax checks prevent lockouts. Require exact full paths and arguments, and applyNOEXECwhen a tool should never spawn child processes. - Replace custom SUID tools: Use dedicated
systemdservices (User=,NoNewPrivileges=yes) orpolkitinstead of SUID wrappers. - Avoid
setcapon shared tools: Never assign capabilities to interpreters or editors; scope privileges to a service unit viaAmbientCapabilities=. - Track drift: Watch
/etc/sudoersandchmod/setxattrevents withauditd.
Common mistakes include leaving cap_setuid on binaries after debugging, trusting wildcards in sudo rules, and assuming env_reset stops built-in shell escapes.
#Summary
- Linux evaluates EUID and capabilities for access checks;
sudo, SUID/SGID, and file capabilities elevate those privileges. - Audit
sudo -lforNOPASSWD, wildcards, preserved environment variables, and shell-escaping tools. - Enumerate SUID/SGID files with
findand capabilities withgetcap -r /, focusing on custom binaries andCAP_SETUID/CAP_DAC_OVERRIDE. - Enforce least privilege with strict
sudoersrules, group memberships, and sandboxedsystemdunits.