utils.h 1009 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __IDMAP_UTILS_H
  3. #define __IDMAP_UTILS_H
  4. #ifndef _GNU_SOURCE
  5. #define _GNU_SOURCE
  6. #endif
  7. #include <errno.h>
  8. #include <linux/types.h>
  9. #include <sched.h>
  10. #include <signal.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <syscall.h>
  16. #include <sys/capability.h>
  17. #include <sys/fsuid.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. extern int get_userns_fd(unsigned long nsid, unsigned long hostid,
  21. unsigned long range);
  22. extern int caps_down(void);
  23. extern int cap_down(cap_value_t down);
  24. extern bool switch_ids(uid_t uid, gid_t gid);
  25. extern int setup_userns(void);
  26. extern int enter_userns(void);
  27. static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
  28. {
  29. if (setns(fd, CLONE_NEWUSER))
  30. return false;
  31. if (!switch_ids(uid, gid))
  32. return false;
  33. if (drop_caps && !caps_down())
  34. return false;
  35. return true;
  36. }
  37. extern uint64_t get_unique_mnt_id(const char *path);
  38. #endif /* __IDMAP_UTILS_H */