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