Linux / OS Fundamentals Series

🛡️ A Top 1% Hands-On for Reproducing the SUID Bit's Danger Yourself and Confirming Least-Privilege Defense via Capabilities

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the twentieth article in the Linux Infrastructure Series. You can follow along with one of the Ubuntu Server environments set up in the Hands-On Prep Manual.

Prerequisite Knowledge

Getting the Big Picture

This hands-on covers two stages.

graph TB
    subgraph Step1["Steps 1-2: The SUID Bit's Danger"]
        A1["Set the SUID bit on a binary<br/>that can escape to a shell"]
        A2["An ordinary user gets a root shell<br/>merely by running it"]
        A1 --> A2
    end
    subgraph Step2["Steps 3-4: Defense via Capabilities"]
        B1["Remove SUID, grant only<br/>a specific capability"]
        B2["Only the needed operation becomes<br/>possible, not all of root's power"]
        B1 --> B2
    end
    Step1 --> Step2

Hands-On Steps

Step 1: Set the SUID Bit on a Binary That Can Escape to a Shell

For testing, create an ordinary user and prepare a demo binary.

sudo useradd -m testuser
sudo cp /usr/bin/find /usr/local/bin/find-suid-demo
sudo chown root:root /usr/local/bin/find-suid-demo
sudo chmod u+s /usr/local/bin/find-suid-demo
ls -l /usr/local/bin/find-suid-demo

Output (relevant part):

-rwsr-xr-x 1 root root ... /usr/local/bin/find-suid-demo

The s in the permission display shows the SUID bit is set. The find command, per GNU findutils’ specification, has a feature — the -exec option — that can run any arbitrary command.

Step 2: Obtain a Root Shell as an Ordinary User

su - testuser
/usr/local/bin/find-suid-demo . -maxdepth 0 -exec /bin/sh -p \; -quit
whoami

Output:

root

testuser, an ordinary user, just obtained a root-privileged shell. This is the result of abusing the mechanism where a binary with the SUID bit set runs with the privileges of the file’s owner (root, here), not the privileges of the user who executed it. The find command itself is a harmless, ordinary command, but this concretely demonstrates how dangerous it is to casually set the SUID bit on a binary that has the ability to run arbitrary commands.

Why a Command Like `find` Falls Into This Problem

SUID is considered dangerous whenever a binary’s own functionality includes a means of invoking a shell or an arbitrary command. find’s -exec, vim’s :!, less’s !, and many other seemingly harmless standard commands actually have this exact property. The judgment call “I just want this one operation to run with root privileges, so I’ll casually set SUID” leads directly to this kind of broad privilege seizure, unintended by the binary’s own author.

Step 3: Remove SUID and Grant Only the Minimum Necessary Privilege via Linux Capabilities

First, disable the SUID binary created in Step 1.

exit
sudo chmod u-s /usr/local/bin/find-suid-demo

Next, configure Capabilities on a separate demo program that only needs the privilege to bind to a specific network port.

cat << 'EOF' > /tmp/bind_demo.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 80))
print("Bound to port 80 successfully")
EOF
sudo cp /usr/bin/python3 /usr/local/bin/python3-cap-demo
sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/python3-cap-demo
getcap /usr/local/bin/python3-cap-demo

Output:

/usr/local/bin/python3-cap-demo cap_net_bind_service=ep

Step 4: Confirm Only the Privileged Operation Is Allowed, as an Ordinary User

su - testuser
/usr/local/bin/python3-cap-demo /tmp/bind_demo.py
whoami

Output:

Bound to port 80 successfully
testuser

Still as the ordinary user testuser, you only succeeded at binding to a port below 1024, something that normally requires root privileges. whoami’s result also still shows testuser, not root. By granting just one specific privilege, cap_net_bind_service, you confirmed that the needed operation alone could be achieved, without handing over the entirety of root’s power.

What a Pro Sees Here (Top 1% Understanding)

Never Choose SUID Just Because “root Privilege Is Needed”

The biggest lesson from this hands-on is that casually choosing the SUID bit, just because “this operation needs root privilege,” ends up letting every other unrelated feature of that binary also run with root privilege. Linux Capabilities split root’s power into meaningful units — like cap_net_bind_service (binding to a privileged port) and cap_net_raw (using a raw socket) — letting you grant only the privilege you actually need, individually. This shares the same underlying idea as the principle of least privilege covered in AWS’s IAM Policy Evaluation Logic: “carve out and hand over only what’s actually needed, rather than all-or-nothing privilege” — a way of thinking that runs through privilege design in general. A top-1% engineer avoids the judgment call “there’s not enough privilege, so just run it as root for now,” and always asks instead, concretely, “what privilege is actually needed here?”

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. You want to check whether an unintended SUID binary exists on a server: find / -perm -4000 -type f 2>/dev/null lists every file with the SUID bit set.
  2. A binary you configured Capabilities on gets a permission error: Check with getcap whether the intended capability is actually configured correctly. Note that recopying a binary loses its configured capabilities.
  3. Removing SUID broke an existing operation: Reconfirm whether that operation genuinely needed SUID, and consider switching to a narrower-scoped alternative, like Capabilities.

Summary

Takeaways to Apply Today

  1. Whenever you run into an operation that needs root privilege, first consider whether Capabilities, rather than SUID, could achieve it.
  2. Include checking for unintended SUID binaries as part of a server’s regular audit.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).