Changes.old: tfix
[man-pages.git] / man2 / openat2.2
blobe2a11162d5201282cda42dc7b7ed099a2afdfc13
1 .\" Copyright (C) 2019 Aleksa Sarai <cyphar@cyphar.com>
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .TH openat2 2 (date) "Linux man-pages (unreleased)"
5 .SH NAME
6 openat2 \- open and possibly create a file (extended)
7 .SH LIBRARY
8 Standard C library
9 .RI ( libc ", " \-lc )
10 .SH SYNOPSIS
11 .nf
12 .BR "#include <fcntl.h>" \
13 "          /* Definition of " O_* " and " S_* " constants */"
14 .BR "#include <linux/openat2.h>" "  /* Definition of " RESOLVE_* " constants */"
15 .BR "#include <sys/syscall.h>" "    /* Definition of " SYS_* " constants */"
16 .B #include <unistd.h>
18 .BI "long syscall(SYS_openat2, int " dirfd ", const char *" pathname ,
19 .BI "             struct open_how *" how ", size_t " size );
20 .fi
22 .IR Note :
23 glibc provides no wrapper for
24 .BR openat2 (),
25 necessitating the use of
26 .BR syscall (2).
27 .SH DESCRIPTION
28 The
29 .BR openat2 ()
30 system call is an extension of
31 .BR openat (2)
32 and provides a superset of its functionality.
34 The
35 .BR openat2 ()
36 system call opens the file specified by
37 .IR pathname .
38 If the specified file does not exist, it may optionally (if
39 .B O_CREAT
40 is specified in
41 .IR how.flags )
42 be created.
44 As with
45 .BR openat (2),
47 .I pathname
48 is a relative pathname, then it is interpreted relative to the
49 directory referred to by the file descriptor
50 .I dirfd
51 (or the current working directory of the calling process, if
52 .I dirfd
53 is the special value
54 .BR AT_FDCWD ).
56 .I pathname
57 is an absolute pathname, then
58 .I dirfd
59 is ignored (unless
60 .I how.resolve
61 contains
62 .BR RESOLVE_IN_ROOT ,
63 in which case
64 .I pathname
65 is resolved relative to
66 .IR dirfd ).
68 Rather than taking a single
69 .I flags
70 argument, an extensible structure (\fIhow\fP) is passed to allow for
71 future extensions.
72 The
73 .I size
74 argument must be specified as
75 .IR "sizeof(struct open_how)" .
76 .\"
77 .SS The open_how structure
78 The
79 .I how
80 argument specifies how
81 .I pathname
82 should be opened, and acts as a superset of the
83 .I flags
84 and
85 .I mode
86 arguments to
87 .BR openat (2).
88 This argument is a pointer to an
89 .I open_how
90 structure,
91 described in
92 .BR open_how (2type).
94 Any future extensions to
95 .BR openat2 ()
96 will be implemented as new fields appended to the
97 .I open_how
98 structure,
99 with a zero value in a new field resulting in the kernel behaving
100 as though that extension field was not present.
101 Therefore, the caller
102 .I must
103 zero-fill this structure on
104 initialization.
105 (See the "Extensibility" section of the
106 .B NOTES
107 for more detail on why this is necessary.)
109 The fields of the
110 .I open_how
111 structure are as follows:
113 .I flags
114 This field specifies
115 the file creation and file status flags to use when opening the file.
116 All of the
117 .B O_*
118 flags defined for
119 .BR openat (2)
120 are valid
121 .BR openat2 ()
122 flag values.
124 Whereas
125 .BR openat (2)
126 ignores unknown bits in its
127 .I flags
128 argument,
129 .BR openat2 ()
130 returns an error if unknown or conflicting flags are specified in
131 .IR how.flags .
133 .I mode
134 This field specifies the
135 mode for the new file, with identical semantics to the
136 .I mode
137 argument of
138 .BR openat (2).
140 Whereas
141 .BR openat (2)
142 ignores bits other than those in the range
143 .I 07777
144 in its
145 .I mode
146 argument,
147 .BR openat2 ()
148 returns an error if
149 .I how.mode
150 contains bits other than
151 .IR 07777 .
152 Similarly, an error is returned if
153 .BR openat2 ()
154 is called with a nonzero
155 .I how.mode
157 .I how.flags
158 does not contain
159 .B O_CREAT
161 .BR O_TMPFILE .
163 .I resolve
164 This is a bit-mask of flags that modify the way in which
165 .B all
166 components of
167 .I pathname
168 will be resolved.
169 (See
170 .BR path_resolution (7)
171 for background information.)
173 The primary use case for these flags is to allow trusted programs to restrict
174 how untrusted paths (or paths inside untrusted directories) are resolved.
175 The full list of
176 .I resolve
177 flags is as follows:
180 .B RESOLVE_BENEATH
181 .\" commit adb21d2b526f7f196b2f3fdca97d80ba05dd14a0
182 Do not permit the path resolution to succeed if any component of the resolution
183 is not a descendant of the directory indicated by
184 .IR dirfd .
185 This causes absolute symbolic links (and absolute values of
186 .IR pathname )
187 to be rejected.
189 Currently, this flag also disables magic-link resolution (see below).
190 However, this may change in the future.
191 Therefore, to ensure that magic links are not resolved,
192 the caller should explicitly specify
193 .BR RESOLVE_NO_MAGICLINKS .
195 .B RESOLVE_IN_ROOT
196 .\" commit 8db52c7e7ee1bd861b6096fcafc0fe7d0f24a994
197 Treat the directory referred to by
198 .I dirfd
199 as the root directory while resolving
200 .IR pathname .
201 Absolute symbolic links are interpreted relative to
202 .IR dirfd .
203 If a prefix component of
204 .I pathname
205 equates to
206 .IR dirfd ,
207 then an immediately following
208 .I ..\&
209 component likewise equates to
210 .I dirfd
211 (just as
212 .I /..\&
213 is traditionally equivalent to
214 .IR / ).
216 .I pathname
217 is an absolute path, it is also interpreted relative to
218 .IR dirfd .
220 The effect of this flag is as though the calling process had used
221 .BR chroot (2)
222 to (temporarily) modify its root directory (to the directory
223 referred to by
224 .IR dirfd ).
225 However, unlike
226 .BR chroot (2)
227 (which changes the filesystem root permanently for a process),
228 .B RESOLVE_IN_ROOT
229 allows a program to efficiently restrict path resolution on a per-open basis.
231 Currently, this flag also disables magic-link resolution.
232 However, this may change in the future.
233 Therefore, to ensure that magic links are not resolved,
234 the caller should explicitly specify
235 .BR RESOLVE_NO_MAGICLINKS .
237 .B RESOLVE_NO_MAGICLINKS
238 .\" commit 278121417a72d87fb29dd8c48801f80821e8f75a
239 Disallow all magic-link resolution during path resolution.
241 Magic links are symbolic link-like objects that are most notably found in
242 .BR proc (5);
243 examples include
244 .IR /proc/ pid /exe
246 .IR /proc/ pid /fd/* .
247 (See
248 .BR symlink (7)
249 for more details.)
251 Unknowingly opening magic links can be risky for some applications.
252 Examples of such risks include the following:
254 .IP \[bu] 3
255 If the process opening a pathname is a controlling process that
256 currently has no controlling terminal (see
257 .BR credentials (7)),
258 then opening a magic link inside
259 .IR /proc/ pid /fd
260 that happens to refer to a terminal
261 would cause the process to acquire a controlling terminal.
262 .IP \[bu]
263 .\" From https://lwn.net/Articles/796868/:
264 .\"     The presence of this flag will prevent a path lookup operation
265 .\"     from traversing through one of these magic links, thus blocking
266 .\"     (for example) attempts to escape from a container via a /proc
267 .\"     entry for an open file descriptor.
268 In a containerized environment,
269 a magic link inside
270 .I /proc
271 may refer to an object outside the container,
272 and thus may provide a means to escape from the container.
275 Because of such risks,
276 an application may prefer to disable magic link resolution using the
277 .B RESOLVE_NO_MAGICLINKS
278 flag.
280 If the trailing component (i.e., basename) of
281 .I pathname
282 is a magic link,
283 .I how.resolve
284 contains
285 .BR RESOLVE_NO_MAGICLINKS ,
287 .I how.flags
288 contains both
289 .B O_PATH
291 .BR O_NOFOLLOW ,
292 then an
293 .B O_PATH
294 file descriptor referencing the magic link will be returned.
296 .B RESOLVE_NO_SYMLINKS
297 .\" commit 278121417a72d87fb29dd8c48801f80821e8f75a
298 Disallow resolution of symbolic links during path resolution.
299 This option implies
300 .BR RESOLVE_NO_MAGICLINKS .
302 If the trailing component (i.e., basename) of
303 .I pathname
304 is a symbolic link,
305 .I how.resolve
306 contains
307 .BR RESOLVE_NO_SYMLINKS ,
309 .I how.flags
310 contains both
311 .B O_PATH
313 .BR O_NOFOLLOW ,
314 then an
315 .B O_PATH
316 file descriptor referencing the symbolic link will be returned.
318 Note that the effect of the
319 .B RESOLVE_NO_SYMLINKS
320 flag,
321 which affects the treatment of symbolic links in all of the components of
322 .IR pathname ,
323 differs from the effect of the
324 .B O_NOFOLLOW
325 file creation flag (in
326 .IR how.flags ),
327 which affects the handling of symbolic links only in the final component of
328 .IR pathname .
330 Applications that employ the
331 .B RESOLVE_NO_SYMLINKS
332 flag are encouraged to make its use configurable
333 (unless it is used for a specific security purpose),
334 as symbolic links are very widely used by end-users.
335 Setting this flag indiscriminately\[em]i.e.,
336 for purposes not specifically related to security\[em]for all uses of
337 .BR openat2 ()
338 may result in spurious errors on previously functional systems.
339 This may occur if, for example,
340 a system pathname that is used by an application is modified
341 (e.g., in a new distribution release)
342 so that a pathname component (now) contains a symbolic link.
344 .B RESOLVE_NO_XDEV
345 .\" commit 72ba29297e1439efaa54d9125b866ae9d15df339
346 Disallow traversal of mount points during path resolution (including all bind
347 mounts).
348 Consequently,
349 .I pathname
350 must either be on the same mount as the directory referred to by
351 .IR dirfd ,
352 or on the same mount as the current working directory if
353 .I dirfd
354 is specified as
355 .BR AT_FDCWD .
357 Applications that employ the
358 .B RESOLVE_NO_XDEV
359 flag are encouraged to make its use configurable (unless it is
360 used for a specific security purpose),
361 as bind mounts are widely used by end-users.
362 Setting this flag indiscriminately\[em]i.e.,
363 for purposes not specifically related to security\[em]for all uses of
364 .BR openat2 ()
365 may result in spurious errors on previously functional systems.
366 This may occur if, for example,
367 a system pathname that is used by an application is modified
368 (e.g., in a new distribution release)
369 so that a pathname component (now) contains a bind mount.
371 .B RESOLVE_CACHED
372 Make the open operation fail unless all path components are already present
373 in the kernel's lookup cache.
374 If any kind of revalidation or I/O is needed to satisfy the lookup,
375 .BR openat2 ()
376 fails with the error
377 .BR EAGAIN .
378 This is useful in providing a fast-path open that can be performed without
379 resorting to thread offload, or other mechanisms that an application might
380 use to offload slower operations.
383 If any bits other than those listed above are set in
384 .IR how.resolve ,
385 an error is returned.
386 .SH RETURN VALUE
387 On success, a new file descriptor is returned.
388 On error, \-1 is returned, and
389 .I errno
390 is set to indicate the error.
391 .SH ERRORS
392 The set of errors returned by
393 .BR openat2 ()
394 includes all of the errors returned by
395 .BR openat (2),
396 as well as the following additional errors:
398 .B E2BIG
399 An extension that this kernel does not support was specified in
400 .IR how .
401 (See the "Extensibility" section of
402 .B NOTES
403 for more detail on how extensions are handled.)
405 .B EAGAIN
406 .I how.resolve
407 contains either
408 .B RESOLVE_IN_ROOT
410 .BR RESOLVE_BENEATH ,
411 and the kernel could not ensure that a ".." component didn't escape (due to a
412 race condition or potential attack).
413 The caller may choose to retry the
414 .BR openat2 ()
415 call.
417 .B EAGAIN
418 .B RESOLVE_CACHED
419 was set, and the open operation cannot be performed using only cached
420 information.
421 The caller should retry without
422 .B RESOLVE_CACHED
423 set in
424 .IR how.resolve .
426 .B EINVAL
427 An unknown flag or invalid value was specified in
428 .IR how .
430 .B EINVAL
431 .I mode
432 is nonzero, but
433 .I how.flags
434 does not contain
435 .B O_CREAT
437 .BR O_TMPFILE .
439 .B EINVAL
440 .I size
441 was smaller than any known version of
442 .IR "struct open_how" .
444 .B ELOOP
445 .I how.resolve
446 contains
447 .BR RESOLVE_NO_SYMLINKS ,
448 and one of the path components was a symbolic link (or magic link).
450 .B ELOOP
451 .I how.resolve
452 contains
453 .BR RESOLVE_NO_MAGICLINKS ,
454 and one of the path components was a magic link.
456 .B EXDEV
457 .I how.resolve
458 contains either
459 .B RESOLVE_IN_ROOT
461 .BR RESOLVE_BENEATH ,
462 and an escape from the root during path resolution was detected.
464 .B EXDEV
465 .I how.resolve
466 contains
467 .BR RESOLVE_NO_XDEV ,
468 and a path component crosses a mount point.
469 .SH STANDARDS
470 Linux.
471 .SH HISTORY
472 Linux 5.6.
473 .\" commit fddb5d430ad9fa91b49b1d34d0202ffe2fa0e179
475 The semantics of
476 .B RESOLVE_BENEATH
477 were modeled after FreeBSD's
478 .BR O_BENEATH .
479 .SH NOTES
480 .SS Extensibility
481 In order to allow for future extensibility,
482 .BR openat2 ()
483 requires the user-space application to specify the size of the
484 .I open_how
485 structure that it is passing.
486 By providing this information, it is possible for
487 .BR openat2 ()
488 to provide both forwards- and backwards-compatibility, with
489 .I size
490 acting as an implicit version number.
491 (Because new extension fields will always
492 be appended, the structure size will always increase.)
493 This extensibility design is very similar to other system calls such as
494 .BR sched_setattr (2),
495 .BR perf_event_open (2),
497 .BR clone3 (2).
499 If we let
500 .I usize
501 be the size of the structure as specified by the user-space application, and
502 .I ksize
503 be the size of the structure which the kernel supports, then there are
504 three cases to consider:
505 .IP \[bu] 3
507 .I ksize
508 equals
509 .IR usize ,
510 then there is no version mismatch and
511 .I how
512 can be used verbatim.
513 .IP \[bu]
515 .I ksize
516 is larger than
517 .IR usize ,
518 then there are some extension fields that the kernel supports
519 which the user-space application
520 is unaware of.
521 Because a zero value in any added extension field signifies a no-op,
522 the kernel
523 treats all of the extension fields not provided by the user-space application
524 as having zero values.
525 This provides backwards-compatibility.
526 .IP \[bu]
528 .I ksize
529 is smaller than
530 .IR usize ,
531 then there are some extension fields which the user-space application
532 is aware of but which the kernel does not support.
533 Because any extension field must have its zero values signify a no-op,
534 the kernel can
535 safely ignore the unsupported extension fields if they are all-zero.
536 If any unsupported extension fields are nonzero, then \-1 is returned and
537 .I errno
538 is set to
539 .BR E2BIG .
540 This provides forwards-compatibility.
542 Because the definition of
543 .I struct open_how
544 may change in the future (with new fields being added when system headers are
545 updated), user-space applications should zero-fill
546 .I struct open_how
547 to ensure that recompiling the program with new headers will not result in
548 spurious errors at run time.
549 The simplest way is to use a designated
550 initializer:
552 .in +4n
554 struct open_how how = { .flags = O_RDWR,
555                         .resolve = RESOLVE_IN_ROOT };
559 or explicitly using
560 .BR memset (3)
561 or similar:
563 .in +4n
565 struct open_how how;
566 memset(&how, 0, sizeof(how));
567 how.flags = O_RDWR;
568 how.resolve = RESOLVE_IN_ROOT;
572 A user-space application that wishes to determine which extensions
573 the running kernel supports can do so by conducting a binary search on
574 .I size
575 with a structure which has every byte nonzero (to find the largest value
576 which doesn't produce an error of
577 .BR E2BIG ).
578 .SH SEE ALSO
579 .BR openat (2),
580 .BR open_how (2type),
581 .BR path_resolution (7),
582 .BR symlink (7)