^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Author: Aleksa Sarai <cyphar@cyphar.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2018-2019 SUSE LLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #ifndef __RESOLVEAT_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define __RESOLVEAT_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); })))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #ifndef SYS_openat2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #ifndef __NR_openat2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define __NR_openat2 437
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif /* __NR_openat2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SYS_openat2 __NR_openat2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #endif /* SYS_openat2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Arguments for how openat2(2) should open the target path. If @resolve is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * zero, then openat2(2) operates very similarly to openat(2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * However, unlike openat(2), unknown bits in @flags result in -EINVAL rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * than being silently ignored. @mode must be zero unless one of {O_CREAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * O_TMPFILE} are set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @flags: O_* flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @mode: O_CREAT/O_TMPFILE file mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @resolve: RESOLVE_* flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct open_how {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __u64 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) __u64 resolve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define OPEN_HOW_SIZE_VER0 24 /* sizeof first published struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define OPEN_HOW_SIZE_LATEST OPEN_HOW_SIZE_VER0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) bool needs_openat2(const struct open_how *how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #ifndef RESOLVE_IN_ROOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* how->resolve flags for openat2(2). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define RESOLVE_NO_XDEV 0x01 /* Block mount-point crossings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) (includes bind-mounts). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define RESOLVE_NO_MAGICLINKS 0x02 /* Block traversal through procfs-style
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) "magic-links". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define RESOLVE_NO_SYMLINKS 0x04 /* Block traversal through all symlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) (implies OEXT_NO_MAGICLINKS) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define RESOLVE_BENEATH 0x08 /* Block "lexical" trickery like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "..", symlinks, and absolute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) paths which escape the dirfd. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define RESOLVE_IN_ROOT 0x10 /* Make all jumps to "/" and ".."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) be scoped inside the dirfd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) (similar to chroot(2)). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #endif /* RESOLVE_IN_ROOT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define E_func(func, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) errno = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (func(__VA_ARGS__) < 0) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ksft_exit_fail_msg("%s:%d %s failed - errno:%d\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) __FILE__, __LINE__, #func, errno); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define E_asprintf(...) E_func(asprintf, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define E_chmod(...) E_func(chmod, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define E_dup2(...) E_func(dup2, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define E_fchdir(...) E_func(fchdir, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define E_fstatat(...) E_func(fstatat, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define E_kill(...) E_func(kill, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define E_mkdirat(...) E_func(mkdirat, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define E_mount(...) E_func(mount, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define E_prctl(...) E_func(prctl, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define E_readlink(...) E_func(readlink, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define E_setresuid(...) E_func(setresuid, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define E_symlinkat(...) E_func(symlinkat, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define E_touchat(...) E_func(touchat, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define E_unshare(...) E_func(unshare, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define E_assert(expr, msg, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!(expr)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ksft_exit_fail_msg("ASSERT(%s:%d) failed (%s): " msg "\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __FILE__, __LINE__, #expr, ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int raw_openat2(int dfd, const char *path, void *how, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int sys_openat2(int dfd, const char *path, struct open_how *how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int sys_openat(int dfd, const char *path, struct open_how *how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int sys_renameat2(int olddirfd, const char *oldpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int newdirfd, const char *newpath, unsigned int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int touchat(int dfd, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) char *fdreadlink(int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) bool fdequal(int fd, int dfd, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) extern bool openat2_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif /* __RESOLVEAT_H__ */