SECURITY ENGINEERING · PART 1 · LINUX AND WINDOWS, FROM THE INSIDE · LINUX · 9 OF 229

Linux Permissions, SUID and sudo: A Security View

How Linux decides who can do what, read as an attacker and a defender: the nine permission bits and their numbers, ownership, the SUID bit that makes passwd and sudo run as root, why a loose SUID program is a privilege-escalation path, world-writable files, and sudo configured for least privilege instead of full root.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Bits and the owner-group-other check
  • SUID runs as owner
  • sudo least privilege
  • audit SUID and sudoers

Notes

The bits

Every file has an owner, a group and nine permission bits: read, write and execute, for the owner, the group and everyone else. Each set is a number. Read is four, write two, execute one, so read plus write is six. Mode six four zero means the owner can read and write, the group can read, and others get nothing. Six zero zero keeps a file to its owner alone, which is what you want for anything holding a secret.

-rw-r----- explained
-  rw-  r--  ---
   owner group other
r=4  w=2  x=1
rw-=6  r--=4  ---=0
640 = owner rw, group r,
      others nothing
600 = owner only
$ chmod 600 secret.txt

The check

When you open a file the kernel checks in a fixed order and stops at the first match. Are you the owner? It uses the owner bits and stops. Otherwise, are you in the file's group? It uses the group bits. Otherwise the others bits. Because it stops early, a strange mode like six zero four lets everyone else read a file while its own group cannot, which surprises people and occasionally leaks data. Root skips all of this and can read anything.

How the kernel decides
  • are you the owner? use the owner bits, stop
  • else in the group? use the group bits, stop
  • else use the others bits
  • so a file 604 lets *others* read even if the group cannot: order matters

What attackers look for

Once an attacker has any shell, the first thing they do is look for permission mistakes. A config file that holds a database password and is readable by everyone. A private key left at six four four instead of six zero zero. A script that a root service runs on a schedule but that any user can edit, so they put their own commands in it. One find command lists every world-writable file. As a defender, you run that command first, and fix what it shows.

First things an attacker greps
  • world-readable config with passwords or API keys
  • private keys readable by all (should be 600)
  • world-writable scripts a service runs as root
  • a writable cron job or systemd unit
  • find / -perm -0002 -type f

The SUID bit

Now the dangerous part. The SUID bit, shown as an s where the owner's execute bit would be, makes a program run as its owner rather than as whoever started it. passwd is owned by root and SUID, so when you change your password, the program runs as root and can edit the shadow file, which you cannot touch directly. sudo, su and a few others work the same way. SUID is useful and necessary, and it is also the single most abused feature in Linux privilege escalation.

SUID: the s in the owner slot
$ ls -l /usr/bin/passwd
-rwsr-xr-x root root
         ^ s = SUID
you run it, it runs as root
that is how you change your
  own password in a
  root-only file
sudo, su, ping: also SUID

Why SUID is risky

Here is the pattern, without a live demo. If a program is SUID root and can be made to run another command, open a shell, or write to any file, then a normal user can borrow its root powers and become root. Attackers keep a catalogue of standard tools that can be tricked this way when they are SUID. The usual cause is an engineer who set SUID on a custom script to avoid a permissions headache. Defenders list every SUID file with one find command, and remove the bit from anything that does not strictly need it.

The escalation pattern
  • a SUID-root program that can run another command, open a shell, or write any file = instant root
  • classic: a custom tool left SUID "to make it easy"
  • defenders audit the list: find / -perm -4000 -type f
  • remove SUID you do not need

sudo

sudo lets a user run a command as root, and records it. The right way is least privilege: grant one exact command, not full root. Here the deploy user may restart one service and nothing else. The wrong way is to grant a broad command, or a program with a shell escape, or all commands. Each of those quietly becomes full root for that user. And read the logs: every sudo use is written to the auth log, which is both an audit trail and an early warning.

sudo, done well
# /etc/sudoers.d/deploy
deploy ALL=(root) NOPASSWD: \
  /usr/bin/systemctl restart
  shoprung

one exact command, nothing
  else
every use is logged to
  /var/log/auth.log

sudo mistakes

The traps in sudo are rules that look narrow but are not. Allowing an editor, because many editors can launch a shell. Allowing a language interpreter, because it runs any code. Allowing an archive tool with a run-a-command option. Or a wildcard matching more than intended. Each hands over full root. A public reference, GTFOBins, lists which binaries become root when granted through sudo or SUID; defenders check their sudoers against it.

These all mean "root"
  • ALL=(ALL) ALL everything
  • NOPASSWD on an editor, find, a scripting language, or tar/zip with a run option
  • NOPASSWD on vim: :!sh
  • a wildcard that matches more than you think
  • grant the exact binary and arguments, read GTFOBins

Defender checklist

A short routine closes most of this. Keep secrets and private keys at six zero zero or six four zero. Remove others' write access from scripts and configs, and audit for world-writable files. List every SUID and SGID file and strip the bit from anything that does not need it. Write sudo rules for exact commands, never editors or interpreters. Mount scratch filesystems like slash tmp so programs there cannot run or be SUID, and read the auth log.

Harden it
  • secrets and keys at 600, 640
  • no world-writable files or scripts; audit -perm -0002
  • list SUID/SGID, remove what is not needed
  • sudo rules: exact commands, no editors or interpreters
  • mount /tmp noexec,nosuid
  • read auth.log for sudo use

Common mistakes

Mistakes. Running chmod seven seven seven to make an error go away, which makes the file writable by everyone. Setting SUID on a custom script to dodge a permissions issue. Leaving secrets world-readable. Granting full sudo for convenience. Running services as root when a dedicated low-privilege user would do. Never auditing the SUID list, so a planted SUID shell sits unnoticed. And never reading the auth log, where every escalation attempt is recorded.

Avoid these
  • chmod 777 to "fix" a problem
  • SUID on a custom script
  • secrets world-readable
  • sudo ALL=(ALL) for convenience
  • services running as root
  • ignoring the SUID inventory
  • never reading auth.log

Say it like this

In the interview: Linux privilege escalation usually comes from permission mistakes. A SUID root binary that can run another command. A sudo rule that grants an editor, an interpreter or an archive tool, each of which can spawn a shell. A world-writable file or script that a root process trusts. I defend by auditing the SUID list and the sudoers rules against references like GTFOBins, keeping least privilege, running services as their own users, and mounting scratch directories without SUID or execute.

In the interview
  • "Through permission mistakes: a SUID-root binary that can
  • run a command, a sudo rule
  • granting an editor or interpreter, a world-writable
  • file a root process uses. I
  • audit SUID and sudoers, keep
  • least privilege, and mount
  • scratch dirs nosuid."

Interview questions

Q1What do the Linux permission numbers mean?

Three digits for owner, group and others. Each is read (4) plus write (2) plus execute (1). So 640 is owner read-write, group read, others none; 600 is owner only; 755 is a typical mode for programs and directories.

Q2In what order does Linux check file permissions?

Owner first, then group, then others, stopping at the first that applies. If you are the owner only the owner bits are used, even if the group or others have more access. Root bypasses the checks entirely.

Q3What is the SUID bit and why does it matter for security?

SUID makes a program run with the privileges of its owner rather than the user who ran it. SUID-root binaries like passwd and sudo need it, but a SUID program that can run commands or write files becomes a privilege-escalation path to root.

Q4How do you find risky SUID files?

List them with find / -perm -4000 -type f (SGID uses -2000). Compare against references like GTFOBins, which show which common binaries can be abused when SUID, and remove the bit from anything that does not need it.

Q5What makes a sudo rule dangerous?

Granting ALL commands, or a specific program that can escape to a shell, such as an editor, a scripting interpreter, or an archive tool with a run option. Each effectively grants full root. Grant exact binaries and arguments instead.

Q6Why are world-writable files a problem?

If a file or script writable by everyone is executed or trusted by a privileged process (a root cron job, a service), any user can edit it to run their own commands with that privilege. Audit with find / -perm -0002 -type f.

Q7How should secrets and private keys be permissioned?

Restrict them to the owner, typically 600, or 640 when a specific group must read them, owned by the right account. SSH refuses to use a private key that is readable by group or others.

Q8How does mounting /tmp with nosuid and noexec help?

noexec stops programs from being run out of the directory and nosuid makes the kernel ignore the SUID bit there, so an attacker who can only write to scratch space cannot drop and run a SUID shell from it.