Special permissions and ACL¶
Exercise special-permissions-and-acl-annex.pdf · PDF
Here is a riddle. The password hashes live in /etc/shadow, and a normal user cannot even read that file. Yet any user can change their own password with passwd. And changing a password means writing into /etc/shadow. So how does Pau write into a file he cannot open?
The three triplets of Linux permissions (owner, group, others; rwx) cannot explain it. And they cannot solve two real problems at Tramuntana Apps either. Files Pau creates in the dev folder belong to the wrong group, so Marta cannot edit them. And Joan wants Pau to look at the design folder for two weeks without joining the design team. Today you get the extra tools that fix all three.
What you will learn
- What the setuid bit does, and why
passwdneeds it - Use the setgid bit on a folder so new files belong to the department's group
- Use the sticky bit so people can only delete their own files in a common folder
- Recognise
s,Sandtinls -l, and write the fourth octal digit (4, 2, 1) - Give one extra user access with an ACL (
setfacl,getfacl), and read its mask - Why an ACL on a folder does not let Pau read the files already inside
The card on the door¶
We keep yesterday's picture all page. Every file and folder has a card on its door: who owns it, which group it belongs to, and three lines of rwx for owner, group and others. Today we add three special stamps to that card, and then extra lines for named people.
Everything happens in deb-lab, in the folders /srv/tramuntana/dev, design and admin you built in Linux permissions. Before every test as another user, run whoami; leave with exit.
setuid: borrowing the owner's hands¶
Look at the card of the passwd program:
There is an s where the owner's x should be.
The setuid bit (set user ID) makes a program run with the identity of its owner, not of the person who starts it.
passwd belongs to root. So when Pau runs it, the program runs as root and can write in /etc/shadow. Pau borrows root's hands, but only inside that one program, and the program only lets him change his own password. Riddle solved.
A little history¶
The idea came from Dennis Ritchie, co-creator of Unix and of the C language, at Bell Labs. He applied for a patent on 9 July 1973; it was granted on 16 January 1979 (US patent 4,135,240, "Protection of data file contents"). Bell Labs later placed it in the public domain. One bit, and it is still in every Linux and macOS machine.
Careful: setuid does not make you root
The program runs as root and does only what its code does. You get no root shell. That is also why a setuid program with a bug is dangerous: the bug runs with root's power. Administrators keep a list of them, and so will you.
Hunt them down:
On a minimal Debian 13 you get nine programs (a desktop with GNOME shows a few more), all owned by root: chfn, chsh, gpasswd, mount, newgrp, passwd, su, sudo, umount. You know three of them: passwd, su and sudo. Each one needs to touch something only root may touch.
Setuid on a script does nothing
Linux ignores setuid on scripts: a root-owned script with the bit set still runs as whoever starts it. Think why: a script is a text file read by an interpreter, and many tricks could make that interpreter run something else with root's power.
The fourth digit¶
There are three special bits. They form a fourth octal digit, written in front of the three you already know:
| Bit | Digit | Shows as | What it does |
|---|---|---|---|
| setuid | 4 | s in the owner's x: rwsr-xr-x |
On a program: run as the file's owner |
| setgid | 2 | s in the group's x: rwxrws--- |
On a folder: new files get the folder's group |
| sticky | 1 | t in the others' x: rwxrwxrwt |
On a folder: only a file's owner can delete it |
Ask for the four digits with stat:
4755 is setuid plus 755. 1777 is the sticky bit plus 777.
Small s, capital S¶
The special letter sits on top of the x, so it has to show both things at once:
| You see | It means |
|---|---|
s (small) |
Special bit and x are both set |
S (capital) |
Special bit set, but no x under it: almost always a mistake |
t / T |
The same for the sticky bit |
setgid: the stamp on new files¶
Yesterday's bug, as it stands now:
New files take the primary group of whoever creates them, and Pau's primary group is pau. Marta is in dev, not in pau, so for this file she counts as "others": read only. Nobody will remember to run chgrp dev on every file. Set the rule on the folder instead:
The setgid bit on a folder makes every file and subfolder created inside it belong to the folder's group.
On the card, setgid is a rubber stamp hanging by the door: every new file gets the department's stamp automatically. After the repair, Pau creates more files:
-rw-rw-r-- 1 pau dev 0 Oct 5 20:52 after.txt
-rw-rw-r-- 1 pau pau 0 Oct 5 20:52 before.txt
drwxrwsr-x 1 pau dev 0 Oct 5 20:52 sub
after.txt belongs to dev with rw- for the group: Marta can edit it now. The new subfolder sub carries the s too, so the rule spreads downwards. Give the other departments the same bit in one go:
Careful: setgid does not touch old files
Only files created from now on get the folder's group. before.txt still belongs to pau. Fix old files by hand with chgrp.
The sticky bit: only cross out your own lines¶
Tramuntana also wants a common folder where all twelve people can drop files. With 777, anyone can delete anyone's file. The folder /tmp has had the same problem for decades, and the same answer:
The sticky bit on a folder means that only the owner of a file (or root) can delete or rename it, even if everybody may write in the folder.
On the card, it is a rule written on the door: "you may only cross out the lines you wrote yourself". Build the common folder:
Marta creates m.txt in it. Pau tries to delete it:
pau$ rm -f /srv/tramuntana/shared/m.txt
rm: cannot remove '/srv/tramuntana/shared/m.txt': Operation not permitted
Switch the sticky bit off (sudo chmod 777) and Pau deletes it without complaint. Put it back with sudo chmod +t. Notice the message: Operation not permitted, not Permission denied. The sticky rule said no, not the rwx letters.
Sticky does not mean nobody can delete
The owner of each file can still delete it, and so can root. The bit only stops everybody else.
When three triplets are not enough: ACL¶
Joan, from design, writes to you: "Pau the intern is going to help us with the app icons for two weeks. He must be able to see our folder, but not change anything."
Try to solve it with the card you have. Put Pau in design? Then he can write. Open the folder to "others"? Then everybody reads. There is no fourth line on the card.
An ACL (access control list) adds extra lines to the card of a file or folder: one line per extra named user or named group.
The tools come in the package acl, which may not be installed yet:
Before the ACL, Pau gets ls: cannot open directory '/srv/tramuntana/design': Permission denied. Now add his line and read the whole card:
# file: srv/tramuntana/design
# owner: root
# group: design
# flags: -s-
user::rwx
user:pau:r-x
group::rwx
mask::rwx
other::---
| Line | Meaning |
|---|---|
user::rwx |
The owner (no name between the colons) |
user:pau:r-x |
The new line: user pau may list and enter |
group::rwx |
The owning group, design |
mask::rwx |
The ceiling for the extra lines (more below) |
other::--- |
Everybody else |
flags: -s- |
The setgid bit you set earlier |
And ls -l now shows a + at the end of the string:
The + says: "this card has extra lines; ask getfacl".
| Command | Meaning |
|---|---|
setfacl -m u:pau:rx dir |
Modify: add or change the line of user pau |
setfacl -m g:admin:r file |
The same for a group |
setfacl -x u:pau file |
Remove that one line |
setfacl -b file |
Remove all the extra lines |
setfacl -d -m u:pau:rx dir |
Default ACL: what new files inside will inherit |
getfacl file |
Show the whole card |
Careful: an ACL does not replace rwx
An ACL extends the permissions. Owner, group and others work exactly as before; the ACL only adds named users and groups, all limited by the mask.
Every file has its own card¶
Pau tests his new access:
pau$ ls /srv/tramuntana/design
logo.txt
pau$ cat /srv/tramuntana/design/logo.txt
cat: /srv/tramuntana/design/logo.txt: Permission denied
He sees the list but cannot open the file. Why? The folder's card lets him in, but logo.txt has its own card (-rw-rw---- joan design), and Pau is not on it. Add him there too:
Now cat prints logo. And touch p.txt in the folder still fails: he has no w.
Default ACL: lines for future files¶
Doing that for every new file is hopeless. A default ACL on the folder is copied onto every file created inside it:
Joan creates new.txt. Its card:
# owner: joan
# group: design
user::rw-
user:pau:r-x #effective:r--
group::rwx #effective:rw-
mask::rw-
other::---
Pau's line arrived by itself. But look at #effective:r--.
The mask: the ceiling¶
The mask is the ceiling for every extra ACL line and for the group: a line can never give more than the mask allows.
getfaclshows the real result as#effective:.
new.txt was created without x, so its mask is rw-. Pau's line says r-x, but under a ceiling of rw- only r-- is left. He can read every new file and change none: exactly what Joan asked for.
flowchart LR
L["<b>Pau's line</b><br>r-x"] --> M{"<b>Mask</b><br>rw-<br>the ceiling"}
M --> E["<b>Effective</b><br>r--<br>only what both allow"]
There is one trap. When a file has an ACL, the middle triplet of ls -l shows the mask, not the group. So a careless chmod g-rwx lowers the ceiling and switches off every extra line at once:
sudo chmod g-rwx /srv/tramuntana/design/logo.txt
sudo getfacl /srv/tramuntana/design/logo.txt # mask::--- and user:pau:r-- #effective:---
sudo setfacl -m m::rw /srv/tramuntana/design/logo.txt # repair: raise the ceiling again
Pau's line was still there; the ceiling had dropped to the floor.
Key takeaways¶
- setuid (4,
sin the owner's x) makes a program run as its owner: that is howpasswdwrites in/etc/shadow. Keep a list of setuid programs. - setgid (2,
sin the group's x) on a folder gives every new file the folder's group: the fix for the department folders. - The sticky bit (1,
tin the others' x) lets only a file's owner (or root) delete it: the rule of/tmpand of the common folder. - A capital
SorTmeans the special bit is set withoutx: almost always a mistake. - An ACL adds lines for named users and groups (
setfacl -m,getfacl); a+inls -ltells you it exists. Each file has its own card; a default ACL covers future files. - The mask is the ceiling of every extra line:
#effective:shows what is really allowed.
Quick check¶
1. /usr/bin/passwd is -rwsr-xr-x root root. When Pau runs it, the program runs as...
- root
- Pau
- the group root only
- nobody at all
The setuid bit (the s in the owner's triplet) makes the program run with the identity of its owner, here root. That is how Pau's password change can be written into /etc/shadow.
2. Which command makes new files in /srv/tramuntana/admin belong to the group admin?
-
chmod 2770 /srv/tramuntana/admin -
chmod 1770 /srv/tramuntana/admin -
chmod 4770 /srv/tramuntana/admin -
chown admin /srv/tramuntana/admin
The fourth digit 2 is setgid: on a folder, new files inherit the folder's group. 1 would be the sticky bit and 4 setuid; chown only changes the folder's owner, not its future files.
3. A folder shows drwxrwxrwt. Who can delete Marta's file inside it?
- Marta and root
- Everybody who can write in the folder
- Nobody, not even Marta
- Anyone in Marta's group
The t is the sticky bit: in that folder only the owner of a file, or root, can delete or rename it, even though everybody may write there.
4. ls -l shows -rw-rw----+. What does the + tell you?
- The file has an ACL
- The file is a link
- The sticky bit is set
- The file is setuid
The + means the card has extra lines: an ACL. ls -l does not show them; getfacl does.
5. getfacl shows user:pau:r-x and mask::r--. What can Pau really do?
- Only read
- Read and run
- Read and write
- Nothing at all
The mask is the ceiling of every extra line. Pau's r-x limited by r-- leaves only r--, which getfacl would show as #effective:r--.
6. You run chmod 4644 script and ls -l shows -rwSr--r--. What does the capital S mean?
- The setuid bit is set but the owner has no
x - The setuid bit and the owner's
xare both set - The file has been given a sticky bit
- The file now belongs to the group
S
The special letter sits on top of the x. A small s means both are set; a capital S means the special bit is there without x, which is almost always a mistake.
7. You give Pau setfacl -m u:pau:rx on the design folder. He can list it, but cat logo.txt says Permission denied. Pick the fix.
- Add an ACL line for Pau on
logo.txtitself - Add the sticky bit to the design folder
- Give the design folder the mode
777 - Run
chmod g+son the design folder
The folder's ACL only lets Pau see the list. Each file has its own card, so logo.txt needs its own line: setfacl -m u:pau:r logo.txt. A default ACL on the folder would cover files created later.
You met one more setuid program today without noticing: sudo. Next: how to hand out root's power in small pieces, one command at a time, with sudo and least privilege.