helpers.h 3.5 KB

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