Skip to contentSkip to conversation

userns: reject out-of-range uid_map/gid_map values

PATCH Applied Natasha Klaus 3 participants 5 messages Sep 30 – Oct 12 Kselftest

lore
Share & tools
lore
#0 lore raw

v2 cover userns: reject out-of-range uid_map/gid_map values

map_write() silently truncates uid_map/gid_map/projid_map values above U32_MAX modulo 2^32 on 64-bit kernels, so e.g. "4294967301 1000 1" is installed as "5 1000 1" without an error. Patch 1 parses each field with kstrtou32() so such values fail with -EINVAL. Patch 2 adds selftests for out-of-range and negative values in each field and for the unchanged in-range path.

This makes the parser more correct; it is not a security fix.

The patch and the selftest were drafted by Claude Code (Claude Opus 5.5) in an interactive session; I reviewed, built and tested them. The assistant flagged the truncation while reading map_write(), and I confirmed it by writing to uid_map on a running kernel.

Tested on v7.3-rc5-37-g551c722f4080 under virtme-ng, x86_64 defconfig with CONFIG_USER_NS: all 12 selftest cases pass on the patched kernel. On an unpatched 6.8 host the four above-u32 cases fail as expected, seven pass and the root-only case is skipped.

Changes in v2:
- Parse each field with kstrtou32() instead of simple_strtoull() plus a separate U32_MAX check (Eric W. Biederman).
- Selftest: add negative values in each field, "0 <uid> 1", the largest valid first id, a line with extra spaces between fields, and the largest count written by a privileged parent (Eric W. Biederman).
- Describe patch 1 as making the parser more correct, not as hardening.
- Rebased onto v7.3-rc5-37-g551c722f4080.

Natasha Klaus (2):
userns: reject uid_map/gid_map values that do not fit in u32 selftests/namespaces: add uid_map/gid_map range tests

 kernel/user_namespace.c                       |  29 +-
 tools/testing/selftests/namespaces/.gitignore |   1 +
 tools/testing/selftests/namespaces/Makefile   |   3 +-
 .../selftests/namespaces/uid_map_range_test.c | 299 ++++++++++++++++++
 4 files changed, 328 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/namespaces/uid_map_range_test.c

base-commit: 551c722f40809618230001baccf219193e22fc5a

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 userns: reject uid_map/gid_map values that do not fit in u32

map_write() parses each line written to uid_map, gid_map or projid_map with simple_strtoul() and stores the results in the u32 fields of struct uid_gid_extent. On 64-bit kernels a value above U32_MAX is silently truncated modulo 2^32. The coding assistant flagged this truncation site while reading map_write(), and I confirmed it by hand on a running kernel, where writing to the uid_map of a fresh user namespace installs a different mapping than the one requested:

  "4294967296 1000 1"  ->  installed as "0 1000 1"
  "4294967297 1000 1"  ->  installed as "1 1000 1"
  "4294967301 1000 1"  ->  installed as "5 1000 1"

The same happens to the lower_first and count fields. Only a literal of 2^64 or more is rejected, and only because simple_strtoul() saturates to ULLONG_MAX, which truncates onto the (u32)-1 value map_write() already refuses. On 32-bit kernels unsigned long is itself 32 bits wide, so the same truncation happens inside simple_strtoul().

The write succeeds, so a runtime that computes ranges and gets one wrong ends up with a different map than it asked for and no error to tell it so.

Parse each field with kstrtou32() instead. A new helper, map_parse_u32(), terminates the field at the next whitespace or NUL, parses it and puts the original character back, leaving pos where simple_strtoul() used to leave it. The checks after each field, the trailing junk check and every later check are unchanged and run in the same order. A field that is not a decimal number fitting in u32 now fails the write with -EINVAL, like any other malformed line.

Besides the range check, the one visible difference is that kstrtou32() accepts a single leading '+', so "+5 1000 1" is now installed as "5 1000 1" where it used to be rejected.

This makes the parser more correct; it is not a security fix: the truncated values are valid ids and every later check still applies to them.

---
kbuf is map_write()'s private copy of the user buffer from memdup_user_nul(): it is NUL-terminated, nothing else refers to it, and map_write() already overwrites each '\n' in it with '\0' and frees it on return. So map_parse_u32() can write a '\0' after each field for kstrtou32() and put the original character back.

 kernel/user_namespace.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
kernel/user_namespace.c +26 -3
index 0bed462e9b2a..1e232f78cfd6 100644
@@ -929,6 +929,26 @@ static bool verify_root_map(const struct file *file,
 	return true;
 }
 
+/*
+ * Parse the decimal u32 at *pos, up to the next whitespace or NUL, and
+ * leave *pos pointing at that character.
+ */
+static int map_parse_u32(char **pos, u32 *res)
+{
+	char *end = *pos;
+	char c;
+	int ret;
+
+	while (*end && !isspace(*end))
+		end++;
+	c = *end;
+	*end = '\0';
+	ret = kstrtou32(*pos, 10, res);
+	*end = c;
+	*pos = end;
+	return ret;
+}
+
 static ssize_t map_write(struct file *file, const char __user *buf,
 			 size_t count, loff_t *ppos,
 			 int cap_setid,
@@ -1001,17 +1021,20 @@ static ssize_t map_write(struct file *file, const char __user *buf,
 		}
 
 		pos = skip_spaces(pos);
-		extent.first = simple_strtoul(pos, &pos, 10);
+		if (map_parse_u32(&pos, &extent.first))
+			goto out;
 		if (!isspace(*pos))
 			goto out;
 
 		pos = skip_spaces(pos);
-		extent.lower_first = simple_strtoul(pos, &pos, 10);
+		if (map_parse_u32(&pos, &extent.lower_first))
+			goto out;
 		if (!isspace(*pos))
 			goto out;
 
 		pos = skip_spaces(pos);
-		extent.count = simple_strtoul(pos, &pos, 10);
+		if (map_parse_u32(&pos, &extent.count))
+			goto out;
 		if (*pos && !isspace(*pos))
 			goto out;
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/2 selftests/namespaces: add uid_map/gid_map range tests

In reply to Natasha Klaus

Check that a uid_map or gid_map line with a value that does not fit in u32, or with a negative value, is rejected with EINVAL and installs nothing, for each of the three fields.

Check that in-range lines are still accepted and read back unchanged:
a plain single-id mapping, first id 0, the largest valid first id 4294967294, and a line with extra spaces between the fields. When run as root in the initial user namespace, also check that the largest count, "0 0 4294967295", is accepted when written by the parent into the uid_map of a child namespace; an unprivileged writer can only map a single id.

Assisted-by LLM
---
 tools/testing/selftests/namespaces/.gitignore |   1 +
 tools/testing/selftests/namespaces/Makefile   |   3 +-
 .../selftests/namespaces/uid_map_range_test.c | 299 ++++++++++++++++++
 3 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/namespaces/uid_map_range_test.c
tools/testing/selftests/namespaces/.gitignore +1 -0
index 0989e80da457..995b082357fa 100644
@@ -10,3 +10,4 @@ cred_change_test
 stress_test
 listns_pagination_bug
 regression_pidfd_setns_test
+uid_map_range_test
tools/testing/selftests/namespaces/Makefile +2 -1
index fbb821652c17..9b22d12708e1 100644
@@ -13,7 +13,8 @@ TEST_GEN_PROGS := nsid_test \
 		  cred_change_test \
 		  stress_test \
 		  listns_pagination_bug \
-		  regression_pidfd_setns_test
+		  regression_pidfd_setns_test \
+		  uid_map_range_test
 
 include ../lib.mk
 
tools/testing/selftests/namespaces/uid_map_range_test.c new file +299 -0

299 lines not on this page · raw message

Signature · 1 line
2.34.1
Acked-by on the v2 series
#3 lore raw
In reply to Natasha Klaus
Quoted 14 lines · Natasha Klaus

map_write() silently truncates uid_map/gid_map/projid_map values above U32_MAX modulo 2^32 on 64-bit kernels, so e.g. "4294967301 1000 1" is installed as "5 1000 1" without an error. Patch 1 parses each field with kstrtou32() so such values fail with -EINVAL. Patch 2 adds selftests for out-of-range and negative values in each field and for the unchanged in-range path.

This makes the parser more correct; it is not a security fix.

The patch and the selftest were drafted by Claude Code (Claude Opus 5.5) in an interactive session; I reviewed, built and tested them. The assistant flagged the truncation while reading map_write(), and I confirmed it by writing to uid_map on a running kernel.

Quoted 4 lines

Tested on v7.3-rc5-37-g551c722f4080 under virtme-ng, x86_64 defconfig with CONFIG_USER_NS: all 12 selftest cases pass on the patched kernel. On an unpatched 6.8 host the four above-u32 cases fail as expected, seven pass and the root-only case is skipped.

I don't see anything to object to in this one.

Quoted 25 lines

Changes in v2:
- Parse each field with kstrtou32() instead of simple_strtoull() plus a separate U32_MAX check (Eric W. Biederman).
- Selftest: add negative values in each field, "0 <uid> 1", the largest valid first id, a line with extra spaces between fields, and the largest count written by a privileged parent (Eric W. Biederman).
- Describe patch 1 as making the parser more correct, not as hardening.
- Rebased onto v7.3-rc5-37-g551c722f4080.

Natasha Klaus (2):
userns: reject uid_map/gid_map values that do not fit in u32 selftests/namespaces: add uid_map/gid_map range tests

 kernel/user_namespace.c                       |  29 +-
 tools/testing/selftests/namespaces/.gitignore |   1 +
 tools/testing/selftests/namespaces/Makefile   |   3 +-
 .../selftests/namespaces/uid_map_range_test.c | 299 ++++++++++++++++++
 4 files changed, 328 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/namespaces/uid_map_range_test.c

base-commit: 551c722f40809618230001baccf219193e22fc5a

#4 lore raw
In reply to Natasha Klaus
Quoted 8 lines · Natasha Klaus

map_write() silently truncates uid_map/gid_map/projid_map values above U32_MAX modulo 2^32 on 64-bit kernels, so e.g. "4294967301 1000 1" is installed as "5 1000 1" without an error. Patch 1 parses each field with kstrtou32() so such values fail with -EINVAL. Patch 2 adds selftests for out-of-range and negative values in each field and for the unchanged in-range path.

[...]

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: reject uid_map/gid_map values that do not fit in u32

      https://git.kernel.org/vfs/vfs/c/09a71b58a25d
[2/2] selftests/namespaces: add uid_map/gid_map range tests
      https://git.kernel.org/vfs/vfs/c/8b7b7739ea6a