| 1 |
.\" Copyright 2016, Michael Kerrisk <[email protected]> |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH cgroup_namespaces 7 2026-02-08 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
cgroup_namespaces \- overview of Linux cgroup namespaces |
| 9 |
.SH DESCRIPTION |
| 10 |
For an overview of namespaces, see |
| 11 |
.BR namespaces (7). |
| 12 |
.P |
| 13 |
Cgroup namespaces virtualize the view of a process's cgroups (see |
| 14 |
.BR cgroups (7)) |
| 15 |
as seen via |
| 16 |
.IR /proc/ pid /cgroup |
| 17 |
and |
| 18 |
.IR /proc/ pid /mountinfo . |
| 19 |
.P |
| 20 |
Each cgroup namespace has its own set of cgroup root directories. |
| 21 |
These root directories are the base points for the relative |
| 22 |
locations displayed in the corresponding records in the |
| 23 |
.IR /proc/ pid /cgroup |
| 24 |
file. |
| 25 |
When a process creates a new cgroup namespace using |
| 26 |
.BR clone (2) |
| 27 |
or |
| 28 |
.BR unshare (2) |
| 29 |
with the |
| 30 |
.B CLONE_NEWCGROUP |
| 31 |
flag, its current |
| 32 |
cgroups directories become the cgroup root directories |
| 33 |
of the new namespace. |
| 34 |
(This applies both for the cgroups version 1 hierarchies |
| 35 |
and the cgroups version 2 unified hierarchy.) |
| 36 |
.P |
| 37 |
When reading the cgroup memberships of a "target" process from |
| 38 |
.IR /proc/ pid /cgroup , |
| 39 |
the pathname shown in the third field of each record will be |
| 40 |
relative to the reading process's root directory |
| 41 |
for the corresponding cgroup hierarchy. |
| 42 |
If the cgroup directory of the target process lies outside |
| 43 |
the root directory of the reading process's cgroup namespace, |
| 44 |
then the pathname will show |
| 45 |
.I ../ |
| 46 |
entries for each ancestor level in the cgroup hierarchy. |
| 47 |
.P |
| 48 |
The following shell session demonstrates the effect of creating |
| 49 |
a new cgroup namespace. |
| 50 |
.P |
| 51 |
First, (as superuser) in a shell in the initial cgroup namespace, |
| 52 |
we create a child cgroup in the |
| 53 |
.I freezer |
| 54 |
hierarchy, and place a process in that cgroup that we will |
| 55 |
use as part of the demonstration below: |
| 56 |
.P |
| 57 |
.in +4n |
| 58 |
.EX |
| 59 |
.RB # " mkdir \-p /sys/fs/cgroup/freezer/sub2" ; |
| 60 |
.RB # " sleep 10000 &" " # Create a process that lives for a while" |
| 61 |
[1] 20124 |
| 62 |
.RB # " echo 20124 > /sys/fs/cgroup/freezer/sub2/cgroup.procs" ; |
| 63 |
.EE |
| 64 |
.in |
| 65 |
.P |
| 66 |
We then create another child cgroup in the |
| 67 |
.I freezer |
| 68 |
hierarchy and put the shell into that cgroup: |
| 69 |
.P |
| 70 |
.in +4n |
| 71 |
.EX |
| 72 |
.RB # " mkdir \-p /sys/fs/cgroup/freezer/sub" ; |
| 73 |
.RB # " echo $$" "; # Show PID of this shell" |
| 74 |
30655 |
| 75 |
.RB # " echo 30655 > /sys/fs/cgroup/freezer/sub/cgroup.procs" ; |
| 76 |
.RB # " cat /proc/self/cgroup | grep freezer" ; |
| 77 |
7:freezer:/sub |
| 78 |
.EE |
| 79 |
.in |
| 80 |
.P |
| 81 |
Next, we use |
| 82 |
.BR unshare (1) |
| 83 |
to create a process running a new shell in new cgroup and mount namespaces: |
| 84 |
.P |
| 85 |
.in +4n |
| 86 |
.EX |
| 87 |
.RB # " PS1='sh2# ' unshare \-Cm bash" ; |
| 88 |
.EE |
| 89 |
.in |
| 90 |
.P |
| 91 |
From the new shell started by |
| 92 |
.BR unshare (1), |
| 93 |
we then inspect the |
| 94 |
.IR /proc/ pid /cgroup |
| 95 |
files of, respectively, the new shell, |
| 96 |
a process that is in the initial cgroup namespace |
| 97 |
.RI ( init , |
| 98 |
with PID 1), and the process in the sibling cgroup |
| 99 |
.RI ( sub2 ): |
| 100 |
.P |
| 101 |
.in +4n |
| 102 |
.EX |
| 103 |
.RB sh2# " cat /proc/self/cgroup | grep freezer" ; |
| 104 |
7:freezer:/ |
| 105 |
.RB sh2# " cat /proc/1/cgroup | grep freezer" ; |
| 106 |
7:freezer:/.. |
| 107 |
.RB sh2# " cat /proc/20124/cgroup | grep freezer" ; |
| 108 |
7:freezer:/../sub2 |
| 109 |
.EE |
| 110 |
.in |
| 111 |
.P |
| 112 |
From the output of the first command, |
| 113 |
we see that the freezer cgroup membership of the new shell |
| 114 |
(which is in the same cgroup as the initial shell) |
| 115 |
is shown defined relative to the freezer cgroup root directory |
| 116 |
that was established when the new cgroup namespace was created. |
| 117 |
(In absolute terms, |
| 118 |
the new shell is in the |
| 119 |
.I /sub |
| 120 |
freezer cgroup, |
| 121 |
and the root directory of the freezer cgroup hierarchy |
| 122 |
in the new cgroup namespace is also |
| 123 |
.IR /sub . |
| 124 |
Thus, the new shell's cgroup membership is displayed as \[aq]/\[aq].) |
| 125 |
.P |
| 126 |
However, when we look in |
| 127 |
.I /proc/self/mountinfo |
| 128 |
we see the following anomaly: |
| 129 |
.P |
| 130 |
.in +4n |
| 131 |
.EX |
| 132 |
.RB sh2# " cat /proc/self/mountinfo | grep freezer" ; |
| 133 |
155 145 0:32 /..\& /sys/fs/cgroup/freezer ... |
| 134 |
.EE |
| 135 |
.in |
| 136 |
.P |
| 137 |
The fourth field of this line |
| 138 |
.RI ( /.. ) |
| 139 |
should show the |
| 140 |
directory in the cgroup filesystem which forms the root of this mount. |
| 141 |
Since by the definition of cgroup namespaces, the process's current |
| 142 |
freezer cgroup directory became its root freezer cgroup directory, |
| 143 |
we should see \[aq]/\[aq] in this field. |
| 144 |
The problem here is that we are seeing a mount entry for the cgroup |
| 145 |
filesystem corresponding to the initial cgroup namespace |
| 146 |
(whose cgroup filesystem is indeed rooted at the parent directory of |
| 147 |
.IR sub ). |
| 148 |
To fix this problem, we must remount the freezer cgroup filesystem |
| 149 |
from the new shell (i.e., perform the mount from a process that is in the |
| 150 |
new cgroup namespace), after which we see the expected results: |
| 151 |
.P |
| 152 |
.in +4n |
| 153 |
.EX |
| 154 |
.RB sh2# " mount \-\-make\-rslave /" "; # Don\[aq]t propagate mount events" |
| 155 |
# to other namespaces |
| 156 |
.RB sh2# " umount /sys/fs/cgroup/freezer" ; |
| 157 |
.RB sh2# " mount \-t cgroup \-o freezer freezer /sys/fs/cgroup/freezer" ; |
| 158 |
.RB sh2# " cat /proc/self/mountinfo | grep freezer" ; |
| 159 |
155 145 0:32 / /sys/fs/cgroup/freezer rw,relatime ... |
| 160 |
.EE |
| 161 |
.in |
| 162 |
.\" |
| 163 |
.SH STANDARDS |
| 164 |
Linux. |
| 165 |
.SH NOTES |
| 166 |
Use of cgroup namespaces requires a kernel that is configured with the |
| 167 |
.B CONFIG_CGROUPS |
| 168 |
option. |
| 169 |
.P |
| 170 |
The virtualization provided by cgroup namespaces serves a number of purposes: |
| 171 |
.IP \[bu] 3 |
| 172 |
It prevents information leaks whereby cgroup directory paths outside of |
| 173 |
a container would otherwise be visible to processes in the container. |
| 174 |
Such leakages could, for example, |
| 175 |
reveal information about the container framework |
| 176 |
to containerized applications. |
| 177 |
.IP \[bu] |
| 178 |
It eases tasks such as container migration. |
| 179 |
The virtualization provided by cgroup namespaces |
| 180 |
allows containers to be isolated from knowledge of |
| 181 |
the pathnames of ancestor cgroups. |
| 182 |
Without such isolation, the full cgroup pathnames (displayed in |
| 183 |
.IR /proc/self/cgroups ) |
| 184 |
would need to be replicated on the target system when migrating a container; |
| 185 |
those pathnames would also need to be unique, |
| 186 |
so that they don't conflict with other pathnames on the target system. |
| 187 |
.IP \[bu] |
| 188 |
It allows better confinement of containerized processes, |
| 189 |
because it is possible to mount the container's cgroup filesystems such that |
| 190 |
the container processes can't gain access to ancestor cgroup directories. |
| 191 |
Consider, for example, the following scenario: |
| 192 |
.RS |
| 193 |
.IP \[bu] 3 |
| 194 |
We have a cgroup directory, |
| 195 |
.IR /cg/1 , |
| 196 |
that is owned by user ID 9000. |
| 197 |
.IP \[bu] |
| 198 |
We have a process, |
| 199 |
.IR X , |
| 200 |
also owned by user ID 9000, |
| 201 |
that is namespaced under the cgroup |
| 202 |
.I /cg/1/2 |
| 203 |
(i.e., |
| 204 |
.I X |
| 205 |
was placed in a new cgroup namespace via |
| 206 |
.BR clone (2) |
| 207 |
or |
| 208 |
.BR unshare (2) |
| 209 |
with the |
| 210 |
.B CLONE_NEWCGROUP |
| 211 |
flag). |
| 212 |
.RE |
| 213 |
.IP |
| 214 |
In the absence of cgroup namespacing, because the cgroup directory |
| 215 |
.I /cg/1 |
| 216 |
is owned (and writable) by UID 9000 and process |
| 217 |
.I X |
| 218 |
is also owned by user ID 9000, process |
| 219 |
.I X |
| 220 |
would be able to modify the contents of cgroups files |
| 221 |
(i.e., change cgroup settings) not only in |
| 222 |
.I /cg/1/2 |
| 223 |
but also in the ancestor cgroup directory |
| 224 |
.IR /cg/1 . |
| 225 |
Namespacing process |
| 226 |
.I X |
| 227 |
under the cgroup directory |
| 228 |
.IR /cg/1/2 , |
| 229 |
in combination with suitable mount operations |
| 230 |
for the cgroup filesystem (as shown above), |
| 231 |
prevents it modifying files in |
| 232 |
.IR /cg/1 , |
| 233 |
since it cannot even see the contents of that directory |
| 234 |
(or of further removed cgroup ancestor directories). |
| 235 |
Combined with correct enforcement of hierarchical limits, |
| 236 |
this prevents process |
| 237 |
.I X |
| 238 |
from escaping the limits imposed by ancestor cgroups. |
| 239 |
.SH SEE ALSO |
| 240 |
.BR unshare (1), |
| 241 |
.BR clone (2), |
| 242 |
.BR setns (2), |
| 243 |
.BR unshare (2), |
| 244 |
.BR proc (5), |
| 245 |
.BR cgroups (7), |
| 246 |
.BR credentials (7), |
| 247 |
.BR namespaces (7), |
| 248 |
.BR user_namespaces (7) |