Three bits above the nine
The mode has room for three more bits above the nine permission bits. In octal they are written as a leading digit. The value 4 is setuid, 2 is setgid, and 1 is the sticky bit. A mode such as 4755 combines setuid with rwxr-xr-x. In ls -l the bits appear in place of an execute letter, as an s for setuid or setgid and as a t for the sticky bit. A capital S or T means the bit is set but the execute bit is not, which is usually a mistake.
Setuid on executable files
When a program file has the setuid bit set, the kernel gives the new process an effective UID equal to the owner of the file, instead of the UID of the user who started it. The real UID stays the same, so the program knows who started it. This is how an ordinary user can run passwd, which must write to a file that only root may change. The program runs as root for the duration of its work.
The effective UID is the identity the kernel uses for permission checks. For a setuid program, that identity is the owner of the file. For every other program, the effective UID equals the real UID. The setgid bit does the same job for the group. It sets the effective GID of the new process to the group of the file.
Setuid is powerful, and it is also a common source of security bugs. A setuid root program that trusts its input or its environment can be tricked into doing things its author never intended. That is why the set of setuid files on a system is kept small, and why you should never set the bit on a script that you wrote yourself.
Setgid on directories
On a directory, the setgid bit has a different effect. New files and subdirectories created inside it take the group of the directory, instead of the primary group of the creator. This is useful for a shared project, because every file lands in the project group without anyone having to run chgrp. New subdirectories also inherit the setgid bit, so the rule continues down the tree.
mkdir shared
chmod 2775 shared
ls -ld sharedThe mode 2775 gives the directory setgid plus full access for the owner and group, and read and execute for others. The ls -ld line shows an s in the group execute position. The lab in this unit checks the sticky bit, and a later unit covers the setgid bit on directories in more depth.
The sticky bit on directories
The sticky bit on a directory restricts deletion. A directory that is writable by everyone, such as /tmp, would otherwise let any user remove any file in it. With the sticky bit set, a user may remove or rename an entry only if the user owns the entry, owns the directory, or is root. The mode of /tmp is 1777, which is the sticky bit on top of full access for everyone.
mkdir drop
chmod 1777 drop
stat -c "%A %a %n" dropThe output shows drwxrwxrwt and the octal value 1777. The t in the last position marks the sticky bit. Setting the bit on a directory you own is a safe experiment, because it only changes who may delete entries inside it.
The sticky bit has a different meaning on regular files on some older systems. On Linux it has no effect on regular files, and you should treat it as a directory-only bit.
Finding special bits with find
The find command can search by mode. The expression -perm -4000 matches files that have every bit in 4000 set, which means setuid. The expression -perm -2000 matches setgid files. Using a minus sign means at least these bits, rather than exactly these bits.
find /usr/bin -perm -4000 -type f 2>/dev/null | headThe output varies from one system to another, and in the lab it depends on which programs are installed. The point of the search is to know which programs elevate their privilege, and to review them when you audit a machine. A short list is normal, and a long unexpected list deserves a closer look.
The sticky bit combines with the other bits in a predictable way. A directory with mode 1777 accepts new entries from anyone, and it protects the entries that others create. A directory with mode 1755 lets only its owner create entries, so the sticky bit adds nothing there. The bit by itself never grants new access. It only narrows who may delete or rename entries.
Setuid and setgid should be audited, not assumed. A program with the setuid bit that appears on a system without a deliberate install is worth reviewing. The find command in this unit gives you the list, and stat gives you the owner, so the two together show what a program is and who it runs as.
Copying a setuid program with cp does not keep the bit unless you ask for it with the preserve option. That default is a protection, because it stops an ordinary copy from silently creating a new privileged program. The bit has to be set on purpose, which is the behavior you want for any mechanism that grants elevated rights.
Hands-on: set and find the bits
Set the sticky bit on a directory you own, then search a directory of your own for files with setuid and setgid bits. The labs for this unit ask you to build exactly that setup, using files you create, and to record the list that find prints. The checks compare the recorded output with the modes you set, so you can see how find matches the bit.