Sub topic 20
File protection and permissions
This chapter assumes you know what a file, a directory and an inode are, and that the kernel runs on behalf of users and processes. It builds on the directory structures chapter. By the end you should be able to evaluate Unix permission bits, compute umask results, and compare access control lists with capability lists. These are the forms GATE CS questions usually take.
Protection versus security
Protection is the set of mechanisms that control which processes or users may access which resources, such as files, memory segments and devices. Security is the wider problem of defending the system against outside attackers and misuse. File protection answers one question: given a subject (a user or process) and an object (a file), which operations are allowed?
The usual operations to control are read, write, execute, append, delete and list (for directories).
The access matrix model
The general model is an access matrix. Rows are domains (usually users or processes), columns are objects, and each cell holds the rights the domain has on that object.
| Domain | file1 | file2 | printer |
|---|---|---|---|
| alice | read, write | read | |
| bob | read | read, write | none |
| carol | none | execute |
A real system never stores this matrix whole, because it is large and mostly empty. It stores it in one of two ways:
- Access control list (ACL): store the matrix by column. Each object keeps a list of (domain, rights) pairs. Checking and revoking rights on one file is easy. Finding everything one user can reach is slow.
- Capability list: store the matrix by row. Each domain keeps a list of unforgeable tokens, one per object it may use. Answering what a process can do is easy. Revoking a right on one object is hard, because the tokens are spread across many domains.
Unix permission bits
Unix compresses the ACL into 9 bits stored in the inode: read (r), write (w) and execute (x) for three classes. The classes are owner (user), group and others. Each triple is one octal digit: r=4, w=2, x=1.
So rwxr-xr-- is 111 101 100, which is octal 754.
The kernel picks exactly one class, in this order:
- If the caller's user ID equals the file's owner, use the owner bits.
- Otherwise, if the file's group is among the caller's groups, use the group bits.
- Otherwise use the others bits.
Only the first matching class is checked. If the owner has --- and others have rwx, the owner is still denied. This is a common exam trap.
Permissions on directories
The bits mean something different on a directory:
- r: list the names in the directory.
- w: create, delete or rename entries in it. Deleting a file needs
won the directory, not on the file. - x: traverse the directory, meaning use it in a path to reach something inside. Without
xon every directory in the path, you cannot open a file there even if the file itself is world-readable.
Default permissions and umask
New files start from a base mode and have the bits of the umask removed. The base is 666 for files and 777 for directories. Programs do not make new files executable by default. The rule is mode = base AND (NOT umask).
With umask 022, files get 644 and directories get 755.
A permission checker in Python
This code implements the class-selection rule and the umask rule.
R, W, X = 4, 2, 1
def mode_to_string(mode):
out = ""
for shift in (6, 3, 0):
bits = (mode >> shift) & 7
out += "r" if bits & R else "-"
out += "w" if bits & W else "-"
out += "x" if bits & X else "-"
return out
def can_access(mode, file_uid, file_gid, uid, gids, want):
if uid == file_uid:
bits = (mode >> 6) & 7
elif file_gid in gids:
bits = (mode >> 3) & 7
else:
bits = mode & 7
return (bits & want) == want
def apply_umask(base, umask):
return base & ~umask & 0o777
print(mode_to_string(0o754)) # rwxr-xr--
print(can_access(0o640, 1000, 50, 1001, {50}, W)) # False
print(can_access(0o640, 1000, 50, 1001, {50}, R)) # True
print(can_access(0o604, 1000, 50, 1000, {50}, R)) # True
print(oct(apply_umask(0o666, 0o022))) # 0o644
print(oct(apply_umask(0o777, 0o027))) # 0o750The fourth check shows the trap: mode 604 gives others read access, yet the owner's bits are 6, so the owner can still read. Try mode 064 with the owner as caller. Access is denied even though group and others have rights.
Special bits
Three more bits sit above the nine, forming a fourth octal digit:
| Bit | Octal | Effect on a file | Effect on a directory |
|---|---|---|---|
| setuid | 4000 | Program runs with the owner's user ID | Ignored on most systems |
| setgid | 2000 | Program runs with the file's group ID | New files inherit the directory's group |
| sticky | 1000 | Historically kept the program in memory | Only the file's owner (or the directory's owner) may delete or rename entries |
The sticky bit on /tmp lets everyone create files there without letting them delete each other's files.
ACLs beyond the nine bits
Nine bits cannot say "alice and bob may write, carol may read". POSIX ACLs add named user and named group entries to a file. When any ACL exists, the group bits in the mode become a mask. The mask limits the maximum rights of all named entries and the owning group. Windows NTFS goes further, using an ordered list of allow and deny entries per file. Exam questions that compare the two usually expect these points:
- Unix mode bits are compact, with a fixed three classes and a fixed 9-bit cost per file.
- ACLs are flexible but take variable space and more work per access check.
- Both authorize at
opentime. The kernel records the granted access in the open-file table entry, so laterreadandwritecalls do not recheck the file's permissions.
Common errors
- Assuming a user falls through to a more permissive class when the owner or group bits deny access.
- Forgetting that removing a file needs
wandxon the parent directory. - Subtracting the umask arithmetically instead of masking bits. For
umask 027and base666, the result is640. Subtraction happens to agree here but gives wrong answers for something likeumask 033on a file:666 - 033 = 633, while the correct bitwise result is644.
Exercise
A file has mode 640, owner alice, group staff. Bob belongs to staff. Carol belongs to no relevant group. Which of these succeed: Bob reads, Bob writes, Carol reads?
Show answer
Bob reads succeeds (group bits are r--). Bob writes fails (group has no w). Carol reads fails (others bits are ---).
Exercise
With umask 027, what mode does a newly created regular file get, and what mode does a new directory get?
Show answer
A file gets 666 AND NOT 027 = 640 (rw-r-----). A directory gets 777 AND NOT 027 = 750 (rwxr-x---).
Exercise
Why is revoking one user's access to one file easy with an ACL but awkward with capability lists?
Show answer
An ACL is stored with the file, so the system edits one list. Capabilities are held by the domains, so the system must find and invalidate every token for that file across all domains.