mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH v2] Add a safe dequeue integrity check for mallocng
@ 2023-09-16  7:08 James Raphael Tiovalen
  2023-09-16 14:58 ` jvoisin
  0 siblings, 1 reply; 3+ messages in thread
From: James Raphael Tiovalen @ 2023-09-16  7:08 UTC (permalink / raw)
  To: musl; +Cc: James Raphael Tiovalen

This commit adds an integrity check to allow for safer dequeuing of the
out-of-band meta structs in mallocng. If the unlikely condition is true
due to some sort of heap metadata corruption, we 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 metadata corruption
attacks from being successfully executed. Having this check here would
reduce the risk of pointer hijacking, mitigate the impact of
attacker-controlled `prev` and `next` pointers that could be obtained
via a vulnerable program, and restrict the tampering of other memory
regions via arbitrary write primitives.
---
v1 -> v2: Modify the check to an assert.
---
 src/malloc/mallocng/meta.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/malloc/mallocng/meta.h b/src/malloc/mallocng/meta.h
index 61ec53f9..847598b5 100644
--- a/src/malloc/mallocng/meta.h
+++ b/src/malloc/mallocng/meta.h
@@ -90,6 +90,7 @@ static inline void queue(struct meta **phead, struct meta *m)
 static inline void dequeue(struct meta **phead, struct meta *m)
 {
 	if (m->next != m) {
+		assert(m->prev->next == m && m->next->prev == m);
 		m->prev->next = m->next;
 		m->next->prev = m->prev;
 		if (*phead == m) *phead = m->next;
--
2.42.0


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

* Re: [musl] [PATCH v2] Add a safe dequeue integrity check for mallocng
  2023-09-16  7:08 [musl] [PATCH v2] Add a safe dequeue integrity check for mallocng James Raphael Tiovalen
@ 2023-09-16 14:58 ` jvoisin
  2023-09-16 16:11   ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: jvoisin @ 2023-09-16 14:58 UTC (permalink / raw)
  To: musl

On 16/09/2023 09:08, James Raphael Tiovalen wrote:
> This commit adds an integrity check to allow for safer dequeuing of the
> out-of-band meta structs in mallocng. If the unlikely condition is true
> due to some sort of heap metadata corruption, we abort.

Since asserts aren't present in production code[1], I don't think that
this change is useful.


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

The metadata in musl's heap implementation are stored out-of-bound.
Should an attacker be able to locate and modify them, it's already game
over. Adding a `m->prev->next == m && m->next->prev == m` would only
impede attackers if they only have an arbitrary read and a one-shot
arbitrary write that can only overwrite one `meta` instead. This seems
pretty contrived (as in "unlikely) to me. Do you have any particular
scenario in mind?


1. https://git.musl-libc.org/cgit/musl/tree/include/assert.h


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

* Re: [musl] [PATCH v2] Add a safe dequeue integrity check for mallocng
  2023-09-16 14:58 ` jvoisin
@ 2023-09-16 16:11   ` Rich Felker
  0 siblings, 0 replies; 3+ messages in thread
From: Rich Felker @ 2023-09-16 16:11 UTC (permalink / raw)
  To: jvoisin; +Cc: musl, James Raphael Tiovalen

On Sat, Sep 16, 2023 at 04:58:45PM +0200, jvoisin wrote:
> On 16/09/2023 09:08, James Raphael Tiovalen wrote:
> > This commit adds an integrity check to allow for safer dequeuing of the
> > out-of-band meta structs in mallocng. If the unlikely condition is true
> > due to some sort of heap metadata corruption, we abort.
> 
> Since asserts aren't present in production code[1], I don't think that
> this change is useful.

Did you read the previous (v1 patch) thread or the code? In mallocng,
assert expands to a lightweight assertion check.

> > This approach is similar to the safe unlinking check performed by glibc.
> 
> The metadata in musl's heap implementation are stored out-of-bound.
> Should an attacker be able to locate and modify them, it's already game
> over. Adding a `m->prev->next == m && m->next->prev == m` would only
> impede attackers if they only have an arbitrary read and a one-shot
> arbitrary write that can only overwrite one `meta` instead. This seems
> pretty contrived (as in "unlikely) to me. Do you have any particular
> scenario in mind?
> 
> 
> 1. https://git.musl-libc.org/cgit/musl/tree/include/assert.h

Indeed, I'm still mildly unsure of the usefulness of this check, but
not really opposed to it unless it turns out to be costly. (Note: many
of the others already there *are* moderately costly, but are based on
very plausible attack models where the data being checked is easily
reachable via application bugs.) Has anyone checked if this measurably
affects performance?

Rich

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

end of thread, other threads:[~2023-09-16 16:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-16  7:08 [musl] [PATCH v2] Add a safe dequeue integrity check for mallocng James Raphael Tiovalen
2023-09-16 14:58 ` jvoisin
2023-09-16 16:11   ` Rich Felker

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).