namespace-util: make "setgroups" users property writable via userns_acquire()

Unprivileged namespaces are only allowed if the "setgroups" file is set
to "deny" for processes. And we need to write it before writing the
gidmap. Hence add a parameter for that.

Then, also patch all current users to actually enable this. The usecase
generally don't need it (because they don't care about unprivileged
userns), but it doesn't hurt to enable the concept anyway in all current
users (none of them actually runs complex userspace in them, but they
mostly use userns_acquire() for idmapped mounts and similar).

Let's anyway make this option explicit in the function call, to indicate
that the concept exists and is applied.
This commit is contained in:
Lennart Poettering
2025-03-17 16:03:18 +01:00
parent 0201114bb7
commit 6431c34b8a
8 changed files with 17 additions and 10 deletions
+10 -3
View File
@@ -524,8 +524,8 @@ int userns_acquire_empty(void) {
return pidref_namespace_open_by_type(&pid, NAMESPACE_USER);
}
int userns_acquire(const char *uid_map, const char *gid_map) {
char path[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
int userns_acquire(const char *uid_map, const char *gid_map, bool setgroups_deny) {
char path[STRLEN("/proc//setgroups") + DECIMAL_STR_MAX(pid_t) + 1];
_cleanup_(pidref_done_sigkill_wait) PidRef pid = PIDREF_NULL;
int r;
@@ -546,6 +546,13 @@ int userns_acquire(const char *uid_map, const char *gid_map) {
if (r < 0)
return log_debug_errno(r, "Failed to write UID map: %m");
if (setgroups_deny) {
xsprintf(path, "/proc/" PID_FMT "/setgroups", pid.pid);
r = write_string_file(path, "deny", WRITE_STRING_FILE_DISABLE_BUFFER);
if (r < 0)
return log_debug_errno(r, "Failed to write setgroups file: %m");
}
xsprintf(path, "/proc/" PID_FMT "/gid_map", pid.pid);
r = write_string_file(path, gid_map, WRITE_STRING_FILE_DISABLE_BUFFER);
if (r < 0)
@@ -713,7 +720,7 @@ int is_idmapping_supported(const char *path) {
if (r < 0)
return r;
userns_fd = r = userns_acquire(uid_map, gid_map);
userns_fd = r = userns_acquire(uid_map, gid_map, /* setgroups_deny= */ true);
if (ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_PRIVILEGE(r) || r == -EINVAL)
return false;
if (r == -ENOSPC) {
+1 -1
View File
@@ -86,7 +86,7 @@ static inline bool userns_shift_range_valid(uid_t shift, uid_t range) {
int parse_userns_uid_range(const char *s, uid_t *ret_uid_shift, uid_t *ret_uid_range);
int userns_acquire_empty(void);
int userns_acquire(const char *uid_map, const char *gid_map);
int userns_acquire(const char *uid_map, const char *gid_map, bool setgroups_deny);
int userns_enter_and_pin(int userns_fd, pid_t *ret_pid);
int userns_get_base_uid(int userns_fd, uid_t *ret_uid, gid_t *ret_gid);