From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F23F33C4544 for ; Sun, 23 Aug 2026 12:53:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787489637; cv=none; b=KVBWGF2Ik/5J6wxtbw85JxM3MPoUGfwfJk+1Yxen50eQkkRhEw8cZKjuK9NkzOor0wExm/wLL7o+YDJiC9JeWzLtzZr7AjSzsNWsMML3O3RJsDWQC9ij+gYq+Z6m11ewwq3QglkEeKfZjzPIkCFAKIgPw24Lgu4KELZzWxV+5hY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787489637; c=relaxed/simple; bh=pfDE5ilD9/MtOD5boSCr08GTdZALEQjop9G7ZZhYAOo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: Cc:Content-Type; b=L7sj9JeZ596pH8SBA1auSY6atvtr/bSU8W3dhuVCUAx27N7J5pUJHvnh4V0ykZAxWHABAhl0Pip1Ol+ot21pLQryAn71ONB54UZ1PRZ5ZbO/XVGs/RXzz+kx0VAq51HEBZOLzxZtbzyJZun0Jw1s3lFxIuKuQqu9WSpPMpUaXWE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--morbo.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vv3UMmuU; arc=none smtp.client-ip=209.85.214.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--morbo.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vv3UMmuU" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2cf7dd9fd91so26451625ad.1 for ; Sun, 23 Aug 2026 05:53:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787489635; x=1788094435; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:from:subject:message-id :references:mime-version:in-reply-to:date:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2j+jEL9TnE7Z2fsNxTUFdS1e1dXzn2rwQVASjQh/j0E=; b=vv3UMmuUprsAZJFH9ENRpdUM/mkwoeOjTY0i6gTGz8BOnplqaDwnY9l99Hhv/NwR+n Bl0J71+NtlyKTiq19H4O98KfrcF/XnOPiwm9ZvtHZrpROOLMzKQgj5d+IooAO15YcGb1 PexWEp1u6edE4WG1IBaFHWegCZZLVW+iJChay9wTJ6V+c/wQrWh3KmCNobuqwxhfvqhL +j5H7Ph7Jl7xbF8vbRMDopcpD/odWOvRG6oQIXmwo32HG90BFTCJJi1Iz2l+ZRqadsiI P5AbPaNGsZDoBzvKnocGtiQpAx7mAH9yBdV5uF368gqMIEA9/bdjK2tsdnwUEX5XozEr ulJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787489635; x=1788094435; h=content-transfer-encoding:content-type:cc:from:subject:message-id :references:mime-version:in-reply-to:date:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=2j+jEL9TnE7Z2fsNxTUFdS1e1dXzn2rwQVASjQh/j0E=; b=PMXkS1JM58CpKM3w4DP/qGGWScK/TAJZ+siPsggrCPiek8ASkFIiogCwWR+CB/9tfP +ox8ZFoB7/qX+3+iKTEyHa9gTvWk8jmfmS9ipAno3n/FARNdfpbdf/N7CsWi5svQH93A waZX+E6IR1CwdMXvVvjSneS8tLOQchG0V3zKr+ghBUMlN6dTf1wnK6mLiYKPIo315j3+ eU6k1qYc5jjke2rk7qEwnjx6PWtnwJkT/fuxNmHVP68VhV6DCoFNWf0f6sFePQUuqx7P OWBrFYZIYsZKwxxJvAF2joogJpgMHMdM/F8SM/09n6u+8zxClfvpiYlE6QQaY2SEnD8j 14Pw== X-Forwarded-Encrypted: i=1; AHgh+RolumllnODhQm349Be5LIqCt+5v3PfJ+idP236jdoimZF5RX/ssByqQUxXqyuPLhKExqU7ktXrvE3sqOak=@vger.kernel.org X-Gm-Message-State: AFuF++mnSl/s0c0drNzlFhsPitAo46G1lGJg3sZLX86/AXMW+tW0qHMv zf6tv3J+SsrYy3GvfcTxwV7wcjlzkoa+QxQmE2d+mLhp7MiLjBj+hKXnG1b8pzMKcS/DOKejU4F u X-Received: from plk19.prod.google.com ([2002:a17:902:e9d3:b0:2d1:1cf0:2039]) (user=morbo job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ec87:b0:2cf:9347:f445 with SMTP id d9443c01a7336-2d64b024c86mr319174525ad.10.1787489635107; Sun, 23 Aug 2026 05:53:55 -0700 (PDT) Date: Sun, 23 Aug 2026 12:53:44 +0000 In-Reply-To: <20260823125155.1136740-3-morbo@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260823125155.1136740-3-morbo@google.com> X-Mailer: git-send-email 2.55.0.860.g4b6b3295ed-goog Message-ID: <20260823125348.1138649-1-morbo@google.com> Subject: [PATCH 2/2] userns: Add KUnit test suite for uid_gid_map From: Bill Wendling Cc: Bill Wendling , Kees Cook , "Gustavo A. R. Silva" , Christian Brauner , Aleksa Sarai , Jan Kara , Nathan Chancellor , Miguel Ojeda , Thomas Gleixner , Nicolas Schier , Gary Guo , "=?UTF-8?q?Thomas=20Wei=C3=9Fschuh?=" , Alice Ryhl , Douglas Anderson , Anand Moon , Oleg Nesterov , codemender-patching+linux@google.com, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 (<=3D 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 Signed-off-by: Bill Wendling --- v2 - Remove Gerrit tag. --- Cc: Kees Cook Cc: "Gustavo A. R. Silva" Cc: Christian Brauner Cc: Aleksa Sarai Cc: Jan Kara Cc: Nathan Chancellor Cc: Miguel Ojeda Cc: Thomas Gleixner Cc: Nicolas Schier Cc: Gary Guo Cc: "Thomas Wei=C3=9Fschuh" Cc: Alice Ryhl Cc: Douglas Anderson Cc: Anand Moon Cc: Oleg Nesterov Cc: codemender-patching+linux@google.com Cc: linux-kernel@vger.kernel.org Cc: linux-hardening@vger.kernel.org --- init/Kconfig | 10 ++++ kernel/.kunitconfig | 3 ++ kernel/user_namespace.c | 4 ++ kernel/user_namespace_kunit.c | 87 +++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 kernel/.kunitconfig create mode 100644 kernel/user_namespace_kunit.c diff --git a/init/Kconfig b/init/Kconfig index f63bf5e05e79..ba6a40b7315a 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1457,6 +1457,16 @@ config USER_NS =20 If unsure, say N. =20 +config USER_NAMESPACE_KUNIT_TEST + tristate "KUnit test for user namespace map insertion" if !KUNIT_ALL_TEST= S + depends on USER_NS && KUNIT + default KUNIT_ALL_TESTS + help + This builds the KUnit test for user namespace uid/gid map insertion. + It validates map insertion, limits, dynamic allocation of the + extended extents array, and mapping sorting functions. + If unsure, say N. + config PID_NS bool "PID Namespaces" default y diff --git a/kernel/.kunitconfig b/kernel/.kunitconfig new file mode 100644 index 000000000000..7314dce05dc2 --- /dev/null +++ b/kernel/.kunitconfig @@ -0,0 +1,3 @@ +CONFIG_KUNIT=3Dy +CONFIG_USER_NS=3Dy +CONFIG_USER_NAMESPACE_KUNIT_TEST=3Dy diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 7e5371d8f515..64c64e1028e8 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1413,3 +1413,7 @@ static __init int user_namespaces_init(void) return 0; } subsys_initcall(user_namespaces_init); + +#if IS_ENABLED(CONFIG_USER_NAMESPACE_KUNIT_TEST) +#include "user_namespace_kunit.c" +#endif diff --git a/kernel/user_namespace_kunit.c b/kernel/user_namespace_kunit.c new file mode 100644 index 000000000000..6d7662ef1916 --- /dev/null +++ b/kernel/user_namespace_kunit.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for user namespace map insertion and sorting. + */ + +#include +#include + +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 =3D 0; i < UID_GID_MAP_MAX_BASE_EXTENTS; i++) { + extent.first =3D i * 10; + extent.lower_first =3D i * 100; + extent.count =3D 5; + + ret =3D insert_extent(&map, &extent); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_EQ(test, map.nr_extents, i + 1); + KUNIT_EXPECT_EQ(test, map.extent[i].first, i * 10); + KUNIT_EXPECT_EQ(test, map.extent[i].lower_first, i * 100); + KUNIT_EXPECT_EQ(test, map.extent[i].count, 5); + } +} + +static void test_user_ns_map_insert_extended(struct kunit *test) +{ + struct uid_gid_map map; + struct uid_gid_extent extent; + int i, ret; + + memset(&map, 0, sizeof(map)); + + /* Insert more than UID_GID_MAP_MAX_BASE_EXTENTS (e.g., 10) elements */ + for (i =3D 0; i < 10; i++) { + extent.first =3D i * 10; + extent.lower_first =3D i * 100; + extent.count =3D 5; + + ret =3D insert_extent(&map, &extent); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_EQ(test, map.nr_extents, i + 1); + + if (i < UID_GID_MAP_MAX_BASE_EXTENTS) { + KUNIT_EXPECT_EQ(test, map.extent[i].first, i * 10); + } else { + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, map.forward); + KUNIT_EXPECT_EQ(test, map.forward[i].first, i * 10); + KUNIT_EXPECT_EQ(test, map.forward[i].lower_first, i * 100); + KUNIT_EXPECT_EQ(test, map.forward[i].count, 5); + } + } + + /* Now sort the map to set up reverse mapping */ + ret =3D sort_idmaps(&map); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, map.reverse); + + /* Verify sorting is correct */ + for (i =3D 0; i < map.nr_extents; i++) { + KUNIT_EXPECT_EQ(test, map.forward[i].count, 5); + KUNIT_EXPECT_EQ(test, map.reverse[i].count, 5); + } + + /* Clean up allocations to avoid leaks */ + kfree(map.forward); + kfree(map.reverse); +} + +static struct kunit_case user_ns_map_test_cases[] =3D { + KUNIT_CASE(test_user_ns_map_insert_base), + KUNIT_CASE(test_user_ns_map_insert_extended), + {} +}; + +static struct kunit_suite user_ns_map_test_suite =3D { + .name =3D "user_ns_map", + .test_cases =3D user_ns_map_test_cases, +}; + +kunit_test_suite(user_ns_map_test_suite); --=20 2.55.0.860.g4b6b3295ed-goog