mailing list of musl libc
 help / color / mirror / code / Atom feed
From: James Raphael Tiovalen <jamestiotio@gmail.com>
To: musl@lists.openwall.com
Cc: James Raphael Tiovalen <jamestiotio@gmail.com>
Subject: [musl] [PATCH] Add a safe dequeue integrity check for mallocng
Date: Sat,  9 Sep 2023 01:49:39 +0800	[thread overview]
Message-ID: <20230908174939.80579-1-jamestiotio@gmail.com> (raw)

This commit adds an integrity check to allow for safer dequeuing of meta
structs in mallocng. If the unlikely condition is true due to some sort
of heap corruption, we print an error message and abort.

This approach is similar to the safe unlinking check performed by glibc.

While this check would not prevent more sophisticated attacks in more
specific scenarios, as shown by the historical exploitation efforts on
glibc, this check would prevent more basic heap corruption attacks from
being successfully executed.
---
 src/malloc/mallocng/meta.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/malloc/mallocng/meta.h b/src/malloc/mallocng/meta.h
index 61ec53f9..57946d01 100644
--- a/src/malloc/mallocng/meta.h
+++ b/src/malloc/mallocng/meta.h
@@ -2,9 +2,11 @@
 #define MALLOC_META_H
 
 #include <stdint.h>
+#include <stdio.h>
 #include <errno.h>
 #include <limits.h>
 #include "glue.h"
+#include "libm.h"
 
 __attribute__((__visibility__("hidden")))
 extern const uint16_t size_classes[];
@@ -90,6 +92,10 @@ static inline void queue(struct meta **phead, struct meta *m)
 static inline void dequeue(struct meta **phead, struct meta *m)
 {
 	if (m->next != m) {
+		if (predict_false(m->prev->next != m || m->next->prev != m)) {
+			fprintf(stderr, "Corrupted doubly-linked meta list\n");
+			abort();
+		}
 		m->prev->next = m->next;
 		m->next->prev = m->prev;
 		if (*phead == m) *phead = m->next;
-- 
2.42.0


             reply	other threads:[~2023-09-08 17:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08 17:49 James Raphael Tiovalen [this message]
2023-09-09  0:48 ` Rich Felker
2023-09-14  5:13   ` James R T
2023-09-14  9:23     ` Joakim Sindholt
2023-09-14 12:18       ` Rich Felker
2023-09-16  6:53         ` James R T

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230908174939.80579-1-jamestiotio@gmail.com \
    --to=jamestiotio@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).