-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioctl_ns.html
453 lines (354 loc) · 13.1 KB
/
ioctl_ns.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:26:03 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>IOCTL_NS</title>
</head>
<body>
<h1 align="center">IOCTL_NS</h1>
<a href="#NAME">NAME</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">ioctl_ns -
ioctl() operations for Linux namespaces</p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>Discovering
namespace relationships</b> <br>
The following <b>ioctl</b>(2) operations are provided to
allow discovery of namespace relationships (see
<b>user_namespaces</b>(7) and <b>pid_namespaces</b>(7)). The
form of the calls is:</p>
<p style="margin-left:17%; margin-top: 1em">new_fd =
ioctl(fd, request);</p>
<p style="margin-left:11%; margin-top: 1em">In each case,
<i>fd</i> refers to a <i>/proc/[pid]/ns/*</i> file. Both
operations return a new file descriptor on success. <b><br>
NS_GET_USERNS</b> (since Linux 4.9)</p>
<p style="margin-left:22%;">Returns a file descriptor that
refers to the owning user namespace for the namespace
referred to by <i>fd</i>.</p>
<p style="margin-left:11%;"><b>NS_GET_PARENT</b> (since
Linux 4.9)</p>
<p style="margin-left:22%;">Returns a file descriptor that
refers to the parent namespace of the namespace referred to
by <i>fd</i>. This operation is valid only for hierarchical
namespaces (i.e., PID and user namespaces). For user
namespaces, <b>NS_GET_PARENT</b> is synonymous with
<b>NS_GET_USERNS</b>.</p>
<p style="margin-left:11%; margin-top: 1em">The new file
descriptor returned by these operations is opened with the
<b>O_RDONLY</b> and <b>O_CLOEXEC</b> (close-on-exec; see
<b>fcntl</b>(2)) flags.</p>
<p style="margin-left:11%; margin-top: 1em">By applying
<b>fstat</b>(2) to the returned file descriptor, one obtains
a <i>stat</i> structure whose <i>st_dev</i> (resident
device) and <i>st_ino</i> (inode number) fields together
identify the owning/parent namespace. This inode number can
be matched with the inode number of another
<i>/proc/[pid]/ns/{pid,user}</i> file to determine whether
that is the owning/parent namespace.</p>
<p style="margin-left:11%; margin-top: 1em">Either of these
<b>ioctl</b>(2) operations can fail with the following
errors:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPERM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The requested namespace is outside of the caller’s
namespace scope. This error can occur if, for example, the
owning user namespace is an ancestor of the caller’s
current user namespace. It can also occur on attempts to
obtain the parent of the initial user or PID namespace.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOTTY</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The operation is not supported by this kernel
version.</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Additionally,
the <b>NS_GET_PARENT</b> operation can fail with the
following error:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="63%">
<p style="margin-top: 1em"><i>fd</i> refers to a
nonhierarchical namespace.</p></td>
<td width="15%">
</td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">See the EXAMPLE
section for an example of the use of these operations.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Discovering
the namespace type</b> <br>
The <b>NS_GET_NSTYPE</b> operation (available since Linux
4.11) can be used to discover the type of namespace referred
to by the file descriptor <i>fd</i>:</p>
<p style="margin-left:17%; margin-top: 1em">nstype =
ioctl(fd, NS_GET_NSTYPE);</p>
<p style="margin-left:11%; margin-top: 1em"><i>fd</i>
refers to a <i>/proc/[pid]/ns/*</i> file.</p>
<p style="margin-left:11%; margin-top: 1em">The return
value is one of the <b>CLONE_NEW*</b> values that can be
specified to <b>clone</b>(2) or <b>unshare</b>(2) in order
to create a namespace.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Discovering
the owner of a user namespace</b> <br>
The <b>NS_GET_OWNER_UID</b> operation (available since Linux
4.11) can be used to discover the owner user ID of a user
namespace (i.e., the effective user ID of the process that
created the user namespace). The form of the call is:</p>
<p style="margin-left:17%; margin-top: 1em">uid_t uid; <br>
ioctl(fd, NS_GET_OWNER_UID, &uid);</p>
<p style="margin-left:11%; margin-top: 1em"><i>fd</i>
refers to a <i>/proc/[pid]/ns/user</i> file.</p>
<p style="margin-left:11%; margin-top: 1em">The owner user
ID is returned in the <i>uid_t</i> pointed to by the third
argument.</p>
<p style="margin-left:11%; margin-top: 1em">This operation
can fail with the following error:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="58%">
<p style="margin-top: 1em"><i>fd</i> does not refer to a
user namespace.</p></td>
<td width="20%">
</td></tr>
</table>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Any of the
above <b>ioctl</b>() operations can return the following
errors:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>ENOTTY</b></p></td>
<td width="2%"></td>
<td width="69%">
<p style="margin-top: 1em"><i>fd</i> does not refer to a
<i>/proc/[pid]/ns/*</i> file.</p></td>
<td width="9%">
</td></tr>
</table>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Namespaces and
the operations described on this page are a
Linux-specific.</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The example
shown below uses the <b>ioctl</b>(2) operations described
above to perform simple discovery of namespace
relationships. The following shell sessions show various
examples of the use of this program.</p>
<p style="margin-left:11%; margin-top: 1em">Trying to get
the parent of the initial user namespace fails, since it has
no parent:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>./ns_show
/proc/self/ns/user p</b> <br>
The parent namespace is outside your namespace scope</p>
<p style="margin-left:11%; margin-top: 1em">Create a
process running <b>sleep</b>(1) that resides in new user and
UTS namespaces, and show that the new UTS namespace is
associated with the new user namespace:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>unshare
-Uu sleep 1000 &</b> <br>
[1] 23235 <br>
$ <b>./ns_show /proc/23235/ns/uts u</b> <br>
Device/Inode of owning user namespace is: [0,3] / 4026532448
<br>
$ <b>readlink /proc/23235/ns/user</b> <br>
user:[4026532448]</p>
<p style="margin-left:11%; margin-top: 1em">Then show that
the parent of the new user namespace in the preceding
example is the initial user namespace:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>readlink
/proc/self/ns/user</b> <br>
user:[4026531837] <br>
$ <b>./ns_show /proc/23235/ns/user p</b> <br>
Device/Inode of parent namespace is: [0,3] / 4026531837</p>
<p style="margin-left:11%; margin-top: 1em">Start a shell
in a new user namespace, and show that from within this
shell, the parent user namespace can’t be discovered.
Similarly, the UTS namespace (which is associated with the
initial user namespace) can’t be discovered.</p>
<p style="margin-left:17%; margin-top: 1em">$
<b>PS1="sh2$ " unshare -U bash</b> <br>
sh2$ <b>./ns_show /proc/self/ns/user p</b> <br>
The parent namespace is outside your namespace scope <br>
sh2$ <b>./ns_show /proc/self/ns/uts u</b> <br>
The owning user namespace is outside your namespace
scope</p>
<p style="margin-left:11%; margin-top: 1em"><b>Program
source</b> <br>
/* ns_show.c</p>
<p style="margin-left:11%; margin-top: 1em">Licensed under
the GNU General Public License v2 or later. <br>
*/ <br>
#include <stdlib.h> <br>
#include <unistd.h> <br>
#include <stdio.h> <br>
#include <fcntl.h> <br>
#include <string.h> <br>
#include <sys/stat.h> <br>
#include <sys/ioctl.h> <br>
#include <errno.h> <br>
#include <sys/sysmacros.h></p>
<p style="margin-left:11%; margin-top: 1em">#ifndef
NS_GET_USERNS <br>
#define NSIO 0xb7 <br>
#define NS_GET_USERNS _IO(NSIO, 0x1) <br>
#define NS_GET_PARENT _IO(NSIO, 0x2) <br>
#endif</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
int fd, userns_fd, parent_fd; <br>
struct stat sb;</p>
<p style="margin-left:11%; margin-top: 1em">if (argc <
2) { <br>
fprintf(stderr, "Usage: %s /proc/[pid]/ns/[file]
[p|u]\n", <br>
argv[0]); <br>
fprintf(stderr, "\nDisplay the result of one or both
" <br>
"of NS_GET_USERNS (u) or NS_GET_PARENT (p)\n" <br>
"for the specified /proc/[pid]/ns/[file]. If neither
" <br>
"'p' nor 'u' is specified,\n" <br>
"NS_GET_USERNS is the default.\n"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">/* Obtain a
file descriptor for the 'ns' file specified <br>
in argv[1] */</p>
<p style="margin-left:11%; margin-top: 1em">fd =
open(argv[1], O_RDONLY); <br>
if (fd == -1) { <br>
perror("open"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">/* Obtain a
file descriptor for the owning user namespace and <br>
then obtain and display the inode number of that namespace
*/</p>
<p style="margin-left:11%; margin-top: 1em">if (argc < 3
|| strchr(argv[2], 'u')) { <br>
userns_fd = ioctl(fd, NS_GET_USERNS);</p>
<p style="margin-left:11%; margin-top: 1em">if (userns_fd
== -1) { <br>
if (errno == EPERM) <br>
printf("The owning user namespace is outside "
<br>
"your namespace scope\n"); <br>
else <br>
perror("ioctl-NS_GET_USERNS"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if
(fstat(userns_fd, &sb) == -1) { <br>
perror("fstat-userns"); <br>
exit(EXIT_FAILURE); <br>
} <br>
printf("Device/Inode of owning user namespace is:
" <br>
"[%lx,%lx] / %ld\n", <br>
(long) major(sb.st_dev), (long) minor(sb.st_dev), <br>
(long) sb.st_ino);</p>
<p style="margin-left:11%; margin-top: 1em">close(userns_fd);
<br>
}</p>
<p style="margin-left:11%; margin-top: 1em">/* Obtain a
file descriptor for the parent namespace and <br>
then obtain and display the inode number of that namespace
*/</p>
<p style="margin-left:11%; margin-top: 1em">if (argc > 2
&& strchr(argv[2], 'p')) { <br>
parent_fd = ioctl(fd, NS_GET_PARENT);</p>
<p style="margin-left:11%; margin-top: 1em">if (parent_fd
== -1) { <br>
if (errno == EINVAL) <br>
printf("Can' get parent namespace of a " <br>
"nonhierarchical namespace\n"); <br>
else if (errno == EPERM) <br>
printf("The parent namespace is outside " <br>
"your namespace scope\n"); <br>
else <br>
perror("ioctl-NS_GET_PARENT"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if
(fstat(parent_fd, &sb) == -1) { <br>
perror("fstat-parentns"); <br>
exit(EXIT_FAILURE); <br>
} <br>
printf("Device/Inode of parent namespace is: [%lx,%lx]
/ %ld\n", <br>
(long) major(sb.st_dev), (long) minor(sb.st_dev), <br>
(long) sb.st_ino);</p>
<p style="margin-left:11%; margin-top: 1em">close(parent_fd);
<br>
}</p>
<p style="margin-left:11%; margin-top: 1em">exit(EXIT_SUCCESS);
<br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>fstat</b>(2),
<b>ioctl</b>(2), <b>proc</b>(5), <b>namespaces</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>