glibc/sysdeps/powerpc/powerpc32
Adhemerval Zanella 89b53077d2 nptl: Fix Race conditions in pthread cancellation [BZ#12683]
The current racy approach is to enable asynchronous cancellation
before making the syscall and restore the previous cancellation
type once the syscall returns, and check if cancellation has happen
during the cancellation entrypoint.

As described in BZ#12683, this approach shows 2 problems:

  1. Cancellation can act after the syscall has returned from the
     kernel, but before userspace saves the return value.  It might
     result in a resource leak if the syscall allocated a resource or a
     side effect (partial read/write), and there is no way to program
     handle it with cancellation handlers.

  2. If a signal is handled while the thread is blocked at a cancellable
     syscall, the entire signal handler runs with asynchronous
     cancellation enabled.  This can lead to issues if the signal
     handler call functions which are async-signal-safe but not
     async-cancel-safe.

For the cancellation to work correctly, there are 5 points at which the
cancellation signal could arrive:

	[ ... )[ ... )[ syscall ]( ...
	   1      2        3    4   5

  1. Before initial testcancel, e.g. [*... testcancel)
  2. Between testcancel and syscall start, e.g. [testcancel...syscall start)
  3. While syscall is blocked and no side effects have yet taken
     place, e.g. [ syscall ]
  4. Same as 3 but with side-effects having occurred (e.g. a partial
     read or write).
  5. After syscall end e.g. (syscall end...*]

And libc wants to act on cancellation in cases 1, 2, and 3 but not
in cases 4 or 5.  For the 4 and 5 cases, the cancellation will eventually
happen in the next cancellable entrypoint without any further external
event.

The proposed solution for each case is:

  1. Do a conditional branch based on whether the thread has received
     a cancellation request;

  2. It can be caught by the signal handler determining that the saved
     program counter (from the ucontext_t) is in some address range
     beginning just before the "testcancel" and ending with the
     syscall instruction.

  3. SIGCANCEL can be caught by the signal handler and determine that
     the saved program counter (from the ucontext_t) is in the address
     range beginning just before "testcancel" and ending with the first
     uninterruptable (via a signal) syscall instruction that enters the
      kernel.

  4. In this case, except for certain syscalls that ALWAYS fail with
     EINTR even for non-interrupting signals, the kernel will reset
     the program counter to point at the syscall instruction during
     signal handling, so that the syscall is restarted when the signal
     handler returns.  So, from the signal handler's standpoint, this
     looks the same as case 2, and thus it's taken care of.

  5. For syscalls with side-effects, the kernel cannot restart the
     syscall; when it's interrupted by a signal, the kernel must cause
     the syscall to return with whatever partial result is obtained
     (e.g. partial read or write).

  6. The saved program counter points just after the syscall
     instruction, so the signal handler won't act on cancellation.
     This is similar to 4. since the program counter is past the syscall
     instruction.

So The proposed fixes are:

  1. Remove the enable_asynccancel/disable_asynccancel function usage in
     cancellable syscall definition and instead make them call a common
     symbol that will check if cancellation is enabled (__syscall_cancel
     at nptl/cancellation.c), call the arch-specific cancellable
     entry-point (__syscall_cancel_arch), and cancel the thread when
     required.

  2. Provide an arch-specific generic system call wrapper function
     that contains global markers.  These markers will be used in
     SIGCANCEL signal handler to check if the interruption has been
     called in a valid syscall and if the syscalls has side-effects.

     A reference implementation sysdeps/unix/sysv/linux/syscall_cancel.c
     is provided.  However, the markers may not be set on correct
     expected places depending on how INTERNAL_SYSCALL_NCS is
     implemented by the architecture.  It is expected that all
     architectures add an arch-specific implementation.

  3. Rewrite SIGCANCEL asynchronous handler to check for both canceling
     type and if current IP from signal handler falls between the global
     markers and act accordingly.

  4. Adjust libc code to replace LIBC_CANCEL_ASYNC/LIBC_CANCEL_RESET to
     use the appropriate cancelable syscalls.

  5. Adjust 'lowlevellock-futex.h' arch-specific implementations to
     provide cancelable futex calls.

Some architectures require specific support on syscall handling:

  * On i386 the syscall cancel bridge needs to use the old int80
    instruction because the optimized vDSO symbol the resulting PC value
    for an interrupted syscall points to an address outside the expected
    markers in __syscall_cancel_arch.  It has been discussed in LKML [1]
    on how kernel could help userland to accomplish it, but afaik
    discussion has stalled.

    Also, sysenter should not be used directly by libc since its calling
    convention is set by the kernel depending of the underlying x86 chip
    (check kernel commit 30bfa7b3488bfb1bb75c9f50a5fcac1832970c60).

  * mips o32 is the only kABI that requires 7 argument syscall, and to
    avoid add a requirement on all architectures to support it, mips
    support is added with extra internal defines.

Checked on aarch64-linux-gnu, arm-linux-gnueabihf, powerpc-linux-gnu,
powerpc64-linux-gnu, powerpc64le-linux-gnu, i686-linux-gnu, and
x86_64-linux-gnu.

[1] https://lkml.org/lkml/2016/3/8/1105
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
2024-08-23 14:27:43 -03:00
..
405 Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
440
464
476 Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
970
a2 Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
bits login: structs utmp, utmpx, lastlog _TIME_BITS independence (bug 30701) 2024-04-19 14:38:17 +02:00
cell Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fpu Convert to autoconf 2.72 (vanilla release, no distribution patches) 2024-06-17 21:15:28 +02:00
power4 powerpc: Remove duplicated llrintf and llrintf32 from libm.a (BZ 31787) 2024-05-23 09:36:08 -03:00
power5 powerpc: Remove optimized isnan 2019-06-12 14:32:36 -03:00
power5+ powerpc: Refactor powerpc32 lround/lroundf/llround/llroundf 2019-06-26 14:32:45 -03:00
power6 Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
power6x Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
power7 Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
power8 PowerPC: Adjust multiarch Implies for PowerPC32 2013-12-06 05:47:05 -06:00
power9 powerpc: Fix POWER9 implies 2016-09-19 09:35:38 -03:00
power10 powerpc: Add support for POWER10 2020-06-29 10:08:38 -03:00
power11 powerpc: Placeholder and infrastructure/build support to add Power11 related changes. 2024-03-19 21:11:34 -05:00
__longjmp-common.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
__longjmp.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
add_n.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
addmul_1.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
atomic-machine.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
bsd-_setjmp.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
bsd-setjmp.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
compat-ppc-mcount.S
configure configure: Use autoconf 2.71 2023-07-17 10:08:10 -04:00
configure.ac powerpc32: Remove unused HAVE_PPC_SECURE_PLT 2022-05-02 08:55:36 -07:00
crti.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
crtn.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-dtprocnum.h
dl-irel.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-machine.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-machine.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-start.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-trampoline.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gprrest0.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gprrest1.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gprsave0.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gprsave1.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Implies
Implies-after powerpc: Move around math-related Implies 2018-05-24 22:49:10 -03:00
libgcc-compat.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
lshift.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Makefile Remove last remnants of have-protected 2023-05-22 13:31:04 +02:00
mcount.c
memset.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mul_1.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ppc-mcount.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
rshift.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
rtld-memset.c
setjmp-common.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
setjmp.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stackguard-macros.h
start.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stpcpy.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
strchr.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
strcmp.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
strcpy.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
strlen.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sub_n.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
submul_1.S Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
symbol-hacks.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sysdep.h nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
tst-audit.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
unwind-arch.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Versions