]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/cgroup_namespaces.7
sched_setaffinity.2: tfix in example shell session (s/grep/egrep)
[thirdparty/man-pages.git] / man7 / cgroup_namespaces.7
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 2017-09-15 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 cgroup_namespaces \- overview of Linux cgroup namespaces
29 .SH DESCRIPTION
30 For an overview of namespaces, see
31 .BR namespaces (7).
32 .PP
33 Cgroup namespaces virtualize the view of a process's cgroups (see
34 .BR cgroups (7))
35 as seen via
36 .IR /proc/[pid]/cgroup
37 and
38 .IR /proc/[pid]/mountinfo .
39 .PP
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
44 file.
45 When a process creates a new cgroup namespace using
46 .BR clone (2)
47 or
48 .BR unshare (2)
49 with the
50 .BR CLONE_NEWCGROUP
51 flag, it enters a new cgroup namespace in which its current
52 cgroups directories become the cgroup root directories
53 of the new namespace.
54 (This applies both for the cgroups version 1 hierarchies
55 and the cgroups version 2 unified hierarchy.)
56 .PP
57 When viewing
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
65 .I ../
66 entries for each ancestor level in the cgroup hierarchy.
67 .PP
68 The following shell session demonstrates the effect of creating
69 a new cgroup namespace.
70 .PP
71 First, (as superuser) we create a child cgroup in the
72 .I freezer
73 hierarchy, and place a process in that cgroup that we will
74 use as part of the demonstration below:
75 .PP
76 .in +4n
77 .EX
78 # \fBmkdir \-p /sys/fs/cgroup/freezer/sub2\fP
79 # \fBsleep 10000 &\fP # Create a process that lives for a while
80 [1] 20124
81 # \fBecho 20124 > /sys/fs/cgroup/freezer/sub2/cgroup.procs\fP
82 .EE
83 .in
84 .PP
85 We then create another child cgroup in the
86 .I freezer
87 hierarchy and put the shell into that cgroup:
88 .PP
89 .in +4n
90 .EX
91 # \fBmkdir \-p /sys/fs/cgroup/freezer/sub\fP
92 # \fBecho $$\fP # Show PID of this shell
93 30655
94 # \fBecho 30655 > /sys/fs/cgroup/freezer/sub/cgroup.procs\fP
95 # \fBcat /proc/self/cgroup | grep freezer\fP
96 7:freezer:/sub
97 .EE
98 .in
99 .PP
100 Next, we use
101 .BR unshare (1)
102 to create a process running a new shell in new cgroup and mount namespaces:
103 .PP
104 .EX
105 .in +4n
106 # \fBunshare \-Cm bash\fP
107 .in
108 .EE
109 .PP
110 We then inspect the
111 .IR /proc/[pid]/cgroup
112 files of, respectively, the new shell process started by the
113 .BR unshare (1)
114 command, a process that is in the original cgroup namespace
115 .RI ( init ,
116 with PID 1), and the process in the sibling cgroup
117 .RI ( sub2 ):
118 .PP
119 .EX
120 .in +4n
121 $ \fBcat /proc/self/cgroup | grep freezer\fP
122 7:freezer:/
123 $ \fBcat /proc/1/cgroup | grep freezer\fP
124 7:freezer:/..
125 $ \fBcat /proc/20124/cgroup | grep freezer\fP
126 7:freezer:/../sub2
127 .in
128 .EE
129 .PP
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.
135 (In absolute terms,
136 the new shell is in the
137 .I /sub
138 freezer cgroup,
139 and the root directory of the freezer cgroup hierarchy
140 in the new cgroup namespace is also
141 .IR /sub .
142 Thus, the new shell's cgroup membership is displayed as \(aq/\(aq.)
143 .PP
144 However, when we look in
145 .IR /proc/self/mountinfo
146 we see the following anomaly:
147 .PP
148 .EX
149 .in +4n
150 # \fBcat /proc/self/mountinfo | grep freezer\fP
151 155 145 0:32 /.. /sys/fs/cgroup/freezer ...
152 .in
153 .EE
154 .PP
155 The fourth field of this line
156 .RI ( /.. )
157 should show the
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
165 .IR sub ).
166 We need to remount the freezer cgroup filesystem
167 inside this cgroup namespace, after which we see the expected results:
168 .PP
169 .EX
170 .in +4n
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 ...
177 .in
178 .EE
179 .\"
180 .SH CONFORMING TO
181 Namespaces are a Linux-specific feature.
182 .SH NOTES
183 Use of cgroup namespaces requires a kernel that is configured with the
184 .B CONFIG_CGROUPS
185 option.
186 .PP
187 The virtualization provided by cgroup namespaces serves a number of purposes:
188 .IP * 2
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.
194 .IP *
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.
204 .IP *
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:
209 .RS 4
210 .IP \(bu 2
211 We have a cgroup directory,
212 .IR /cg/1 ,
213 that is owned by user ID 9000.
214 .IP \(bu
215 We have a process,
216 .IR X ,
217 also owned by user ID 9000,
218 that is namespaced under the cgroup
219 .IR /cg/1/2
220 (i.e.,
221 .I X
222 was placed in a new cgroup namespace via
223 .BR clone (2)
224 or
225 .BR unshare (2)
226 with the
227 .BR CLONE_NEWCGROUP
228 flag).
229 .RE
230 .IP
231 In the absence of cgroup namespacing, because the cgroup directory
232 .IR /cg/1
233 is owned (and writable) by UID 9000 and process
234 .I X
235 is also owned by user ID 9000, then process
236 .I X
237 would be able to modify the contents of cgroups files
238 (i.e., change cgroup settings) not only in
239 .IR /cg/1/2
240 but also in the ancestor cgroup directory
241 .IR /cg/1 .
242 Namespacing process
243 .IR X
244 under the cgroup directory
245 .IR /cg/1/2 ,
246 in combination with suitable mount operations
247 for the cgroup filesystem (as shown above),
248 prevents it modifying files in
249 .IR /cg/1 ,
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
254 .I X
255 from escaping the limits imposed by ancestor cgroups.
256 .SH SEE ALSO
257 .BR unshare (1),
258 .BR clone (2),
259 .BR setns (2),
260 .BR unshare (2),
261 .BR proc (5),
262 .BR cgroups (7),
263 .BR credentials (7),
264 .BR namespaces (7),
265 .BR user_namespaces (7)