setuid, setgid and the sticky bit

Video 115 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:46, with English and Hindi audio.

Besides read, write and execute, a file can carry three special bits. They change who a program runs as, which group new files get, and who may delete a file in a shared folder. You will see them in every listing of a Linux server, often on the password program and on the temporary folder. This article explains each bit, shows how to spot it in a listing, and why each one exists.

What you will learn

Three special bits and what each one does

Each of these bits appears in the same letter row as read, write and execute, so one listing shows them all together.

Spotting the bits in a listing

Show the mode and owner of the passwd program
stat -c '%A %U %G %n' /usr/bin/passwd
Show the mode and owner of the temporary folder
stat -c '%A %U %G %n' /tmp

In the letters, the setuid bit shows as a small s where the owner's execute letter would be. The temporary folder has the sticky bit, shown as a t at the very end. An s in the group's execute place means setgid on a program, which runs it with the file's group.

Why the passwd program needs setuid

The passwd program lets any user change their own password. To do that, it must write to the shadow file, which only root can write. So the program carries the setuid bit, and it runs with root's rights while it does that one job.

Note: Setuid programs are powerful. A bug in one gives an attacker the owner's rights, so keep such programs up to date and few in number.

The sticky bit in a shared folder

Imagine alice saves a draft into a shared folder. Everyone may add files there, so this is allowed. When bob tries to delete the draft, the sticky bit refuses, because the file is not his. Alice can delete her own draft without any trouble.

The sticky bit does not stop anyone from adding files. It only stops people from removing files that belong to someone else.

Reviewing setuid files on a server

A find search with the right test can list every setuid file on a machine. Review that list when you audit a server, and ask whether each program still needs the bit.

Try it yourself

You need a Linux machine, such as an Ubuntu server, and only read access. Compare the letters of two listings, and find the special bit in each.

  1. Show the mode and owner of the passwd program. Look for an s in the owner's execute place.

    stat -c '%A %U %G %n' /usr/bin/passwd

    What you should see: The shell prints -rwsr-xr-x root root /usr/bin/passwd, where the s marks setuid.

  2. Show the mode and owner of the temporary folder. Look for a t at the very end of the letters.

    stat -c '%A %U %G %n' /tmp

    What you should see: The shell prints drwxrwxrwt root root /tmp, where the t marks the sticky bit.

Terms

setuid
A file bit that makes a program run with the rights of the file's owner, not of the person who started it.
Sticky bit
A folder bit that lets only a file's owner delete it, even when everyone may add files to the folder.

Check yourself

Why does the passwd program carry the setuid bit?

It must write to the shadow file, which only root can write. Setuid lets it run with the owner's rights for that job.

What does the sticky bit allow in a shared folder?

Anyone may add files, but only the owner of a file may delete it.

Where does the s of setuid appear in a listing?

In the owner's execute place, where the x would normally be.

Previous: umask: why new files get their rights. Next: sudo and the sudoers file.