mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: julien.voisin@dustri.org
Cc: musl@lists.openwall.com
Subject: Re: [musl] Protect pthreads' mutexes against use-after-destroy
Date: Sun, 21 Jan 2024 12:03:03 -0500	[thread overview]
Message-ID: <20240121170302.GA4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <820837e29ea605142a934e672d670fbbd9d44cbd@dustri.org>

On Sun, Jan 21, 2024 at 12:06:14PM +0000, julien.voisin@dustri.org wrote:
> > Draft attached in case anyone wants to play with it. This could
> > probably be something we could consider to adopt.
> 
> Couldn't a macro like `#define mutex_is_destroyed (!(m->_m_type & 8) && (m->_m_lock == 0x3fffffff)` be
> used instead? Or at least named constants instead of `8` and `0x3fffffff`..

Maybe something like that, but that's a change I'd like to make in a
consistent uniform way for all of the uses of magic numbers in the
mutex implementation. Just introducing it in a single place like this
doesn't really help readability; in some ways it makes it less
readable since you can't see how it's interacting with the other
tests.

If doing it, I think it would probably make more sense not to have
that predicate macro, but instead something like:

	if (own == M_UNRECOVERABLE && !(m->_m_type & MT_ROBUST))

because seeing the individual parts is relevant to understanding:

> Also, the code-style seems inconsistent:
> 
> ```
> +       if (own == 0x3fffffff) {
> +               /* Catch use-after-destroy */
> +               if (!(type & 8)) a_crash();                                                                                                                                                      
> +               return ENOTRECOVERABLE;
> +       }
> ```
> 
> vs 
> 
> ```
> +                       /* Catch use-after-destroy */
> +                       if (own == 0x3fffffff && !(type & 8)) a_crash();
>                         return EPERM;
> ```
> 
> Both are the same check, yet only one has both conditions in a single `if`.

These are decision trees on what to do in exceptional cases. The aim
is not to present a consistent "style" between two functions that do
different things and have different decision trees for how to act.

Rich

      reply	other threads:[~2024-01-21 17:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-09 14:37 jvoisin
2024-01-09 19:07 ` Rich Felker
2024-01-09 23:27   ` enh
2024-01-10  1:58     ` Rich Felker
2024-01-12 16:53       ` enh
2024-01-10  1:55   ` Rich Felker
2024-01-10 21:24     ` Rich Felker
2024-01-21  3:43   ` Rich Felker
2024-01-21 12:06     ` julien.voisin
2024-01-21 17:03       ` Rich Felker [this message]

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=20240121170302.GA4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=julien.voisin@dustri.org \
    --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).