1 .\" Copyright (C) 2019 Aleksa Sarai <cyphar@cyphar.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .TH OPENAT2 2 2022-09-09 "Linux man-pages (unreleased)"
6 openat2 \- open and possibly create a file (extended)
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 );
23 glibc provides no wrapper for
25 necessitating the use of
30 system call is an extension of
32 and provides a superset of its functionality.
36 system call opens the file specified by
38 If the specified file does not exist, it may optionally (if
48 is a relative pathname, then it is interpreted relative to the
49 directory referred to by the file descriptor
51 (or the current working directory of the calling process, if
57 is an absolute pathname, then
65 is resolved relative to
68 Rather than taking a single
70 argument, an extensible structure (\fIhow\fP) is passed to allow for
74 argument must be specified as
75 .IR "sizeof(struct open_how)" .
77 .SS The open_how structure
80 argument specifies how
82 should be opened, and acts as a superset of the
88 This argument is a pointer to an
94 Any future extensions to
96 will be implemented as new fields appended to the
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
103 zero-fill this structure on
105 (See the "Extensibility" section of the
107 for more detail on why this is necessary.)
111 structure are as follows:
115 the file creation and file status flags to use when opening the file.
126 ignores unknown bits in its
130 returns an error if unknown or conflicting flags are specified in
134 This field specifies the
135 mode for the new file, with identical semantics to the
142 ignores bits other than those in the range
150 contains bits other than
152 Similarly, an error is returned if
154 is called with a nonzero
164 This is a bit-mask of flags that modify the way in which
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.
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
185 This causes absolute symbolic links (and absolute values of
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 .
196 .\" commit 8db52c7e7ee1bd861b6096fcafc0fe7d0f24a994
197 Treat the directory referred to by
199 as the root directory while resolving
201 Absolute symbolic links are interpreted relative to
203 If a prefix component of
207 then an immediately following
209 component likewise equates to
213 is traditionally equivalent to
217 is an absolute path, it is also interpreted relative to
220 The effect of this flag is as though the calling process had used
222 to (temporarily) modify its root directory (to the directory
227 (which changes the filesystem root permanently for a process),
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
246 .IR /proc/ pid /fd/* .
251 Unknowingly opening magic links can be risky for some applications.
252 Examples of such risks include the following:
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
260 that happens to refer to a terminal
261 would cause the process to acquire a controlling terminal.
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,
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
280 If the trailing component (i.e., basename) of
285 .BR RESOLVE_NO_MAGICLINKS ,
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.
300 .BR RESOLVE_NO_MAGICLINKS .
302 If the trailing component (i.e., basename) of
307 .BR RESOLVE_NO_SYMLINKS ,
316 file descriptor referencing the symbolic link will be returned.
318 Note that the effect of the
319 .B RESOLVE_NO_SYMLINKS
321 which affects the treatment of symbolic links in all of the components of
323 differs from the effect of the
325 file creation flag (in
327 which affects the handling of symbolic links only in the final component of
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\(emi.e.,
336 for purposes not specifically related to security\(emfor all uses of
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.
345 .\" commit 72ba29297e1439efaa54d9125b866ae9d15df339
346 Disallow traversal of mount points during path resolution (including all bind
350 must either be on the same mount as the directory referred to by
352 or on the same mount as the current working directory if
357 Applications that employ the
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\(emi.e.,
363 for purposes not specifically related to security\(emfor all uses of
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.
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,
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
385 an error is returned.
387 On success, a new file descriptor is returned.
388 On error, \-1 is returned, and
390 is set to indicate the error.
392 The set of errors returned by
394 includes all of the errors returned by
396 as well as the following additional errors:
399 An extension that this kernel does not support was specified in
401 (See the "Extensibility" section of
403 for more detail on how extensions are handled.)
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
419 was set, and the open operation cannot be performed using only cached
421 The caller should retry without
427 An unknown flag or invalid value was specified in
441 was smaller than any known version of
442 .IR "struct open_how" .
447 .BR RESOLVE_NO_SYMLINKS ,
448 and one of the path components was a symbolic link (or magic link).
453 .BR RESOLVE_NO_MAGICLINKS ,
454 and one of the path components was a magic link.
461 .BR RESOLVE_BENEATH ,
462 and an escape from the root during path resolution was detected.
467 .BR RESOLVE_NO_XDEV ,
468 and a path component crosses a mount point.
471 first appeared in Linux 5.6.
472 .\" commit fddb5d430ad9fa91b49b1d34d0202ffe2fa0e179
474 This system call is Linux-specific.
478 were modeled after FreeBSD's
482 In order to allow for future extensibility,
484 requires the user-space application to specify the size of the
486 structure that it is passing.
487 By providing this information, it is possible for
489 to provide both forwards- and backwards-compatibility, with
491 acting as an implicit version number.
492 (Because new extension fields will always
493 be appended, the structure size will always increase.)
494 This extensibility design is very similar to other system calls such as
495 .BR sched_setattr (2),
496 .BR perf_event_open (2),
502 be the size of the structure as specified by the user-space application, and
504 be the size of the structure which the kernel supports, then there are
505 three cases to consider:
511 then there is no version mismatch and
513 can be used verbatim.
519 then there are some extension fields that the kernel supports
520 which the user-space application
522 Because a zero value in any added extension field signifies a no-op,
524 treats all of the extension fields not provided by the user-space application
525 as having zero values.
526 This provides backwards-compatibility.
532 then there are some extension fields which the user-space application
533 is aware of but which the kernel does not support.
534 Because any extension field must have its zero values signify a no-op,
536 safely ignore the unsupported extension fields if they are all-zero.
537 If any unsupported extension fields are nonzero, then \-1 is returned and
541 This provides forwards-compatibility.
543 Because the definition of
545 may change in the future (with new fields being added when system headers are
546 updated), user-space applications should zero-fill
548 to ensure that recompiling the program with new headers will not result in
549 spurious errors at runtime.
550 The simplest way is to use a designated
555 struct open_how how = { .flags = O_RDWR,
556 .resolve = RESOLVE_IN_ROOT };
567 memset(&how, 0, sizeof(how));
569 how.resolve = RESOLVE_IN_ROOT;
573 A user-space application that wishes to determine which extensions
574 the running kernel supports can do so by conducting a binary search on
576 with a structure which has every byte nonzero (to find the largest value
577 which doesn't produce an error of
581 .BR open_how (2type),
582 .BR path_resolution (7),