Skip to contentSkip to conversation

Add __counted_by_ptr attribute to struct uid_gid_map

PATCH Superseded Bill Wendling 8 participants 35 messages Aug 23 – Sep 9

lore
Share & tools
lore
#0 lore raw

cover Add __counted_by_ptr attribute to struct uid_gid_map

Bill Wendling (2):
userns: Add __counted_by_ptr attribute to struct uid_gid_map
userns: Add KUnit test suite for uid_gid_map

 include/linux/user_namespace.h |  4 +-
 init/Kconfig                   | 10 ++++
 kernel/.kunitconfig            |  3 ++
 kernel/user_namespace.c        | 12 +++--
 kernel/user_namespace_kunit.c  | 87 ++++++++++++++++++++++++++++++++++
 5 files changed, 110 insertions(+), 6 deletions(-)
 create mode 100644 kernel/.kunitconfig
 create mode 100644 kernel/user_namespace_kunit.c
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
#1 lore raw

PATCH 1/2 userns: Add __counted_by_ptr attribute to struct uid_gid_map

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents];
	else
		dest = &map->forward[map->nr_extents];

	*dest = *extent;
	map->nr_extents++;

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

	map->nr_extents++;
	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents - 1];
	else
		dest = &map->forward[map->nr_extents - 1];

	*dest = *extent;
Assisted-by Gemini Next

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 include/linux/user_namespace.h | 4 ++--
 kernel/user_namespace.c        | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
 			u32 nr_extents;
 		};
 		struct {
-			struct uid_gid_extent *forward;
-			struct uid_gid_extent *reverse;
+			struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+			struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
 		};
 	};
 };
kernel/user_namespace.c +4 -4
index 0bed462e9b2a..7e5371d8f515 100644
@@ -809,13 +809,13 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 		map->reverse = NULL;
 	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	map->nr_extents++;
+	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+		dest = &map->extent[map->nr_extents - 1];
 	else
-		dest = &map->forward[map->nr_extents];
+		dest = &map->forward[map->nr_extents - 1];
 
 	*dest = *extent;
-	map->nr_extents++;
 	return 0;
 }
 
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
#6 lore raw

PATCH 2/2 userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling

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

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 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
init/Kconfig +10 -0
index f63bf5e05e79..ba6a40b7315a 100644
@@ -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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 7e5371d8f515..64c64e1028e8 100644
@@ -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
kernel/user_namespace_kunit.c new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
#7 lore raw

PATCH 2/2 userns: Add KUnit test suite for uid_gid_map

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

---
v2 - Remove Gerrit tag.
---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 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
init/Kconfig +10 -0
index f63bf5e05e79..ba6a40b7315a 100644
@@ -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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 7e5371d8f515..64c64e1028e8 100644
@@ -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
kernel/user_namespace_kunit.c new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
#11 lore raw

Re: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling

Hi Bill,

Quoted 3 lines

+config USER_NAMESPACE_KUNIT_TEST
+ tristate "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS
+ depends on USER_NS && KUNIT

The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y

kernel/user_namespace_kunit.c line 69

Quoted 5 lines

+ /* Verify sorting is correct */
+ for (i = 0; i < map.nr_extents; i++) {

kernel/user_namespace_kunit.c new file +3 -0
+  KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+  KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+ }

This doesn't verify any sorting. Every extent was inserted with count 5, so the loop passes even if sort_idmaps() did nothing. Either assert that forward is ordered by .first and reverse by .lower_first, or drop the sorting claim from the changelog.

Quoted 3 lines

+ /* Clean up allocations to avoid leaks */
+ kfree(map.forward);
+ kfree(map.reverse);

Nice.

No tag, add me into V2, please?

Thanks!

Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling
Quoted 6 lines · Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

Ack.

Quoted 6 lines

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

love it.

Quoted 4 lines

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

Resounding ack.

Quoted 8 lines
	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents];
	else
		dest = &map->forward[map->nr_extents];

	*dest = *extent;
	map->nr_extents++;
Quoted 6 lines

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

oh!

Quoted 11 lines

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

	map->nr_extents++;
	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents - 1];
	else
		dest = &map->forward[map->nr_extents - 1];

	*dest = *extent;
Quoted 1 line
Assisted-by Gemini Next

Id like to wonder what the hell that model is Gemini 3.5 pro?, I gave it a Google and saw nothing.

Btw.

Quoted 6 lines

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <cyphar>

@cyphar.com>

Quoted 47 lines
Cc Jan Kara <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Miguel Ojeda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Nicolas Schier <[email protected]>
Cc Gary Guo <[email protected]>
Cc "Thomas Weißschuh" <[email protected]>
Cc Alice Ryhl <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Anand Moon <[email protected]>
Cc Oleg Nesterov <[email protected]>

---
include/linux/user_namespace.h | 4 ++--
kernel/user_namespace.c | 8 ++++----

2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
 			u32 nr_extents;
 		};
 		struct {
-			struct uid_gid_extent *forward;
-			struct uid_gid_extent *reverse;
+			struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+			struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
 		};
 	};
 };
kernel/user_namespace.c +2 -2
index 0bed462e9b2a..7e5371d8f515 100644
@@ -809,13 +809,13 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 		map->reverse = NULL;
 	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	map->nr_extents++;
+	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)

kernel/user_namespace.c static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent) · line 821

Quoted 10 lines
kernel/user_namespace.c +2 -2
+		dest = &map->extent[map->nr_extents - 1];
 	else
-		dest = &map->forward[map->nr_extents];
+		dest = &map->forward[map->nr_extents - 1];
 
 	*dest = *extent;
-	map->nr_extents++;
 	return 0;
 }
 

Thanks!

#3 lore raw

Re: [PATCH 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling
Quoted 36 lines · Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents];
	else
		dest = &map->forward[map->nr_extents];

	*dest = *extent;
	map->nr_extents++;

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

Quoted 7 lines
	map->nr_extents++;
	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents - 1];
	else
		dest = &map->forward[map->nr_extents - 1];

	*dest = *extent;

I leave this to you and other reviewers (add Alexey), you can safely ignore my nit.

To me

	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent;
	else
		dest = &map->forward;

	map->nr_extents++;
	dest[map->nr_extents - 1] = *extent;

looks a bit more clear, but this is minor/subjective.

Either way, I think this needs a short comment to explain why do we need to increment ->nr_extents first, then subtract 1. IOW, to explain why (say)

	dest[map->nr_extents++] = *extent;

would be wrong.

Oleg.

Quoted 66 lines
Assisted-by Gemini Next

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 include/linux/user_namespace.h | 4 ++--
 kernel/user_namespace.c        | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
 			u32 nr_extents;
 		};
 		struct {
-			struct uid_gid_extent *forward;
-			struct uid_gid_extent *reverse;
+			struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+			struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
 		};
 	};
 };
kernel/user_namespace.c +4 -4
index 0bed462e9b2a..7e5371d8f515 100644
@@ -809,13 +809,13 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 		map->reverse = NULL;
 	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	map->nr_extents++;
+	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+		dest = &map->extent[map->nr_extents - 1];
 	else
-		dest = &map->forward[map->nr_extents];
+		dest = &map->forward[map->nr_extents - 1];
 
 	*dest = *extent;
-	map->nr_extents++;
 	return 0;
 }
 
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
Reviewed-by on v1 1/2
#5 lore raw

Re: [PATCH 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling
Quoted 43 lines · Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents];
	else
		dest = &map->forward[map->nr_extents];

	*dest = *extent;
	map->nr_extents++;

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

	map->nr_extents++;
	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents - 1];
	else
		dest = &map->forward[map->nr_extents - 1];

	*dest = *extent;
Quoted 2 lines
Assisted-by Gemini Next

Thanks
-Gustavo

Quoted 61 lines

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
  include/linux/user_namespace.h | 4 ++--
  kernel/user_namespace.c        | 8 ++++----
  2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
  			u32 nr_extents;
  		};
  		struct {
-			struct uid_gid_extent *forward;
-			struct uid_gid_extent *reverse;
+			struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+			struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
  		};
  	};
  };
kernel/user_namespace.c +4 -4
index 0bed462e9b2a..7e5371d8f515 100644
@@ -809,13 +809,13 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
  		map->reverse = NULL;
  	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	map->nr_extents++;
+	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+		dest = &map->extent[map->nr_extents - 1];
  	else
-		dest = &map->forward[map->nr_extents];
+		dest = &map->forward[map->nr_extents - 1];
 
  	*dest = *extent;
-	map->nr_extents++;
  	return 0;
  }
 
#13 lore raw

Re: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling
Quoted 8 lines · Bill Wendling

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.

AFAIU patch 1 is supposed to not change any behavior. You could move the unit test to the front to make that clearer and also validate it.

Quoted 2 lines
Assisted-by Gemini Next
Change-Id If0c2c197a35cd7429cf0d2d6e3b33f0d9f0be66c

Change-Id should not be used upstream.
See Documentation/dev-tools/checkpatch.rst.

Quoted 28 lines

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 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

(...)

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+static void test_user_ns_map_insert_extended(struct kunit *test)
+{
+	struct uid_gid_map map;
+	struct uid_gid_extent extent;
+	int i, ret;

(...)

kernel/user_namespace_kunit.c line 63

Quoted 4 lines
kernel/user_namespace_kunit.c new file +4 -0
+	/* 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);

KUNIT_EXPECT_*() will *not* abort the test when the assertion fails ...

kernel/user_namespace_kunit.c line 68

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+
+	/* 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);

... leading to a crash here if map.reverse is invalid.

To also abort the test on assertion failure use KUNIT_ASSERT_*().

kernel/user_namespace_kunit.c line 71

Quoted 3 lines
kernel/user_namespace_kunit.c new file +3 -0
+	}
+
+	/* Clean up allocations to avoid leaks */

Pointless comment. This is true for every single call of kfree().

Quoted 3 lines
kernel/user_namespace_kunit.c new file +3 -0
+	kfree(map.forward);
+	kfree(map.reverse);
+}

(...)

#12 lore raw

Re: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bradley Morgan

Hi Bradley,

Quoted 7 lines · Bradley Morgan

Hi Bill,

Quoted 3 lines

+config USER_NAMESPACE_KUNIT_TEST
+ tristate "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS
+ depends on USER_NS && KUNIT

Quoted 8 lines

The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y

Okay.

Quoted 11 lines

kernel/user_namespace_kunit.c line 69

Quoted 5 lines

+ /* Verify sorting is correct */
+ for (i = 0; i < map.nr_extents; i++) {

kernel/user_namespace_kunit.c new file +3 -0
+  KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+  KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+ }

This doesn't verify any sorting. Every extent was inserted with count 5, so the loop passes even if sort_idmaps() did nothing. Either assert that forward is ordered by .first and reverse by .lower_first, or drop the sorting claim from the changelog.

Good call. I added in checking for actual values.

Quoted 8 lines
Quoted 3 lines

+ /* Clean up allocations to avoid leaks */
+ kfree(map.forward);
+ kfree(map.reverse);

Nice.

No tag, add me into V2, please?

Done!

-bw

#14 lore raw

Re: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Thomas Weißschuh

Hi Thomas,

Quoted 7 lines · Thomas Weißschuh
Quoted 5 lines · Bill Wendling

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).

Quoted 8 lines
Quoted 3 lines

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.

AFAIU patch 1 is supposed to not change any behavior. You could move the unit test to the front to make that clearer and also validate it.

Patch 1 generates code to assert if a memory address outside of the bounds of a pointer is accessed. So it does change the behavior. Should I still move the test case?

Quoted 6 lines
Quoted 2 lines
Assisted-by Gemini Next
Change-Id If0c2c197a35cd7429cf0d2d6e3b33f0d9f0be66c

Change-Id should not be used upstream.
See Documentation/dev-tools/checkpatch.rst.

I had removed it in v2. :-)

Quoted 52 lines
Quoted 28 lines

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 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

(...)

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+static void test_user_ns_map_insert_extended(struct kunit *test)
+{
+     struct uid_gid_map map;
+     struct uid_gid_extent extent;
+     int i, ret;

(...)

kernel/user_namespace_kunit.c line 63

Quoted 4 lines
kernel/user_namespace_kunit.c new file +4 -0
+     /* 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);

KUNIT_EXPECT_*() will *not* abort the test when the assertion fails ...

kernel/user_namespace_kunit.c line 68

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+
+     /* 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);
Quoted 4 lines

... leading to a crash here if map.reverse is invalid.

To also abort the test on assertion failure use KUNIT_ASSERT_*().

Roger that! I changed them to KUNIT_ASSERT_*.

Quoted 6 lines

kernel/user_namespace_kunit.c line 71

Quoted 3 lines
kernel/user_namespace_kunit.c new file +3 -0
+     }
+
+     /* Clean up allocations to avoid leaks */

Pointless comment. This is true for every single call of kfree().

Kiboshed.

Thanks!
-bw

#8 lore raw

PATCH v3 2/2 userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling

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

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 init/Kconfig                  | 10 ++++
 kernel/.kunitconfig           |  3 ++
 kernel/user_namespace.c       |  4 ++
 kernel/user_namespace_kunit.c | 91 +++++++++++++++++++++++++++++++++++
 4 files changed, 108 insertions(+)
 create mode 100644 kernel/.kunitconfig
 create mode 100644 kernel/user_namespace_kunit.c
init/Kconfig +10 -0
index f63bf5e05e79..ba6a40b7315a 100644
@@ -1457,6 +1457,16 @@ config USER_NS
 
 	  If unsure, say N.
 
+config USER_NAMESPACE_KUNIT_TEST
+	bool "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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 7e5371d8f515..64c64e1028e8 100644
@@ -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
kernel/user_namespace_kunit.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#9 lore raw

Re: [PATCH v3 2/2] userns: Add KUnit test suite for uid_gid_map

Review

Quoted 4 lines

+ /* Insert more than UID_GID_MAP_MAX_BASE_EXTENTS (e.g., 10) elements */
+ for (i = 0; i < 10; i++) {

(quoted diff) +2 -0
+   extent.first = i * 10;
+   extent.lower_first = i * 100;

Both .first and .lower_first increase monotonically with i. So forward[] is already sorted by .first and reverse[] is already sorted by .lower_first before sort_idmaps() runs. The sort is a no op on this input.

Review

Quoted 4 lines

+ /* Verify sorting is correct */
+ for (i = 0; i < map.nr_extents; i++) {

(quoted diff) +2 -0
+   KUNIT_EXPECT_EQ(test, map.forward[i].first, i * 10);
+   KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);

This passes even if sort_idmaps() did nothing. The test cannot tell a working sort from a broken one.

To actually verify sorting, insert the extents out of order so that sort_idmaps() has to rearrange them. For example:

    extent.first = (9 - i) * 10;
    extent.lower_first = (9 - i) * 100;

Then after sort_idmaps(), forward[0].first should be 0, forward[1] should be 10, etc. That proves the sort moved things.

Nits:

Quoted 1 line

+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, map.forward);

Redundant. insert_extent() just allocated it on the 6th call, and the previous KUNIT_EXPECT_EQ(test, ret, 0) already confirmed success. If you want to keep it, at least put it after the loop where it protects the sort, not inside the insertion loop where it tests something that just succeeded.

Quoted 1 line

+ KUNIT_EXPECT_EQ(test, ret, 0);

Should be KUNIT_ASSERT_EQ. If insert_extent() fails the test continues and the next assertions run on uninitialized data. Same for the base test.

No tag yet, fix the test data and I'll tag v4.

--- Thanks!
https://lore.kernel.org/all/[email protected]/

#22 lore raw

PATCH v4 1/2 userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents];
	else
		dest = &map->forward[map->nr_extents];

	*dest = *extent;
	map->nr_extents++;

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

	map->nr_extents++;
	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents - 1];
	else
		dest = &map->forward[map->nr_extents - 1];

	*dest = *extent;

---
v2 - Remove Gerrit tag.
v4 - Added comment explaning the change. Corrected the "Assisted-by" tag.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 include/linux/user_namespace.h |  4 ++--
 kernel/user_namespace.c        | 12 ++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
 			u32 nr_extents;
 		};
 		struct {
-			struct uid_gid_extent *forward;
-			struct uid_gid_extent *reverse;
+			struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+			struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
 		};
 	};
 };
kernel/user_namespace.c +8 -4
index 0bed462e9b2a..786dbf0506ca 100644
@@ -809,13 +809,17 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 		map->reverse = NULL;
 	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	/*
+	 * nr_extents must be updated before the extent and forward arrays are
+	 * accessed, otherwise KSAN will assert an out-of-bounds error.
+	 */
+	map->nr_extents++;
+	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+		dest = &map->extent[map->nr_extents - 1];
 	else
-		dest = &map->forward[map->nr_extents];
+		dest = &map->forward[map->nr_extents - 1];
 
 	*dest = *extent;
-	map->nr_extents++;
 	return 0;
 }
 
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#23 lore raw

PATCH v4 2/2 userns: Add KUnit test suite for uid_gid_map

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:3.1-pro-preview

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
v4 - Actually test on unsorted data. Corrected the "Assisted-by" tag.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 init/Kconfig                  | 10 ++++
 kernel/.kunitconfig           |  3 ++
 kernel/user_namespace.c       |  4 ++
 kernel/user_namespace_kunit.c | 92 +++++++++++++++++++++++++++++++++++
 4 files changed, 109 insertions(+)
 create mode 100644 kernel/.kunitconfig
 create mode 100644 kernel/user_namespace_kunit.c
init/Kconfig +10 -0
index f63bf5e05e79..d460344539a5 100644
@@ -1457,6 +1457,16 @@ config USER_NS
 
 	  If unsure, say N.
 
+config USER_NAMESPACE_KUNIT_TEST
+	bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+	depends on KUNIT=y
+	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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#10 lore raw

Re: [PATCH v3 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bradley Morgan
Quoted 38 lines · Bradley Morgan

Review

Quoted 5 lines

+ /* Insert more than UID_GID_MAP_MAX_BASE_EXTENTS (e.g., 10) elements */
+ for (i = 0; i < 10; i++) {

(quoted diff) +2 -0
+   extent.first = i * 10;
+   extent.lower_first = i * 100;

Both .first and .lower_first increase monotonically with i. So forward[] is already sorted by .first and reverse[] is already sorted by .lower_first before sort_idmaps() runs. The sort is a no op on this input.

Review

Quoted 4 lines

+ /* Verify sorting is correct */
+ for (i = 0; i < map.nr_extents; i++) {

(quoted diff) +2 -0
+   KUNIT_EXPECT_EQ(test, map.forward[i].first, i * 10);
+   KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);

This passes even if sort_idmaps() did nothing. The test cannot tell a working sort from a broken one.

To actually verify sorting, insert the extents out of order so that sort_idmaps() has to rearrange them. For example:

    extent.first = (9 - i) * 10;
    extent.lower_first = (9 - i) * 100;

Then after sort_idmaps(), forward[0].first should be 0, forward[1] should be 10, etc. That proves the sort moved things.

Nits:

Quoted 1 line

+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, map.forward);

Redundant. insert_extent() just allocated it on the 6th call, and the previous KUNIT_EXPECT_EQ(test, ret, 0) already confirmed success. If you want to keep it, at least put it after the loop where it protects the sort, not inside the insertion loop where it tests something that just succeeded.

Quoted 6 lines
Quoted 1 line

+ KUNIT_EXPECT_EQ(test, ret, 0);

Should be KUNIT_ASSERT_EQ. If insert_extent() fails the test continues and the next assertions run on uninitialized data. Same for the base test.

Billionth times the charm!

I hope I've addressed everyone's concerns. Obviously let me know if I didn't. :-)

Quoted 1 line

No tag yet, fix the test data and I'll tag v4.

I added your "Reviewed-by" on "PATCH 1/2", but not this one.

-bw

Quoted 3 lines

--- Thanks!
https://lore.kernel.org/all/[email protected]/

#4 lore raw

Re: [PATCH 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Oleg Nesterov
Quoted 61 lines · Oleg Nesterov
Quoted 36 lines · Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

      if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
              dest = &map->extent[map->nr_extents];
      else
              dest = &map->forward[map->nr_extents];

      *dest = *extent;
      map->nr_extents++;

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

Quoted 7 lines
      map->nr_extents++;
      if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
              dest = &map->extent[map->nr_extents - 1];
      else
              dest = &map->forward[map->nr_extents - 1];

      *dest = *extent;

I leave this to you and other reviewers (add Alexey), you can safely ignore my nit.

To me

        if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
                dest = &map->extent;
        else
                dest = &map->forward;

        map->nr_extents++;
        dest[map->nr_extents - 1] = *extent;

looks a bit more clear, but this is minor/subjective.

Quoted 8 lines

Either way, I think this needs a short comment to explain why do we need to increment ->nr_extents first, then subtract 1. IOW, to explain why (say)

        dest[map->nr_extents++] = *extent;

would be wrong.

Hi Oleg,

I added a comment before the code in question explaining why it's organized that way. I certainly don't hate your version, but for simplicity of reviews I just left the code as is.

-bw

Quoted 69 lines

Oleg.

Quoted 66 lines
Assisted-by Gemini Next

---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 include/linux/user_namespace.h | 4 ++--
 kernel/user_namespace.c        | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
                      u32 nr_extents;
              };
              struct {
-                     struct uid_gid_extent *forward;
-                     struct uid_gid_extent *reverse;
+                     struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+                     struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
              };
      };
 };
kernel/user_namespace.c +4 -5
index 0bed462e9b2a..7e5371d8f515 100644
@@ -809,13 +809,13 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
              map->reverse = NULL;
      }
 
-     if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-             dest = &map->extent[map->nr_extents];
+     map->nr_extents++;
+     if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+             dest = &map->extent[map->nr_extents - 1];
      else
-             dest = &map->forward[map->nr_extents];
+             dest = &map->forward[map->nr_extents - 1];
 
      *dest = *extent;
-     map->nr_extents++;
      return 0;
 }
 
--

2.55.0.860.g4b6b3295ed-goog

Tested-by Reviewed-by on v4 2/2
#24 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling
Quoted 10 lines · Bill Wendling

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:3.1-pro-preview

Now for all the work s/you/your AI done, I've done the tests and they passed, and they LGTM

Thanks for your patch mate, (pls slow down tho, we can review oh so quick)

Quoted 167 lines

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
v4 - Actually test on unsorted data. Corrected the "Assisted-by" tag.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---

init/Kconfig                  | 10 ++++
kernel/.kunitconfig           |  3 ++
kernel/user_namespace.c       |  4 ++
kernel/user_namespace_kunit.c | 92 +++++++++++++++++++++++++++++++++++
4 files changed, 109 insertions(+)

create mode 100644 kernel/.kunitconfig
create mode 100644 kernel/user_namespace_kunit.c

init/Kconfig +10 -0
index f63bf5e05e79..d460344539a5 100644
@@ -1457,6 +1457,16 @@ config USER_NS
 
 	  If unsure, say N.
 
+config USER_NAMESPACE_KUNIT_TEST
+	bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+	depends on KUNIT=y
+	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

kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +82 -0
new file mode 100644
index 000000000000..88467361efdf
@@ -0,0 +1,92 @@
+// 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_ASSERT_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++) {
+		int value = 9 - i;
+
+		extent.first = value * 10;
+		extent.lower_first = value * 100;
+		extent.count = 5;
+
+		ret = insert_extent(&map, &extent);
+		KUNIT_ASSERT_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, value * 10);
+		} else {
+			KUNIT_EXPECT_EQ(test, map.forward[i].first, value * 10);
+			KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, value  * 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_ASSERT_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].first, i * 10);
+		KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, i * 100);
+		KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+
+		KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+		KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+		KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+	}
+
+	kfree(map.forward);
+	kfree(map.reverse);
+}
+
+static struct kunit_case user_ns_map_test_cases[] = {
+	KUNIT_CASE(test_user_ns_map_insert_base),

kernel/user_namespace_kunit.c line 92

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+	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);

--- Thanks!
https://lore.kernel.org/all/[email protected]/

#15 lore raw

Re: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling
Quoted 13 lines · Bill Wendling
Quoted 11 lines · Thomas Weißschuh
Quoted 8 lines · Bill Wendling

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.

Quoted 6 lines
Quoted 4 lines

AFAIU patch 1 is supposed to not change any behavior. You could move the unit test to the front to make that clearer and also validate it.

Patch 1 generates code to assert if a memory address outside of the bounds of a pointer is accessed. So it does change the behavior.

It should not change the behavior observed by a well-behaved caller. And the test only simulates such a well-behaved caller.

Quoted 1 line

Should I still move the test case?

I think so. At least if you resend the series anyways. Make sure to also run the test before your other patch is applied.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Assisted-by Gemini Next
Change-Id If0c2c197a35cd7429cf0d2d6e3b33f0d9f0be66c

Change-Id should not be used upstream.
See Documentation/dev-tools/checkpatch.rst.

I had removed it in v2. :-)

Thanks!

(...)

#25 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling
Quoted 51 lines · Bill Wendling

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:3.1-pro-preview

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
v4 - Actually test on unsorted data. Corrected the "Assisted-by" tag.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 init/Kconfig                  | 10 ++++
 kernel/.kunitconfig           |  3 ++
 kernel/user_namespace.c       |  4 ++
 kernel/user_namespace_kunit.c | 92 +++++++++++++++++++++++++++++++++++
 4 files changed, 109 insertions(+)
 create mode 100644 kernel/.kunitconfig
 create mode 100644 kernel/user_namespace_kunit.c
init/Kconfig +0 -0
index f63bf5e05e79..d460344539a5 100644

init/Kconfig config USER_NS · line 1462

Quoted 6 lines
init/Kconfig +3 -0
@@ -1457,6 +1457,16 @@ config USER_NS
 
 	  If unsure, say N.
 
+config USER_NAMESPACE_KUNIT_TEST
+	bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+	depends on KUNIT=y

Urgh.

Quoted 57 lines
init/Kconfig +7 -0
+	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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +20 -0
new file mode 100644
index 000000000000..88467361efdf
@@ -0,0 +1,92 @@
+// 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;

kernel/user_namespace_kunit.c line 30

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+		extent.count = 5;
+
+		ret = insert_extent(&map, &extent);
+		KUNIT_ASSERT_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);
+	}
+}

The extended test below already tests everything the 'base' one does. Do we need both?

kernel/user_namespace_kunit.c line 40

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+
+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 */

When UID_GID_MAP_MAX_BASE_EXTENTS is ever increased, this might not be true anymore. Add an assertion or make the iteration count dynamic.

Quoted 25 lines
kernel/user_namespace_kunit.c new file +25 -0
+	for (i = 0; i < 10; i++) {
+		int value = 9 - i;
+
+		extent.first = value * 10;
+		extent.lower_first = value * 100;
+		extent.count = 5;
+
+		ret = insert_extent(&map, &extent);
+		KUNIT_ASSERT_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, value * 10);
+		} else {
+			KUNIT_EXPECT_EQ(test, map.forward[i].first, value * 10);
+			KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, value  * 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_ASSERT_NOT_ERR_OR_NULL(test, map.reverse);
+

kernel/user_namespace_kunit.c line 75

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+	/* Verify sorting is correct */
+	for (i = 0; i < map.nr_extents; i++) {
+		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);
+
+		KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+		KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+		KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+	}

Isn't this the same as the original order?

Quoted 4 lines
kernel/user_namespace_kunit.c new file +4 -0
+
+	kfree(map.forward);
+	kfree(map.reverse);
+}

(...)

#26 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

Quoted 54 lines · Thomas Weißschuh
Quoted 52 lines · Bill Wendling

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:3.1-pro-preview

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
v4 - Actually test on unsorted data. Corrected the "Assisted-by" tag.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 init/Kconfig                  | 10 ++++
 kernel/.kunitconfig           |  3 ++
 kernel/user_namespace.c       |  4 ++
 kernel/user_namespace_kunit.c | 92 +++++++++++++++++++++++++++++++++++
 4 files changed, 109 insertions(+)
 create mode 100644 kernel/.kunitconfig
 create mode 100644 kernel/user_namespace_kunit.c
init/Kconfig +0 -0
index f63bf5e05e79..d460344539a5 100644
@@ -1457,6 +1457,16 @@ config USER_NS
 
Quoted 8 lines

init/Kconfig config USER_NS · line 1462

Quoted 5 lines
init/Kconfig +3 -0
        If unsure, say N.
 
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

Quoted 62 lines
Quoted 62 lines
init/Kconfig +7 -0
+     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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +25 -0
new file mode 100644
index 000000000000..88467361efdf
@@ -0,0 +1,92 @@
+// 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_ASSERT_EQ(test, ret, 0);
+             KUNIT_EXPECT_EQ(test, map.nr_extents, i + 1);
Quoted 9 lines

kernel/user_namespace_kunit.c line 30

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+             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);
+     }
+}

The extended test below already tests everything the 'base' one does. Do we need both?

The one below tests the sorting algorithm.

Quoted 5 lines
Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+
+static void test_user_ns_map_insert_extended(struct kunit *test)
+{
+     struct uid_gid_map map;
+     struct uid_gid_extent extent;
Quoted 9 lines

kernel/user_namespace_kunit.c line 40

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+     int i, ret;
+
+     memset(&map, 0, sizeof(map));
+
+     /* Insert more than UID_GID_MAP_MAX_BASE_EXTENTS (e.g., 10) elements */

When UID_GID_MAP_MAX_BASE_EXTENTS is ever increased, this might not be true anymore. Add an assertion or make the iteration count dynamic.

Sure, I can make it something like "UID_GID_MAP_MAX_BASE_EXTENTS + 42" or something. Let me know about the "Urgh" comment above first.

Quoted 29 lines
Quoted 29 lines
kernel/user_namespace_kunit.c new file +29 -0
+     for (i = 0; i < 10; i++) {
+             int value = 9 - i;
+
+             extent.first = value * 10;
+             extent.lower_first = value * 100;
+             extent.count = 5;
+
+             ret = insert_extent(&map, &extent);
+             KUNIT_ASSERT_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, value * 10);
+             } else {
+                     KUNIT_EXPECT_EQ(test, map.forward[i].first, value * 10);
+                     KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, value  * 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_ASSERT_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].first, i * 10);
+             KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, i * 100);
Quoted 9 lines

kernel/user_namespace_kunit.c line 75

Quoted 6 lines
kernel/user_namespace_kunit.c new file +6 -0
+             KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+
+             KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+             KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+             KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+     }

Isn't this the same as the original order?

No. The original order uses "9 - i" for the base value, so it's not in sorted order (though it's not exactly random either).

-bw

Quoted 6 lines
Quoted 4 lines
kernel/user_namespace_kunit.c new file +4 -0
+
+     kfree(map.forward);
+     kfree(map.reverse);
+}

(...)

#27 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

Hi Bill,

Quoted 2 lines · Bill Wendling

On Thu, Aug 27, 2026 at 6:37 AM Thomas Weißschuh
<[email protected]> wrote:

(...)

Quoted 7 lines
Quoted 6 lines

init/Kconfig config USER_NS · line 1462

Quoted 3 lines
init/Kconfig +3 -0
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

Sorry for this non-descript review comment.

Quoted 7 lines

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

The problem is that KUNIT can be built as module, which would prevent this test from being built. We have include/kunit/visibility.h to export certain symbols only to tests and avoid this issue.
But I can see that some maintaines don't like this pattern, so maybe they can chime in at some point.

Quoted 71 lines
Quoted 62 lines
Quoted 62 lines
init/Kconfig +7 -0
+     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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +25 -0
new file mode 100644
index 000000000000..88467361efdf
@@ -0,0 +1,92 @@
+// 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_ASSERT_EQ(test, ret, 0);
+             KUNIT_EXPECT_EQ(test, map.nr_extents, i + 1);
Quoted 9 lines

kernel/user_namespace_kunit.c line 30

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+             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);
+     }
+}

The extended test below already tests everything the 'base' one does. Do we need both?

Quoted 1 line

The one below tests the sorting algorithm.

It *also* tests the insertion, no?
(Especially if the conditional on UID_GID_MAP_MAX_BASE_EXTENTS is removed)

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+
+static void test_user_ns_map_insert_extended(struct kunit *test)
+{
+     struct uid_gid_map map;
+     struct uid_gid_extent extent;
Quoted 9 lines

kernel/user_namespace_kunit.c line 40

Quoted 5 lines
kernel/user_namespace_kunit.c new file +5 -0
+     int i, ret;
+
+     memset(&map, 0, sizeof(map));
+
+     /* Insert more than UID_GID_MAP_MAX_BASE_EXTENTS (e.g., 10) elements */

When UID_GID_MAP_MAX_BASE_EXTENTS is ever increased, this might not be true anymore. Add an assertion or make the iteration count dynamic.

Quoted 2 lines

Sure, I can make it something like "UID_GID_MAP_MAX_BASE_EXTENTS + 42" or something.

That sounds good.

Quoted 38 lines
Quoted 29 lines
Quoted 29 lines
kernel/user_namespace_kunit.c new file +29 -0
+     for (i = 0; i < 10; i++) {
+             int value = 9 - i;
+
+             extent.first = value * 10;
+             extent.lower_first = value * 100;
+             extent.count = 5;
+
+             ret = insert_extent(&map, &extent);
+             KUNIT_ASSERT_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, value * 10);
+             } else {
+                     KUNIT_EXPECT_EQ(test, map.forward[i].first, value * 10);
+                     KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, value  * 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_ASSERT_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].first, i * 10);
+             KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, i * 100);
Quoted 9 lines

kernel/user_namespace_kunit.c line 75

Quoted 6 lines
kernel/user_namespace_kunit.c new file +6 -0
+             KUNIT_EXPECT_EQ(test, map.forward[i].count, 5);
+
+             KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+             KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+             KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+     }

Isn't this the same as the original order?

Quoted 2 lines

No. The original order uses "9 - i" for the base value, so it's not in sorted order (though it's not exactly random either).

Indeed, sorry for missing this.
If you send a new revision, maybe add a small comment.

Thomas

#28 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

Hi Thomas,

Quoted 27 lines · Thomas Weißschuh

Hi Bill,

Quoted 2 lines · Bill Wendling

On Thu, Aug 27, 2026 at 6:37 AM Thomas Weißschuh
<[email protected]> wrote:

(...)

Quoted 7 lines
Quoted 6 lines

init/Kconfig config USER_NS · line 1462

Quoted 3 lines
init/Kconfig +3 -0
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

Sorry for this non-descript review comment.

Quoted 7 lines

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

Quoted 5 lines

The problem is that KUNIT can be built as module, which would prevent this test from being built. We have include/kunit/visibility.h to export certain symbols only to tests and avoid this issue.
But I can see that some maintaines don't like this pattern, so maybe they can chime in at some point.

Bradley commented on this earlier (which is why I mentioned EXEC_KUNIT_TEST):

<comment>
The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y
</comment>

So there's a conflict and, because I'm not a KUnit guru, I'm not sure which way is "best".

Quoted 71 lines
Quoted 71 lines
Quoted 71 lines
Quoted 57 lines
init/Kconfig +7 -0
+     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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +20 -0
new file mode 100644
index 000000000000..88467361efdf
@@ -0,0 +1,92 @@
+// 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;

kernel/user_namespace_kunit.c line 30

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+             extent.count = 5;
+
+             ret = insert_extent(&map, &extent);
+             KUNIT_ASSERT_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);
+     }
+}

The extended test below already tests everything the 'base' one does. Do we need both?

Quoted 5 lines
Quoted 1 line

The one below tests the sorting algorithm.

It *also* tests the insertion, no?
(Especially if the conditional on UID_GID_MAP_MAX_BASE_EXTENTS is removed)

Correct. So there are three types of tests we should run here:

1. Insertions and accesses that don't go over the initial extents size.
2. Insertions and accesses that do go over the initial extents size.
3. Accesses outside of the number of entries.

Test (1) is a "smoke" test, where the struct is tested and no sanitizer code is used. Test (2) makes sure that we can still go over the UID_GID_MAP_MAX_BASE_EXTENTS size and the sanitizer won't activate. Test (3) (which I'll add in my next upload) throws a sanitizer exception.

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 25 lines
kernel/user_namespace_kunit.c new file +25 -0
+     for (i = 0; i < 10; i++) {
+             int value = 9 - i;
+
+             extent.first = value * 10;
+             extent.lower_first = value * 100;
+             extent.count = 5;
+
+             ret = insert_extent(&map, &extent);
+             KUNIT_ASSERT_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, value * 10);
+             } else {
+                     KUNIT_EXPECT_EQ(test, map.forward[i].first, value * 10);
+                     KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, value  * 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_ASSERT_NOT_ERR_OR_NULL(test, map.reverse);
+

kernel/user_namespace_kunit.c line 75

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+     /* Verify sorting is correct */
+     for (i = 0; i < map.nr_extents; i++) {
+             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);
+
+             KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+             KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+             KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+     }

Isn't this the same as the original order?

Quoted 6 lines
Quoted 2 lines

No. The original order uses "9 - i" for the base value, so it's not in sorted order (though it's not exactly random either).

Indeed, sorry for missing this.
If you send a new revision, maybe add a small comment.

Ack.

-bw

#29 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

Quoted 40 lines · Bill Wendling
Quoted 24 lines · Thomas Weißschuh
Quoted 2 lines · Bill Wendling

On Thu, Aug 27, 2026 at 6:37 AM Thomas Weißschuh
<[email protected]> wrote:

(...)

Quoted 7 lines
Quoted 6 lines

init/Kconfig config USER_NS · line 1462

Quoted 3 lines
init/Kconfig +3 -0
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

Sorry for this non-descript review comment.

Quoted 7 lines

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

Quoted 5 lines

The problem is that KUNIT can be built as module, which would prevent this test from being built. We have include/kunit/visibility.h to export certain symbols only to tests and avoid this issue.
But I can see that some maintaines don't like this pattern, so maybe they can chime in at some point.

Bradley commented on this earlier (which is why I mentioned EXEC_KUNIT_TEST):

<comment>
The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

Quoted 6 lines

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y
</comment>

So there's a conflict and, because I'm not a KUnit guru, I'm not sure which way is "best".

It's subjective. So as mentioned before, the preference of the maintainers should go into it. The aproach I prefer requires a bit more setup boilerplate but make the tests usable in more circumstances.

Quoted 76 lines
Quoted 71 lines
Quoted 71 lines
Quoted 71 lines
Quoted 57 lines
init/Kconfig +7 -0
+     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
kernel/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..7314dce05dc2
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_USER_NS=y
+CONFIG_USER_NAMESPACE_KUNIT_TEST=y
kernel/user_namespace.c +4 -0
index 786dbf0506ca..0e7373085af9 100644
@@ -1417,3 +1417,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
kernel/user_namespace_kunit.c new file +20 -0
new file mode 100644
index 000000000000..88467361efdf
@@ -0,0 +1,92 @@
+// 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;

kernel/user_namespace_kunit.c line 30

Quoted 10 lines
kernel/user_namespace_kunit.c new file +10 -0
+             extent.count = 5;
+
+             ret = insert_extent(&map, &extent);
+             KUNIT_ASSERT_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);
+     }
+}

The extended test below already tests everything the 'base' one does. Do we need both?

Quoted 5 lines
Quoted 1 line

The one below tests the sorting algorithm.

It *also* tests the insertion, no?
(Especially if the conditional on UID_GID_MAP_MAX_BASE_EXTENTS is removed)

Quoted 8 lines

Correct. So there are three types of tests we should run here:

1. Insertions and accesses that don't go over the initial extents size.
2. Insertions and accesses that do go over the initial extents size.
3. Accesses outside of the number of entries.

Test (1) is a "smoke" test, where the struct is tested and no sanitizer code is used.

A "smoke" test is useful when the more complete tests can not be run regularly. But here both test cases will always run right after each other. Test (2) is just as cheap as this one.

*Not* testing the overflow checking here sounds also weird. Test (2) will excercise the same code, which is not using the checking, anyways.

Quoted 3 lines

Test (2) makes sure that we can still go over
the UID_GID_MAP_MAX_BASE_EXTENTS size and the sanitizer won't activate.

Nice.

Quoted 1 line

Test (3) (which I'll add in my next upload) throws a sanitizer exception.

What is the point of testing this specifically for user namespaces? Normally we expect a used subsystem to work as advertised. It is that used subsystem's responsibility to test that it does so. If there is currently no test that validates __counted_by then it surely should be created. But not here.

Thomas

#30 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

Quoted 46 lines · Thomas Weißschuh
Quoted 44 lines · Bill Wendling
Quoted 24 lines · Thomas Weißschuh
Quoted 2 lines · Bill Wendling

On Thu, Aug 27, 2026 at 6:37 AM Thomas Weißschuh
<[email protected]> wrote:

(...)

Quoted 7 lines
Quoted 6 lines

init/Kconfig config USER_NS · line 1462

Quoted 3 lines
init/Kconfig +3 -0
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

Sorry for this non-descript review comment.

Quoted 7 lines

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

Quoted 5 lines

The problem is that KUNIT can be built as module, which would prevent this test from being built. We have include/kunit/visibility.h to export certain symbols only to tests and avoid this issue.
But I can see that some maintaines don't like this pattern, so maybe they can chime in at some point.

Bradley commented on this earlier (which is why I mentioned EXEC_KUNIT_TEST):

<comment>
The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y
</comment>

Quoted 7 lines
Quoted 2 lines

So there's a conflict and, because I'm not a KUnit guru, I'm not sure which way is "best".

It's subjective. So as mentioned before, the preference of the maintainers should go into it. The aproach I prefer requires a bit more setup boilerplate but make the tests usable in more circumstances.

Because user_namespace.c is always built-in (USER_NS is a bool), compiling the test into vmlinux causes linker failures if CONFIG_KUNIT=m. Using the "visibility.h" version also strips static from insert_extent() and sort_idmaps() or exporting internal user namespace functions into the kernel symbol table, which isn't ideal.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

The extended test below already tests everything the 'base' one does. Do we need both?

The one below tests the sorting algorithm.

It *also* tests the insertion, no?
(Especially if the conditional on UID_GID_MAP_MAX_BASE_EXTENTS is removed)

Quoted 8 lines

Correct. So there are three types of tests we should run here:

1. Insertions and accesses that don't go over the initial extents size.
2. Insertions and accesses that do go over the initial extents size.
3. Accesses outside of the number of entries.

Test (1) is a "smoke" test, where the struct is tested and no sanitizer code is used.

Quoted 7 lines

A "smoke" test is useful when the more complete tests can not be run regularly. But here both test cases will always run right after each other. Test (2) is just as cheap as this one.

*Not* testing the overflow checking here sounds also weird. Test (2) will excercise the same code, which is not using the checking, anyways.

I suppose this depends on one's view of how testing should be done. I prefer to have small test cases which directly test a specific feature. Other test cases would still run the same code, but they focus on features. So yes, the same code is ran many times during testing, but that's fine, because testing is meant to test one feature at a time. The benefit of this approach is if a test case fails, it's easier to determine what feature is responsible.

Quoted 6 lines
Quoted 3 lines

Test (2) makes sure that we can still go over
the UID_GID_MAP_MAX_BASE_EXTENTS size and the sanitizer won't activate.

Nice.

Quoted 8 lines
Quoted 1 line

Test (3) (which I'll add in my next upload) throws a sanitizer exception.

What is the point of testing this specifically for user namespaces? Normally we expect a used subsystem to work as advertised. It is that used subsystem's responsibility to test that it does so. If there is currently no test that validates __counted_by then it surely should be created. But not here.

This would directly test that the attribute on the struct field is caught by UBSAN. I'm not sure how we could more directly test it otherwise...

-bw

#31 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

Quoted 55 lines · Bill Wendling
Quoted 46 lines · Thomas Weißschuh
Quoted 44 lines · Bill Wendling
Quoted 24 lines · Thomas Weißschuh
Quoted 2 lines · Bill Wendling

On Thu, Aug 27, 2026 at 6:37 AM Thomas Weißschuh
<[email protected]> wrote:

(...)

Quoted 7 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

Sorry for this non-descript review comment.

Quoted 7 lines

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

Quoted 5 lines

The problem is that KUNIT can be built as module, which would prevent this test from being built. We have include/kunit/visibility.h to export certain symbols only to tests and avoid this issue.
But I can see that some maintaines don't like this pattern, so maybe they can chime in at some point.

Bradley commented on this earlier (which is why I mentioned EXEC_KUNIT_TEST):

<comment>
The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y
</comment>

Quoted 7 lines
Quoted 2 lines

So there's a conflict and, because I'm not a KUnit guru, I'm not sure which way is "best".

It's subjective. So as mentioned before, the preference of the maintainers should go into it. The aproach I prefer requires a bit more setup boilerplate but make the tests usable in more circumstances.

Quoted 5 lines

Because user_namespace.c is always built-in (USER_NS is a bool), compiling the test into vmlinux causes linker failures if CONFIG_KUNIT=m. Using the "visibility.h" version also strips static from insert_extent() and sort_idmaps() or exporting internal user namespace functions into the kernel symbol table, which isn't ideal.

The export namespacing should limit it to just KUnit, so that should be okay, I would think? My guide for this in the past has been "did the kernel get less fast/safe because of Kunit exports, etc?" I don't _think_ that's the case here.

So, I guess it's down to a matter of taste by USER_NS maintainers? I've move Christian to "To:" from "Cc:", as I think he is the defacto USER_NS maintainer? I don't know, though, there is no MAINTAINERS entry for it.

Quoted 11 lines
Quoted 8 lines
Quoted 1 line

Test (3) (which I'll add in my next upload) throws a sanitizer exception.

What is the point of testing this specifically for user namespaces? Normally we expect a used subsystem to work as advertised. It is that used subsystem's responsibility to test that it does so. If there is currently no test that validates __counted_by then it surely should be created. But not here.

This would directly test that the attribute on the struct field is caught by UBSAN. I'm not sure how we could more directly test it otherwise...

It is tricky to catch the traps sanely with KUnit. All the Oops tests I've written for it are in LKDTM, and only __bdos is tested in lib/tests/overflow_kunit.c. I was playing with catching Oops in KUnit for testing KCFI recently, but haven't come back to finishing it safely:
https://lore.kernel.org/lkml/[email protected]/

-Kees

Signature · 1 line
Kees Cook
#33 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling
Quoted 11 lines · Bill Wendling

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:3.1-pro-preview

Various style nit-picks as dictated by
Documentation/dev-tools/kunit/style.rst

(We may need to teach Sashiko about KUnit style?)

Quoted 1 line

kernel/user_namespace_kunit.c | 92 +++++++++++++++++++++++++++++++++++

Like below's naming, this should be named after the suite, and in the tests/ subdir (even when #included), so it should be:
kernel/tests/user_ns_map_kunit.c

Quoted 1 line

+config USER_NAMESPACE_KUNIT_TEST

The suite and Kconfig should match, so this should be USER_NS_MAP_KUNIT_TEST

init/Kconfig config USER_NS · line 1463

Quoted 3 lines
init/Kconfig +3 -0
+	bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+	depends on KUNIT=y
+	default KUNIT_ALL_TESTS

I think you need a "depends on USER_NS" ?

kernel/user_namespace_kunit.c line 90

Quoted 4 lines
kernel/user_namespace_kunit.c new file +4 -0
+static struct kunit_suite user_ns_map_test_suite = {
+	.name = "user_ns_map",
+	.test_cases = user_ns_map_test_cases,
+};

-Kees

Signature · 1 line
Kees Cook
#32 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Kees Cook
Quoted 62 lines · Kees Cook
Quoted 55 lines · Bill Wendling
Quoted 46 lines · Thomas Weißschuh
Quoted 44 lines · Bill Wendling
Quoted 24 lines · Thomas Weißschuh
Quoted 2 lines · Bill Wendling

On Thu, Aug 27, 2026 at 6:37 AM Thomas Weißschuh
<[email protected]> wrote:

(...)

Quoted 7 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+config USER_NAMESPACE_KUNIT_TEST
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y

Urgh.

?? What's wrong? It's identical to the conditional for EXEC_KUNIT_TEST:

Sorry for this non-descript review comment.

Quoted 7 lines

config EXEC_KUNIT_TEST
bool "Build execve tests" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals.

Quoted 5 lines

The problem is that KUNIT can be built as module, which would prevent this test from being built. We have include/kunit/visibility.h to export certain symbols only to tests and avoid this issue.
But I can see that some maintaines don't like this pattern, so maybe they can chime in at some point.

Bradley commented on this earlier (which is why I mentioned EXEC_KUNIT_TEST):

<comment>
The test is #include'd into user_namespace.c, which is builtin (USER_NS is a bool), so =m here still compiles the suite into vmlinux. With KUNIT=m that calls kunit symbols that live in a module, and the link fails. Make it bool and depend on KUNIT=y, like EXEC_KUNIT_TEST:

bool "KUnit test for user namespace map insertion" if !KUNIT_ALL_TESTS depends on USER_NS && KUNIT=y
</comment>

Quoted 7 lines
Quoted 2 lines

So there's a conflict and, because I'm not a KUnit guru, I'm not sure which way is "best".

It's subjective. So as mentioned before, the preference of the maintainers should go into it. The aproach I prefer requires a bit more setup boilerplate but make the tests usable in more circumstances.

Quoted 5 lines

Because user_namespace.c is always built-in (USER_NS is a bool), compiling the test into vmlinux causes linker failures if CONFIG_KUNIT=m. Using the "visibility.h" version also strips static from insert_extent() and sort_idmaps() or exporting internal user namespace functions into the kernel symbol table, which isn't ideal.

Quoted 9 lines

The export namespacing should limit it to just KUnit, so that should be okay, I would think? My guide for this in the past has been "did the kernel get less fast/safe because of Kunit exports, etc?" I don't _think_ that's the case here.

So, I guess it's down to a matter of taste by USER_NS maintainers? I've move Christian to "To:" from "Cc:", as I think he is the defacto USER_NS maintainer? I don't know, though, there is no MAINTAINERS entry for it.

My understanding of Bradley's comment is that we could use visibility.h, but that doesn't give us much because we couldn't run these tests with CONFIG_KUNIT=m anyway. But as I said, I'm not a KUnit guru, which is why I'm leaving the decision to maintainers and reviewers. I'm fine with using the "visibility.h" if that's what people want, and it works.

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
Quoted 1 line

Test (3) (which I'll add in my next upload) throws a sanitizer exception.

What is the point of testing this specifically for user namespaces? Normally we expect a used subsystem to work as advertised. It is that used subsystem's responsibility to test that it does so. If there is currently no test that validates __counted_by then it surely should be created. But not here.

This would directly test that the attribute on the struct field is caught by UBSAN. I'm not sure how we could more directly test it otherwise...

Quoted 6 lines

It is tricky to catch the traps sanely with KUnit. All the Oops tests I've written for it are in LKDTM, and only __bdos is tested in lib/tests/overflow_kunit.c. I was playing with catching Oops in KUnit for testing KCFI recently, but haven't come back to finishing it safely:
https://lore.kernel.org/lkml/[email protected]/

I was using the kunit "run/catch" functions, but it's not complete because they don't tell what underlying issue actually caused the trap, only that a trap occurred. So not ideal. Maybe the LKDTM is the best way to go then...

-bw

#34 lore raw

Re: [PATCH v4 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Kees Cook
Quoted 33 lines · Kees Cook
Quoted 11 lines · Bill Wendling

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:3.1-pro-preview

Various style nit-picks as dictated by
Documentation/dev-tools/kunit/style.rst

(We may need to teach Sashiko about KUnit style?)

Quoted 1 line

kernel/user_namespace_kunit.c | 92 +++++++++++++++++++++++++++++++++++

Like below's naming, this should be named after the suite, and in the tests/ subdir (even when #included), so it should be:
kernel/tests/user_ns_map_kunit.c

Quoted 1 line

+config USER_NAMESPACE_KUNIT_TEST

The suite and Kconfig should match, so this should be USER_NS_MAP_KUNIT_TEST

init/Kconfig config USER_NS · line 1463

Quoted 3 lines
init/Kconfig +3 -0
+     bool "Test user namespace map insertion" if !KUNIT_ALL_TESTS
+     depends on KUNIT=y
+     default KUNIT_ALL_TESTS
Quoted 8 lines

I think you need a "depends on USER_NS" ?

kernel/user_namespace_kunit.c line 90

Quoted 4 lines
kernel/user_namespace_kunit.c new file +4 -0
+static struct kunit_suite user_ns_map_test_suite = {
+     .name = "user_ns_map",
+     .test_cases = user_ns_map_test_cases,
+};

"Roger that" for all of the above. I was waiting for more comments before proceeding, but I think I'll send out the "visibility.h" version, with the above fixes, so that we have something concrete to discuss.

-bw

#16 lore raw

PATCH v5 1/2 userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

However, doing so has runtime implications. In the original implementation of insert_extent(), elements are written to map->forward[map->nr_extents] before map->nr_extents is incremented:

	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents];
	else
		dest = &map->forward[map->nr_extents];

	*dest = *extent;
	map->nr_extents++;

At the time of writing to 'map->forward[map->nr_extents]', map->nr_extents is still 5, but we are accessing index 5 (which is the 6th element). Under __counted_by_ptr(nr_extents), the compiler and KASAN expect the accessed index to be strictly less than map->nr_extents. Therefore, accessing index 5 when the count is 5 triggers an out-of-bounds panic/trap at runtime.

To resolve this, insert_extent() is refactored to increment map->nr_extents first, and then use map->nr_extents - 1 as the index:

	map->nr_extents++;
	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
		dest = &map->extent[map->nr_extents - 1];
	else
		dest = &map->forward[map->nr_extents - 1];

	*dest = *extent;

---
v4 - Correct the "Assisted-by" tag.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 include/linux/user_namespace.h |  4 ++--
 kernel/user_namespace.c        | 12 ++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)
include/linux/user_namespace.h +2 -2
index e38d9e60569f..2962256eddf7 100644
@@ -29,8 +29,8 @@ struct uid_gid_map { /* 64 bytes -- 1 cache line */
 			u32 nr_extents;
 		};
 		struct {
-			struct uid_gid_extent *forward;
-			struct uid_gid_extent *reverse;
+			struct uid_gid_extent *forward __counted_by_ptr(nr_extents);
+			struct uid_gid_extent *reverse __counted_by_ptr(nr_extents);
 		};
 	};
 };
kernel/user_namespace.c +8 -4
index 0bed462e9b2a..786dbf0506ca 100644
@@ -809,13 +809,17 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 		map->reverse = NULL;
 	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	/*
+	 * nr_extents must be updated before the extent and forward arrays are
+	 * accessed, otherwise KSAN will assert an out-of-bounds error.
+	 */
+	map->nr_extents++;
+	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
+		dest = &map->extent[map->nr_extents - 1];
 	else
-		dest = &map->forward[map->nr_extents];
+		dest = &map->forward[map->nr_extents - 1];
 
 	*dest = *extent;
-	map->nr_extents++;
 	return 0;
 }
 
Signature · 1 line
2.55.0.979.g7e5102b832-goog
#17 lore raw

PATCH v5 2/2 userns: Add KUnit test suite for uid_gid_map

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:3.1-pro-preview

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
v4 - Actually test on unsorted data. Corrected the "Assisted-by" tag. v5 - Convert the KUnit test to a format that follows the KUnit style
guild more closely. This includes some renameing and using macros in the "visibility.h" header. We no longer #include the test file into the implementation file. This should be much cleaner.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 include/linux/user_namespace.h   |  6 ++
 init/Kconfig                     | 11 ++++
 kernel/Makefile                  |  1 +
 kernel/tests/.kunitconfig        |  4 ++
 kernel/tests/user_ns_map_kunit.c | 98 ++++++++++++++++++++++++++++++++
 kernel/user_namespace.c          |  8 ++-
 6 files changed, 126 insertions(+), 2 deletions(-)
 create mode 100644 kernel/tests/.kunitconfig
 create mode 100644 kernel/tests/user_ns_map_kunit.c
include/linux/user_namespace.h +6 -0
index 2962256eddf7..633157781edc 100644
@@ -207,6 +207,12 @@ extern bool in_userns(const struct user_namespace *ancestor,
 		       const struct user_namespace *child);
 extern bool current_in_userns(const struct user_namespace *target_ns);
 struct ns_common *ns_get_owner(struct ns_common *ns);
+
+#if IS_ENABLED(CONFIG_USER_NS_MAP_KUNIT_TEST)
+extern int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent);
+extern int sort_idmaps(struct uid_gid_map *map);
+#endif /* CONFIG_USER_NS_MAP_KUNIT_TEST */
+
 #else
 
 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
init/Kconfig +11 -0
index 8583d9f06c52..27c1ffc675bf 100644
@@ -1457,6 +1457,17 @@ config USER_NS
 
 	  If unsure, say N.
 
+config USER_NS_MAP_KUNIT_TEST
+	tristate "KUint 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
kernel/Makefile +1 -0
index 1e1a31673577..2a64282749b8 100644
@@ -141,6 +141,7 @@ obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o
 
 obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o
 obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o
+obj-$(CONFIG_USER_NS_MAP_KUNIT_TEST) += tests/user_ns_map_kunit.o
 
 CFLAGS_kstack_erase.o += $(DISABLE_KSTACK_ERASE)
 CFLAGS_kstack_erase.o += $(call cc-option,-mgeneral-regs-only)
kernel/tests/.kunitconfig new file +4 -0
new file mode 100644
index 000000000000..b3d1206fd81a
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_NAMESPACES=y
+CONFIG_USER_NS=y
+CONFIG_USER_NS_MAP_KUNIT_TEST=y
kernel/tests/user_ns_map_kunit.c new file +98 -0

98 lines not on this page · raw message

kernel/user_namespace.c +6 -2
index 786dbf0506ca..d8cbefd36598 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <kunit/visibility.h>
 #include <linux/export.h>
 #include <linux/nsproxy.h>
 #include <linux/slab.h>
@@ -786,7 +787,8 @@ static bool mappings_overlap(struct uid_gid_map *new_map,
  * Takes care to allocate a 4K block of memory if the number of mappings exceeds
  * UID_GID_MAP_MAX_BASE_EXTENTS.
  */
-static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
+VISIBLE_IF_KUNIT int insert_extent(struct uid_gid_map *map,
+				   struct uid_gid_extent *extent)
 {
 	struct uid_gid_extent *dest;
 
@@ -822,6 +824,7 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 	*dest = *extent;
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(insert_extent);
 
 /* cmp function to sort() forward mappings */
 static int cmp_extents_forward(const void *a, const void *b)
@@ -857,7 +860,7 @@ static int cmp_extents_reverse(const void *a, const void *b)
  * sort_idmaps - Sorts an array of idmap entries.
  * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
  */
-static int sort_idmaps(struct uid_gid_map *map)
+VISIBLE_IF_KUNIT int sort_idmaps(struct uid_gid_map *map)
 {
 	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
 		return 0;
@@ -878,6 +881,7 @@ static int sort_idmaps(struct uid_gid_map *map)
 
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(sort_idmaps);
 
 /**
  * verify_root_map() - check the uid 0 mapping
Signature · 1 line
2.55.0.979.g7e5102b832-goog
Reviewed-by on v5 1/2
#20 lore raw

Re: [PATCH v5 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling
Quoted 4 lines · Bill Wendling

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'.

Since I am CC'ed again... (thanks btw)

Personally, I am not a huge fan of __counted_by_ptr, at least in this particular case. But 1/2 looks technically correct, I think it doesn't hurt ;)

So, FWIW

kernel/user_namespace.c static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent) · line 816

Quoted 13 lines
kernel/user_namespace.c +5 -2
@@ -809,13 +809,17 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 		map->reverse = NULL;
 	}
 
-	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
-		dest = &map->extent[map->nr_extents];
+	/*
+	 * nr_extents must be updated before the extent and forward arrays are
+	 * accessed, otherwise KSAN will assert an out-of-bounds error.
+	 */
+	map->nr_extents++;

Yes, but perhaps the comment should be more explicit... I mean, something like

... otherwise KSAN ... because of __counted_by_ptr(nr_extents)

Nevermind, this is just a minor/subjective nit.

Oleg.

#21 lore raw

Re: [PATCH v5 1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

In reply to Bill Wendling
Quoted 6 lines · Bill Wendling

The compiler attribute __counted_by_ptr associates a pointer field of a struct with a sibling field within the same struct that specifies the element count of the allocated memory. This enables KASAN and fortified bounds-checking to detect out-of-bounds accesses to the pointer field at runtime.

Quoted 8 lines

We can add the __counted_by_ptr attribute to the 'forward' and 'reverse' pointer fields of 'struct uid_gid_map', which are counted by 'nr_extents'. Since 'nr_extents' is defined in a sibling anonymous struct inside an anonymous union, the nearest common non-anonymous struct level is 'struct uid_gid_map' itself, which is supported by the compiler.

[...]

Applied to the namespace-7.4.misc branch of the vfs/vfs.git tree. Patches in the namespace-7.4.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: namespace-7.4.misc

[1/2] userns: Add __counted_by_ptr attribute to struct uid_gid_map

      https://git.kernel.org/vfs/vfs/c/b31397fe3007
[2/2] userns: Add KUnit test suite for uid_gid_map
      https://git.kernel.org/vfs/vfs/c/f6f1a2496ba5
#18 lore raw

Re: [PATCH v5 2/2] userns: Add KUnit test suite for uid_gid_map

In reply to Bill Wendling
Quoted 77 lines · Bill Wendling

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:3.1-pro-preview

---
v2 - Remove Gerrit tag.
v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/
- Fixed Kconfig tests.
v4 - Actually test on unsorted data. Corrected the "Assisted-by" tag. v5 - Convert the KUnit test to a format that follows the KUnit style
guild more closely. This includes some renameing and using macros in the "visibility.h" header. We no longer #include the test file into the implementation file. This should be much cleaner.
---
Cc: Bradley Morgan <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Aleksa Sarai <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: "Thomas Weißschuh" <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Douglas Anderson <[email protected]>
Cc: Anand Moon <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---

include/linux/user_namespace.h   |  6 ++
init/Kconfig                     | 11 ++++
kernel/Makefile                  |  1 +
kernel/tests/.kunitconfig        |  4 ++
kernel/tests/user_ns_map_kunit.c | 98 ++++++++++++++++++++++++++++++++
kernel/user_namespace.c          |  8 ++-
6 files changed, 126 insertions(+), 2 deletions(-)

create mode 100644 kernel/tests/.kunitconfig create mode 100644 kernel/tests/user_ns_map_kunit.c

include/linux/user_namespace.h +6 -0
index 2962256eddf7..633157781edc 100644
@@ -207,6 +207,12 @@ extern bool in_userns(const struct user_namespace *ancestor,
 		       const struct user_namespace *child);
 extern bool current_in_userns(const struct user_namespace *target_ns);
 struct ns_common *ns_get_owner(struct ns_common *ns);
+
+#if IS_ENABLED(CONFIG_USER_NS_MAP_KUNIT_TEST)
+extern int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent);
+extern int sort_idmaps(struct uid_gid_map *map);
+#endif /* CONFIG_USER_NS_MAP_KUNIT_TEST */
+
 #else
 

static inline struct user_namespace *get_user_ns(struct user_namespace *ns)

init/Kconfig +0 -0
index 8583d9f06c52..27c1ffc675bf 100644

init/Kconfig config USER_NS · line 1461

Quoted 5 lines
init/Kconfig +2 -0
@@ -1457,6 +1457,17 @@ config USER_NS
 
 	  If unsure, say N.
 
+config USER_NS_MAP_KUNIT_TEST
+	tristate "KUint test for user

Hmm! s/KUint/KUnit

namespace map insertion" if !KUNIT_ALL_TESTS

Quoted 178 lines
init/Kconfig +9 -0
+	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

kernel/Makefile +0 -0
index 1e1a31673577..2a64282749b8 100644
@@ -141,6 +141,7 @@ obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o
 

obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o +obj-$(CONFIG_USER_NS_MAP_KUNIT_TEST) += tests/user_ns_map_kunit.o

CFLAGS_kstack_erase.o += $(DISABLE_KSTACK_ERASE) CFLAGS_kstack_erase.o += $(call cc-option,-mgeneral-regs-only)

kernel/tests/.kunitconfig new file +4 -0
new file mode 100644
index 000000000000..b3d1206fd81a
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_NAMESPACES=y
+CONFIG_USER_NS=y
+CONFIG_USER_NS_MAP_KUNIT_TEST=y
kernel/tests/user_ns_map_kunit.c new file +98 -0
new file mode 100644
index 000000000000..24c21e43a36c
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for user namespace map insertion and sorting.
+ */
+
+#define pr_fmt(fmt) "user_namespace: " fmt
+
+#include <kunit/test.h>
+#include <linux/user_namespace.h>
+
+#define NR_EXTENTS (UID_GID_MAP_MAX_BASE_EXTENTS + 5)
+
+static void user_ns_map_insert(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 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_ASSERT_EQ(test, ret, 0);
+	}
+
+	KUNIT_EXPECT_EQ(test, map.nr_extents, UID_GID_MAP_MAX_BASE_EXTENTS);
+
+	/* Verify the elements ended up in the 'extent' array */
+	for (i = 0; i < UID_GID_MAP_MAX_BASE_EXTENTS; i++) {
+		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 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 elements */
+	for (i = 0; i < NR_EXTENTS; i++) {
+		int value = 9 - i;
+
+		extent.first = value * 10;
+		extent.lower_first = value * 100;
+		extent.count = 5;
+
+		ret = insert_extent(&map, &extent);
+		KUNIT_ASSERT_EQ(test, ret, 0);
+	}
+
+	KUNIT_EXPECT_EQ(test, map.nr_extents, NR_EXTENTS);
+
+	/* Now sort the map to set up reverse mapping */
+	ret = sort_idmaps(&map);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, map.reverse);
+
+	/* Verify the elements are in 'forward' and that sorting is correct */
+	for (i = 0; i < map.nr_extents; i++) {
+		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);
+
+		KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+		KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+		KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+	}
+
+	kfree(map.forward);
+	kfree(map.reverse);
+}
+
+static struct kunit_case user_ns_map_test_cases[] = {
+	KUNIT_CASE(user_ns_map_insert),
+	KUNIT_CASE(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);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("KUnit test for user namespace map insertion");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
kernel/user_namespace.c +5 -2
index 786dbf0506ca..d8cbefd36598 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <kunit/visibility.h>
 #include <linux/export.h>
 #include <linux/nsproxy.h>
 #include <linux/slab.h>
@@ -786,7 +787,8 @@ static bool mappings_overlap(struct uid_gid_map *new_map,
 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
 * UID_GID_MAP_MAX_BASE_EXTENTS.
 */
-static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
+VISIBLE_IF_KUNIT int insert_extent(struct uid_gid_map *map,
+				   struct uid_gid_extent *extent)
 {
 	struct uid_gid_extent *dest;
 
@@ -822,6 +824,7 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
 	*dest = *extent;
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(insert_extent);
 
 /* cmp function to sort() forward mappings */
 static int cmp_extents_forward(const void *a, const void *b)
@@ -857,7 +860,7 @@ static int cmp_extents_reverse(const void *a, const void *b)
 * sort_idmaps - Sorts an array of idmap entries.
 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
 */
-static int sort_idmaps(struct uid_gid_map *map)
+VISIBLE_IF_KUNIT int sort_idmaps(struct uid_gid_map *map)
 {
 	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
 		return 0;

kernel/user_namespace.c static int sort_idmaps(struct uid_gid_map *map) · line 887

Quoted 7 lines
kernel/user_namespace.c +1 -0
@@ -878,6 +881,7 @@ static int sort_idmaps(struct uid_gid_map *map)
 
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(sort_idmaps);
 
 /**
 * verify_root_map() - check the uid 0 mapping

--- Thanks!
https://lore.kernel.org/all/[email protected]/

#19 lore raw

Re: [PATCH v5 2/2] userns: Add KUnit test suite for uid_gid_map

Quoted 7 lines · Bradley Morgan
Quoted 6 lines · Bill Wendling
init/Kconfig +0 -0
index 8583d9f06c52..27c1ffc675bf 100644
@@ -1457,6 +1457,17 @@ config USER_NS
 
Quoted 7 lines

init/Kconfig config USER_NS

Quoted 4 lines
init/Kconfig +2 -0
        If unsure, say N.
 
+config USER_NS_MAP_KUNIT_TEST
+      tristate "KUint test for user

Hmm! s/KUint/KUnit

*headdesk*

-bw

Quoted 189 lines

namespace map insertion" if !KUNIT_ALL_TESTS

Quoted 178 lines
init/Kconfig +9 -0
+      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

kernel/Makefile +0 -0
index 1e1a31673577..2a64282749b8 100644
@@ -141,6 +141,7 @@ obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o
 

obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o +obj-$(CONFIG_USER_NS_MAP_KUNIT_TEST) += tests/user_ns_map_kunit.o

CFLAGS_kstack_erase.o += $(DISABLE_KSTACK_ERASE) CFLAGS_kstack_erase.o += $(call cc-option,-mgeneral-regs-only)

kernel/tests/.kunitconfig new file +4 -0
new file mode 100644
index 000000000000..b3d1206fd81a
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_NAMESPACES=y
+CONFIG_USER_NS=y
+CONFIG_USER_NS_MAP_KUNIT_TEST=y
kernel/tests/user_ns_map_kunit.c new file +98 -0
new file mode 100644
index 000000000000..24c21e43a36c
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for user namespace map insertion and sorting.
+ */
+
+#define pr_fmt(fmt) "user_namespace: " fmt
+
+#include <kunit/test.h>
+#include <linux/user_namespace.h>
+
+#define NR_EXTENTS (UID_GID_MAP_MAX_BASE_EXTENTS + 5)
+
+static void user_ns_map_insert(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 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_ASSERT_EQ(test, ret, 0);
+      }
+
+      KUNIT_EXPECT_EQ(test, map.nr_extents, UID_GID_MAP_MAX_BASE_EXTENTS);
+
+      /* Verify the elements ended up in the 'extent' array */
+      for (i = 0; i < UID_GID_MAP_MAX_BASE_EXTENTS; i++) {
+              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 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 elements */
+      for (i = 0; i < NR_EXTENTS; i++) {
+              int value = 9 - i;
+
+              extent.first = value * 10;
+              extent.lower_first = value * 100;
+              extent.count = 5;
+
+              ret = insert_extent(&map, &extent);
+              KUNIT_ASSERT_EQ(test, ret, 0);
+      }
+
+      KUNIT_EXPECT_EQ(test, map.nr_extents, NR_EXTENTS);
+
+      /* Now sort the map to set up reverse mapping */
+      ret = sort_idmaps(&map);
+      KUNIT_ASSERT_EQ(test, ret, 0);
+      KUNIT_ASSERT_NOT_ERR_OR_NULL(test, map.reverse);
+
+      /* Verify the elements are in 'forward' and that sorting is correct */
+      for (i = 0; i < map.nr_extents; i++) {
+              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);
+
+              KUNIT_EXPECT_EQ(test, map.reverse[i].first, i * 10);
+              KUNIT_EXPECT_EQ(test, map.reverse[i].lower_first, i * 100);
+              KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5);
+      }
+
+      kfree(map.forward);
+      kfree(map.reverse);
+}
+
+static struct kunit_case user_ns_map_test_cases[] = {
+      KUNIT_CASE(user_ns_map_insert),
+      KUNIT_CASE(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);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("KUnit test for user namespace map insertion");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
kernel/user_namespace.c +5 -2
index 786dbf0506ca..d8cbefd36598 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <kunit/visibility.h>
 #include <linux/export.h>
 #include <linux/nsproxy.h>
 #include <linux/slab.h>
@@ -786,7 +787,8 @@ static bool mappings_overlap(struct uid_gid_map *new_map,
 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
 * UID_GID_MAP_MAX_BASE_EXTENTS.
 */
-static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
+VISIBLE_IF_KUNIT int insert_extent(struct uid_gid_map *map,
+                                 struct uid_gid_extent *extent)
 {
      struct uid_gid_extent *dest;
 
@@ -822,6 +824,7 @@ static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
      *dest = *extent;
      return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(insert_extent);
 
 /* cmp function to sort() forward mappings */
 static int cmp_extents_forward(const void *a, const void *b)
@@ -857,7 +860,7 @@ static int cmp_extents_reverse(const void *a, const void *b)
 * sort_idmaps - Sorts an array of idmap entries.
 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
 */
-static int sort_idmaps(struct uid_gid_map *map)
+VISIBLE_IF_KUNIT int sort_idmaps(struct uid_gid_map *map)
 {
      if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
              return 0;

kernel/user_namespace.c static int sort_idmaps(struct uid_gid_map *map) · line 887

Quoted 7 lines
kernel/user_namespace.c +1 -0
@@ -878,6 +881,7 @@ static int sort_idmaps(struct uid_gid_map *map)
 
      return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(sort_idmaps);
 
 /**
 * verify_root_map() - check the uid 0 mapping

--- Thanks!
https://lore.kernel.org/all/[email protected]/