Resource Limits and Cgroups
What you'll learn: How to use cgroups to cap service resource consumption and prevent resource exhaustion attacks.
Even isolated services can wreak havoc if they consume unlimited CPU, memory, or I/O. An attacker exploiting a service—or even a legitimate service with a bug—can starve other processes, effectively creating a denial-of-service condition on your own system.
What Are Cgroups?
Control groups (cgroups) are a Linux kernel feature that limits, accounts for, and isolates resource usage of process groups. Think of cgroups as invisible cages around your services: each cage has walls that define maximum CPU time, memory size, disk I/O bandwidth, and more.
When systemd starts a service, it automatically places it in a dedicated cgroup. You configure the walls of that cage through systemd unit directives.
Why This Matters for Security
Without resource limits, a compromised web server could:
- Consume all available memory, triggering the OOM killer
- Monopolize CPU, slowing critical services to a crawl
- Exhaust disk I/O, making the system unresponsive
Resource limits act as containment barriers—even if an attacker gains control, they can't consume resources beyond your defined thresholds.
Common Limits to Configure
In systemd unit files:
MemoryMax=512M— Hard memory ceilingCPUQuota=50%— Limit to 50% of one CPU coreTasksMax=100— Maximum number of processes/threadsIOWeight=100— Relative I/O priority (10-1000 scale)
These translate directly into cgroup v2 settings under /sys/fs/cgroup/.
Key Takeaway: Cgroups let you enforce strict resource boundaries on services, preventing compromised or misbehaving processes from exhausting system resources and ensuring critical services remain available even under attack.