public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Zack Weinberg <zackw@panix.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH v4 02/21] nptl: Fix Race conditions in pthread cancellation [BZ#12683]
Date: Tue, 7 Apr 2020 14:24:03 -0400	[thread overview]
Message-ID: <CAKCAbMiHxdo+Vv5j+J7_pMn_NhmRQjDiqb8OYgZpccm-_Jk5yA@mail.gmail.com> (raw)
In-Reply-To: <20200403203201.7494-3-adhemerval.zanella@linaro.org>

On Fri, Apr 3, 2020 at 4:32 PM Adhemerval Zanella via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> This patch is the initial fix for race conditions in NPTL cancellation
> code by redefining how cancellable syscalls are defined and handled.
> The current buggy approach is to enable asynchronous cancellation
> before making the syscall and restore the previous cancellation
> type once the syscall returns.

I want to see this bug fixed.  Unfortunately I don't know the guts of
NPTL well enough to review your patches completely, but here are a few
things I noticed:

> As a side note regarding SIGCANCEL and SIGTIMER being the the same,
> it should not impact timer_create functionality.  It arranges for
> SIGCANCEL/SIGTIMER to be sent to the internal helper thread, which
> in turn check if the si.si_code is SI_TIMER and call pthread_exit
> otherwise (sysdeps/unix/sysv/linux/timer_routines.c:129).

Can we be absolutely certain that SIGCANCEL/SIGTIMER will always be
sent to a specific thread and not to a process?

> +  /* Add SIGCANCEL on ignored sigmask to avoid the handler to be called
> +     again.  */
> +  ucontext_block_sigcancel (ctx);
> +
> +  /* Check if asynchronous cancellation mode is set or if interrupted
> +     instruction pointer falls within the cancellable syscall bridge.  For
> +     interruptable syscalls that might generate external side-effects (partial
> +     reads or writes, for instance), the kernel will set the IP to after
> +     '__syscall_cancel_arch_end', thus disabling the cancellation and allowing
> +     the process to handle such conditions.  */
> +  if (self->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS
> +      || cancellation_pc_check (ctx))
> +    __do_cancel (PTHREAD_CANCELED);

Shouldn't this check happen _before_ we block further SIGCANCELs?
If cancellation_pc_check fails, because the signal was delivered on
exit from a system call that has had side effects, don't we need to
be able to receive future SIGCANCELs in order for the next cancellation
point to trigger?

>    /* Install the cancellation signal handler.  If for some reason we
>       cannot install the handler we do not abort.  Maybe we should, but
>       it is only asynchronous cancellation which is affected.  */

1) I think the third sentence of this comment has always been wrong.
2) Perhaps, if we cannot install a handler for SIGCANCEL, we should set
   a global flag which causes all calls to pthread_cancel to fail?

> +    /* Install the handle to change the threads' uid/gid.  */

Typo: handle -> handler (it was wrong before, but you may as well fix it)

> +    struct sigaction sa;
> +    __sigemptyset (&sa.sa_mask);
> +    sa.sa_sigaction = sighandler_setxid;
> +    sa.sa_flags = SA_SIGINFO | SA_RESTART;
> +    __libc_sigaction (SIGSETXID, &sa, NULL);

Unrelated preexisting bug, but I think we probably _should_ crash the
whole process if the SIGSETXID handler cannot be installed,
particularly when __libc_enable_secure is true.

> +  /* Avoid signaling when thread attempts cancel itself (pthread_kill
> +     is expensive).  */
> +  if (pd == THREAD_SELF)
> +    {
> +      if (pd->cancelstate == PTHREAD_CANCEL_ENABLE
> +         && pd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
> +       __pthread_exit (PTHREAD_CANCELED);
> +      return 0;

This works because __pthread_exit is actually a tiny wrapper around
__do_cancel, but I think the logic would be easier to understand,
here, if it called __do_cancel.

zw

  reply	other threads:[~2020-04-07 18:24 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-03 20:31 [PATCH v4 00/21] " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 01/21] nptl: Do not close the pipe on tst-cancel{2,3} Adhemerval Zanella
2020-04-07 15:24   ` Zack Weinberg
2020-04-07 20:07     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 02/21] nptl: Fix Race conditions in pthread cancellation [BZ#12683] Adhemerval Zanella
2020-04-07 18:24   ` Zack Weinberg [this message]
2020-04-08 14:13     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 03/21] nptl: x86_64: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 04/21] nptl: x32: " Adhemerval Zanella
2020-04-03 21:22   ` Joseph Myers
2020-04-07 12:47     ` Adhemerval Zanella
2020-04-07 12:54       ` H.J. Lu
2020-04-07 13:33         ` Adhemerval Zanella
2020-04-07 13:40           ` H.J. Lu
2020-04-07 13:41             ` H.J. Lu
2020-04-07 13:55               ` Adhemerval Zanella
2020-04-07 13:59                 ` H.J. Lu
2020-04-07 14:04                   ` Adhemerval Zanella
2020-04-07 15:45                     ` H.J. Lu
2020-04-07 16:16                       ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 05/21] nptl: i386: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 06/21] nptl: ia64: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 07/21] nptl: mips: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 08/21] nptl: aarch64: " Adhemerval Zanella
2020-04-12 15:29   ` Stepan Golosunov
2020-04-15 14:30     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 09/21] nptl: arm: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 10/21] nptl: powerpc: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 11/21] nptl: microblaze: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 12/21] nptl: sparc: " Adhemerval Zanella
2020-04-12 15:33   ` Stepan Golosunov
2020-04-14 16:54     ` Stepan Golosunov
2020-04-15 14:48       ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 13/21] nptl: hppa: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 14/21] nptl: m68k: " Adhemerval Zanella
2020-04-03 21:34   ` Andreas Schwab
2020-04-07 12:46     ` Adhemerval Zanella
2020-04-12 15:42   ` Stepan Golosunov
2020-04-15 14:51     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 15/21] nptl: alpha: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 16/21] nptl: sh: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 17/21] nptl: riscv: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 18/21] nptl: s390: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 19/21] nptl: nios2: " Adhemerval Zanella
2020-04-03 20:32 ` [PATCH v4 20/21] nptl: csky: " Adhemerval Zanella
2020-04-03 20:32 ` [PATCH v4 21/21] Linux: Remove sysdep-cancel header Adhemerval Zanella

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=CAKCAbMiHxdo+Vv5j+J7_pMn_NhmRQjDiqb8OYgZpccm-_Jk5yA@mail.gmail.com \
    --to=zackw@panix.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).