diff options
author | Christian Brauner <brauner@kernel.org> | 2025-02-19 14:23:25 +0100 |
---|---|---|
committer | Christian Brauner <brauner@kernel.org> | 2025-02-19 14:32:12 +0100 |
commit | c68946ee7eb7fb5dc19c8d11f7e4e696c8ee6508 (patch) | |
tree | 67a1c859eaf030908e1eab876a5a87db15e379e8 /tools/testing/selftests/filesystems/utils.h | |
parent | 96f09432596aafac23722e6849efb9ae62e5f541 (diff) |
selftests/filesystems: add utils.{c,h}
Add a new set of helpers that will be used in follow-up patches.
Signed-off-by: Christian Brauner <brauner@kernel.org>
Diffstat (limited to 'tools/testing/selftests/filesystems/utils.h')
-rw-r--r-- | tools/testing/selftests/filesystems/utils.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/testing/selftests/filesystems/utils.h b/tools/testing/selftests/filesystems/utils.h new file mode 100644 index 000000000000..f35001a75f99 --- /dev/null +++ b/tools/testing/selftests/filesystems/utils.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __IDMAP_UTILS_H +#define __IDMAP_UTILS_H + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <errno.h> +#include <linux/types.h> +#include <sched.h> +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syscall.h> +#include <sys/capability.h> +#include <sys/fsuid.h> +#include <sys/types.h> +#include <unistd.h> + +extern int get_userns_fd(unsigned long nsid, unsigned long hostid, + unsigned long range); + +extern int caps_down(void); + +extern bool switch_ids(uid_t uid, gid_t gid); + +static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps) +{ + if (setns(fd, CLONE_NEWUSER)) + return false; + + if (!switch_ids(uid, gid)) + return false; + + if (drop_caps && !caps_down()) + return false; + + return true; +} + +#endif /* __IDMAP_UTILS_H */ |