How to Run Restricted Files as a Non-Root User
CREATED: 2024 / 02 / 04 Sun
UPDATED: 2024 / 02 / 04 Sun
How to Run Restricted Files as a Non-Root User
While looking into how to run Docker containers without root, I dug into how Linux handles this. In Linux, non-root users can execute files they don’t have permission to run via the following methods:
- Set setuid or setgid on the executable
- Grant the necessary permissions using capabilities
setuid and setgid
setuid and setgid are features that allow non-privileged users to execute files. setuid permits a regular user to run a file with the owner’s privileges; setgid permits execution with the owning group’s privileges.
For example, checking the permissions of the ping command shows it is owned by the root user and root group. The owner has full permissions, while the group and others have only read and execute:
# Built a Ubuntu 22.04 environment on MacBook using lima
# Ubuntu template borrowed from:
# https://github.com/lima-vm/lima/blob/master/examples/ubuntu-lts.yaml
$ which ping
/usr/bin/ping
$ ls -la /usr/bin/ping
-rwxr-xr-x 1 root root 72344 Feb 5 2022 /usr/bin/ping
Copying this executable changes the owner and group:
$ cp /usr/bin/ping ./myping
$ ls -la ./
-rwxr-xr-x 1 user user 72344 Feb 1 12:00 myping
In this state, myping can actually be executed — but only because net.ipv4.ping_group_range includes the current user’s group ID.
Running the following command shows that users with group IDs from 0 to 2147483647 can execute ping without issue (this is the behavior from Ubuntu 20.04 onwards; older versions would prevent non-root execution):
$ id # Check current user's UID, GID, and groups
uid=501(user) gid=1000(user) groups=1000(user)
$ sudo sysctl net.ipv4.ping_group_range
net.ipv4.ping_group_range = 0 2147483647
So let’s temporarily disable this (since we want to talk about setuid and setgid).
Setting it to 1 0 will now properly prevent myping from executing:
$ sudo sysctl -w net.ipv4.ping_group_range="1 0"
net.ipv4.ping_group_range = 1 0
$ ./myping 8.8.8.8
# nothing...
To make this executable, we need setuid.
First, change the owner of the executable to root so that execution is delegated to the owner’s (root’s) privileges.
Then run chmod +s (setuid) to allow regular users to execute the file:
$ sudo chown root ./myping
$ sudo chmod +s ./myping
$ ls -la
-rwsr-sr-x 1 root user 72344 Feb 1 12:00 myping
$ ./myping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
...
And it runs successfully.
While setuid grants execution as the owner, setgid grants execution as a member of the owning group. The steps are similar to what we did above — I’ll skip the commands for setgid.
capabilities
While setuid and setgid are convenient, they can be vulnerable to privilege escalation attacks — a malicious program could exploit the temporary elevated privileges.
capabilities provide an alternative way to grant specific permissions to executables, allowing non-root users to run files without the risks of setuid or setgid.
capabilities are assigned to processes and files.
Regular users have no capabilities assigned, while root has ep — meaning all capabilities.
$ ps
PID TTY TIME CMD
3519 pts/0 00:00:00 bash
3989 pts/0 00:00:00 ps
$ getpcaps 3519
3519: = # regular user's capabilities
$ sudo bash
root@lima-ubuntu-lts:/home/user.linux# ps
PID TTY TIME CMD
4008 pts/2 00:00:00 sudo
4009 pts/2 00:00:00 bash
4017 pts/2 00:00:00 ps
root@lima-ubuntu-lts:/home/user.linux# getpcaps 4009
4009: =ep # root user's capabilities
Let’s restore myping to the regular user’s ownership and remove setuid:
$ sudo chmod -s ./myping
$ sudo chown user ./myping
$ ls -la
-rwxr-xr-x 1 user user 72344 Feb 1 12:00 myping
Now we’ll grant a capability to myping to make it executable.
Running ping requires permission to open a RAW socket, which regular users don’t have — causing the permission error.
So we’ll grant myping the CAP_NET_RAW capability for opening RAW sockets.
Note that granting capabilities requires the CAP_SETFCAP capability, which root already has, so we use sudo:
$ sudo setcap 'cap_net_raw+p' ./myping
$ ./myping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
...
myping now runs successfully.
Appendix
You can view the full list of capabilities with man capabilities.