Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Author: Aleksa Sarai <cyphar@cyphar.com>
0004  * Copyright (C) 2018-2019 SUSE LLC.
0005  */
0006 
0007 #ifndef __RESOLVEAT_H__
0008 #define __RESOLVEAT_H__
0009 
0010 #define _GNU_SOURCE
0011 #include <stdint.h>
0012 #include <stdbool.h>
0013 #include <errno.h>
0014 #include <linux/types.h>
0015 #include "../kselftest.h"
0016 
0017 #define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X)))
0018 #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); })))
0019 
0020 #ifndef SYS_openat2
0021 #ifndef __NR_openat2
0022 #define __NR_openat2 437
0023 #endif /* __NR_openat2 */
0024 #define SYS_openat2 __NR_openat2
0025 #endif /* SYS_openat2 */
0026 
0027 /*
0028  * Arguments for how openat2(2) should open the target path. If @resolve is
0029  * zero, then openat2(2) operates very similarly to openat(2).
0030  *
0031  * However, unlike openat(2), unknown bits in @flags result in -EINVAL rather
0032  * than being silently ignored. @mode must be zero unless one of {O_CREAT,
0033  * O_TMPFILE} are set.
0034  *
0035  * @flags: O_* flags.
0036  * @mode: O_CREAT/O_TMPFILE file mode.
0037  * @resolve: RESOLVE_* flags.
0038  */
0039 struct open_how {
0040     __u64 flags;
0041     __u64 mode;
0042     __u64 resolve;
0043 };
0044 
0045 #define OPEN_HOW_SIZE_VER0  24 /* sizeof first published struct */
0046 #define OPEN_HOW_SIZE_LATEST    OPEN_HOW_SIZE_VER0
0047 
0048 bool needs_openat2(const struct open_how *how);
0049 
0050 #ifndef RESOLVE_IN_ROOT
0051 /* how->resolve flags for openat2(2). */
0052 #define RESOLVE_NO_XDEV     0x01 /* Block mount-point crossings
0053                     (includes bind-mounts). */
0054 #define RESOLVE_NO_MAGICLINKS   0x02 /* Block traversal through procfs-style
0055                     "magic-links". */
0056 #define RESOLVE_NO_SYMLINKS 0x04 /* Block traversal through all symlinks
0057                     (implies OEXT_NO_MAGICLINKS) */
0058 #define RESOLVE_BENEATH     0x08 /* Block "lexical" trickery like
0059                     "..", symlinks, and absolute
0060                     paths which escape the dirfd. */
0061 #define RESOLVE_IN_ROOT     0x10 /* Make all jumps to "/" and ".."
0062                     be scoped inside the dirfd
0063                     (similar to chroot(2)). */
0064 #endif /* RESOLVE_IN_ROOT */
0065 
0066 #define E_func(func, ...)                             \
0067     do {                                      \
0068         errno = 0;                            \
0069         if (func(__VA_ARGS__) < 0)                    \
0070             ksft_exit_fail_msg("%s:%d %s failed - errno:%d\n",    \
0071                        __FILE__, __LINE__, #func, errno); \
0072     } while (0)
0073 
0074 #define E_asprintf(...)     E_func(asprintf,    __VA_ARGS__)
0075 #define E_chmod(...)        E_func(chmod,       __VA_ARGS__)
0076 #define E_dup2(...)     E_func(dup2,        __VA_ARGS__)
0077 #define E_fchdir(...)       E_func(fchdir,      __VA_ARGS__)
0078 #define E_fstatat(...)      E_func(fstatat,     __VA_ARGS__)
0079 #define E_kill(...)     E_func(kill,        __VA_ARGS__)
0080 #define E_mkdirat(...)      E_func(mkdirat,     __VA_ARGS__)
0081 #define E_mount(...)        E_func(mount,       __VA_ARGS__)
0082 #define E_prctl(...)        E_func(prctl,       __VA_ARGS__)
0083 #define E_readlink(...)     E_func(readlink,    __VA_ARGS__)
0084 #define E_setresuid(...)    E_func(setresuid,   __VA_ARGS__)
0085 #define E_symlinkat(...)    E_func(symlinkat,   __VA_ARGS__)
0086 #define E_touchat(...)      E_func(touchat,     __VA_ARGS__)
0087 #define E_unshare(...)      E_func(unshare,     __VA_ARGS__)
0088 
0089 #define E_assert(expr, msg, ...)                    \
0090     do {                                \
0091         if (!(expr))                        \
0092             ksft_exit_fail_msg("ASSERT(%s:%d) failed (%s): " msg "\n", \
0093                        __FILE__, __LINE__, #expr, ##__VA_ARGS__); \
0094     } while (0)
0095 
0096 int raw_openat2(int dfd, const char *path, void *how, size_t size);
0097 int sys_openat2(int dfd, const char *path, struct open_how *how);
0098 int sys_openat(int dfd, const char *path, struct open_how *how);
0099 int sys_renameat2(int olddirfd, const char *oldpath,
0100           int newdirfd, const char *newpath, unsigned int flags);
0101 
0102 int touchat(int dfd, const char *path);
0103 char *fdreadlink(int fd);
0104 bool fdequal(int fd, int dfd, const char *path);
0105 
0106 extern bool openat2_supported;
0107 
0108 #endif /* __RESOLVEAT_H__ */