]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/cgroup_namespaces.7
cgroup_namespaces.7: Remove redundant use of 'sh -c' in shell session
[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 First, (as superuser) we create a child cgroup in the
71 .I freezer
72 hierarchy, and put the shell into that cgroup:
73 .PP
74 .in +4n
75 .EX
76 # \fBmkdir \-p /sys/fs/cgroup/freezer/sub\fP
77 # \fBecho $$\fP # Show PID of this shell
78 30655
79 # \fBecho 30655 > /sys/fs/cgroup/freezer/sub/cgroup.procs\fP
80 # \fBcat /proc/self/cgroup | grep freezer\fP
81 7:freezer:/sub
82 .EE
83 .in
84 .PP
85 Next, we use
86 .BR unshare (1)
87 to create a process running a new shell in new cgroup and mount namespaces:
88 .PP
89 .EX
90 .in +4n
91 # \fBunshare \-Cm bash\fP
92 .in
93 .EE
94 .PP
95 We then inspect the
96 .IR /proc/[pid]/cgroup
97 files of, respectively, the new shell process started by the
98 .BR unshare (1)
99 command, a process that is in the original cgroup namespace
100 .RI ( init ,
101 with PID 1), and a process in a sibling cgroup
102 .RI ( sub2 ):
103 .PP
104 .EX
105 .in +4n
106 $ \fBcat /proc/self/cgroup | grep freezer\fP
107 7:freezer:/
108 $ \fBcat /proc/1/cgroup | grep freezer\fP
109 7:freezer:/..
110 $ \fBcat /proc/20124/cgroup | grep freezer\fP
111 7:freezer:/../sub2
112 .in
113 .EE
114 .PP
115 From the output of the first command,
116 we see that the freezer cgroup membership of the new shell
117 (which is in the same cgroup as the initial shell)
118 is shown defined relative to the freezer cgroup root directory
119 that was established when the new cgroup namespace was created.
120 (In absolute terms,
121 the new shell is in the
122 .I /sub
123 freezer cgroup,
124 and the root directory of the freezer cgroup hierarchy
125 in the new cgroup namespace is also
126 .IR /sub .
127 Thus, the new shell's cgroup membership is displayed as \(aq/\(aq.)
128 .PP
129 However, when we look in
130 .IR /proc/self/mountinfo
131 we see the following anomaly:
132 .PP
133 .EX
134 .in +4n
135 # \fBcat /proc/self/mountinfo | grep freezer\fP
136 155 145 0:32 /.. /sys/fs/cgroup/freezer ...
137 .in
138 .EE
139 .PP
140 The fourth field of this line
141 .RI ( /.. )
142 should show the
143 directory in the cgroup filesystem which forms the root of this mount.
144 Since by the definition of cgroup namespaces, the process's current
145 freezer cgroup directory became its root freezer cgroup directory,
146 we should see \(aq/\(aq in this field.
147 The problem here is that we are seeing a mount entry for the cgroup
148 filesystem corresponding to our initial shell process's cgroup namespace
149 (whose cgroup filesystem is indeed rooted in the parent directory of
150 .IR sub ).
151 We need to remount the freezer cgroup filesystem
152 inside this cgroup namespace, after which we see the expected results:
153 .PP
154 .EX
155 .in +4n
156 # \fBmount \-\-make\-rslave /\fP # Don't propagate mount events
157 # to other namespaces
158 # \fBumount /sys/fs/cgroup/freezer\fP
159 # \fBmount \-t cgroup \-o freezer freezer /sys/fs/cgroup/freezer\fP
160 # \fBcat /proc/self/mountinfo | grep freezer\fP
161 155 145 0:32 / /sys/fs/cgroup/freezer rw,relatime ...
162 .in
163 .EE
164 .PP
165 Use of cgroup namespaces requires a kernel that is configured with the
166 .B CONFIG_CGROUPS
167 option.
168 .\"
169 .SH CONFORMING TO
170 Namespaces are a Linux-specific feature.
171 .SH NOTES
172 The virtualization provided by cgroup namespaces serves a number of purposes:
173 .IP * 2
174 It prevents information leaks whereby cgroup directory paths outside of
175 a container would otherwise be visible to processes in the container.
176 Such leakages could, for example,
177 reveal information about the container framework
178 to containerized applications.
179 .IP *
180 It eases tasks such as container migration.
181 The virtualization provided by cgroup namespaces
182 allows containers to be isolated from knowledge of
183 the pathnames of ancestor cgroups.
184 Without such isolation, the full cgroup pathnames (displayed in
185 .IR /proc/self/cgroups )
186 would need to be replicated on the target system when migrating a container;
187 those pathnames would also need to be unique,
188 so that they don't conflict with other pathnames on the target system.
189 .IP *
190 It allows better confinement of containerized processes,
191 because it is possible to mount the container's cgroup filesystems such that
192 the container processes can't gain access to ancestor cgroup directories.
193 Consider, for example, the following scenario:
194 .RS 4
195 .IP \(bu 2
196 We have a cgroup directory,
197 .IR /cg/1 ,
198 that is owned by user ID 9000.
199 .IP \(bu
200 We have a process,
201 .IR X ,
202 also owned by user ID 9000,
203 that is namespaced under the cgroup
204 .IR /cg/1/2
205 (i.e.,
206 .I X
207 was placed in a new cgroup namespace via
208 .BR clone (2)
209 or
210 .BR unshare (2)
211 with the
212 .BR CLONE_NEWCGROUP
213 flag).
214 .RE
215 .IP
216 In the absence of cgroup namespacing, because the cgroup directory
217 .IR /cg/1
218 is owned (and writable) by UID 9000 and process
219 .I X
220 is also owned by user ID 9000, then process
221 .I X
222 would be able to modify the contents of cgroups files
223 (i.e., change cgroup settings) not only in
224 .IR /cg/1/2
225 but also in the ancestor cgroup directory
226 .IR /cg/1 .
227 Namespacing process
228 .IR X
229 under the cgroup directory
230 .IR /cg/1/2 ,
231 in combination with suitable mount operations
232 for the cgroup filesystem (as shown above),
233 prevents it modifying files in
234 .IR /cg/1 ,
235 since it cannot even see the contents of that directory
236 (or of further removed cgroup ancestor directories).
237 Combined with correct enforcement of hierarchical limits,
238 this prevents process
239 .I X
240 from escaping the limits imposed by ancestor cgroups.
241 .SH SEE ALSO
242 .BR unshare (1),
243 .BR clone (2),
244 .BR setns (2),
245 .BR unshare (2),
246 .BR proc (5),
247 .BR cgroups (7),
248 .BR credentials (7),
249 .BR namespaces (7),
250 .BR user_namespaces (7)