]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/cgroup_namespaces.7
pwritev2.2: New link to readv(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.\"
26.TH CGROUP_NAMESPACES 7 2016-05-05 "Linux" "Linux Programmer's Manual"
27.SH NAME
28cgroup_namespaces \- overview of Linux cgroup namespaces
29.SH DESCRIPTION
30For an overview of namespaces, see
31.BR namespaces (7).
32
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 .
39
40Each cgroup namespace has its own set of cgroup root directories,
41which are the base points for the relative locations displayed in
42.IR /proc/[pid]/cgroup .
43When a process creates a new cgroup namespace using
44.BR clone (2)
45or
46.BR unshare (2)
47with the
48.BR CLONE_NEWCGROUP
29179416
MK
49flag, it enters a new cgroup namespace in which its current
50cgroups directories become the cgroup root directories
51of the new namespace.
c736cecc
MK
52(This applies both for the cgroups version 1 hierarchies
53and the cgroups version 2 unified hierarchy.)
54
55When viewing
56.IR /proc/[pid]/cgroup ,
57the pathname shown in the third field of each record will be
58relative to the reading process's cgroup root directory.
59If the cgroup directory of the target process lies outside
60the root directory of the reading process's cgroup namespace,
61then the pathname will show
62.I ../
63entries for each ancestor level in the cgroup hierarchy.
64
65The following shell session demonstrates the effect of creating
66a new cgroup namespace.
67First, (as superuser) we create a child cgroup in the
68.I freezer
69hierarchy, and put the shell into that cgroup:
70
71.nf
72.in +4n
73# \fBmkdir \-p /sys/fs/cgroup/freezer/sub\fP
74# \fBecho $$\fP # Show PID of this shell
7530655
76# \fBsh \-c 'echo 30655 > /sys/fs/cgroup/sub'\fP
77# \fBcat /proc/self/cgroup | grep freezer\fP
787:freezer:/sub
79.in
80.fi
81
82Next, we use
83.BR unshare (1)
84to create a process running a new shell in new cgroup and mount namespaces:
85
86.nf
87.in +4n
88# \fBunshare \-Cm bash\fP
89.in
90.fi
91
92We then inspect the
93.IR /proc/[pid]/cgroup
94files of, respectively, the new shell process started by the
95.BR unshare (1)
96command, a process that is in the original cgroup namespace
97.RI ( init ,
98with PID 1), and a process in a sibling cgroup:
99
100.nf
101.in +4n
102$ \fBcat /proc/self/cgroup | grep freezer\fP
1037:freezer:/
104$ \fBcat /proc/1/cgroup | grep freezer\fP
1057:freezer:/..
106$ \fBcat /proc/20124/cgroup | grep freezer\fP
1077:freezer:/../sub2
108.in
109.fi
110
111However, when we look in
112.IR /proc/self/mountinfo
113we see the following anomaly:
114
115.nf
116.in +4n
117# \fBcat /proc/self/mountinfo | grep freezer\fP
118155 145 0:32 /.. /sys/fs/cgroup/freezer ...
119.in
120.fi
121
e1b70806 122The fourth field of this file should show the
c736cecc
MK
123directory in the cgroup filesystem which forms the root of this mount.
124Since by the definition of cgroup namespaces, the process's current
125freezer cgroup directory became its root freezer cgroup directory,
126we should see \(aq/\(aq in this field.
127The problem here is that we are seeing a mount entry for the cgroup
128filesystem corresponding to our initial shell process's cgroup namespace
129(whose cgroup filesystem is indeed rooted in the parent directory of
130.IR sub ).
131We need to remount the freezer cgroup filesystem
132inside this cgroup namespace, after which we see the expected results:
133
134.nf
135.in +4n
3011d629 136# \fBmount \-\-make\-rslave /\fP # Don't propagate mount events
c736cecc 137 # to other namespaces
3011d629
MK
138# \fBumount /sys/fs/cgroup/freezer\fP
139# \fBmount \-t cgroup \-o freezer freezer /sys/fs/cgroup/freezer\fP
140# \fBcat /proc/self/mountinfo | grep freezer\fP
c736cecc
MK
141155 145 0:32 / /sys/fs/cgroup/freezer rw,relatime ...
142.in
143.fi
144
145Use of cgroup namespaces requires a kernel that is configured with the
146.B CONFIG_CGROUPS
147option.
148.\"
149.SH NOTES
150Among the purposes served by the
151virtualization provided by cgroup namespaces are the following:
152.IP * 2
153It prevents information leaks whereby cgroup directory paths outside of
154a container would otherwise be visible to processes in the container.
155Such leakages could, for example,
156reveal information about the container framework
157to containerized applications.
158.IP *
10b547c5
MK
159It eases tasks such as container migration.
160The virtualization provided by cgroup namespaces
161allows containers to be isolated from knowledge of
162the pathnames of ancestor cgroups.
0191a7b9
MK
163Without such isolation, the full cgroup pathnames (displayed in
164.IR /proc/self/cgroups )
165would need to be replicated on the target system when migrating a container;
10b547c5
MK
166those pathnames would also need to be unique,
167so that they don't conflict with other pathnames on the target system.
168.IP *
a531b2cf 169It allows better confinement of containerized processes,
a2b7dba5
MK
170because it is possible to mount the container's cgroup filesystems such that
171the container processes can't gain access to ancestor cgroup directories.
c736cecc
MK
172Consider, for example, the following scenario:
173.RS 4
174.IP \(bu 2
175We have a cgroup directory,
176.IR /cg/1 ,
177that is owned by user ID 9000.
178.IP \(bu
179We have a process,
180.IR X ,
181also owned by user ID 9000,
182that is namespaced under the cgroup
183.IR /cg/1/2
184(i.e.,
185.I X
186was placed in a new cgroup namespace via
187.BR clone (2)
188or
189.BR unshare (2)
190with the
191.BR CLONE_NEWCGROUP
192flag).
193.RE
194.IP
195In the absence of cgroup namespacing, because the cgroup directory
196.IR /cg/1
ef6f9539 197is owned (and writable) by UID 9000 and process
bcedc0c2
MK
198.I X
199is also owned by user ID 9000, then process
200.I X
201would be able to modify the contents of cgroups files
202(i.e., change cgroup settings) not only in
c736cecc
MK
203.IR /cg/1/2
204but also in the ancestor cgroup directory
205.IR /cg/1 .
206Namespacing process
207.IR X
208under the cgroup directory
cc267b37
MK
209.IR /cg/1/2 ,
210in combination with suitable mount operations
211for the cgroup filesystem (as shown above),
c736cecc
MK
212prevents it modifying files in
213.IR /cg/1 ,
214since it cannot even see the contents of that directory
215(or of further removed cgroup ancestor directories).
216Combined with correct enforcement of hierarchical limits,
3ab5cbda 217this prevents process X from escaping the limits imposed
c736cecc
MK
218by ancestor cgroups.
219.SH CONFORMING TO
220Namespaces are a Linux-specific feature.
221.SH SEE ALSO
222.BR unshare (1),
223.BR clone (2),
224.BR setns (2),
225.BR unshare (2),
226.BR proc (5),
227.BR cgroups (7),
228.BR credentials (7),
229.BR user_namespaces (7)