Go to file
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
advisories Document CVE-2024-33599, CVE-2024-33600, CVE-2024-33601, CVE-2024-33602 2024-05-06 15:12:31 -04:00
argp Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
assert assert: Mark __assert_fail as cold 2024-07-26 20:41:00 +08:00
benchtests benchtests: Add random memset benchmark 2024-08-07 14:58:46 +01:00
bits socket: Use may_alias on sockaddr structs (bug 19622) 2024-05-18 09:33:19 +02:00
catgets Fix conditionals on mtrace-based tests (bug 31892) 2024-07-01 17:20:30 +02:00
ChangeLog.old Add ChangeLog file 2024-07-21 18:33:37 +02:00
conform conform: Reformat Makefile. 2024-02-25 13:38:16 -05:00
csu Add crt1-2.0.o for glibc 2.0 compatibility tests 2024-05-06 07:49:40 -07:00
ctype ctype: Reformat Makefile. 2024-02-25 13:38:16 -05:00
debug support: Add FAIL test failure helper 2024-07-26 13:21:34 +01:00
dirent powerpc: Remove duplicated versionsort from libm.a (BZ 31789) 2024-05-23 13:41:29 -03:00
dlfcn dlfcn: Reformat Makefile. 2024-02-25 13:38:16 -05:00
elf nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
gmon Define write_profiling functions only in profile library [BZ #31756] 2024-05-22 06:12:55 -07:00
gnulib Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
hesiod hesiod: Reformat Makefile. 2024-02-25 13:38:16 -05:00
htl hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
hurd x86_64 hurd: ensure we have a large enough buffer to receive exception_raise requests. 2024-07-30 16:59:12 +02:00
iconv iconv: Fix matching of multi-character transliterations (bug 31859) 2024-08-05 16:49:57 +02:00
iconvdata iconv: ISO-2022-CN-EXT: fix out-of-bound writes when writing escape sequence (CVE-2024-2961) 2024-04-17 09:21:40 -03:00
include Define __libc_initial for the static libc 2024-08-09 16:17:14 +02:00
inet Revert "inet: Avoid label at end of compound statement in tst-if_nameindex" 2024-08-21 20:06:33 +02:00
intl Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
io Test mkdirat use of mode argument 2024-08-22 11:25:14 +00:00
libio ungetc: Fix backup buffer leak on program exit [BZ #27821] 2024-08-15 13:56:13 -04:00
locale support: Use macros for *stat wrappers 2024-08-16 16:05:20 +02:00
localedata support: Use macros for *stat wrappers 2024-08-16 16:05:20 +02:00
login login: Re-flow and sort multiline Makefile definitions 2024-08-07 11:02:03 -03:00
mach mach: Drop some unnecessary vm_param.h includes 2024-01-03 21:59:54 +01:00
malloc malloc: Link threading tests with $(shared-thread-library) 2024-08-20 16:16:25 +02:00
manual ungetc: Fix uninitialized read when putting into unused streams [BZ #27821] 2024-08-15 13:55:07 -04:00
math added inputs giving large errors on x86_64 for new C23 functions 2024-08-07 14:28:46 +02:00
mathvec Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
misc Add mremap tests 2024-08-01 05:06:12 -07:00
nis Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
nptl nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
nptl_db Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
nscd nscd: Use time_t for return type of addgetnetgrentX 2024-05-02 18:59:29 +02:00
nss nss: Fix incorrect switch fall-through in tst-nss-gai-actions 2024-08-07 15:00:25 +02:00
po po/*: regenerate (only line number changes) 2024-07-21 17:50:35 +02:00
posix support: Use macros for *stat wrappers 2024-08-16 16:05:20 +02:00
resolv resolv: Fix tst-resolv-short-response for older GCC (bug 32042) 2024-08-01 21:07:48 +02:00
resource Always define __USE_TIME_BITS64 when 64 bit time_t is used 2024-04-02 15:28:36 -03:00
rt debug: Fix clang mq_open fortify wrapper (BZ 31917) 2024-06-27 13:32:48 -03:00
scripts Use binutils 2.43 branch in build-many-glibcs.py 2024-08-06 17:00:50 +00:00
setjmp Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
signal signal/Makefile: Split and sort tests 2024-07-01 13:47:27 +02:00
socket Fix name space violation in fortify wrappers (bug 32052) 2024-08-05 16:49:58 +02:00
soft-fp soft-fp: Add brain format support 2024-02-01 19:06:54 +01:00
stdio-common Add more tests of getline 2024-08-21 19:58:14 +00:00
stdlib support: Use macros for *stat wrappers 2024-08-16 16:05:20 +02:00
string string: strerror, strsignal cannot use buffer after dlmopen (bug 32026) 2024-08-19 15:48:03 +02:00
sunrpc Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
support support: Use macros for *stat wrappers 2024-08-16 16:05:20 +02:00
sysdeps nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
sysvipc Always define __USE_TIME_BITS64 when 64 bit time_t is used 2024-04-02 15:28:36 -03:00
termios Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
time time/Makefile: Split and sort tests 2024-07-12 17:33:28 +02:00
timezone timezone: sync to TZDB 2024a 2024-04-07 13:35:48 -07:00
wcsmbs Fix name space violation in fortify wrappers (bug 32052) 2024-08-05 16:49:58 +02:00
wctype Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
.clang-format Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
.gitattributes Assume __NR_openat is always defined 2016-03-23 23:35:08 +01:00
.gitignore Add *.pyc to .gitignore 2015-05-18 15:26:26 +05:30
abi-tags Remove the bulk of the NaCl port. 2017-05-20 08:09:10 -04:00
aclocal.m4 Convert to autoconf 2.72 (vanilla release, no distribution patches) 2024-06-17 21:15:28 +02:00
config.h.in Add pthread_getname_np and pthread_setname_np for Hurd 2024-07-16 09:21:52 +02:00
config.make.in manual: add syscalls 2024-07-09 11:54:29 +02:00
configure Turn on -Wimplicit-fallthrough by default if available 2024-08-09 15:34:53 +02:00
configure.ac Turn on -Wimplicit-fallthrough by default if available 2024-08-09 15:34:53 +02:00
CONTRIBUTED-BY crypt: Remove libcrypt support 2023-10-30 13:03:59 -03:00
COPYING Update to latest versions of GPL-2.0 and LGPL-2.1 2013-09-09 12:52:48 +10:00
COPYING.LIB Update to latest versions of GPL-2.0 and LGPL-2.1 2013-09-09 12:52:48 +10:00
extra-lib.mk Rename cppflags-iterator.mk to libof-iterator.mk, remove extra-modules.mk. 2017-05-09 07:06:29 -04:00
gen-locales.mk locale: Handle loading a missing locale twice (Bug 14247) 2024-04-22 16:03:00 -04:00
INSTALL install.texi: bump "latest verified" versions 2024-07-21 00:27:35 +02:00
libc-abis riscv: support GNU indirect function 2021-01-10 21:25:13 -05:00
libof-iterator.mk Rename cppflags-iterator.mk to libof-iterator.mk, remove extra-modules.mk. 2017-05-09 07:06:29 -04:00
LICENSES Relicense IBM portions of resolv/base64.c resolv/res_debug.c. 2024-01-26 13:33:36 -05:00
MAINTAINERS Add MAINTAINERS 2017-05-11 13:38:30 -04:00
Makeconfig Turn on -Wimplicit-fallthrough by default if available 2024-08-09 15:34:53 +02:00
Makefile Pass -nostdlib -nostartfiles together with -r [BZ #31753] 2024-05-19 16:29:02 -07:00
Makefile.help math: Add support for auto static math tests 2024-05-21 16:53:27 -03:00
Makefile.in New make target to only build benchmark binaries 2016-04-20 10:23:28 +05:30
Makerules Support compiling .S files with additional options 2024-02-25 09:22:40 -08:00
NEWS resolv: Implement strict-error stub resolver option (bug 27929) 2024-07-24 12:10:37 +02:00
o-iterator.mk
README Remove ia64-linux-gnu 2024-01-08 17:09:36 -03:00
Rules Rules: Also build memcheck tests even when not running them 2024-08-20 16:23:03 +02:00
SECURITY.md Adapt the security policy for the security page 2023-12-05 09:15:10 -05:00
SHARED-FILES localedata: Fix several issues with the set of characters considered 0-width [BZ #31370] 2024-05-15 14:31:06 +02:00
shlib-versions crypt: Remove libcrypt support 2023-10-30 13:03:59 -03:00
test-skeleton.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
version.h Increase version number to 2.40.9000 2024-07-21 18:49:35 +02:00

This directory contains the sources of the GNU C Library.
See the file "version.h" for what release version you have.

The GNU C Library is the standard system C library for all GNU systems,
and is an important part of what makes up a GNU system.  It provides the
system API for all programs written in C and C-compatible languages such
as C++ and Objective C; the runtime facilities of other programming
languages use the C library to access the underlying operating system.

In GNU/Linux systems, the C library works with the Linux kernel to
implement the operating system behavior seen by user applications.
In GNU/Hurd systems, it works with a microkernel and Hurd servers.

The GNU C Library implements much of the POSIX.1 functionality in the
GNU/Hurd system, using configurations i[4567]86-*-gnu and x86_64-gnu.

When working with Linux kernels, this version of the GNU C Library
requires Linux kernel version 3.2 or later.

Also note that the shared version of the libgcc_s library must be
installed for the pthread library to work correctly.

The GNU C Library supports these configurations for using Linux kernels:

	aarch64*-*-linux-gnu
	alpha*-*-linux-gnu
	arc*-*-linux-gnu
	arm-*-linux-gnueabi
	csky-*-linux-gnuabiv2
	hppa-*-linux-gnu
	i[4567]86-*-linux-gnu
	x86_64-*-linux-gnu	Can build either x86_64 or x32
	loongarch64-*-linux-gnu Hardware floating point, LE only.
	m68k-*-linux-gnu
	microblaze*-*-linux-gnu
	mips-*-linux-gnu
	mips64-*-linux-gnu
	or1k-*-linux-gnu
	powerpc-*-linux-gnu	Hardware or software floating point, BE only.
	powerpc64*-*-linux-gnu	Big-endian and little-endian.
	s390-*-linux-gnu
	s390x-*-linux-gnu
	riscv32-*-linux-gnu
	riscv64-*-linux-gnu
	sh[34]-*-linux-gnu
	sparc*-*-linux-gnu
	sparc64*-*-linux-gnu

If you are interested in doing a port, please contact the glibc
maintainers; see https://www.gnu.org/software/libc/ for more
information.

See the file INSTALL to find out how to configure, build, and install
the GNU C Library.  You might also consider reading the WWW pages for
the C library at https://www.gnu.org/software/libc/.

The GNU C Library is (almost) completely documented by the Texinfo manual
found in the `manual/' subdirectory.  The manual is still being updated
and contains some known errors and omissions; we regret that we do not
have the resources to work on the manual as much as we would like.  For
corrections to the manual, please file a bug in the `manual' component,
following the bug-reporting instructions below.  Please be sure to check
the manual in the current development sources to see if your problem has
already been corrected.

Please see https://www.gnu.org/software/libc/bugs.html for bug reporting
information.  We are now using the Bugzilla system to track all bug reports.
This web page gives detailed information on how to report bugs properly.

The GNU C Library is free software.  See the file COPYING.LIB for copying
conditions, and LICENSES for notices about a few contributions that require
these additional notices to be distributed.  License copyright years may be
listed using range notation, e.g., 1996-2015, indicating that every year in
the range, inclusive, is a copyrightable year that would otherwise be listed
individually.