CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 3 of 7 · 5 min read

The permission bits

Think of a club with three notices on the door: one for the owner, one for members and one for visitors. A person reads only the notice that fits them, the first that applies, and ignores the others. The analogy stops in one place: a person can read all notices, but the kernel stops at the first class that matches.

In plain words: A file has a mode of nine permission bits in three groups of three. The classes are owner (the user), group and other. In each class the bits are read (4), write (2) and execute (1). rwxr-x--- is 111 101 000, which is 750 in octal. The kernel's rule has one tricky point. It does not add the classes together. It picks one class: if the process's user ID equals the file's owner, the owner bits apply; otherwise, if one of its groups equals the file's group, the group bits apply; otherwise the other bits apply. Only the chosen class is looked at.

A worked example. Take a file owned by user 65534, group 65534, with mode 0407, which is r-----rwx. The owner has read only, the group has nothing, and others have all three. The owner is the first match, so the owner gets only read, although everyone else can read, write and execute. The program below asks the kernel with access after really becoming the user, and compares with the rule.

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <grp.h>
#include <unistd.h>

/* The Unix rule: pick ONE class (owner, then group, then others) and use only its bits. */
static int expect(const struct stat *st, uid_t u, gid_t g, int want) {   /* want: 4 r, 2 w, 1 x */
    int bits = (u == st->st_uid) ? (st->st_mode >> 6) & 7
             : (g == st->st_gid) ? (st->st_mode >> 3) & 7
             :                     st->st_mode & 7;
    return (bits & want) == want;
}

int main(int argc, char **argv) {
    if (argc < 4) return 1;
    uid_t u = (uid_t)atoi(argv[2]); gid_t g = (gid_t)atoi(argv[3]);
    struct stat st; if (stat(argv[1], &st)) { perror("stat"); return 1; }
    printf("%s: owner %u, group %u, mode %04o; asking as uid %u gid %u\n",
           argv[1], st.st_uid, st.st_gid, st.st_mode & 07777, u, g);
    int wants[3] = {4, 2, 1}; const char *names[3] = {"read", "write", "execute"};
    for (int i = 0; i < 3; i++) {
        pid_t p = fork();
        if (p == 0) {                                        /* child: really become that user */
            if (setgroups(0, NULL) || setgid(g) || setuid(u)) _exit(2);
            _exit(access(argv[1], i == 0 ? R_OK : i == 1 ? W_OK : X_OK) == 0 ? 0 : 1);
        }
        int status; waitpid(p, &status, 0);
        int real = WIFEXITED(status) && WEXITSTATUS(status) == 0;
        int model = expect(&st, u, g, wants[i]);
        printf("  %-8s kernel: %-3s rule: %-3s %s\n", names[i], real ? "yes" : "no", model ? "yes" : "no",
               real == model ? "" : "MISMATCH");
    }
    return 0;
}

Output on the test machine:

f407: owner 65534, group 65534, mode 0407; asking as uid 65534 gid 65534
  read     kernel: yes rule: yes 
  write    kernel: no  rule: no  
  execute  kernel: no  rule: no  
f407: owner 65534, group 65534, mode 0407; asking as uid 1000 gid 1000
  read     kernel: yes rule: yes 
  write    kernel: yes rule: yes 
  execute  kernel: yes rule: yes 
f640: owner 65534, group 1000, mode 0640; asking as uid 1000 gid 1000
  read     kernel: yes rule: yes 
  write    kernel: no  rule: no  
  execute  kernel: no  rule: no  
f640: owner 65534, group 1000, mode 0640; asking as uid 2000 gid 2000
  read     kernel: no  rule: no  
  write    kernel: no  rule: no  
  execute  kernel: no  rule: no  

The owner is denied writing, and a stranger (uid 1000) is allowed. The kernel and the rule agree in all 12 answers. For f640, uid 1000 matches the group, so it gets r-- and cannot write, and uid 2000 gets the other bits, which are ---.

The key lines. The rule means that making a file less open for the owner than for others is possible, and sometimes is a mistake. It also means that the other class is not "everyone else except owner and group", in the sense of adding: the three classes are exclusive.

Permission bits (mode)

The mode of a Unix file is a set of nine permission bits plus three special bits. The nine are read, write and execute for the owner, the group and others. The kernel selects the first matching class (owner, then group, then other) and applies only its three bits.

Example: mode 0640 gives the owner read and write, the group read, and others nothing.

Read it as

Translate rwxr-x--x by classes: rwx is 7, r-x is 5, --x is 1, so the octal mode is 751.

Check yourself: 1. What is the octal mode of rw-r-----? 2. Why can the owner of a file with mode 0407 not write to it? 3. How does the kernel choose between the class bits?