Part 3 of 7 · 4 min read
Resource limits
Think of a prepaid card with a spending cap. The holder can spend freely up to the cap, and a shop refuses a payment above it. The analogy stops in one place: a card holder can ask the bank for a higher cap, while a process can lower its own hard limit and, without extra power, never raise it again.
In plain words: Each process has resource limits (rlimit), set with setrlimit and read with getrlimit. Each limit has two values. The soft limit is the one the kernel enforces. The hard limit is the ceiling for the soft limit. A process may change its soft limit anywhere up to the hard limit, and may lower its hard limit, but raising the hard limit needs the capability CAP_SYS_RESOURCE (page 4). Common limits are RLIMIT_NOFILE (open files), RLIMIT_CPU (CPU seconds), RLIMIT_AS (address space, Level 6), RLIMIT_NPROC (processes of a user) and RLIMIT_CORE (core file size). A limit stops an attacker from using the machine for a fork bomb, an endless loop or a memory flood.
A worked example. The first part of this program (48 lines in all) sets limits and tries to open a file.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <unistd.h>
static void try_open(const char *when) {
int fd = open("/etc/hostname", O_RDONLY);
printf("%-18s open: %s\n", when, fd >= 0 ? "ok" : strerror(errno));
if (fd >= 0) close(fd);
fflush(stdout);
}
int main(void) {
struct rlimit old, rl;
getrlimit(RLIMIT_NOFILE, &old);
rl = (struct rlimit){ 3, old.rlim_max }; /* soft limit: 3 files */
setrlimit(RLIMIT_NOFILE, &rl);
try_open("soft limit 3");
setrlimit(RLIMIT_NOFILE, &old); /* a soft limit can be raised */
try_open("soft limit restored");
rl = (struct rlimit){ 3, 3 }; /* now lower the hard limit */
setrlimit(RLIMIT_NOFILE, &rl);
int r = setrlimit(RLIMIT_NOFILE, &old); /* try to raise it again */
printf("%-18s raise hard limit: %s\n", "hard limit 3",
r == 0 ? "ok" : strerror(errno));
struct sock_filter f[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_openat, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
};
struct sock_fprog prog = { sizeof f / sizeof f[0], f };
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { perror("seccomp"); return 1; }
try_open("seccomp filter");
puts("write still works");
return 0;
}
Output on the test machine:
soft limit 3 open: Too many open files
soft limit restored open: ok
hard limit 3 raise hard limit: Operation not permitted
seccomp filter open: Operation not permitted
write still works
The soft limit of 3 failed the open with Too many open files (the code EMFILE), because descriptors 0, 1 and 2 already use three slots. The process raised the soft limit back and the open worked. After the hard limit was lowered to 3, the attempt to raise it failed with Operation not permitted, so a lowered hard limit is a one-way door. (The test machine's root lacks CAP_SYS_RESOURCE, as page 4 showed, so even root could not undo it.) The last two lines belong to section 3.
The key lines. A limit caps one resource of one process (or one user, for RLIMIT_NPROC). A cap for a whole group of processes needs control groups, which Level 11 covers.
Resource limit
A resource limit (rlimit) is a per-process cap on a resource such as open files, CPU time or memory. It has a soft value, which the kernel enforces, and a hard value, which is the ceiling that the soft value may reach.
Example: RLIMIT_NOFILE with soft 3 makes the fourth open fail with EMFILE.
Check yourself: 1. What is the difference between the soft and the hard limit? 2. Why did the open fail with a soft limit of 3 in a program that had opened nothing? 3. Why can a process not undo a lowered hard limit?