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;
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)
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) {
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);
return log_oom();
}
- userns_fd = userns_acquire(uid_map, gid_map);
+ userns_fd = userns_acquire(uid_map, gid_map, /* setgroups_deny= */ true);
if (userns_fd < 0)
return log_error_errno(userns_fd, "Failed to allocate user namespace: %m");
log_debug("Creating userns with mapping:\n%s", text);
- userns_fd = userns_acquire(text, text); /* same uid + gid mapping */
+ userns_fd = userns_acquire(text, text, /* setgroups_deny= */ true); /* same uid + gid mapping */
if (userns_fd < 0)
return log_error_errno(userns_fd, "Failed to allocate user namespace: %m");
if (r < 0)
return r;
- _cleanup_close_ int idmap_userns_fd = userns_acquire(new_uid_map, new_uid_map);
+ _cleanup_close_ int idmap_userns_fd = userns_acquire(new_uid_map, new_uid_map, /* setgroups_deny= */ true);
if (idmap_userns_fd < 0)
return log_debug_errno(idmap_userns_fd, "Failed to acquire user namespace for id mapping: %m");
host_tmpfs = make_tmpfs_fsmount();
assert_se(host_tmpfs >= 0);
- userns_fd = userns_acquire("0 0 1", "0 0 1");
+ userns_fd = userns_acquire("0 0 1", "0 0 1", /* setgroups_deny= */ true);
if (userns_fd < 0)
return log_error_errno(userns_fd, "Failed to make user namespace: %m");
}
/* We always assign the same UID and GID ranges */
- userns_fd = userns_acquire(line, line);
+ userns_fd = userns_acquire(line, line, /* setgroups_deny= */ true);
if (userns_fd < 0)
return log_debug_errno(userns_fd, "Failed to acquire new userns: %m");
TEST(userns_get_base_uid) {
_cleanup_close_ int fd = -EBADF;
- fd = userns_acquire("0 1 1", "0 2 1");
+ fd = userns_acquire("0 1 1", "0 2 1", /* setgroups_deny= */ true);
if (ERRNO_IS_NEG_NOT_SUPPORTED(fd))
return (void) log_tests_skipped("userns is not supported");
if (ERRNO_IS_NEG_PRIVILEGE(fd))