mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: fork, set*id(synccall), cancellation -- nasty interaction
Date: Fri, 26 Apr 2013 20:37:31 -0400	[thread overview]
Message-ID: <20130427003730.GS20323@brightrain.aerifal.cx> (raw)
In-Reply-To: <20130426172722.GA21854@brightrain.aerifal.cx>

On Fri, Apr 26, 2013 at 01:27:22PM -0400, Rich Felker wrote:
> I've run across a nasty set of race conditions I'm trying to solve:
> 
> 1. __synccall, needed for multi-threaded set*id, needs to obtain a
> lock that prevents thread creation and other things. However, setuid
> and setgid are specified to be async-signal-safe. They will presently
> hang if called from a signal handler that interrupted pthread_create
> or several other functions.

I have a solution, but it's ugly and would decrease thread creation
performance by more than 10%. For static linked programs, the penalty
could be eliminated if __synccall is not used, but adding the logic to
do this would make the code much uglier.

The concept is fairly simple:

1. Fully protect the thread count by the "ptc" rwlock, so that if
another thread holds __inhibit_ptc(), the thread count can neither
increase nor decrease.

2. Prevent any calls to __synccall from interrupting code paths that
hold the "ptc" lock.

However, the requirements this translates into are:

1. pthread_create must block application signals unconditionally.
Right now it only does so in the special case of applying scheduling
changes to the new thread.

2. pthread_exit must perform the following acrobatics: First, block
application signals. Then, __acquire_ptc(). Then, block all signals,
then decrement the thread count and __release_ptc(). This adds both an
extra lock/unlock step and a second sigprocmask syscall to the exit
procedure. The two-step signal blocking is needed because, if all
signals were blocked at the time of the __acquire_ptc() call, it could
deadlock with another thread calling __synccall that had already
successfully performed __inhibit_ptc() and begun the broadcast.

3. The other user of __inhibit_ptc(), dlopen, would either need to
block signals for its duration, or the "ptc" rwlock could be replaced
by a two-way symmetric lock (allowing multiple 'readers' or multiple
'writers' but not both).

Basically, it's doable, but ugly. I'm still looking for better
solutions...

Rich


      reply	other threads:[~2013-04-27  0:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-26 17:27 Rich Felker
2013-04-27  0:37 ` 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=20130427003730.GS20323@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --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).