Skip to content

Lesson 7 of 14 · 20m

Permissions, setuid, and sudo

Nine mode bits plus setuid explain sudo itself, why 777 is a shell waiting to happen, and why `/etc/sudoers` is only touched through `visudo`.

chmod 777 is the answer juniors reach for, and it "works" the way leaving your house unlocked works. -rwxrwxrwx on a file means every local user can write executable content into it; on a webroot, that's a shell anyone on the box can deliver through the web server. The permission model isn't arbitrary ceremony — nine bits decide who may read, write, and enter, and learning them once ends a whole class of confused afternoons.

The bits

Three triples — owner, group, other — each rwx worth 4+2+1:

  • chmod 644 file — owner writes, everyone reads. Default for files.
  • chmod 755 dir — owner writes, everyone enters and lists. Default for directories and scripts.
  • chmod 600 file — owner only. Private keys, secrets.
  • chmod u+x, chmod go-w — symbolic form when you're adjusting, not setting.

Directories get the interesting semantics: r lists entries, w creates and deletes entries, x lets you traverse — open a file whose name you already know. A 711 directory denies ls but permits cat dir/known-file. Write permission on a directory — not on the file — controls deletion, which is why rm can remove a file you can't edit.

umask sets the bits new files don't get: umask 022 produces 644 files and 755 dirs. When a new file comes out 664, the editor didn't do it — the umask did.

Ownership and groups

chown user:group file, chgrp, and id to see your groups. usermod -aG docker user adds a supplementary group — the -a is load-bearing. usermod -G docker user replaces the entire supplementary list, the classic way to remove yourself from sudo/wheel while trying to add docker.

chmod g+s on a directory means new files inherit the directory's group — the shared-workdir trick that keeps a team's files group-writable without anyone running chgrp by hand.

setuid and the sticky bit

ls -l "$(which sudo)" shows -rwsr-xr-x. 1 root root. That s — the setuid bit — is the whole mechanism behind sudo: the binary runs as its owner (root) no matter who invokes it. It's also attack surface: CVE-2021-3156, "Baron Samedit" (Qualys, January 2021), was a heap overflow in sudo's argument parsing, present since 2011, turning any local user into root. find / -perm -4000 -type f lists every setuid binary on the box — run it on a system you maintain and explain each line.

/tmp is drwxrwxrwt: the t (sticky bit) means anyone can create files there but only the owner can delete their own. Without it, any user could delete another's temp files.

sudo

sudo reads /etc/sudoers to decide what you're allowed to run — and that file is edited only through visudo, which syntax-checks on save. A broken sudoers locks every sudoer out; that's the footgun visudo was invented for. The line %sudo ALL=(ALL:ALL) ALL means: group sudo, any host, as any user and group.

  • sudo -l — what you may run.
  • sudo -i — root login shell.
  • sudo -u postgres psql — run as a non-root user; the underused flag that makes service accounts workable.
  • sudoedit file — edit a root-owned file with your editor via a temp copy; the answer that's not chmod 666.
  • sudo -k — drop the cached credentials early (the timestamp_timeout default is 5 minutes upstream; Debian ships 15).
  • NOPASSWD: in sudoers removes the prompt for automation — and removes the only thing standing between a compromised service account and root. Know what it costs.

Practice

  • Least permission that works: 644/755/600, group access before world access, a shared group before 777.
  • visudo for sudoers, sudo -u for service accounts, sudoedit for root files.
  • Audit find / -perm -4000 -type f once per box; everything there is root code reachable by a local user.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.