glibc/sysdeps/x86_64
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
..
64
fpu added inputs giving large errors on x86_64 for new C23 functions 2024-08-07 14:28:46 +02:00
htl
multiarch x86: Unifies 'strnlen-evex' and 'strnlen-evex512' implementations. 2024-08-19 07:34:02 -07:00
nptl nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
sys
x32 x86-64: Remove sysdeps/x86_64/x32/dl-machine.h 2024-07-25 00:17:21 -07:00
____longjmp_chk.S
__longjmp.S
_mcount.S
abort-instr.h
add_n.S
addmul_1.S
bsd-_setjmp.S
bsd-setjmp.S
check-cet.awk
configure Convert to autoconf 2.72 (vanilla release, no distribution patches) 2024-06-17 21:15:28 +02:00
configure.ac
crti.S
crtn.S
dl-cache.h
dl-cet.c
dl-dtprocnum.h
dl-hwcaps-subdirs.c
dl-irel.h
dl-machine.h x86-64: Remove sysdeps/x86_64/x32/dl-machine.h 2024-07-25 00:17:21 -07:00
dl-plt-rewrite.h
dl-procinfo.c
dl-procruntime.c elf: Remove _DL_PLATFORMS_COUNT 2024-06-18 10:45:36 +02:00
dl-runtime.h
dl-tls.c elf: Support recursive use of dynamic TLS in interposed malloc 2024-07-01 19:02:11 +02:00
dl-tls.h
dl-tlsdesc-dynamic.h
dl-tlsdesc.h
dl-tlsdesc.S
dl-trampoline-save.h
dl-trampoline-state.h
dl-trampoline.h
dl-trampoline.S
feature-control.h
ifuncmain8.c
ifuncmod8.c
Implies
isa-default-impl.h
isa.h
jmpbuf-offsets.h
jmpbuf-unwind.h
l10nflist.c
ldsodefs.h
libc-start.h
link_map.h
link-defines.sym
locale-defines.sym
localplt.data
lshift.S
machine-gmon.h
Makefile
math-use-builtins-ffs.h
memchr.S
memcmp.S
memcmpeq.S
memcpy_chk.S
memcpy.S
memmove_chk.S
memmove.S
mempcpy_chk.S
mempcpy.S
memrchr.S
memset_chk.S
memset.S
mp_clz_tab.c
mul_1.S
preconfigure
preconfigure.ac
rawmemchr.S
rshift.S
rtld-offsets.sym
setjmp.S
stackguard-macros.h
stackinfo.h
start.S
stpcpy.S
stpncpy.S
strcasecmp_l-nonascii.c
strcasecmp_l.S
strcasecmp.S
strcat.S
strchr-isa-default-impl.h
strchr.S
strchrnul.S
strcmp.S
strcpy.S
strcspn-generic.c
strcspn.c
strlen.S
strncase_l-nonascii.c
strncase_l.S
strncase.S
strncat.S
strncmp.S
strncpy.S
strnlen.S
strpbrk-generic.c
strpbrk.c
strrchr.S
strspn-generic.c
strspn.c
sub_n.S
submul_1.S
sysdep.h
tls_get_addr.S
tlsdesc.c
tlsdesc.sym
tst-audit3.c
tst-audit4-aux.c
tst-audit4.c
tst-audit5.c
tst-audit6.c
tst-audit7.c
tst-audit10-aux.c
tst-audit10.c
tst-audit.h
tst-auditmod3a.c
tst-auditmod3b.c
tst-auditmod4a.c
tst-auditmod4b.c
tst-auditmod5a.c
tst-auditmod5b.c
tst-auditmod6a.c
tst-auditmod6b.c
tst-auditmod6c.c
tst-auditmod7a.c
tst-auditmod7b.c
tst-auditmod10a.c
tst-auditmod10b.c
tst-avx512-aux.c
tst-avx512.c
tst-avx512mod.c
tst-avx-aux.c
tst-avx.c
tst-avxmod.c
tst-cet-legacy-1.c
tst-cet-legacy-1a.c
tst-cet-legacy-2.c
tst-cet-legacy-2a.c
tst-cet-legacy-3.c
tst-cet-legacy-4.c
tst-cet-legacy-4a.c
tst-cet-legacy-4b.c
tst-cet-legacy-4c.c
tst-cet-legacy-5.c
tst-cet-legacy-5a.c
tst-cet-legacy-5b.c
tst-cet-legacy-6.c
tst-cet-legacy-6a.c
tst-cet-legacy-6b.c
tst-cet-legacy-7.c
tst-cet-legacy-8.c
tst-cet-legacy-9-static.c
tst-cet-legacy-9.c
tst-cet-legacy-10-static.c
tst-cet-legacy-10.c
tst-cet-legacy-10a-static.c
tst-cet-legacy-10a.c
tst-cet-legacy-mod-1.c
tst-cet-legacy-mod-2.c
tst-cet-legacy-mod-4.c
tst-cet-legacy-mod-5.c
tst-cet-legacy-mod-5a.c
tst-cet-legacy-mod-5b.c
tst-cet-legacy-mod-5c.c
tst-cet-legacy-mod-6.c
tst-cet-legacy-mod-6a.c
tst-cet-legacy-mod-6b.c
tst-cet-legacy-mod-6c.c
tst-cet-legacy-mod-6d.c
tst-glibc-hwcaps.c
tst-gnu2-tls2mod1.S
tst-platform-1.c
tst-platformmod-1.c
tst-platformmod-2.c
tst-plt-rewrite1.c
tst-plt-rewrite2.c
tst-plt-rewritemod1.c
tst-plt-rewritemod2.c
tst-quad1.c
tst-quad1pie.c
tst-quad2.c
tst-quad2pie.c
tst-quadmod1.S
tst-quadmod1pie.S
tst-quadmod2.S
tst-quadmod2pie.S
tst-rsi-strlen.c
tst-rsi-wcslen.c
tst-shstk-legacy-1-extra.S
tst-shstk-legacy-1a-static.c
tst-shstk-legacy-1a.c
tst-shstk-legacy-1b-static.c
tst-shstk-legacy-1b.c
tst-shstk-legacy-1c-static.c
tst-shstk-legacy-1c.c
tst-shstk-legacy-1d-static.c
tst-shstk-legacy-1d.c
tst-shstk-legacy-1e-static.c
tst-shstk-legacy-1e-static.sh
tst-shstk-legacy-1e.c
tst-shstk-legacy-1e.sh
tst-shstk-legacy-1f.c
tst-shstk-legacy-1g.c
tst-shstk-legacy-1g.sh
tst-shstk-legacy-mod-1.c
tst-split-dynreloc.c
tst-split-dynreloc.lds
tst-sse.c
tst-ssemod.c
varshift.c
Versions
wcpcpy-generic.c
wcpcpy.S
wcpncpy-generic.c
wcpncpy.S
wcscat-generic.c
wcscat.S
wcschr.S
wcscmp.S
wcscpy-generic.c
wcscpy.S
wcslen.S
wcsncat-generic.c
wcsncat.S
wcsncmp-generic.c
wcsncmp.S
wcsncpy-generic.c
wcsncpy.S
wcsnlen-generic.c
wcsnlen.S
wcsrchr.S
wmemchr.S
wmemcmp.S
wmemset_chk.S
wmemset.S
wordcopy.c
x86-lp_size.h