From nobody Mon Feb 9 13:33:44 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E9191E25F4; Mon, 20 Jan 2025 14:09:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737382193; cv=none; b=rSa3BlIKFg4Ca+RXy6MyH4mgHgk5I1QC5BeRIZLOqP8Q2DiJo6d/rXBVfzIPxUggGjvxs0kFsW40qOjL2MWC158yYGSSciPS2RXbhk+8TPnDE7M6f42fY3l4FtnAUd+qNnNxWXVAJSTmK3mk1r+fZU91R7WvtBR7SzVuGNCINeE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737382193; c=relaxed/simple; bh=iO54gGGAG5AFoGnuF65FTvlTPDdABM86p8xV86UBslE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n5hG2nUehklq9FLG//H4MGSGHBj++hlWkV/7T7B3ofDGGi0grNT8/AAO0Bu/+JG+XXL2PkDWhS7IvW2UFCNw+x6c+US4D61Emzy5FREfuXr0XOuFJCbsk9RYugn7/kiUXinIRGazwK8c3pNvJ+ar+5Of8KKM7PI4iwMw+jcnHdw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FPfUMLTu; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rETqDUmD; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FPfUMLTu"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rETqDUmD" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1737382190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ra6oY9qhR7iHUWWddvK05MGuCrM9P5GPJWJo5lGlMCQ=; b=FPfUMLTu4TzoqMngodlfbdtfcQ0uolfqZtKIyHGb5aNGX9XR2ik9XItHrNnElj3cTj7FSH 3kYXu1P8FUATE2B9xw/seKQI6jkbbql+oXYLqRxGZGhAB0PKqyHjcZxtoAMq4f7ozudtN2 VXoGQ9l3vpOfL9QJtYohfMmfCz3WNwch9x7p9k8DVUmSKii0Decz4xy/gX2epKY+eGV3Kh GPUrDaUQFVlUqBmX6PA4PlC+ECc5NvbVWPzBQ7Tu6Q4pUiVBF7KQnTMe2gpvYReYxDZ4O6 yAmnwj9P08kl9MzYP2Pbkuj1yeUwcJfj8LlqlTrT0/Ba64b+EQw2u0bHW6O9oQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1737382190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ra6oY9qhR7iHUWWddvK05MGuCrM9P5GPJWJo5lGlMCQ=; b=rETqDUmDZvLlzeO/KpLSvr/GSA+ZR9whoBvW5IqIpuXM3omvAZ3livm8TenQrzOdA7uUDa jxmyadfJR9FB8UAQ== Date: Mon, 20 Jan 2025 15:09:40 +0100 Subject: [PATCH 1/2] of: address: Fix empty resource handling in __of_address_resource_bounds() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250120-of-address-overflow-v1-1-dd68dbf47bce@linutronix.de> References: <20250120-of-address-overflow-v1-0-dd68dbf47bce@linutronix.de> In-Reply-To: <20250120-of-address-overflow-v1-0-dd68dbf47bce@linutronix.de> To: Rob Herring , Saravana Kannan Cc: Basharath Hussain Khaja , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , stable@vger.kernel.org X-Developer-Signature: v=1; a=ed25519-sha256; t=1737382189; l=1515; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=iO54gGGAG5AFoGnuF65FTvlTPDdABM86p8xV86UBslE=; b=ms5GmHavQ/k9IGC17cI2VD5Ki7AhyZyowb91MShGXolACbUT42f6oWYncR74vQX4/GEQFPSmN KLSJT2Fo2ztCCXUuD/r4skqqkiJD67vBt6C+KaZdRhh4AS9JgPpXCwb X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= "resource->end" needs to always be equal to "resource->start + size - 1". The previous version of the function did not perform the "- 1" in case of an empty resource. Also make sure to allow an empty resource at address 0. Reported-by: Basharath Hussain Khaja Closes: https://lore.kernel.org/lkml/20250108140414.13530-1-basharath@couth= it.com/ Fixes: 1a52a094c2f0 ("of: address: Unify resource bounds overflow checking") Cc: stable@vger.kernel.org Signed-off-by: Thomas Wei=C3=9Fschuh --- drivers/of/address.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/of/address.c b/drivers/of/address.c index 8770004d9b085f1ff40f693d695d284a6ef3dfde..5c0663066a7f3816a05077f9912= 4ca25e3c152d7 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -200,17 +200,15 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 = *range, int na, int ns, =20 static int __of_address_resource_bounds(struct resource *r, u64 start, u64= size) { - u64 end =3D start; - if (overflows_type(start, r->start)) return -EOVERFLOW; - if (size && check_add_overflow(end, size - 1, &end)) - return -EOVERFLOW; - if (overflows_type(end, r->end)) - return -EOVERFLOW; =20 r->start =3D start; - r->end =3D end; + + if (!size) + r->end =3D wrapping_sub(typeof(r->end), r->start, 1); + else if (size && check_add_overflow(r->start, size - 1, &r->end)) + return -EOVERFLOW; =20 return 0; } --=20 2.48.1 From nobody Mon Feb 9 13:33:44 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB25C1E2842; Mon, 20 Jan 2025 14:09:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737382195; cv=none; b=mwr9C+ZNHZFbfCLLZhzUKEtLYC5mDI+s6bSUdoCPsrOEBBhMOcfag3ppngwZUboWIAYwxebgnpvIcIdFwLOEEq6q/SVFKXnxRAjlc+Zo4QdfkbWzzAjAVAsS8mESISyWAHjF16bUJjd9DGazSEqUTQhONtaY8dfF7/PF1Ks3qSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737382195; c=relaxed/simple; bh=gOHcCsybXsCvfw84Ct+sSDfpztWb1jN3TKKvFCR6eT4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=m+x9f9fa974XfPE3VRPI+DVnMwS+nBMDQrBzhPADmbO7LlMMqAte6FnOGnZyhNjt4RlAnoOYHA50UvG1hRDWSjHrzqs3AuXrFp1XDg3hoIpJb9AU/uNNcFtut80r6i6lD105Az7lllv1GUSzV/mqIzynTY2bFAsHAtvO0GAoQTU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Vr9APz0r; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=y+BFT+a0; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Vr9APz0r"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="y+BFT+a0" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1737382191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x465JJNRGOdcBdDnCSb84eitmz3SHSIO0GVIdMHJ+ns=; b=Vr9APz0r92kWP7rVFuP1X/SQ3WtjzIa9Zwu5wQLvTEalWOmB60hBq3aT1EV0eyDBIDw90F 2MHKi3TvCXknVJ8UqSLbswIGnemwRYzByriBjH42GozVji1BZ3DBSICJnJ/Sv5YnbUF29u bTeQIkiF6Qi3QhsAYFePNRRxhSR5AEr9NxyBerbNVOXzjTFhyBr+/DFQBEOOm6l4QeFJqb TbE0AVcHGIaPYMNuoBrQ9Pdsovsp5wbBDPs9EsDQA/DrOL1CZizsXIDl8UT0C8F5BZi5rr wlyZhCE7lr45PNZVoaxTUZB35oytEUOJ8j5lHeqWoClXpetIS9wDVWz7aQvzlQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1737382191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x465JJNRGOdcBdDnCSb84eitmz3SHSIO0GVIdMHJ+ns=; b=y+BFT+a0Yte5gC+Btotx33Rci1qD/dP+4mCUzThh85spZEMgWKggLtT7XqOkUqqk5Ms9AN VerWubbxB7lZ8UAQ== Date: Mon, 20 Jan 2025 15:09:41 +0100 Subject: [PATCH 2/2] of: address: Add kunit test for __of_address_resource_bounds() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250120-of-address-overflow-v1-2-dd68dbf47bce@linutronix.de> References: <20250120-of-address-overflow-v1-0-dd68dbf47bce@linutronix.de> In-Reply-To: <20250120-of-address-overflow-v1-0-dd68dbf47bce@linutronix.de> To: Rob Herring , Saravana Kannan Cc: Basharath Hussain Khaja , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1737382189; l=5640; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=gOHcCsybXsCvfw84Ct+sSDfpztWb1jN3TKKvFCR6eT4=; b=bvq6C7TfEoJaUBVb2OPQQTdSAgo8mIWF3C9aKF2pA0teH4WVwH/oRC7yuFP0Yfh5nGrEX5pA1 +Bp9iwivRnvAooV5wdo6k/7buD5Cv3Wd6Ijs28Nr1mTm+UqV+Svvz1A X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= The overflow checking has to deal with different datatypes and edgecases. Add a new kunit testcase to make sure it works correctly. Signed-off-by: Thomas Wei=C3=9Fschuh --- Technically it's possible to run this unittest with !CONFIG_OF_ADDRESS, so there is an explicit check inside the test. It would also be possible to add a dedicated source file, but that seemed like a lot of churn to me. --- drivers/of/address.c | 5 +- drivers/of/of_private.h | 4 ++ drivers/of/of_test.c | 120 ++++++++++++++++++++++++++++++++++++++++++++= +++- 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/drivers/of/address.c b/drivers/of/address.c index 5c0663066a7f3816a05077f99124ca25e3c152d7..84b8d16919dc4aae966a09f7fe0= 606b2e51c3dde 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -16,6 +16,8 @@ #include #include /* for bus_dma_region */ =20 +#include + #include "of_private.h" =20 /* Max address size we deal with */ @@ -198,7 +200,7 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *r= ange, int na, int ns, =20 #endif /* CONFIG_PCI */ =20 -static int __of_address_resource_bounds(struct resource *r, u64 start, u64= size) +VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 = start, u64 size) { if (overflows_type(start, r->start)) return -EOVERFLOW; @@ -212,6 +214,7 @@ static int __of_address_resource_bounds(struct resource= *r, u64 start, u64 size) =20 return 0; } +EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds); =20 /* * of_pci_range_to_resource - Create a resource from an of_pci_range diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h index ea5a0951ec5e107bab265ab5f6c043e2bfb15ecc..0d29d71ac7c62ae91bfac0c8d15= 18622729575cd 100644 --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -188,4 +188,8 @@ void __init fdt_scan_reserved_mem_reg_nodes(void); =20 bool of_fdt_device_is_available(const void *blob, unsigned long node); =20 +#if IS_ENABLED(CONFIG_KUNIT) +int __of_address_resource_bounds(struct resource *r, u64 start, u64 size); +#endif + #endif /* _LINUX_OF_PRIVATE_H */ diff --git a/drivers/of/of_test.c b/drivers/of/of_test.c index b0557ded838fdf70f0b679c31ead38f501371304..b8eb1d4b76fdf3a16fc5dd24f8e= 6ed0c3fca5e9c 100644 --- a/drivers/of/of_test.c +++ b/drivers/of/of_test.c @@ -2,6 +2,7 @@ /* * KUnit tests for OF APIs */ +#include #include #include =20 @@ -54,8 +55,125 @@ static struct kunit_suite of_dtb_suite =3D { .init =3D of_dtb_test_init, }; =20 +struct of_address_resource_bounds_case { + u64 start; + u64 size; + int ret; + + resource_size_t res_start; + resource_size_t res_end; +}; + +static void of_address_resource_bounds_case_desc(const struct of_address_r= esource_bounds_case *p, + char *name) +{ + snprintf(name, KUNIT_PARAM_DESC_SIZE, "start=3D0x%016llx,size=3D0x%016llx= ", p->start, p->size); +} + +static const struct of_address_resource_bounds_case of_address_resource_bo= unds_cases[] =3D { + { + .start =3D 0, + .size =3D 0, + .ret =3D 0, + .res_start =3D 0, + .res_end =3D -1, + }, + { + .start =3D 0, + .size =3D 0x1000, + .ret =3D 0, + .res_start =3D 0, + .res_end =3D 0xfff, + }, + { + .start =3D 0x1000, + .size =3D 0, + .ret =3D 0, + .res_start =3D 0x1000, + .res_end =3D 0xfff, + }, + { + .start =3D 0x1000, + .size =3D 0x1000, + .ret =3D 0, + .res_start =3D 0x1000, + .res_end =3D 0x1fff, + }, + { + .start =3D 1, + .size =3D RESOURCE_SIZE_MAX, + .ret =3D 0, + .res_start =3D 1, + .res_end =3D RESOURCE_SIZE_MAX, + }, + { + .start =3D RESOURCE_SIZE_MAX, + .size =3D 1, + .ret =3D 0, + .res_start =3D RESOURCE_SIZE_MAX, + .res_end =3D RESOURCE_SIZE_MAX, + }, + { + .start =3D 2, + .size =3D RESOURCE_SIZE_MAX, + .ret =3D -EOVERFLOW, + }, + { + .start =3D RESOURCE_SIZE_MAX, + .size =3D 2, + .ret =3D -EOVERFLOW, + }, + { + .start =3D 0x100000000ULL, + .size =3D 1, + .ret =3D sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW, + .res_start =3D (resource_size_t)0x100000000, + .res_end =3D (resource_size_t)0x100000000, + }, + { + .start =3D 0x1000, + .size =3D 0xffffffff, + .ret =3D sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW, + .res_start =3D (resource_size_t)0x1000, + .res_end =3D (resource_size_t)0x100000ffe, + }, +}; + +KUNIT_ARRAY_PARAM(of_address_resource_bounds, + of_address_resource_bounds_cases, of_address_resource_bounds_case_desc= ); + +static void of_address_resource_bounds(struct kunit *test) +{ + const struct of_address_resource_bounds_case *param; + struct resource r; /* Intentionally uninitialized */ + int ret; + + if (!IS_ENABLED(CONFIG_OF_ADDRESS)) + kunit_skip(test, "CONFIG_OF_ADDRESS not enabled\n"); + + param =3D test->param_value; + + ret =3D __of_address_resource_bounds(&r, param->start, param->size); + KUNIT_EXPECT_EQ(test, param->ret, ret); + if (ret =3D=3D 0) { + KUNIT_EXPECT_EQ(test, param->res_start, r.start); + KUNIT_EXPECT_EQ(test, param->res_end, r.end); + KUNIT_EXPECT_EQ(test, param->size, resource_size(&r)); + } +} + +static struct kunit_case of_address_test_cases[] =3D { + KUNIT_CASE_PARAM(of_address_resource_bounds, of_address_resource_bounds_g= en_params), + {} +}; + +static struct kunit_suite of_address_suite =3D { + .name =3D "of_address", + .test_cases =3D of_address_test_cases, +}; + kunit_test_suites( - &of_dtb_suite, + &of_dtb_suite, &of_address_suite, ); MODULE_DESCRIPTION("KUnit tests for OF APIs"); MODULE_LICENSE("GPL"); --=20 2.48.1