public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] driver core: platform: added arguments check for platform_device_add_resources()
@ 2023-03-07  5:01 Xujun Leng
  2023-03-09 14:39 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Xujun Leng @ 2023-03-07  5:01 UTC (permalink / raw)
  To: gregkh, rafael; +Cc: linux-kernel, Xujun Leng

In the follow two cases, platform_device_add_resources() can lead an
invalid address access:
1) If (!res && num > 0), pdev->resource will be set to NULL but
   pdev->num_resources > 0, then a later platform_get_resource() will
   cause invalid address access.
2) If (res && num == 0), because num == 0 cause kmalloc_slab() returns
   ZERO_SIZE_PTR, then kmemdup() will copy data to the invalid address
   ZERO_SIZE_PTR.

Signed-off-by: Xujun Leng <lengxujun2007@126.com>
---
 drivers/base/platform.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 77510e4f47de..a060941c3076 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -606,6 +606,9 @@ int platform_device_add_resources(struct platform_device *pdev,
 {
 	struct resource *r = NULL;
 
+	if ((!res && num > 0) || (res && num == 0))
+		return -EINVAL;
+
 	if (res) {
 		r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
 		if (!r)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-10 12:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-07  5:01 [PATCH] driver core: platform: added arguments check for platform_device_add_resources() Xujun Leng
2023-03-09 14:39 ` Greg KH
2023-03-10  6:55   ` Xujun Leng
2023-03-10  7:51     ` Greg KH
2023-03-10 12:57       ` Xujun Leng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox