Skip to main content
Bash and Linux

Audit Risky Permissions

mediumUsers and permissions

Problem statement

Use find to list files and folders with risky permissions: anything every user can write to, and programs with the SUID or SGID bit. These are standard checks in a security audit, because a world-writable script can be changed by anyone, and a careless SUID program can hand out root.

The script builds this tree, with some risky modes planted on purpose:

app tree

TEXT
app/config.yml 644 normal config
app/upload.sh 777 anyone can change it
app/logs/ 777 anyone can write, no sticky bit
app/tmp/ 1777 anyone can write, sticky bit on
bin/backup-tool 4755 SUID
bin/report 2755 SGID
bin/normal 755 normal program

Print four lists, each line as mode path, sorted by path:

  1. World-writable files.
  2. World-writable folders that do not have the sticky bit.
  3. Folders that have the sticky bit.
  4. Programs with SUID or SGID.

Expected output:

TEXT
== world-writable files ==
777 ./app/upload.sh
== world-writable folders without the sticky bit ==
777 ./app/logs
== folders with the sticky bit (like /tmp) ==
1777 ./app/tmp
== SUID or SGID programs ==
4755 ./bin/backup-tool
2755 ./bin/report

Hints

Hint 1: find -perm -o+w matches anything where others have write, whatever the other bits are. Add -type f or -type d to pick files or folders.

Approach

Optimal: find -perm

Covers: find -type, find -perm -mode, find -perm /mode, !, -exec stat {} +, the sticky bit, SUID, SGID, 2>/dev/null, -xdev.

Three special bits sit in front of the normal mode. You already know rwx for owner, group and others. A fourth digit, written in front, holds three special bits:

Digit Bit Shows in ls -l as Means
4 SUID (set user ID) s in the owner's x spot: -rwsr-xr-x the program runs as its owner, not as you
2 SGID (set group ID) s in the group's x spot: -rwxr-sr-x the program runs with the file's group; on a folder, new files inherit its group
1 sticky t in the others' x spot: drwxrwxrwt in a folder, only a file's owner can delete or rename it

So 4755 is SUID plus 755, and 1777 is sticky plus 777.

Why each one can be risky.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart LR subgraph RISK["Fix these"] direction TB A["upload.sh 777
anyone can edit the code"]:::red B2["logs/ 777
anyone can delete files"]:::red A ~~~ B2 end subgraph CHECK["Check these"] direction TB D["backup-tool 4755
runs as its owner"]:::yellow C["tmp/ 1777
sticky, this is fine"]:::green D ~~~ C end RISK ~~~ CHECK classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style RISK fill:transparent,stroke:#dc2626,stroke-width:2px style CHECK fill:transparent,stroke:#d97706,stroke-width:2px
  • World-writable file: anyone on the machine can change it. If it is a script that root or a cron job runs, anyone can make root run their code.
  • World-writable folder without sticky: anyone can delete or replace files that other users put there.
  • World-writable folder with sticky: this is normal and safe for shared scratch space. /tmp is the famous example.
  • SUID or SGID program: it runs with someone else's power. That is needed for a few tools, but every one is a place where a bug can give a user root.

SUID in one picture. /usr/bin/passwd is owned by root and has SUID. When asha runs it, it runs as root, which is the only way it can update the protected password file. The program itself makes sure she can only change her own password.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB U["asha runs passwd"]:::blue --> P{{"passwd has SUID root"}}:::purple P --> R["it runs as root"]:::yellow R --> S[("password file
updated")]:::green classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px

How find -perm matches. find walks a folder tree and tests every entry. The -perm test has three forms, and the sign in front changes the meaning:

Test Matches when Example
-perm 644 the mode is exactly 644 rarely what you want
-perm -o+w or -perm -002 all the listed bits are on (others may have more) world-writable
-perm /6000 any one of the listed bits is on SUID or SGID

Tests written one after another must all be true, like an AND. ! flips the next test. So -type d -perm -o+w ! -perm -1000 reads as "a folder, writable by others, and not sticky".

Walking through the code. The # Setup: lines only build the sample tree and set the modes, so skip past them.

  1. -type f -perm -o+w finds files that others can write.
  2. -type d -perm -o+w ! -perm -1000 finds writable folders that lack the sticky bit.
  3. -type d -perm -1000 finds sticky folders, to show what "safe shared" looks like.
  4. -type f -perm /6000 finds SUID or SGID files.

Each line ends with -exec stat -c '%a %n' {} +. That runs stat on the found paths, printing the mode and the name. The {} is replaced by the paths, and + passes many paths to one stat call instead of one call per file. sort -k2 sorts by the second column, the path, because find returns entries in disk order.

Edge cases. If nothing matches, find prints nothing and stat is never run, so an empty list means "clean". On a real server, search from / with 2>/dev/null to hide "Permission denied" for folders you cannot read, and add -xdev to stay on one disk and skip /proc and network mounts.

# Setup: a small app tree with some risky permissions planted on purpose
cd "$(mktemp -d)"
mkdir -p app/logs app/tmp bin
printf 'name: shop\n' > app/config.yml;   chmod 644  app/config.yml
printf 'echo upload\n' > app/upload.sh;   chmod 777  app/upload.sh
chmod 777  app/logs                       # anyone can write, no sticky bit
chmod 1777 app/tmp                        # anyone can write, sticky bit on (like /tmp)
printf 'tool\n' > bin/backup-tool;        chmod 4755 bin/backup-tool   # SUID
printf 'tool\n' > bin/report;             chmod 2755 bin/report        # SGID
printf 'tool\n' > bin/normal;             chmod 755  bin/normal

echo "== world-writable files =="
find . -type f -perm -o+w -exec stat -c '%a %n' {} + | sort -k2

echo "== world-writable folders without the sticky bit =="
find . -type d -perm -o+w ! -perm -1000 -exec stat -c '%a %n' {} + | sort -k2

echo "== folders with the sticky bit (like /tmp) =="
find . -type d -perm -1000 -exec stat -c '%a %n' {} + | sort -k2

echo "== SUID or SGID programs =="
find . -type f -perm /6000 -exec stat -c '%a %n' {} + | sort -k2

Interview follow-ups

  • Turn this into a daily check that only reports new findings.

    Write today's results to a sorted file, for example find / -xdev -type f -perm /6000 2>/dev/null | sort > /var/lib/audit/suid.today. Keep yesterday's list as suid.known. comm -13 suid.known suid.today prints only the lines that are new today. Mail or log those lines, and if they are expected, copy today's file over the known list. Run it from cron as root so it can read every folder.

Frequently asked questions

With a minus, every bit you list must be on: -perm -6000 only matches files that have both SUID and SGID, which is rare. With a slash, any one bit is enough: -perm /6000 matches SUID, SGID or both, which is what an audit wants. With no sign, the mode must match exactly, so -perm 777 misses a file with 1777. Old guides use +6000 for "any bit"; GNU find removed that form, so use /.

Run find / -xdev -type f -perm /6000 2>/dev/null. -xdev keeps find on the disk where it started, so it skips /proc, /sys and network shares, which can be huge or slow. Repeat the command for other real disks if you have them, like /home or /data. 2>/dev/null throws away "Permission denied" lines, so you only see results. Running it with sudo lets it look inside every folder.

A stock system has a short list of SUID root programs that need it: passwd, sudo, su, mount, umount, ping on some systems, and a few others. The exact list depends on the distribution. The useful habit is to save the list on a fresh server and compare later; anything new is worth a question. A SUID copy of a shell, or a SUID file in a home folder or /tmp, is a strong sign of a break-in.