Skip to main content
Be Bitwise

Linux Security Modules

Kernel Security Mechanisms · Security Fundamentals track

5 min read

In this module

Beyond Traditional Unix Permissions

The classic Unix permission model — owner/group/other crossed with read/write/execute — is a form of Discretionary Access Control (DAC). The file's owner decides who can touch it. That model carries a fatal flaw: compromise a process running as root and the attacker inherits root's unlimited access to everything on the box. Linux Security Modules add Mandatory Access Control (MAC) and assorted tooling that puts limits on what even a privileged process can do.

SELinux

SELinux (Security-Enhanced Linux), born of an NSA and Red Hat collaboration, slaps a security context (a label) onto every process and every resource. A centralised policy spells out exactly which operations are allowed between which contexts, and the kernel enforces that policy regardless of what traditional Unix permissions say.

A security context looks like user:role:type:level. The piece that does the heavy lifting is the type. Policy rules specify allowed interactions between types:

text
# Allow the httpd_t type to read files labelled httpd_content_t
allow httpd_t httpd_content_t:file { read open getattr };
# Deny httpd_t from reading shadow_t (password file)
# (This is the default - anything not explicitly allowed is denied)

SELinux runs in three modes:

  • Enforcing: policy violations are blocked and logged
  • Permissive: policy violations are logged but not blocked — handy while you're authoring policy
  • Disabled: SELinux is completely off

The point of SELinux is confinement. Pop an Apache instance running as root and the attacker still only reaches what the httpd_t type is allowed to reach. The compromised process can't read /etc/shadow, load kernel modules, or sniff other services' data, no matter what its Unix UID happens to be.

Type Enforcement in Practice

SELinux policy gets surgical. Android boxes every app and system service into its own security domain. Policy covers file access, IPC mechanisms, network operations, and hardware resources for each domain. That's why Android malware that breaks out of one app sandbox still runs face-first into SELinux on the other side.

AppArmor

AppArmor takes the simpler road. Instead of labels, it does pathname-based access control. Each confined program gets a profile listing which files it can touch and what operations it can perform:

text
# AppArmor profile for nginx
/usr/sbin/nginx {
/etc/nginx/** r, # Read nginx config
/var/log/nginx/** w, # Write to logs
/var/www/** r, # Read web content
/run/nginx.pid rw, # PID file
network inet stream, # TCP networking
deny /etc/shadow r, # Explicitly deny shadow file
}

AppArmor trades flexibility for clarity — easier to read and configure, harder to express anything elaborate. It's the default MAC system on Ubuntu, SUSE, and Debian.

Seccomp-BPF

Seccomp-BPF doesn't pretend to be a full MAC system. It's a syscall filter. A process installs a BPF (Berkeley Packet Filter) program, and the kernel runs it before every syscall. The filter can allow, deny, trace, or kill the calling process, based on the syscall number and its arguments.

c
// Install a seccomp filter that only allows read, write, and exit
struct sock_filter filter[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_read, 2, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_write, 1, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_exit_group, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL),
};
struct sock_fprog prog = {
.len = sizeof(filter) / sizeof(filter[0]),
.filter = filter,
};
prctl(PR_SET_NO_NEW_PRIVS, 1); // Cannot gain new privileges
prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);

Seccomp-BPF earns its keep in sandboxing because the Linux kernel ships hundreds of syscalls and every one is a potential attack surface. A bug in an obscure call like keyctl or io_uring is irrelevant to a sandboxed process that can't even reach those syscalls. Chrome, Firefox, Docker, systemd, and most security-sensitive Linux applications all run BPF filters today.

Linux Capabilities

Traditional Unix runs a binary privilege model: you're root (UID 0) and can do anything, or you're not and you can't. Linux capabilities chop root's powers into individually grantable pieces:

  • CAP_NET_BIND_SERVICE: Bind to ports below 1024
  • CAP_SYS_PTRACE: Trace and debug other processes
  • CAP_SYS_MODULE: Load kernel modules
  • CAP_NET_RAW: Use raw sockets
  • CAP_SYS_ADMIN: A broad "catch-all" for miscellaneous privileged operations

A web server whose only privileged requirement is binding port 80 can run with CAP_NET_BIND_SERVICE and nothing else. Pop the daemon and the attacker inherits that one capability instead of the entire keys-to-the-kingdom UID 0 grant.

bash
# Run a program with only the capability to bind low ports
setcap cap_net_bind_service=+ep /usr/bin/myserver

Namespaces for Isolation

Linux namespaces hand a process a private view of system resources. Each namespace type partitions a different facet:

  • PID namespace: the process sees its own PID space — it's PID 1 inside, something else outside
  • Mount namespace: the process sees its own filesystem hierarchy
  • Network namespace: the process gets its own network stack, interfaces, and routing
  • User namespace: the process maps UIDs differently — root inside, unprivileged outside
  • IPC namespace: separate System V IPC and POSIX message-queue namespaces

Containers (Docker, Podman) glue namespaces together with cgroups and Seccomp-BPF to build isolated environments. Each container brings its own PID space, filesystem view, and network stack, while sharing the host kernel underneath.

Defence in Depth on Linux

A properly hardened Linux service stacks several mechanisms: SELinux or AppArmor for MAC, Seccomp-BPF for syscall filtering, capabilities instead of running as root, namespaces for resource isolation, cgroups for resource limits. Each layer absorbs a different class of attack, so a full compromise means breaking every one of them.