]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/cgroup_namespaces.7
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2...
[thirdparty/man-pages.git] / man7 / cgroup_namespaces.7
CommitLineData
c736cecc
MK
1.\" Copyright (c) 2016 by Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
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.
7.\"
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.
12.\"
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
19.\" professionally.
20.\"
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\" %%%LICENSE_END
24.\"
25.\"
63121bd4 26.TH CGROUP_NAMESPACES 7 2019-08-02 "Linux" "Linux Programmer's Manual"
c736cecc
MK
27.SH NAME
28cgroup_namespaces \- overview of Linux cgroup namespaces
29.SH DESCRIPTION
30For an overview of namespaces, see
31.BR namespaces (7).
40749137 32.PP
c736cecc
MK
33Cgroup namespaces virtualize the view of a process's cgroups (see
34.BR cgroups (7))
35as seen via
36.IR /proc/[pid]/cgroup
37and
38.IR /proc/[pid]/mountinfo .
40749137 39.PP
aa864d82
MK
40Each cgroup namespace has its own set of cgroup root directories.
41These root directories are the base points for the relative
42locations displayed in the corresponding records in the
43.IR /proc/[pid]/cgroup
44file.
c736cecc
MK
45When a process creates a new cgroup namespace using
46.BR clone (2)
47or
48.BR unshare (2)
49with the
50.BR CLONE_NEWCGROUP
ef129697 51flag, its current
29179416
MK
52cgroups directories become the cgroup root directories
53of the new namespace.
c736cecc
MK
54(This applies both for the cgroups version 1 hierarchies
55and the cgroups version 2 unified hierarchy.)
40749137 56.PP
727e5609 57When reading the cgroup memberships of a "target" process from
c736cecc
MK
58.IR /proc/[pid]/cgroup ,
59the pathname shown in the third field of each record will be
aa864d82
MK
60relative to the reading process's root directory
61for the corresponding cgroup hierarchy.
c736cecc
MK
62If the cgroup directory of the target process lies outside
63the root directory of the reading process's cgroup namespace,
64then the pathname will show
65.I ../
66entries for each ancestor level in the cgroup hierarchy.
40749137 67.PP
c736cecc
MK
68The following shell session demonstrates the effect of creating
69a new cgroup namespace.
c9a35b01 70.PP
727e5609
MK
71First, (as superuser) in a shell in the initial cgroup namespace,
72we create a child cgroup in the
c736cecc 73.I freezer
c9a35b01
MK
74hierarchy, and place a process in that cgroup that we will
75use as part of the demonstration below:
76.PP
77.in +4n
78.EX
79# \fBmkdir \-p /sys/fs/cgroup/freezer/sub2\fP
80# \fBsleep 10000 &\fP # Create a process that lives for a while
81[1] 20124
82# \fBecho 20124 > /sys/fs/cgroup/freezer/sub2/cgroup.procs\fP
83.EE
84.in
85.PP
86We then create another child cgroup in the
87.I freezer
88hierarchy and put the shell into that cgroup:
40749137 89.PP
c736cecc 90.in +4n
b8302363 91.EX
c736cecc
MK
92# \fBmkdir \-p /sys/fs/cgroup/freezer/sub\fP
93# \fBecho $$\fP # Show PID of this shell
9430655
e39f614f 95# \fBecho 30655 > /sys/fs/cgroup/freezer/sub/cgroup.procs\fP
c736cecc
MK
96# \fBcat /proc/self/cgroup | grep freezer\fP
977:freezer:/sub
b8302363 98.EE
e646a1ba 99.in
40749137 100.PP
c736cecc
MK
101Next, we use
102.BR unshare (1)
103to create a process running a new shell in new cgroup and mount namespaces:
40749137 104.PP
32bc5a71 105.EX
c736cecc 106.in +4n
f3da99c4 107# \fBPS1="sh2# " unshare \-Cm bash\fP
c736cecc 108.in
32bc5a71 109.EE
40749137 110.PP
727e5609
MK
111From the new shell started by
112.BR unshare (1),
113we then inspect the
c736cecc 114.IR /proc/[pid]/cgroup
727e5609
MK
115files of, respectively, the new shell,
116a process that is in the initial cgroup namespace
c736cecc 117.RI ( init ,
c9a35b01 118with PID 1), and the process in the sibling cgroup
aa864d82 119.RI ( sub2 ):
40749137 120.PP
32bc5a71 121.EX
c736cecc 122.in +4n
f3da99c4 123sh2# \fBcat /proc/self/cgroup | grep freezer\fP
c736cecc 1247:freezer:/
f3da99c4 125sh2# \fBcat /proc/1/cgroup | grep freezer\fP
c736cecc 1267:freezer:/..
f3da99c4 127sh2# \fBcat /proc/20124/cgroup | grep freezer\fP
c736cecc
MK
1287:freezer:/../sub2
129.in
32bc5a71 130.EE
89cbd279
MK
131.PP
132From the output of the first command,
133we see that the freezer cgroup membership of the new shell
134(which is in the same cgroup as the initial shell)
135is shown defined relative to the freezer cgroup root directory
136that was established when the new cgroup namespace was created.
137(In absolute terms,
138the new shell is in the
139.I /sub
140freezer cgroup,
141and the root directory of the freezer cgroup hierarchy
142in the new cgroup namespace is also
143.IR /sub .
144Thus, the new shell's cgroup membership is displayed as \(aq/\(aq.)
145.PP
c736cecc
MK
146However, when we look in
147.IR /proc/self/mountinfo
148we see the following anomaly:
40749137 149.PP
32bc5a71 150.EX
c736cecc 151.in +4n
f3da99c4 152sh2# \fBcat /proc/self/mountinfo | grep freezer\fP
c736cecc
MK
153155 145 0:32 /.. /sys/fs/cgroup/freezer ...
154.in
32bc5a71 155.EE
40749137 156.PP
aa864d82
MK
157The fourth field of this line
158.RI ( /.. )
159should show the
c736cecc
MK
160directory in the cgroup filesystem which forms the root of this mount.
161Since by the definition of cgroup namespaces, the process's current
162freezer cgroup directory became its root freezer cgroup directory,
163we should see \(aq/\(aq in this field.
164The problem here is that we are seeing a mount entry for the cgroup
727e5609
MK
165filesystem corresponding to the initial cgroup namespace
166(whose cgroup filesystem is indeed rooted at the parent directory of
c736cecc 167.IR sub ).
727e5609
MK
168To fix this problem, we must remount the freezer cgroup filesystem
169from the new shell (i.e., perform the mount from a process that is in the
170new cgroup namespace), after which we see the expected results:
40749137 171.PP
32bc5a71 172.EX
c736cecc 173.in +4n
f3da99c4
MK
174sh2# \fBmount \-\-make\-rslave /\fP # Don't propagate mount events
175 # to other namespaces
176sh2# \fBumount /sys/fs/cgroup/freezer\fP
177sh2# \fBmount \-t cgroup \-o freezer freezer /sys/fs/cgroup/freezer\fP
178sh2# \fBcat /proc/self/mountinfo | grep freezer\fP
c736cecc
MK
179155 145 0:32 / /sys/fs/cgroup/freezer rw,relatime ...
180.in
32bc5a71 181.EE
c736cecc 182.\"
e664450b
MK
183.SH CONFORMING TO
184Namespaces are a Linux-specific feature.
c736cecc 185.SH NOTES
d190902b
MK
186Use of cgroup namespaces requires a kernel that is configured with the
187.B CONFIG_CGROUPS
188option.
189.PP
4d9b3039 190The virtualization provided by cgroup namespaces serves a number of purposes:
c736cecc
MK
191.IP * 2
192It prevents information leaks whereby cgroup directory paths outside of
193a container would otherwise be visible to processes in the container.
194Such leakages could, for example,
195reveal information about the container framework
196to containerized applications.
197.IP *
10b547c5
MK
198It eases tasks such as container migration.
199The virtualization provided by cgroup namespaces
200allows containers to be isolated from knowledge of
201the pathnames of ancestor cgroups.
0191a7b9
MK
202Without such isolation, the full cgroup pathnames (displayed in
203.IR /proc/self/cgroups )
204would need to be replicated on the target system when migrating a container;
10b547c5
MK
205those pathnames would also need to be unique,
206so that they don't conflict with other pathnames on the target system.
207.IP *
a531b2cf 208It allows better confinement of containerized processes,
a2b7dba5
MK
209because it is possible to mount the container's cgroup filesystems such that
210the container processes can't gain access to ancestor cgroup directories.
c736cecc
MK
211Consider, for example, the following scenario:
212.RS 4
213.IP \(bu 2
214We have a cgroup directory,
215.IR /cg/1 ,
216that is owned by user ID 9000.
217.IP \(bu
218We have a process,
219.IR X ,
220also owned by user ID 9000,
221that is namespaced under the cgroup
222.IR /cg/1/2
223(i.e.,
224.I X
225was placed in a new cgroup namespace via
226.BR clone (2)
227or
228.BR unshare (2)
229with the
230.BR CLONE_NEWCGROUP
231flag).
232.RE
233.IP
234In the absence of cgroup namespacing, because the cgroup directory
235.IR /cg/1
ef6f9539 236is owned (and writable) by UID 9000 and process
bcedc0c2
MK
237.I X
238is also owned by user ID 9000, then process
239.I X
240would be able to modify the contents of cgroups files
241(i.e., change cgroup settings) not only in
c736cecc
MK
242.IR /cg/1/2
243but also in the ancestor cgroup directory
244.IR /cg/1 .
245Namespacing process
246.IR X
247under the cgroup directory
cc267b37
MK
248.IR /cg/1/2 ,
249in combination with suitable mount operations
250for the cgroup filesystem (as shown above),
c736cecc
MK
251prevents it modifying files in
252.IR /cg/1 ,
253since it cannot even see the contents of that directory
254(or of further removed cgroup ancestor directories).
255Combined with correct enforcement of hierarchical limits,
2a785d2a
MK
256this prevents process
257.I X
258from escaping the limits imposed by ancestor cgroups.
c736cecc
MK
259.SH SEE ALSO
260.BR unshare (1),
261.BR clone (2),
262.BR setns (2),
263.BR unshare (2),
264.BR proc (5),
265.BR cgroups (7),
266.BR credentials (7),
61256f9f 267.BR namespaces (7),
c736cecc 268.BR user_namespaces (7)