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 ED641271456 for ; Wed, 26 Aug 2026 20:43:28 +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=1787777011; cv=none; b=J9OWAX8GNZafhHKB+yAy3P0KUs2ud0/znsOeTlp+un9psjkuUesFRBROyXRWTl6tK1p/+YoifYjShUpN5EGVdhBSITWUpmibTql4jaPejn0oWdNDK3IuoOnckBRDMI2mS/Xw6iVucwKm8E8MPdaXuqOEnVH2eUbHY6o8c4dHSIw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787777011; c=relaxed/simple; bh=+wvIFJde0eGzV7xbbKkkI48jk0bMcja36Ofn4X9f18g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: Cc:Content-Type; b=NOkZHgn6SPvLWUypH9MHXTIddvCnO9cxDsqKbkJYeWRCGOXxas/qcNyvwy10X+Pgq7aQdUSbI6y9yHEmHrOduhtMMdm2s2YH0RnvBmWXuFh3+t2HMLcH2indy8vJrxMS1X/+QE2zS9kR8jHwQyD7KX+ImIUvtotXaGcMwWvfKLc= 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=mVCjMB5/; 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="mVCjMB5/" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2d5d00fd19bso27729905ad.1 for ; Wed, 26 Aug 2026 13:43:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787777008; x=1788381808; 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=B3rNRO0DQLiTL2MF3mmKESEPiAVFJRAni21Ahkz6nEM=; b=mVCjMB5/eBwAKBmeJaTjc1lagBfcqOnvl59ZCt7ZudOWa09bgHFJuYMK82iJbh3rhM 296scy+vEjXWcYwn9c7Rat1NtakblsHcllUlEii+nQdSvf3JFE7PtENgQ6MUzsmMzCs/ c02BY3NVCTy+zrACqcz+zNKLKXM8fLFOTd6UT+i7RDyQjHfNpyqcJlytBs9wvdphVxWo vj78B2fTWhGMn/ldLJIcg+IzoVOBTglZiweWqSJzzGeXalOcf4q2RW5Y0H/jLPwXz1/D uN6Z9hAPMIu3mL0A/Ye7QFCK1T3u3/AFyRCHfL486JEN/iAPhsU2sHX8B9+28Xatelhm 6GKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787777008; x=1788381808; 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=B3rNRO0DQLiTL2MF3mmKESEPiAVFJRAni21Ahkz6nEM=; b=NEZBceKzvOkIDPOPoIqRmgmubdS5gXa+PdoENwlY8xImLhqSJj8vV0uduFV15335jh gAYXQRnkTZx769SOJ0siHZJ0RV3pYwiUJPiQZU+AqKNqCdkgQicfAg65iQwdfOyTgubf Y8TrmjVfocagTJFIs0MwkSfez8TSuiOXGXjL5koIRCKWlqnCZOmVUyshfLznlhsCocxU 6Rj8UrZsaqFmlLeVNiL+qj3mqekNQwfK4V+YYWxGJMrhfLzBOY9cjZdE/p3u3clL3bsX z5GhXOlpW/P1qR50/zlOLwqaJoz9M2F3frqTSGCx0QibD8unMoBJDiHpnIgIfDRs0+Fv 5PNA== X-Forwarded-Encrypted: i=1; AHgh+Rq+t+Ckw6Hu2UjQCqmssvlX02QpZID9nCJlM+HAVQwyNFyUbxauAYytFlCvXpYQ/cgOQlImnkTJ34a697A=@vger.kernel.org X-Gm-Message-State: AFuF++n1Dfo+FsF/tOnyzr/cpzksc4Z6el1oAzNt/AUH4jhmuEoJ38ik qJ0idc1vHkLT3IerB3Ex/Hf/5IHftF2XlffpXb86OUcy3qA5+wdX/tNcPvBshWo+vvT27bKUNvL q X-Received: from plblc14.prod.google.com ([2002:a17:902:fa8e:b0:2d5:62cd:db79]) (user=morbo job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:19ed:b0:2d7:107c:917b with SMTP id d9443c01a7336-2d7107c934fmr150202705ad.0.1787777008081; Wed, 26 Aug 2026 13:43:28 -0700 (PDT) Date: Wed, 26 Aug 2026 20:43:17 +0000 In-Reply-To: <20260823125348.1138649-1-morbo@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260823125348.1138649-1-morbo@google.com> X-Mailer: git-send-email 2.55.0.897.gb25b4bd76c-goog Message-ID: <20260826204323.3974187-1-morbo@google.com> Subject: [PATCH v3 2/2] userns: Add KUnit test suite for uid_gid_map From: Bill Wendling Cc: Bill Wendling , Bradley Morgan , "=?UTF-8?q?Thomas=20Wei=C3=9Fschuh?=" , Kees Cook , "Gustavo A. R. Silva" , Christian Brauner , Aleksa Sarai , Jan Kara , Nathan Chancellor , Miguel Ojeda , Thomas Gleixner , Nicolas Schier , Gary Guo , 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. v3 - s/KUNIT_EXPECT_NOT_ERR_OR_NULL/KUNIT_ASSERT_NOT_ERR_OR_NULL/ - Fixed Kconfig tests. --- Cc: Bradley Morgan Cc: Thomas Wei=C3=9Fschuh 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 | 91 +++++++++++++++++++++++++++++++++++ 4 files changed, 108 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 + 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 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..d97dbac65e50 --- /dev/null +++ b/kernel/user_namespace_kunit.c @@ -0,0 +1,91 @@ +// 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_ASSERT_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_ASSERT_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].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[] =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.897.gb25b4bd76c-goog