linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: fix potential invalid pointer dereference in kmemdup()
@ 2023-03-07  9:03 Xujun Leng
  2023-03-07 10:04 ` David Hildenbrand
  0 siblings, 1 reply; 6+ messages in thread
From: Xujun Leng @ 2023-03-07  9:03 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, Xujun Leng

If kmemdup() was called with src == NULL, then memcpy() source address
is fatal, and if kmemdup() was called with len == 0, kmalloc_track_caller()
will return ZERO_SIZE_PTR to variable p, then memcpy() destination address
is fatal. Both 2 cases will cause an invalid pointer dereference.

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

diff --git a/mm/util.c b/mm/util.c
index dd12b9531ac4..d1a3b3d2988e 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -128,6 +128,9 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp)
 {
 	void *p;
 
+	if (!src || len == 0)
+		return NULL;
+
 	p = kmalloc_track_caller(len, gfp);
 	if (p)
 		memcpy(p, src, len);
-- 
2.25.1



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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-07  9:03 [PATCH] mm: fix potential invalid pointer dereference in kmemdup() Xujun Leng
2023-03-07 10:04 ` David Hildenbrand
2023-03-09  6:46   ` Xujun Leng
2023-03-09  9:01     ` David Hildenbrand
     [not found]       ` <20230309100415.2382-1-lengxujun2007@126.com>
2023-03-09 13:22         ` David Hildenbrand
2023-03-10  8:06           ` Xujun Leng

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