HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/basic/namespace-util.c
Git blob: 64796e5c1dd305f138ab40c3f5593e7fe51dfad1
Lines 1001–1114 of 1,114
* and pid namespaces with CAP_SYS_ADMIN, with a narrow exception for lookups of the
* caller's own user namespace and its ancestors. To avoid conflating "namespace is dead"
* with "kernel refused us", we refuse early with -EPERM when we aren't in the initial
* user/pid namespace or missing CAP_SYS_ADMIN and let the caller skip the check. */
if (ns_id == 0)
return -EINVAL;
r = namespace_is_init(NAMESPACE_USER);
if (r < 0)
return r;
if (r == 0)
return -EPERM;
r = namespace_is_init(NAMESPACE_PID);
if (r < 0)
return r;
if (r == 0)
return -EPERM;
r = have_effective_cap(CAP_SYS_ADMIN);
if (r < 0)
return r;
if (r == 0)
return -EPERM;
/* The natural way to write this would be a compound designated initializer:
*
* union { ... } fh = {
* .file_handle.handle_bytes = sizeof(struct nsfs_file_handle),
* .file_handle.handle_type = FILEID_NSFS,
* };
*
* but that only zero-initializes the named struct members of struct file_handle.
* struct file_handle ends with a flexible array (`unsigned char f_handle[]`), whose
* storage comes from the overlapping `space[]` member of the union. Bytes in that storage
* are not covered by the partial struct initializer and end up as stack garbage. Zero the
* entire union first, then fill in the fields explicitly. */
union {
struct file_handle file_handle;
uint8_t space[offsetof(struct file_handle, f_handle) + sizeof(struct nsfs_file_handle)];
} fh = {};
fh.file_handle.handle_bytes = sizeof(struct nsfs_file_handle);
fh.file_handle.handle_type = FILEID_NSFS;
/* The first 8 bytes of struct nsfs_file_handle (see <linux/nsfs.h>, uapi since kernel v6.18)
* are __u64 ns_id; the remaining ns_type/ns_inum fields stay zero so the kernel looks up by
* id alone. The kernel made lookup-by-id-only an explicit ABI guarantee in v6.19 via commit
* 04173501a69e ("nstree: allow lookup solely based on inode"). */
unaligned_write_ne64(fh.file_handle.f_handle, ns_id);
return RET_NERRNO(open_by_handle_at(FD_NSFS_ROOT, &fh.file_handle, O_PATH|O_CLOEXEC));
}
int is_idmapping_supported(const char *path) {
_cleanup_close_ int mount_fd = -EBADF, userns_fd = -EBADF, dir_fd = -EBADF;
int r;
assert(path);
if (!mount_new_api_supported())
return false;
userns_fd = r = userns_acquire_self_root();
if (ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_PRIVILEGE(r) || r == -EINVAL)
return false;
if (r == -ENOSPC) {
log_debug_errno(r, "Failed to acquire new user namespace, user.max_user_namespaces seems to be exhausted or maybe even zero, assuming ID-mapping is not supported: %m");
return false;
}
if (r < 0)
return log_debug_errno(r, "Failed to acquire new user namespace for checking if '%s' supports ID-mapping: %m", path);
dir_fd = r = RET_NERRNO(open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
return false;
if (r < 0)
return log_debug_errno(r, "Failed to open '%s', cannot determine if ID-mapping is supported: %m", path);
mount_fd = r = RET_NERRNO(open_tree(dir_fd, "", AT_EMPTY_PATH | OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC));
if (ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_PRIVILEGE(r) || r == -EINVAL)
return false;
if (r < 0)
return log_debug_errno(r, "Failed to open mount tree '%s', cannot determine if ID-mapping is supported: %m", path);
r = RET_NERRNO(mount_setattr(mount_fd, "", AT_EMPTY_PATH,
&(struct mount_attr) {
.attr_set = MOUNT_ATTR_IDMAP | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NOEXEC | MOUNT_ATTR_RDONLY | MOUNT_ATTR_NODEV,
.userns_fd = userns_fd,
}, sizeof(struct mount_attr)));
if (ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_PRIVILEGE(r) || r == -EINVAL)
return false;
if (r < 0)
return log_debug_errno(r, "Failed to set mount attribute to '%s', cannot determine if ID-mapping is supported: %m", path);
return true;
}
int netns_acquire(void) {
_cleanup_(pidref_done_sigkill_wait) PidRef pid = PIDREF_NULL;
int r;
/* Forks off a process in a new network namespace, acquires a network namespace fd, and then kills
* the process again. This way we have a netns fd that is not bound to any process. */
r = pidref_safe_fork("(sd-mknetns)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_NEW_NETNS|FORK_FREEZE, &pid);
if (r < 0)
return log_debug_errno(r, "Failed to fork process into new netns: %m");
assert(r > 0);
return pidref_namespace_open_by_type(&pid, NAMESPACE_NET);
}