From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795910338; b=iSR3qHPzKWciA8Plrce64Q+BYKWiz3FrZUSVXj40MqFD2vhkT+x17pxhLu/eH8vcRRtc HLM+BcdkWgCJfRFudQ0hXf4amtMKhbjILHH2p3G0RETKEf4jDaCdI3T+t1b4sIie0Kzh7 LY2blz4CmYedaq0kRPfOK+0GhncP4j7ohUPTsM4Un+maf/LiU4jUsE5AM2cgp9J4BNjzc ZJkQ3Ri25DhTyXD7G2dMyB97EL40TYs+SSmwjjoHpRVQGEiYQNmmrma46ITeuNkMkdlIf cyjkybm/LF8UgE3tfjDie0PI/mSpK6DezYUrCqdD9AxLECPcqKfYXPh9IrxW/APA8Hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910338; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=PSpzlRWMQthMrjHyEFWS5Zj3yGcc76i9zn+YgalUkxA=; b=Nj/hyrYOto7FjsL+HaIEJPsn3apxolWOPO6DckL0520lqdZJHwPfhsJaqo43+BTCgqAt 5pTQyjSE6ybBiMyHopj37zUyKW26RAG9QCKUuno0VM4MKLmz9sbx3syaJ1hUqJ2ousuK+ CNRX2enxWNK7htrPpIQLdcmoq7vvo3i1Nao6/s2PDZ5P+obbnElyeiRfPvtnw/XV5RU3z QTOAO5g7DVUiXJOvtE9G0Qy7U4IH7TQT0e2Kah+OxhRqZo6e3JlfJ9P+yuj7VpuGeNFPT WUIiSm16X93RCbDhjjE9tQXtISc5GUc8RkjNycCEGgiJvUnu5DANUuRArrb6EHEh31Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kPIrpjUx; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 9EE97606A335; Sat, 28 Nov 2026 23:58:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kPIrpjUx Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 8B21A1F00924; Sat, 28 Nov 2026 23:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795910338; bh=PSpzlRWMQthMrjHyEFWS5Zj3yGcc76i9zn+YgalUkxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kPIrpjUxc2XPxCdDyk2x8KfdHMmhhl+2XgnFI1QJqSmchs6MZfhJXpWE+pjy0Qpqn Pi02X8ANniW9ruqvPWEayA8zVzYVsmfRXfN/hUtOz1do7BFQ+Ua/YbVbr6Dl4f1wx+ JN7rPcIP8dubmMc10pxz6teLVQrlUgTMX3jW+x4EDei8FoV/Up8Aqemgo9XgxPNkOP dt3LAbhNu/Z5b9/T8eHCrBjxZOWMGmB8MvxUh9czi433QAMQZGts/wdxrcZYpvbd2h 0xulz06sKsSwYcw/l53EhnzxRYf2RJ112Rm9icLLu7q5Q+NeXA0ZaFY91zSgBlO48R Siaz3JohlhXMg== From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Aleksa Sarai , Alexander Mikhalitsyn , Alexander Viro , Andy Lutomirski , Arnd Bergmann , Christian Brauner , David Howells , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , Hugh Dickins , James Morris , Jan Kara , Jeff Layton , John Johansen , Jonathan Corbet , Josef Bacik , Kees Cook , KP Singh , Lennart Poettering , Matt Bobrowski , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Serge Hallyn , Shuah Khan , Stephen Smalley , Zygmunt Krynicki Subject: [RFC PATCH v8 10/29] userns: Factor out install_user_ns_cred() Date: Sun, 29 Nov 2026 01:57:49 +0200 Message-ID: <20261128235808.2369970-11-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261128235808.2369970-1-jarkko@kernel.org> References: <20261128235808.2369970-1-jarkko@kernel.org> X-Mailing-List: linux-arch@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit userns_install() updates a credential's owning user namespace and rebinds its ucounts inline, which cannot be reused by fork paths that switch a newly allocated child credential into a target user namespace. Factor this logic out into install_user_ns_cred() with a !CONFIG_USER_NS stub so callers outside kernel/user_namespace.c can install a target user namespace on a prepared struct cred. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/user_namespace.h | 7 +++++++ kernel/user_namespace.c | 12 ++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index b4a50edfe003..ecee91aa2257 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -182,6 +182,7 @@ static inline struct user_namespace *get_user_ns(struct user_namespace *ns) } extern int create_user_ns(struct cred *new); +int install_user_ns_cred(struct cred *cred, struct user_namespace *user_ns); extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred); extern void __put_user_ns(struct user_namespace *ns); @@ -224,6 +225,12 @@ static inline int create_user_ns(struct cred *new) return -EINVAL; } +static inline int install_user_ns_cred(struct cred *cred, + struct user_namespace *user_ns) +{ + return -EINVAL; +} + static inline int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) { diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index e5054e273e93..fe60a71f0ba3 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1613,6 +1613,13 @@ static void userns_put(struct ns_common *ns) put_user_ns(to_user_ns(ns)); } +int install_user_ns_cred(struct cred *cred, struct user_namespace *user_ns) +{ + put_user_ns(cred->user_ns); + set_cred_user_ns(cred, get_user_ns(user_ns)); + return set_cred_ucounts(cred); +} + static int userns_install(struct nsset *nsset, struct ns_common *ns) { struct user_namespace *user_ns = to_user_ns(ns); @@ -1638,10 +1645,7 @@ static int userns_install(struct nsset *nsset, struct ns_common *ns) if (!cred) return -EINVAL; - put_user_ns(cred->user_ns); - set_cred_user_ns(cred, get_user_ns(user_ns)); - - if (set_cred_ucounts(cred) < 0) + if (install_user_ns_cred(cred, user_ns) < 0) return -EINVAL; return 0; -- 2.47.3