Debian Codegraph @

src/basic/namespace-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "basic-forward.h"
5
6 typedef enum NamespaceType {
7 NAMESPACE_CGROUP,
8 NAMESPACE_IPC,
9 NAMESPACE_NET,
10 NAMESPACE_MOUNT,
11 NAMESPACE_PID,
12 NAMESPACE_USER,
13 NAMESPACE_UTS,
14 NAMESPACE_TIME,
15 _NAMESPACE_TYPE_MAX,
16 _NAMESPACE_TYPE_INVALID = -EINVAL,
17 } NamespaceType;
18
19 extern const struct namespace_info {
20 const char *proc_name;
21 const char *proc_path;
22 unsigned long clone_flag;
23 unsigned long pidfd_get_ns_ioctl_cmd;
24 ino_t root_inode;
25 } namespace_info[_NAMESPACE_TYPE_MAX + 1];
26
27 NamespaceType clone_flag_to_namespace_type(unsigned long clone_flag);
28
29 bool namespace_type_supported(NamespaceType type);
30
31 int pidref_namespace_open_by_type(const PidRef *pidref, NamespaceType type);
32 int namespace_open_by_type(NamespaceType type);
33
34 int pidref_namespace_open(
35 const PidRef *pidref,
36 int *ret_pidns_fd,
37 int *ret_mntns_fd,
38 int *ret_netns_fd,
39 int *ret_userns_fd,
40 int *ret_root_fd);
41 int namespace_open(
42 pid_t pid,
43 int *ret_pidns_fd,
44 int *ret_mntns_fd,
45 int *ret_netns_fd,
46 int *ret_userns_fd,
47 int *ret_root_fd);
48
49 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
50
51 int fd_is_namespace(int fd, NamespaceType type);
52 int is_our_namespace(int fd, NamespaceType type);
53 int are_our_namespaces(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
54
55 int namespace_is_init(NamespaceType type);
56
57 int pidref_in_same_namespace(PidRef *pid1, PidRef *pid2, NamespaceType type);
58 int in_same_namespace(pid_t pid1, pid_t pid2, NamespaceType type);
59
60 int namespace_get_leader(PidRef *pidref, NamespaceType type, PidRef *ret);
61
62 int detach_mount_namespace(void);
63 int detach_mount_namespace_harder(uid_t target_uid, gid_t target_gid);
64 int detach_mount_namespace_userns(int userns_fd);
65
66 static inline bool userns_shift_range_valid(uid_t shift, uid_t range) {
67 /* Checks that the specified userns range makes sense, i.e. contains at least one UID, and the end
68 * doesn't overflow uid_t. */
69
70 assert_cc((uid_t) -1 > 0); /* verify that uid_t is unsigned */
71
72 if (range <= 0)
73 return false;
74
75 if (shift > (uid_t) -1 - range)
76 return false;
77
78 return true;
79 }
80
81 int parse_userns_uid_range(const char *s, uid_t *ret_uid_shift, uid_t *ret_uid_range);
82
83 int userns_acquire_empty(void);
84 int userns_acquire(const char *uid_map, const char *gid_map, bool setgroups_deny);
85 int userns_acquire_self_root(void);
86 int userns_enter_and_pin(int userns_fd, PidRef *ret);
87 bool userns_supported(void);
88
89 int userns_get_base_uid(int userns_fd, uid_t *ret_uid, gid_t *ret_gid);
90
91 int namespace_open_by_id(uint64_t ns_id);
92
93 int process_is_owned_by_uid(const PidRef *pidref, uid_t uid);
94
95 int is_idmapping_supported(const char *path);
96
97 int netns_acquire(void);