1 .\" Copyright (c) 2016 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH CGROUP_NAMESPACES 7 2019-03-06 "Linux" "Linux Programmer's Manual"
28 cgroup_namespaces \- overview of Linux cgroup namespaces
30 For an overview of namespaces, see
33 Cgroup namespaces virtualize the view of a process's cgroups (see
36 .IR /proc/[pid]/cgroup
38 .IR /proc/[pid]/mountinfo .
40 Each cgroup namespace has its own set of cgroup root directories.
41 These root directories are the base points for the relative
42 locations displayed in the corresponding records in the
43 .IR /proc/[pid]/cgroup
45 When a process creates a new cgroup namespace using
51 flag, it enters a new cgroup namespace in which its current
52 cgroups directories become the cgroup root directories
54 (This applies both for the cgroups version 1 hierarchies
55 and the cgroups version 2 unified hierarchy.)
58 .IR /proc/[pid]/cgroup ,
59 the pathname shown in the third field of each record will be
60 relative to the reading process's root directory
61 for the corresponding cgroup hierarchy.
62 If the cgroup directory of the target process lies outside
63 the root directory of the reading process's cgroup namespace,
64 then the pathname will show
66 entries for each ancestor level in the cgroup hierarchy.
68 The following shell session demonstrates the effect of creating
69 a new cgroup namespace.
71 First, (as superuser) we create a child cgroup in the
73 hierarchy, and place a process in that cgroup that we will
74 use as part of the demonstration below:
78 # \fBmkdir \-p /sys/fs/cgroup/freezer/sub2\fP
79 # \fBsleep 10000 &\fP # Create a process that lives for a while
81 # \fBecho 20124 > /sys/fs/cgroup/freezer/sub2/cgroup.procs\fP
85 We then create another child cgroup in the
87 hierarchy and put the shell into that cgroup:
91 # \fBmkdir \-p /sys/fs/cgroup/freezer/sub\fP
92 # \fBecho $$\fP # Show PID of this shell
94 # \fBecho 30655 > /sys/fs/cgroup/freezer/sub/cgroup.procs\fP
95 # \fBcat /proc/self/cgroup | grep freezer\fP
102 to create a process running a new shell in new cgroup and mount namespaces:
106 # \fBunshare \-Cm bash\fP
111 .IR /proc/[pid]/cgroup
112 files of, respectively, the new shell process started by the
114 command, a process that is in the original cgroup namespace
116 with PID 1), and the process in the sibling cgroup
121 $ \fBcat /proc/self/cgroup | grep freezer\fP
123 $ \fBcat /proc/1/cgroup | grep freezer\fP
125 $ \fBcat /proc/20124/cgroup | grep freezer\fP
130 From the output of the first command,
131 we see that the freezer cgroup membership of the new shell
132 (which is in the same cgroup as the initial shell)
133 is shown defined relative to the freezer cgroup root directory
134 that was established when the new cgroup namespace was created.
136 the new shell is in the
139 and the root directory of the freezer cgroup hierarchy
140 in the new cgroup namespace is also
142 Thus, the new shell's cgroup membership is displayed as \(aq/\(aq.)
144 However, when we look in
145 .IR /proc/self/mountinfo
146 we see the following anomaly:
150 # \fBcat /proc/self/mountinfo | grep freezer\fP
151 155 145 0:32 /.. /sys/fs/cgroup/freezer ...
155 The fourth field of this line
158 directory in the cgroup filesystem which forms the root of this mount.
159 Since by the definition of cgroup namespaces, the process's current
160 freezer cgroup directory became its root freezer cgroup directory,
161 we should see \(aq/\(aq in this field.
162 The problem here is that we are seeing a mount entry for the cgroup
163 filesystem corresponding to our initial shell process's cgroup namespace
164 (whose cgroup filesystem is indeed rooted in the parent directory of
166 We need to remount the freezer cgroup filesystem
167 inside this cgroup namespace, after which we see the expected results:
171 # \fBmount \-\-make\-rslave /\fP # Don't propagate mount events
172 # to other namespaces
173 # \fBumount /sys/fs/cgroup/freezer\fP
174 # \fBmount \-t cgroup \-o freezer freezer /sys/fs/cgroup/freezer\fP
175 # \fBcat /proc/self/mountinfo | grep freezer\fP
176 155 145 0:32 / /sys/fs/cgroup/freezer rw,relatime ...
181 Namespaces are a Linux-specific feature.
183 Use of cgroup namespaces requires a kernel that is configured with the
187 The virtualization provided by cgroup namespaces serves a number of purposes:
189 It prevents information leaks whereby cgroup directory paths outside of
190 a container would otherwise be visible to processes in the container.
191 Such leakages could, for example,
192 reveal information about the container framework
193 to containerized applications.
195 It eases tasks such as container migration.
196 The virtualization provided by cgroup namespaces
197 allows containers to be isolated from knowledge of
198 the pathnames of ancestor cgroups.
199 Without such isolation, the full cgroup pathnames (displayed in
200 .IR /proc/self/cgroups )
201 would need to be replicated on the target system when migrating a container;
202 those pathnames would also need to be unique,
203 so that they don't conflict with other pathnames on the target system.
205 It allows better confinement of containerized processes,
206 because it is possible to mount the container's cgroup filesystems such that
207 the container processes can't gain access to ancestor cgroup directories.
208 Consider, for example, the following scenario:
211 We have a cgroup directory,
213 that is owned by user ID 9000.
217 also owned by user ID 9000,
218 that is namespaced under the cgroup
222 was placed in a new cgroup namespace via
231 In the absence of cgroup namespacing, because the cgroup directory
233 is owned (and writable) by UID 9000 and process
235 is also owned by user ID 9000, then process
237 would be able to modify the contents of cgroups files
238 (i.e., change cgroup settings) not only in
240 but also in the ancestor cgroup directory
244 under the cgroup directory
246 in combination with suitable mount operations
247 for the cgroup filesystem (as shown above),
248 prevents it modifying files in
250 since it cannot even see the contents of that directory
251 (or of further removed cgroup ancestor directories).
252 Combined with correct enforcement of hierarchical limits,
253 this prevents process
255 from escaping the limits imposed by ancestor cgroups.
265 .BR user_namespaces (7)