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=1796094668; b=Hjxq8wDcjQjAlSJHDbcOzq5+dUczLU1c0arN+Woo7acl8uyopQBuMlOynNMIxw3ve8in ItnZZis8o1woE4JiAWbIKwiUIGwuFZxxtFjx2ZiKbdQh8vD0wP/qfwaVvyF1WxpL2TVe9 +6u9qmn695U62TlVKT9fKPo9Ox7lY/qAIoQOvjwKpXHGRckdQ7wPibdSDnto1b4ze34lR IclGQoB9oT8dg3qz/WwlH7CRwZ5Hg1+8BcmehsCQnndBOsGOonNTqmKq1Z7y1SY9ueXht fIelSu9sbGNoSDkSDdMErLcK+Cm/5Zll+Di+aQKpfv8Ym0xNzcyX6E0mTNhHVS5T7mQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796094668; 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=FVujIHNRNpDScIiDsK3jQv0Yv1xEquuJXDaLYYJTNPFdZzviVubOXkAs8jI494ssSAF2 JFKIZh8zuu6MKSxLFuKzuqLDeH13T+Q1L6Nuy0nZVGs32yf+a4s1NsnUSg5+1PpWR/Kgt aXigAL27RaYR1lBjzByoR1PIgNuMc8rDaXO0uYR8M3Xs6EUpSWxzFYKmDujV6u2Sfkr+R JtXccG8e67Ea0aKDSVPkRIwcHO5Nh4sEXCqq7xIu4155j20neeQ4yIftyQg7Iezd9nzTw Rff66a+yy8Y3We9CuG1Wpdekp9WrnWSjKeisS2aPA9EGjjrqSJVXm0SYsAVP2RW7xYg== 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=NGFx+9gG; 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 0EBE8606A320; Tue, 1 Dec 2026 03:11:08 +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=NGFx+9gG Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 326651F0093A; Tue, 1 Dec 2026 03:11:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796094667; bh=PSpzlRWMQthMrjHyEFWS5Zj3yGcc76i9zn+YgalUkxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NGFx+9gG81QSOSo7liTV3kXD4HyOnjPwowNP5EImiuomWCUbm0I8Cz6k9TUXYOKxF dauyA7i4l9r6vO1ZI5aXg4yHO2rfT6uChpB38D2aunEDIDKodrBf7Bv/7CQi4ctBoT RXMq6w8WuF1P6AIKp+V59/bICYIqFB2ybxOkKUvRw5N8ElFcAt9K5A+cT/+T1Ql51b Nity3krlWWlYQTwXsI0bOkpkbXhkEkaLTdU8sms1LtOU8RUlwYSgQ9unZcyFqSQOpj 60T5M8tqMV0DOY/zY1wxYhyTHC+kw3CQ5O0DBg3u9cmlGRDs9rLr+MRlZ6u7uw6ZZv dyIDY1dsHawCQ== 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-integrity@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 , Alexei Starovoitov , Andrew Morton , Andrii Nakryiko , Andy Lutomirski , Arnd Bergmann , Baolin Wang , Ben Segall , Christian Brauner , Daniel Borkmann , David Hildenbrand , David Howells , David Woodhouse , Davidlohr Bueso , Dietmar Eggemann , Eduard Zingerman , Emil Tsalapatis , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , =?UTF-8?q?G=C3=BCnther=20Noack?= , Hugh Dickins , Ihor Solodrai , Ingo Molnar , James Morris , Jan Kara , Jason Gunthorpe , Jeff Layton , Jiri Olsa , John Johansen , Jonathan Corbet , Josef Bacik , Juri Lelli , Kees Cook , K Prateek Nayak , KP Singh , Kumar Kartikeya Dwivedi , Lennart Poettering , "Liam R. Howlett" , Manfred Spraul , Martin KaFai Lau , Matt Bobrowski , Mel Gorman , Michal Hocko , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Mike Rapoport , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , "Serge E. Hallyn" , Serge Hallyn , Shuah Khan , Song Liu , Stephen Smalley , Steven Rostedt , Valentin Schneider , Vincent Guittot , Yonghong Song , Zygmunt Krynicki Subject: [RFC PATCH v9 10/29] userns: Factor out install_user_ns_cred() Date: Tue, 1 Dec 2026 05:09:42 +0200 Message-ID: <20261201031002.593830-11-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org> References: <20261201031002.593830-1-jarkko@kernel.org> X-Mailing-List: linux-security-module@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