162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Author: Aleksa Sarai <cyphar@cyphar.com>
462306a36Sopenharmony_ci * Copyright (C) 2018-2019 SUSE LLC.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __RESOLVEAT_H__
862306a36Sopenharmony_ci#define __RESOLVEAT_H__
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#define _GNU_SOURCE
1162306a36Sopenharmony_ci#include <stdint.h>
1262306a36Sopenharmony_ci#include <stdbool.h>
1362306a36Sopenharmony_ci#include <errno.h>
1462306a36Sopenharmony_ci#include <linux/types.h>
1562306a36Sopenharmony_ci#include "../kselftest.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X)))
1862306a36Sopenharmony_ci#define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); })))
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#ifndef SYS_openat2
2162306a36Sopenharmony_ci#ifndef __NR_openat2
2262306a36Sopenharmony_ci#define __NR_openat2 437
2362306a36Sopenharmony_ci#endif /* __NR_openat2 */
2462306a36Sopenharmony_ci#define SYS_openat2 __NR_openat2
2562306a36Sopenharmony_ci#endif /* SYS_openat2 */
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/*
2862306a36Sopenharmony_ci * Arguments for how openat2(2) should open the target path. If @resolve is
2962306a36Sopenharmony_ci * zero, then openat2(2) operates very similarly to openat(2).
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * However, unlike openat(2), unknown bits in @flags result in -EINVAL rather
3262306a36Sopenharmony_ci * than being silently ignored. @mode must be zero unless one of {O_CREAT,
3362306a36Sopenharmony_ci * O_TMPFILE} are set.
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * @flags: O_* flags.
3662306a36Sopenharmony_ci * @mode: O_CREAT/O_TMPFILE file mode.
3762306a36Sopenharmony_ci * @resolve: RESOLVE_* flags.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_cistruct open_how {
4062306a36Sopenharmony_ci	__u64 flags;
4162306a36Sopenharmony_ci	__u64 mode;
4262306a36Sopenharmony_ci	__u64 resolve;
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define OPEN_HOW_SIZE_VER0	24 /* sizeof first published struct */
4662306a36Sopenharmony_ci#define OPEN_HOW_SIZE_LATEST	OPEN_HOW_SIZE_VER0
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cibool needs_openat2(const struct open_how *how);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#ifndef RESOLVE_IN_ROOT
5162306a36Sopenharmony_ci/* how->resolve flags for openat2(2). */
5262306a36Sopenharmony_ci#define RESOLVE_NO_XDEV		0x01 /* Block mount-point crossings
5362306a36Sopenharmony_ci					(includes bind-mounts). */
5462306a36Sopenharmony_ci#define RESOLVE_NO_MAGICLINKS	0x02 /* Block traversal through procfs-style
5562306a36Sopenharmony_ci					"magic-links". */
5662306a36Sopenharmony_ci#define RESOLVE_NO_SYMLINKS	0x04 /* Block traversal through all symlinks
5762306a36Sopenharmony_ci					(implies OEXT_NO_MAGICLINKS) */
5862306a36Sopenharmony_ci#define RESOLVE_BENEATH		0x08 /* Block "lexical" trickery like
5962306a36Sopenharmony_ci					"..", symlinks, and absolute
6062306a36Sopenharmony_ci					paths which escape the dirfd. */
6162306a36Sopenharmony_ci#define RESOLVE_IN_ROOT		0x10 /* Make all jumps to "/" and ".."
6262306a36Sopenharmony_ci					be scoped inside the dirfd
6362306a36Sopenharmony_ci					(similar to chroot(2)). */
6462306a36Sopenharmony_ci#endif /* RESOLVE_IN_ROOT */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define E_func(func, ...)						      \
6762306a36Sopenharmony_ci	do {								      \
6862306a36Sopenharmony_ci		errno = 0;						      \
6962306a36Sopenharmony_ci		if (func(__VA_ARGS__) < 0)				      \
7062306a36Sopenharmony_ci			ksft_exit_fail_msg("%s:%d %s failed - errno:%d\n",    \
7162306a36Sopenharmony_ci					   __FILE__, __LINE__, #func, errno); \
7262306a36Sopenharmony_ci	} while (0)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define E_asprintf(...)		E_func(asprintf,	__VA_ARGS__)
7562306a36Sopenharmony_ci#define E_chmod(...)		E_func(chmod,		__VA_ARGS__)
7662306a36Sopenharmony_ci#define E_dup2(...)		E_func(dup2,		__VA_ARGS__)
7762306a36Sopenharmony_ci#define E_fchdir(...)		E_func(fchdir,		__VA_ARGS__)
7862306a36Sopenharmony_ci#define E_fstatat(...)		E_func(fstatat,		__VA_ARGS__)
7962306a36Sopenharmony_ci#define E_kill(...)		E_func(kill,		__VA_ARGS__)
8062306a36Sopenharmony_ci#define E_mkdirat(...)		E_func(mkdirat,		__VA_ARGS__)
8162306a36Sopenharmony_ci#define E_mount(...)		E_func(mount,		__VA_ARGS__)
8262306a36Sopenharmony_ci#define E_prctl(...)		E_func(prctl,		__VA_ARGS__)
8362306a36Sopenharmony_ci#define E_readlink(...)		E_func(readlink,	__VA_ARGS__)
8462306a36Sopenharmony_ci#define E_setresuid(...)	E_func(setresuid,	__VA_ARGS__)
8562306a36Sopenharmony_ci#define E_symlinkat(...)	E_func(symlinkat,	__VA_ARGS__)
8662306a36Sopenharmony_ci#define E_touchat(...)		E_func(touchat,		__VA_ARGS__)
8762306a36Sopenharmony_ci#define E_unshare(...)		E_func(unshare,		__VA_ARGS__)
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define E_assert(expr, msg, ...)					\
9062306a36Sopenharmony_ci	do {								\
9162306a36Sopenharmony_ci		if (!(expr))						\
9262306a36Sopenharmony_ci			ksft_exit_fail_msg("ASSERT(%s:%d) failed (%s): " msg "\n", \
9362306a36Sopenharmony_ci					   __FILE__, __LINE__, #expr, ##__VA_ARGS__); \
9462306a36Sopenharmony_ci	} while (0)
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciint raw_openat2(int dfd, const char *path, void *how, size_t size);
9762306a36Sopenharmony_ciint sys_openat2(int dfd, const char *path, struct open_how *how);
9862306a36Sopenharmony_ciint sys_openat(int dfd, const char *path, struct open_how *how);
9962306a36Sopenharmony_ciint sys_renameat2(int olddirfd, const char *oldpath,
10062306a36Sopenharmony_ci		  int newdirfd, const char *newpath, unsigned int flags);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciint touchat(int dfd, const char *path);
10362306a36Sopenharmony_cichar *fdreadlink(int fd);
10462306a36Sopenharmony_cibool fdequal(int fd, int dfd, const char *path);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciextern bool openat2_supported;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#endif /* __RESOLVEAT_H__ */
109