Skip to main content
Sudo Rules, SUID/SGID Binaries & Linux Capabilities
Lesson 4 of 12

Sudo Rules, SUID/SGID Binaries & Linux Capabilities

Learn how Linux delegates elevated privileges through sudoers rules, SUID/SGID bits, and kernel capabilities, how to audit each mechanism safely in a lab, and how to harden misconfigurations.

Intermediate 30 min Linux & Windows
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Explain the difference between real and effective user IDs during privilege transitions
  • Audit sudoers rules with sudo -l to identify unsafe commands, wildcards, and environment preservation
  • Enumerate SUID and SGID binaries with find and distinguish standard OS tools from risky custom files
  • Inspect Linux file capabilities with getcap and explain why powerful capabilities grant root-equivalent access
  • Apply least-privilege hardening to sudoers, file permissions, and systemd services

Recommended first: Linux Fundamentals Recap: ACLs, PAM & SSH HardeningLinux Processes, systemd Sandboxing & auditd LogsLinux Hardening: Kernel Sysctl, AppArmor & SELinux

#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
Privilege transition via SUID or sudoPrivilege transition via SUID or sudoUserKernelexecve("/usr/bin/passwd", ...)Check SUID bit (owned by root)Process keeps caller RUID, gains EUID=0Runs passwd with permission to update /etc/shadow

#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:

Checking sudo permissions on a lab host
$ 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/find

Here, 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 as root.
  • Wildcards and env_keep: unvalidated wildcards allow path traversal or flag injection, while preserving LD_PRELOAD with SETENV enables 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:

Enumerating SUID and SGID files in a lab
$ find / -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null
/usr/bin/sudo
/usr/bin/passwd
/usr/local/bin/diag-tool

Focus 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:

Inspecting file capabilities with getcap
$ getcap -r / 2>/dev/null
/usr/bin/ping cap_net_raw=ep
/usr/local/bin/python3.11 cap_setuid=ep

The =ep suffix marks the capability as effective and permitted. High-risk capabilities on shared binaries break isolation:

  • CAP_SETUID: lets a process call setuid(0) and become root; on python3 or tar it equals full SUID root.
  • CAP_DAC_OVERRIDE: bypasses file read, write, and execute permission checks.
  • CAP_DAC_READ_SEARCH: bypasses read and directory search checks, exposing /etc/shadow or 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 apply NOEXEC when a tool should never spawn child processes.
  • Replace custom SUID tools: Use dedicated systemd services (User=, NoNewPrivileges=yes) or polkit instead of SUID wrappers.
  • Avoid setcap on shared tools: Never assign capabilities to interpreters or editors; scope privileges to a service unit via AmbientCapabilities=.
  • Track drift: Watch /etc/sudoers and chmod/setxattr events with auditd.

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 -l for NOPASSWD, wildcards, preserved environment variables, and shell-escaping tools.
  • Enumerate SUID/SGID files with find and capabilities with getcap -r /, focusing on custom binaries and CAP_SETUID/CAP_DAC_OVERRIDE.
  • Enforce least privilege with strict sudoers rules, group memberships, and sandboxed systemd units.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge