Index | Thread | Search

From:
Otto Moerbeek <otto@drijf.net>
Subject:
malloc: more strict chunk pointer checking
To:
tech@openbsd.org
Date:
Wed, 11 Nov 2026 16:25:39 +0100

Download raw body.

Thread
From Acts1631 <acts1631kjv@proton.me>, try harder to detect modified
chunk pointers.

OK?

  -Otto

Index: stdlib/malloc.c
===================================================================
RCS file: /home/cvs/src/lib/libc/stdlib/malloc.c,v
diff -u -p -r1.302 malloc.c
--- stdlib/malloc.c	4 Nov 2026 12:37:25 -0000	1.302
+++ stdlib/malloc.c	11 Nov 2026 15:21:47 -0000
@@ -1248,15 +1248,18 @@ validate_canary(struct dir_info *d, u_ch
 static inline uint32_t
 find_chunknum(struct dir_info *d, struct chunk_info *info, void *ptr, int check)
 {
+	uintptr_t offset;
 	uint32_t chunknum;
 
 	if (info->canary != (u_short)d->canary1)
 		wrterror(d, "chunk info corrupted");
 
 	/* Find the chunk number on the page */
-	chunknum = ((uintptr_t)ptr & MALLOC_PAGEMASK) / B2ALLOC(info->bucket);
+	offset = (uintptr_t)ptr & MALLOC_PAGEMASK;
+	chunknum = offset / B2ALLOC(info->bucket);
 
-	if ((uintptr_t)ptr & (MALLOC_MINSIZE - 1))
+	if (chunknum >= info->total ||
+	    offset != chunknum * B2ALLOC(info->bucket))
 		wrterror(d, "modified chunk-pointer %p", ptr);
 	if (CHUNK_FREE(info, chunknum))
 		wrterror(d, "double free %p", ptr);