| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "shared-forward.h" |
| 5 |
|
| 6 |
struct userns_restrict_bpf; |
| 7 |
|
| 8 |
#define USER_NAMESPACE_CGROUPS_DELEGATE_MAX 16U |
| 9 |
#define USER_NAMESPACE_NETIFS_DELEGATE_MAX 16U |
| 10 |
#define USER_NAMESPACE_DELEGATIONS_MAX 16U |
| 11 |
|
| 12 |
typedef struct DelegatedUserNamespaceInfo { |
| 13 |
uint64_t userns_inode; |
| 14 |
uid_t start_uid; |
| 15 |
gid_t start_gid; |
| 16 |
uint32_t size; |
| 17 |
/* We track all the previous owners of the delegation so we can restore the previous owner of each |
| 18 |
* delegated range when a user namespace with delegated ranges is freed. */ |
| 19 |
uint64_t *ancestor_userns; |
| 20 |
size_t n_ancestor_userns; |
| 21 |
} DelegatedUserNamespaceInfo; |
| 22 |
|
| 23 |
#define DELEGATED_USER_NAMESPACE_INFO_NULL (DelegatedUserNamespaceInfo) { \ |
| 24 |
.start_uid = UID_INVALID, \ |
| 25 |
.start_gid = GID_INVALID, \ |
| 26 |
} |
| 27 |
|
| 28 |
void delegated_userns_info_done(DelegatedUserNamespaceInfo *info); |
| 29 |
|
| 30 |
typedef struct UserNamespaceInfo { |
| 31 |
uid_t owner; |
| 32 |
char *name; |
| 33 |
uint64_t userns_inode; |
| 34 |
uint64_t userns_id; /* Unique namespace identifier from NS_GET_ID, 0 if unavailable */ |
| 35 |
uint32_t size; |
| 36 |
uid_t start_uid; |
| 37 |
uid_t target_uid; |
| 38 |
gid_t start_gid; |
| 39 |
gid_t target_gid; |
| 40 |
uint64_t *cgroups; |
| 41 |
size_t n_cgroups; |
| 42 |
char **netifs; |
| 43 |
DelegatedUserNamespaceInfo *delegates; |
| 44 |
size_t n_delegates; |
| 45 |
} UserNamespaceInfo; |
| 46 |
|
| 47 |
UserNamespaceInfo* userns_info_new(void); |
| 48 |
UserNamespaceInfo* userns_info_free(UserNamespaceInfo *userns); |
| 49 |
|
| 50 |
DEFINE_TRIVIAL_CLEANUP_FUNC(UserNamespaceInfo*, userns_info_free); |
| 51 |
|
| 52 |
bool userns_info_has_cgroup(UserNamespaceInfo *userns, uint64_t cgroup_id); |
| 53 |
int userns_info_add_cgroup(UserNamespaceInfo *userns, uint64_t cgroup_id); |
| 54 |
int userns_info_remove_cgroups(UserNamespaceInfo *userns); |
| 55 |
|
| 56 |
int userns_info_add_netif(UserNamespaceInfo *userns, const char *netif); |
| 57 |
int userns_info_remove_netifs(UserNamespaceInfo *userns); |
| 58 |
|
| 59 |
bool userns_name_is_valid(const char *name); |
| 60 |
|
| 61 |
int userns_registry_open_fd(void); |
| 62 |
int userns_registry_lock(int dir_fd); |
| 63 |
|
| 64 |
int userns_registry_load_by_start_uid(int dir_fd, uid_t start, UserNamespaceInfo **ret); |
| 65 |
int userns_registry_load_by_start_gid(int dir_fd, gid_t start, UserNamespaceInfo **ret); |
| 66 |
int userns_registry_load_by_userns_inode(int dir_fd, uint64_t inode, UserNamespaceInfo **ret); |
| 67 |
int userns_registry_load_by_name(int dir_fd, const char *name, UserNamespaceInfo **ret); |
| 68 |
|
| 69 |
int userns_info_verify_fd(int userns_fd, const UserNamespaceInfo *info); |
| 70 |
|
| 71 |
/* Releases all resources tied to a user namespace: removes BPF allowlist entries (if a bpf handle is |
| 72 |
* given), drops the corresponding fd from systemd's fdstore, removes cgroups and netifs recorded for |
| 73 |
* it, and unlinks the registry entry. The caller must already hold the registry lock (e.g. via |
| 74 |
* userns_registry_lock()). The _by_inode variant loads the registry entry; prefer the _by_info |
| 75 |
* variant where the caller already has it. */ |
| 76 |
void userns_registry_release_by_info(struct userns_restrict_bpf *bpf, int dir_fd, UserNamespaceInfo *info); |
| 77 |
void userns_registry_release_by_userns_inode(struct userns_restrict_bpf *bpf, int dir_fd, uint64_t inode); |
| 78 |
|
| 79 |
typedef enum UserNamespaceReapStatus { |
| 80 |
USERNS_REAP_RELEASED, /* Confirmed dead via its kernel id — registry entry released. */ |
| 81 |
USERNS_REAP_ALIVE, /* Still alive — left untouched. */ |
| 82 |
USERNS_REAP_INDETERMINATE, /* Liveness couldn't be determined for this entry — it predates id |
| 83 |
tracking, or no entry is registered for the inode any more (e.g. a |
| 84 |
dead ancestor still referenced by a delegation chain). */ |
| 85 |
USERNS_REAP_UNSUPPORTED, /* Namespaces can't be looked up by id in this environment at all (old |
| 86 |
kernel, or not in the initial user namespace). Applies to every |
| 87 |
entry, so callers sweeping many of them can stop probing. */ |
| 88 |
_USERNS_REAP_MAX, |
| 89 |
_USERNS_REAP_INVALID = -EINVAL, |
| 90 |
} UserNamespaceReapStatus; |
| 91 |
|
| 92 |
/* Probes the registered user namespace with the given inode for liveness via its recorded kernel |
| 93 |
* namespace id and, if it is authoritatively dead, releases its registry entry (restoring any ranges |
| 94 |
* it received via delegation to their ancestors). Returns a non-negative UserNamespaceReapStatus |
| 95 |
* describing what happened, or a negative errno on genuine failure. The caller must hold the registry |
| 96 |
* lock (or otherwise be free of concurrent writers). */ |
| 97 |
int userns_registry_reap_if_dead(struct userns_restrict_bpf *bpf, int dir_fd, uint64_t inode); |
| 98 |
|
| 99 |
int userns_registry_store(int dir_fd, UserNamespaceInfo *info); |
| 100 |
int userns_registry_remove(int dir_fd, UserNamespaceInfo *info); |
| 101 |
|
| 102 |
int userns_registry_name_exists(int dir_fd, const char *name); |
| 103 |
int userns_registry_uid_exists(int dir_fd, uid_t start); |
| 104 |
int userns_registry_gid_exists(int dir_fd, gid_t start); |
| 105 |
|
| 106 |
int userns_registry_per_uid(int dir_fd, uid_t owner); |
| 107 |
|
| 108 |
int userns_registry_delegation_uid_exists(int dir_fd, uid_t start); |
| 109 |
int userns_registry_delegation_gid_exists(int dir_fd, gid_t start); |
| 110 |
int userns_registry_load_delegation_by_uid(int dir_fd, uid_t start, DelegatedUserNamespaceInfo *ret); |
| 111 |
int userns_registry_load_delegation_by_gid(int dir_fd, gid_t start, DelegatedUserNamespaceInfo *ret); |