From: Bill Wendling <morbo@google.com>
To: linux-kernel@vger.kernel.org
Cc: "Bill Wendling" <morbo@google.com>, "Kees Cook" <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
"Christian Brauner" <brauner@kernel.org>,
"Aleksa Sarai" <cyphar@cyphar.com>, "Jan Kara" <jack@suse.cz>,
"Nathan Chancellor" <nathan@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Thomas Gleixner" <tglx@kernel.org>,
"Nicolas Schier" <nsc@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"Alice Ryhl" <aliceryhl@google.com>,
"Douglas Anderson" <dianders@chromium.org>,
"Anand Moon" <linux.amoon@gmail.com>,
"Oleg Nesterov" <oleg@redhat.com>,
codemender-patching+linux@google.com,
linux-hardening@vger.kernel.org
Subject: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map
Date: Sun, 23 Aug 2026 12:51:48 +0000 [thread overview]
Message-ID: <20260823125155.1136740-3-morbo@google.com> (raw)
In-Reply-To: <20260823125155.1136740-1-morbo@google.com>
Add a KUnit test suite to verify the insertion and sorting of mappings
in struct uid_gid_map. This test suite validates both base extent
insertion (<= 5 mappings) and extended extent insertion (> 5 mappings,
which triggers the allocation of the forward and reverse pointers).
This is especially useful for verifying that the __counted_by_ptr
attribute added to 'forward' and 'reverse' pointers works correctly
without causing any runtime bounds-checking panics or traps.
Assisted-by: Gemini Next
Change-Id: If0c2c197a35cd7429cf0d2d6e3b33f0d9f0be66c
Signed-off-by: Bill Wendling <morbo@google.com>
---
Cc: Kees Cook <kees@kernel.org>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Nicolas Schier <nsc@kernel.org>
Cc: Gary Guo <gary@garyguo.net>
Cc: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Douglas Anderson <dianders@chromium.org>
Cc: Anand Moon <linux.amoon@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: codemender-patching+linux@google.com
Cc: linux-kernel@vger.kernel.org
Cc: linux-hardening@vger.kernel.org
---
init/Kconfig | 10 ++++
kernel/.kunitconfig | 3 ++
kernel/user_namespace.c | 4 ++
kernel/user_namespace_kunit.c | 87 +++++++++++++++++++++++++++++++++++
4 files changed, 104 insertions(+)
create mode 100644 kernel/.kunitconfig
create mode 100644 kernel/user_namespace_kunit.c
diff --git a/init/Kconfig b/init/Kconfig
index f63bf5e05e79..ba6a40b7315a 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1457,6 +1457,16 @@ config USER_NS
If unsure, say N.
+config USER_NAMESPACE_KUNIT_TEST
+ tristate "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS
+ depends on USER_NS && KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds the KUnit test for user namespace uid/gid map insertion.
+ It validates map insertion, limits, dynamic allocation of the
+ extended extents array, and mapping sorting functions.
+ If unsure, say N.
+
config PID_NS
bool "PID Namespaces"
default y
diff --git a/kernel/.kunitconfig b/kernel/.kunitconfig
new file mode 100644
index 000000000000..7314dce05dc2
--- /dev/null
+++ b/kernel/.kunitconfig
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index 7e5371d8f515..64c64e1028e8 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -1413,3 +1413,7 @@ static __init int user_namespaces_init(void)
return 0;
}
subsys_initcall(user_namespaces_init);
+
+#if IS_ENABLED(CONFIG_USER_NAMESPACE_KUNIT_TEST)
+#include "user_namespace_kunit.c"
+#endif
diff --git a/kernel/user_namespace_kunit.c b/kernel/user_namespace_kunit.c
new file mode 100644
index 000000000000..6d7662ef1916
--- /dev/null
+++ b/kernel/user_namespace_kunit.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for user namespace map insertion and sorting.
+ */
+
+#include <kunit/test.h>
+#include <linux/user_namespace.h>
+
+static void test_user_ns_map_insert_base(struct kunit *test)
+{
+ struct uid_gid_map map;
+ struct uid_gid_extent extent;
+ int i, ret;
+
+ memset(&map, 0, sizeof(map));
+
+ /* Insert up to UID_GID_MAP_MAX_BASE_EXTENTS (5) elements */
+ for (i = 0; i < UID_GID_MAP_MAX_BASE_EXTENTS; i++) {
+ extent.first = i * 10;
+ extent.lower_first = i * 100;
+ extent.count = 5;
+
+ ret = insert_extent(&map, &extent);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, map.nr_extents, i + 1);
+ KUNIT_EXPECT_EQ(test, map.extent[i].first, i * 10);
+ KUNIT_EXPECT_EQ(test, map.extent[i].lower_first, i * 100);
+ KUNIT_EXPECT_EQ(test, map.extent[i].count, 5);
+ }
+}
+
+static void test_user_ns_map_insert_extended(struct kunit *test)
+{
+ struct uid_gid_map map;
+ struct uid_gid_extent extent;
+ int i, ret;
+
+ memset(&map, 0, sizeof(map));
+
+ /* Insert more than UID_GID_MAP_MAX_BASE_EXTENTS (e.g., 10) elements */
+ for (i = 0; i < 10; i++) {
+ extent.first = i * 10;
+ extent.lower_first = i * 100;
+ extent.count = 5;
+
+ ret = insert_extent(&map, &extent);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, map.nr_extents, i + 1);
+
+ if (i < UID_GID_MAP_MAX_BASE_EXTENTS) {
+ KUNIT_EXPECT_EQ(test, map.extent[i].first, i * 10);
+ } else {
+ KUNIT_EXPECT_NOT_ERR_OR_NULL(test, map.forward);
+ KUNIT_EXPECT_EQ(test, map.forward[i].first, i * 10);
+ KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, i * 100);
+ KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+ }
+ }
+
+ /* Now sort the map to set up reverse mapping */
+ ret = sort_idmaps(&map);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_NOT_ERR_OR_NULL(test, map.reverse);
+
+ /* Verify sorting is correct */
+ for (i = 0; i < map.nr_extents; i++) {
+ KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+ KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+ }
+
+ /* Clean up allocations to avoid leaks */
+ kfree(map.forward);
+ kfree(map.reverse);
+}
+
+static struct kunit_case user_ns_map_test_cases[] = {
+ KUNIT_CASE(test_user_ns_map_insert_base),
+ KUNIT_CASE(test_user_ns_map_insert_extended),
+ {}
+};
+
+static struct kunit_suite user_ns_map_test_suite = {
+ .name = "user_ns_map",
+ .test_cases = user_ns_map_test_cases,
+};
+
+kunit_test_suite(user_ns_map_test_suite);
--
2.55.0.860.g4b6b3295ed-goog
next prev parent reply other threads:[~2026-08-23 12:52 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-23 12:51 [PATCH 0/2] Add __counted_by_ptr attribute to struct uid_gid_map Bill Wendling
2026-08-23 12:51 ` [PATCH 1/2] userns: " Bill Wendling
2026-08-23 13:50 ` Bradley Morgan
2026-08-23 14:52 ` Oleg Nesterov
2026-08-26 22:05 ` Bill Wendling
2026-08-24 19:18 ` Gustavo A. R. Silva
2026-08-23 12:51 ` Bill Wendling [this message]
2026-08-23 12:53 ` [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map Bill Wendling
2026-08-26 20:43 ` [PATCH v3 " Bill Wendling
2026-08-26 21:21 ` Bradley Morgan
2026-08-26 22:04 ` Bill Wendling
2026-08-23 13:48 ` [PATCH " Bradley Morgan
2026-08-26 20:37 ` Bill Wendling
2026-08-24 6:13 ` Thomas Weißschuh
2026-08-26 20:40 ` Bill Wendling
2026-08-27 13:25 ` Thomas Weißschuh
2026-09-08 22:27 ` [PATCH v5 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map Bill Wendling
2026-09-08 22:27 ` [PATCH v5 2/2] userns: Add KUnit test suite for uid_gid_map Bill Wendling
2026-09-09 15:36 ` Bradley Morgan
2026-09-09 19:54 ` Bill Wendling
2026-09-08 23:01 ` [PATCH v5 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map Oleg Nesterov
2026-09-09 7:46 ` Christian Brauner
2026-08-26 22:00 ` [PATCH v4 " Bill Wendling
2026-08-26 22:00 ` [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map Bill Wendling
2026-08-26 22:27 ` Bradley Morgan
2026-08-27 13:36 ` Thomas Weißschuh
2026-08-27 19:27 ` Bill Wendling
2026-08-31 9:22 ` Thomas Weißschuh
2026-09-03 20:21 ` Bill Wendling
2026-09-04 8:02 ` Thomas Weißschuh
2026-09-04 20:37 ` Bill Wendling
2026-09-04 23:34 ` Kees Cook
2026-09-08 17:44 ` Bill Wendling
2026-09-04 23:41 ` Kees Cook
2026-09-08 17:46 ` Bill Wendling
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260823125155.1136740-3-morbo@google.com \
--to=morbo@google.com \
--cc=aliceryhl@google.com \
--cc=brauner@kernel.org \
--cc=codemender-patching+linux@google.com \
--cc=cyphar@cyphar.com \
--cc=dianders@chromium.org \
--cc=gary@garyguo.net \
--cc=gustavoars@kernel.org \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux.amoon@gmail.com \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=ojeda@kernel.org \
--cc=oleg@redhat.com \
--cc=tglx@kernel.org \
--cc=thomas.weissschuh@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.