public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools/nolibc/stdlib: fix memory error in realloc()
@ 2024-01-09 23:44 Brennan Xavier McManus
  2024-01-10  4:11 ` Ammar Faizi
  0 siblings, 1 reply; 3+ messages in thread
From: Brennan Xavier McManus @ 2024-01-09 23:44 UTC (permalink / raw)
  To: Willy Tarreau
  Cc: Thomas Weißschuh, Ammar Faizi, Linux Kernel Mailing List

Pass user_p_len to memcpy() instead of heap->len to prevent realloc()
from copying an extra sizeof(heap) bytes from beyond the allocated
region.

Signed-off-by: Brennan Xavier McManus <bxmcmanus@gmail.com>
---

All tests from Ammar's original test program pass:
https://gist.github.com/ammarfaizi2/db0af6aa0b95a0c7478bce64e349f021
This fix was tested with the following added test for realloc():
https://github.com/brennan913/nolibc-test/blob/main/nolibc_test.c#L73-L120

 tools/include/nolibc/stdlib.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index bacfd35c5156..5be9d3c7435a 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -185,7 +185,7 @@ void *realloc(void *old_ptr, size_t new_size)
 	if (__builtin_expect(!ret, 0))
 		return NULL;
 
-	memcpy(ret, heap->user_p, heap->len);
+	memcpy(ret, heap->user_p, user_p_len);
 	munmap(heap, heap->len);
 	return ret;
 }
-- 
2.39.2


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

* Re: [PATCH] tools/nolibc/stdlib: fix memory error in realloc()
  2024-01-09 23:44 [PATCH] tools/nolibc/stdlib: fix memory error in realloc() Brennan Xavier McManus
@ 2024-01-10  4:11 ` Ammar Faizi
  2024-01-10  6:17   ` Willy Tarreau
  0 siblings, 1 reply; 3+ messages in thread
From: Ammar Faizi @ 2024-01-10  4:11 UTC (permalink / raw)
  To: Brennan Xavier McManus
  Cc: Willy Tarreau, Thomas Weißschuh, Linux Kernel Mailing List

On Tue, Jan 09, 2024 at 06:44:02PM -0500, Brennan Xavier McManus wrote:
> Pass user_p_len to memcpy() instead of heap->len to prevent realloc()
> from copying an extra sizeof(heap) bytes from beyond the allocated
> region.
> 
> Signed-off-by: Brennan Xavier McManus <bxmcmanus@gmail.com>
> ---
> 
> All tests from Ammar's original test program pass:
> https://gist.github.com/ammarfaizi2/db0af6aa0b95a0c7478bce64e349f021
> This fix was tested with the following added test for realloc():
> https://github.com/brennan913/nolibc-test/blob/main/nolibc_test.c#L73-L120

Hello Brennan,

Thank you for the fix!

Cc: stable@vger.kernel.org
Reviewed-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Fixes: 0e0ff638400be8f497a35b51a4751fd823f6bd6a ("tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`")

-- 
Ammar Faizi


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

* Re: [PATCH] tools/nolibc/stdlib: fix memory error in realloc()
  2024-01-10  4:11 ` Ammar Faizi
@ 2024-01-10  6:17   ` Willy Tarreau
  0 siblings, 0 replies; 3+ messages in thread
From: Willy Tarreau @ 2024-01-10  6:17 UTC (permalink / raw)
  To: Ammar Faizi
  Cc: Brennan Xavier McManus, Thomas Weißschuh,
	Linux Kernel Mailing List

Hi,

On Wed, Jan 10, 2024 at 11:11:37AM +0700, Ammar Faizi wrote:
> On Tue, Jan 09, 2024 at 06:44:02PM -0500, Brennan Xavier McManus wrote:
> > Pass user_p_len to memcpy() instead of heap->len to prevent realloc()
> > from copying an extra sizeof(heap) bytes from beyond the allocated
> > region.
> > 
> > Signed-off-by: Brennan Xavier McManus <bxmcmanus@gmail.com>
> > ---
> > 
> > All tests from Ammar's original test program pass:
> > https://gist.github.com/ammarfaizi2/db0af6aa0b95a0c7478bce64e349f021
> > This fix was tested with the following added test for realloc():
> > https://github.com/brennan913/nolibc-test/blob/main/nolibc_test.c#L73-L120
> 
> Hello Brennan,
> 
> Thank you for the fix!
> 
> Cc: stable@vger.kernel.org
> Reviewed-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
> Fixes: 0e0ff638400be8f497a35b51a4751fd823f6bd6a ("tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`")

Thanks both for the fix and the review! I've pushed it to the "fixes"
branch. Thomas, for now I updated the branch to match "next" (which just
got merged) so as to preserve patch ordering, we'll just rebase it once
6.8-rc1 is out.

Willy

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

end of thread, other threads:[~2024-01-10  6:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-09 23:44 [PATCH] tools/nolibc/stdlib: fix memory error in realloc() Brennan Xavier McManus
2024-01-10  4:11 ` Ammar Faizi
2024-01-10  6:17   ` Willy Tarreau

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