CGroups
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Basic Information
Linux Control Groups, or cgroups, are a feature of the Linux kernel that allows the allocation, limitation, and prioritization of system resources like CPU, memory, and disk I/O among process groups. They offer a mechanism for managing and isolating the resource usage of process collections, beneficial for purposes such as resource limitation, workload isolation, and resource prioritization among different process groups.
There are two versions of cgroups: version 1 and version 2. Both can be used concurrently on a system. The primary distinction is that cgroups version 2 introduces a hierarchical, tree-like structure, enabling more nuanced and detailed resource distribution among process groups. Additionally, version 2 brings various enhancements, including:
In addition to the new hierarchical organization, cgroups version 2 also introduced several other changes and improvements, such as support for new resource controllers, better support for legacy applications, and improved performance.
Overall, cgroups version 2 offers more features and better performance than version 1, but the latter may still be used in certain scenarios where compatibility with older systems is a concern.
You can list the v1 and v2 cgroups for any process by looking at its cgroup file in /proc/<pid>. You can start by looking at your shell’s cgroups with this command:
The output structure is as follows:
Numbers 2–12: cgroups v1, with each line representing a different cgroup. Controllers for these are specified adjacent to the number.
Number 1: Also cgroups v1, but solely for management purposes (set by, e.g., systemd), and lacks a controller.
Number 0: Represents cgroups v2. No controllers are listed, and this line is exclusive on systems only running cgroups v2.
The names are hierarchical, resembling file paths, indicating the structure and relationship between different cgroups.
Names like /user.slice or /system.slice specify the categorization of cgroups, with user.slice typically for login sessions managed by systemd and system.slice for system services.
Viewing cgroups
The filesystem is typically utilized for accessing cgroups, diverging from the Unix system call interface traditionally used for kernel interactions. To investigate a shell's cgroup configuration, one should examine the /proc/self/cgroup file, which reveals the shell's cgroup. Then, by navigating to the /sys/fs/cgroup (or /sys/fs/cgroup/unified
) directory and locating a directory that shares the cgroup's name, one can observe various settings and resource usage information pertinent to the cgroup.
![](https://book.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F129538173-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-L_2uGJGU7AVNRcqRvEi%252Fuploads%252FTDxbVbGhBpjN4t2z4QJe%252Fimage.png%3Falt%3Dmedia%26token%3D771ccb9e-db4c-4ef8-ae4f-4ad88bb92c96&width=768&dpr=4&quality=100&sign=4741b3df&sv=1)
The key interface files for cgroups are prefixed with cgroup. The cgroup.procs file, which can be viewed with standard commands like cat, lists the processes within the cgroup. Another file, cgroup.threads, includes thread information.
![](https://book.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F129538173-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-L_2uGJGU7AVNRcqRvEi%252Fuploads%252FPQfkzKMMmC7K6rvwCih1%252Fimage.png%3Falt%3Dmedia%26token%3Dae7fd8e6-877d-4487-b5f6-7f28cafb49b2&width=768&dpr=4&quality=100&sign=4db50e8e&sv=1)
Cgroups managing shells typically encompass two controllers that regulate memory usage and process count. To interact with a controller, files bearing the controller's prefix should be consulted. For instance, pids.current would be referenced to ascertain the count of threads in the cgroup.
![](https://book.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F129538173-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-L_2uGJGU7AVNRcqRvEi%252Fuploads%252Fei1S4ZoazyH3TgQTxko1%252Fimage.png%3Falt%3Dmedia%26token%3D799ff16f-fdee-49bf-a6c1-cbe16fbf0a78&width=768&dpr=4&quality=100&sign=4ef905b1&sv=1)
The indication of max in a value suggests the absence of a specific limit for the cgroup. However, due to the hierarchical nature of cgroups, limits might be imposed by a cgroup at a lower level in the directory hierarchy.
Manipulating and Creating cgroups
Processes are assigned to cgroups by writing their Process ID (PID) to the cgroup.procs
file. This requires root privileges. For instance, to add a process:
Similarly, modifying cgroup attributes, like setting a PID limit, is done by writing the desired value to the relevant file. To set a maximum of 3,000 PIDs for a cgroup:
Creating new cgroups involves making a new subdirectory within the cgroup hierarchy, which prompts the kernel to automatically generate necessary interface files. Though cgroups without active processes can be removed with rmdir
, be aware of certain constraints:
Processes can only be placed in leaf cgroups (i.e., the most nested ones in a hierarchy).
A cgroup cannot possess a controller absent in its parent.
Controllers for child cgroups must be explicitly declared in the
cgroup.subtree_control
file. For example, to enable CPU and PID controllers in a child cgroup:
The root cgroup is an exception to these rules, allowing direct process placement. This can be used to remove processes from systemd management.
Monitoring CPU usage within a cgroup is possible through the cpu.stat
file, displaying total CPU time consumed, helpful for tracking usage across a service's subprocesses:
![](https://book.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F129538173-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-L_2uGJGU7AVNRcqRvEi%252Fuploads%252Fa9bI9flvCt5IOCeI3vHq%252Fimage.png%3Falt%3Dmedia%26token%3D4702f687-113b-48da-8e84-4fb1fc9a533d&width=768&dpr=4&quality=100&sign=1a6308d8&sv=1)
References
Book: How Linux Works, 3rd Edition: What Every Superuser Should Know By Brian Ward
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Last updated