mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Stefan Jumarea <stefanjumarea02@gmail.com>, musl@lists.openwall.com
Subject: Re: [musl] [PATCH] mallocng: Add MTE support for Aarch64
Date: Tue, 11 Jun 2024 10:46:25 -0400	[thread overview]
Message-ID: <20240611144624.GP10433@brightrain.aerifal.cx> (raw)
In-Reply-To: <20240611140922.GF3766212@port70.net>

On Tue, Jun 11, 2024 at 04:09:22PM +0200, Szabolcs Nagy wrote:
> * Stefan Jumarea <stefanjumarea02@gmail.com> [2024-06-10 15:36:25 +0300]:
> > @@ -102,17 +107,30 @@ void free(void *p)
> >  {
> >  	if (!p) return;
> >  
> > +#ifdef MEMTAG
> > +	void *untagged = (void *)((uint64_t)p & ~MTE_TAG_MASK);
> > +#else
> > +	void *untagged = p;
> > +#endif
> > +
> >  	struct meta *g = get_meta(p);
> ....
> >  static inline struct meta *get_meta(const unsigned char *p)
> >  {
> >  	assert(!((uintptr_t)p & 15));
> > -	int offset = *(const uint16_t *)(p - 2);
> > -	int index = get_slot_index(p);
> > -	if (p[-4]) {
> > +#ifdef MEMTAG
> > +	const unsigned char *untagged = (const unsigned char *)((uint64_t)p & ~MTE_TAG_MASK);
> > +#else
> > +	const unsigned char *untagged = p;
> > +#endif
> 
> if free just drops the tag, then incorrectly tagged pointer
> will not be detected. musl does some use-after-free checks,
> so i dont know how important this is, but i'd check that the
> passed pointer matches the memory tag (unless 0 sized and
> that the tag is non-0) otherwise a forged pointer may cause
> corruption.

Yes. Would just accessing a byte at the start fix this?

> i don't see where you enable tagged pointer abi and checks
> (prctl) or where you add PROT_MTE to the mmapped memory.
> https://www.kernel.org/doc/html/latest/arch/arm64/memory-tagging-extension.html
> https://www.kernel.org/doc/html/latest/arch/arm64/tagged-address-abi.html
> (i think we want the synchronous tag check option.)
> note there is software that assumes page granularity for
> memory protection e.g. does read access at p&-4096, and
> there may software that assumes the top bits of a valid
> pointer is 0 so unconditionally enabling tagging and tag
> checks can be an issue.

I don't think that's a problem. malloc has no contract to allow those
things.

> another potential issue is the early ALIGN_UP of the malloc
> size: this overflows malloc(-1) and i think changes
> malloc_usable_size(malloc(1)).

Yes, I saw that was incorrect and don't understand the motivation. If
it's to avoid tag mismatch at the final 12b, that could be done where
the size class is selected instead. But it would be better if we could
make it work with the tag mismatch (not sure how hard that is) so this
space is still usable (ignoring tag when accessing the header).

> iirc i changed IB when i tried out mte with mallocng.
> 
> i would avoid excessive ifdefs in the code, e.g. by using
> 'p = untag(p);' and define untag appropriately in a header.
> (this might as well do the tag checks when mte is enabled,

Yes.

> but might need special-casing 0 sized allocations.)

Zero-sized allocations could maybe be implemented as a wrong tag? But
then we'd need a way for free to let them pass untrapped.

> tagging large areas can be slow, just like 'dc zva,x0' in
> aarch64 memset, there is 'dc gva,x0' that tags an entire
> cacheline, alternatively it may be better to just rely on
> page protection for large allocations to avoid tagging
> overheads (don't even mmap them as PROT_MTE). i've never
> actually benchmarked what makes sense in practice, i'd at
> least put the hand written loop behind an abstraction.

That loses a lot of the benefit of MTE. Page protections don't work
because there would be too many VMAs if we put guard pages after each
mmap-serviced allocation. However, one thing you can do to protect
against overflow in mmap-serviced allocations without tagging the
whole thing with O(n) operation is just tagging the tail up to end of
page. This would make accesses past the end trap.

Rich

  reply	other threads:[~2024-06-11 14:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-10 12:36 Stefan Jumarea
2024-06-10 15:59 ` Rich Felker
2024-06-11 14:09 ` Szabolcs Nagy
2024-06-11 14:46   ` Rich Felker [this message]
2024-06-11 15:37     ` Stefan Jumarea
2024-06-11 16:42       ` Rich Felker
2024-06-11 17:13         ` Stefan Jumarea
2024-06-11 19:39           ` Szabolcs Nagy

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=20240611144624.GP10433@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=stefanjumarea02@gmail.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).