ENع
←Protection and sandboxing
Lesson 53 · I/O, virtualisation and isolation

Protection, privilege and sandboxing

14 min10 steps5 questionsAdvanced
About this lesson

The kernel checks every request a program makes. Ordinary checks ask who is asking: the process's user ID against a file's owner and permission bits. But a program that handles untrusted data can be hijacked, and then it has every right its user has. A sandbox applies least privilege: it gives one program fewer rights than its user. Follow five lines of C that open an image, are refused /etc/shadow, switch on seccomp strict mode, read the image, and are killed when they reach for the user's private key. User mode and kernel mode explains the gate every request goes through; Hardware support for virtualisation, the Software lesson Containers and deployment, and the Linux lesson Namespaces and cgroups: how containers work show the bigger walls built on the same ideas.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 53 · Interactive
01 / 10

Keeping programs apart

Protection means each program may touch only what it is allowed to. Every request a program makes enters the kernel through the system call gate, so the kernel can check each one. Our program opens an image, cat.png, and reads it.

protection    each program touches only what it may
the gate      every request is a system call
the kernel    checks every request, every time
              ("complete mediation", OSTEP ch. 53)

the program (C99, Linux):

#include <fcntl.h>
#include <sys/prctl.h>
#include <unistd.h>

int main(void) {
    char buf[64];
    int fd;
    fd = open("cat.png", O_RDONLY);
    open("/etc/shadow", O_RDONLY);
    prctl(PR_SET_SECCOMP, 1);
    read(fd, buf, 64);
    open(".ssh/id_rsa", O_RDONLY);
    return 0;
}
Loading figure…
or ← →
Read the lesson step by step
  1. Keeping programs apart

    Protection means each program may touch only what it is allowed to. Every request a program makes enters the kernel through the system call gate, so the kernel can check each one. Our program opens an image, cat.png, and reads it.

    protection    each program touches only what it may
    the gate      every request is a system call
    the kernel    checks every request, every time
                  ("complete mediation", OSTEP ch. 53)
    
    the program (C99, Linux):
    
    #include <fcntl.h>
    #include <sys/prctl.h>
    #include <unistd.h>
    
    int main(void) {
        char buf[64];
        int fd;
        fd = open("cat.png", O_RDONLY);
        open("/etc/shadow", O_RDONLY);
        prctl(PR_SET_SECCOMP, 1);
        read(fd, buf, 64);
        open(".ssh/id_rsa", O_RDONLY);
        return 0;
    }
  2. Who is asking?

    Each process carries credentials: here user ID 1000, alice. Each file records an owner and permission bits that say who may read or write it. The kernel compares the two on every open.

    process      uid 1000 (alice)
    
    file           owner   may alice read?
    cat.png        alice   yes  (owner bits rw-)
    /etc/shadow    root    no  (other bits ---)
    .ssh/id_rsa    alice   yes  (owner bits rw-)
    
    rule: owner bits if your uid owns the file,
          group bits if you are in its group,
          else the other bits
  3. Line 1: allowed

    open() traps into the kernel. alice owns cat.png and the owner may read it, so the kernel opens it and hands back file descriptor 3, a small number that now stands for this open file.

    line 1   fd = open("cat.png", O_RDONLY);
    syscall  openat (glibc open() uses openat)
    filter   off
    check    uid 1000 owns cat.png -> owner bits rw- -> r: yes
    result   fd = 3   (0, 1, 2 are taken: lowest free is 3)
  4. Line 2: refused

    /etc/shadow holds password hashes. It belongs to root, and other users get no read bit. alice is neither, so the kernel refuses: open() returns -1 with the error EACCES. The program is told no and carries on.

    line 2   open("/etc/shadow", O_RDONLY);
    check    owner root, uid 1000 is not root
             other bits --- -> r: no
    result   -1, errno = EACCES (permission denied)
    
    a refusal is an error code, not a crash
    root (uid 0) would pass: CAP_DAC_OVERRIDE
    skips these checks (capabilities(7))
  5. One bug away

    cat.png came from the internet. If a bug in the image code lets crafted bytes take over the program, the attacker runs as alice, with every right alice has, including her private key. Least privilege says: keep only the rights you need.

    the kernel checks WHO asks, not WHY
    a hijacked program still has uid 1000
    so it may read .ssh/id_rsa (owner rw-)
    
    least privilege (OSTEP ch. 53):
      give a process the minimum privileges
      it needs to do its job
    this program only needs: read the image
  6. Line 3: into the sandbox

    prctl() turns on seccomp strict mode, a filter the kernel runs on every system call this thread makes from now on. Only four calls pass: read, write, _exit and sigreturn. Any other call, even one to undo it, is fatal.

    line 3   prctl(PR_SET_SECCOMP, 1);
             1 = SECCOMP_MODE_STRICT (<linux/seccomp.h>)
    filter   off -> strict
    allowed  read, write, _exit, sigreturn
    other    the thread is killed (SIGKILL)
    
    the program already holds what it needs:
    fd 3, open for reading cat.png
  7. Line 4: still allowed

    read is on the list, so the call passes the filter. Then the usual checks run: fd 3 is open for reading. The kernel copies the first 64 bytes of cat.png into buf. Honest work still works.

    line 4   read(fd, buf, 64);
    filter   read: on the list -> pass
    check    fd 3 open for reading -> yes
    result   64   (bytes copied into buf)
  8. Line 5: the attack

    Now suppose the attacker's code runs and asks for alice's private key. open() becomes the openat system call, which is not on the list. The filter stops it at the door, before any file lookup or permission check.

    line 5   open(".ssh/id_rsa", O_RDONLY);
             (stands for code an attacker slipped in)
    syscall  openat
    filter   openat: not on the list -> stop
             the call never runs
  9. The kernel ends it

    In strict mode the penalty for a call off the list is death: the kernel sends SIGKILL and the process ends. The key was never opened, even though alice's own permission bits would have allowed it.

    signal   SIGKILL   (cannot be caught or ignored)
    program  ends; .ssh/id_rsa never opened
    
    without the sandbox:   uid 1000 owns the key -> fd
    with the sandbox:      openat not allowed  -> killed
  10. Walls inside walls

    Protection comes in layers. The CPU's modes guard the kernel, credentials guard each user's files, and a sandbox shrinks one program's rights further. Containers and virtual machines add bigger walls around whole groups of programs.

    the rules as a Python simulation:
    
    ALLOWED = {"read", "write", "_exit", "sigreturn"}  # seccomp strict
    FILES = {"cat.png": (1000, 0o644),       # (owner uid, mode)
             "/etc/shadow": (0, 0o640),
             ".ssh/id_rsa": (1000, 0o600)}
    uid, strict = 1000, False
    
    def syscall(name, path=None):
        if strict and name not in ALLOWED:   # the filter goes first
            return "SIGKILL"
        if path:                             # then the permission bits
            owner, mode = FILES[path]
            bits = mode >> 6 if uid == owner else mode & 7
            if not bits & 4:
                return "EACCES"
        return "ok"
    
    print(syscall("openat", "cat.png"))
    print(syscall("openat", "/etc/shadow"))
    strict = True                            # prctl(PR_SET_SECCOMP, 1)
    print(syscall("read"))
    print(syscall("openat", ".ssh/id_rsa"))
    
    prints:
    ok
    EACCES
    ok
    SIGKILL

Quiz Unlocks after the last step.