public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bugdal at aerifal dot cx" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug nptl/12683] Race conditions in pthread cancellation
Date: Sun, 12 Jan 2014 23:55:00 -0000	[thread overview]
Message-ID: <bug-12683-131-2CLDJzFSnY@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-12683-131@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=12683

--- Comment #14 from Rich Felker <bugdal at aerifal dot cx> ---
>From XSH 2.9.5 Thread Cancellation:

"The side-effects of acting upon a cancellation request while suspended during
a call of a function are the same as the side-effects that may be seen in a
single-threaded program when a call to a function is interrupted by a signal
and the given function returns [EINTR]. Any such side-effects occur before any
cancellation cleanup handlers are called."

This is the important paragraph. By requiring that the side-effects on
cancellation match the side effects on EINTR, the standard requires that
cancellation cannot be acted upon if other irreversible side effects have
already taken place. For example, if a file descriptor has been closed, data
transferred, etc. then cancellation can't happen. The following paragraph
explains further:

"Whenever a thread has cancelability enabled and a cancellation request has
been made with that thread as the target, and the thread then calls any
function that is a cancellation point (such as pthread_testcancel() or read()),
the cancellation request shall be acted upon before the function returns."

This is simple. If cancellation is already pending when a cancellation point is
called, it must be acted upon. The next part is less clear:

"If a thread has cancelability enabled and a cancellation request is made with
the thread as a target while the thread is suspended at a cancellation point,
the thread shall be awakened and the cancellation request shall be acted upon.
It is unspecified whether the cancellation request is acted upon or whether the
cancellation request remains pending and the thread resumes normal execution
if:

* The thread is suspended at a cancellation point and the event for which it is
waiting occurs

* A specified timeout expired

before the cancellation request is acted upon."

This is covering the case of blocking syscalls. If a cancellation request
arrives during a blocking syscall, it's normally acted upon, but there's one
race condition being described: it's possible that the "event being waited for"
arrives just before the cancellation request arrives, but before the target
thread unblocks. In this case, it's implementation-defined whether cancellation
is acted upon (in which case, by the first paragraph, the event remains
pending) or the event is acted upon (in which case the cancellation request
remains pending). The reason for there being two bullet points above is that
some blocking syscalls wait for either an event or a timeout (think of
sem_timedwait or recv with a timeout set by setsockopt), and in that case, the
timeout can also be 'consumed' and cause the cancellation to remain pending.

Anyway, this race condition is the whole matter at hand here. The two
possibilities allowed (again, due to the limitations imposed by the first
paragrah) are acting on the event while leaving cancellation pending, or acting
on cancellation while leaving the event pending. But glibc also has a race
window where it can act on both the event, producing side effects (because the
kernel already has), and act on cancellation. This makes it non-conforming and
makes it impossible to use cancellation safely.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


  parent reply	other threads:[~2014-01-12 23:55 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-18 22:28 [Bug nptl/12683] New: " bugdal at aerifal dot cx
2011-04-18 22:35 ` [Bug nptl/12683] " bugdal at aerifal dot cx
2011-09-21 18:30 ` bugdal at aerifal dot cx
2012-04-29  2:56 ` bugdal at aerifal dot cx
2012-04-29  2:57 ` bugdal at aerifal dot cx
2012-09-22 23:13 ` bugdal at aerifal dot cx
2013-08-16 15:32 ` carlos at redhat dot com
2013-08-16 15:34 ` carlos at redhat dot com
2013-08-16 15:34 ` carlos at redhat dot com
2013-08-16 16:22 ` bugdal at aerifal dot cx
2013-08-16 16:59 ` carlos at redhat dot com
2013-08-16 17:14 ` bugdal at aerifal dot cx
2013-08-16 18:09 ` carlos at redhat dot com
2014-01-10 20:25 ` carlos at redhat dot com
2014-01-10 21:31 ` carlos at redhat dot com
2014-01-10 22:37 ` bugdal at aerifal dot cx
2014-01-12 18:31 ` carlos at redhat dot com
2014-01-12 23:55 ` bugdal at aerifal dot cx [this message]
2014-01-13  1:52 ` carlos at redhat dot com
2014-01-13  4:37 ` bugdal at aerifal dot cx
2014-01-14 14:51 ` carlos at redhat dot com
2014-02-16 19:42 ` jackie.rosen at hushmail dot com
2014-05-28 19:47 ` schwab at sourceware dot org
2014-05-28 19:47 ` schwab at sourceware dot org
2014-06-27 13:35 ` fweimer at redhat dot com
2014-07-19 18:44 ` sstewartgallus00 at mylangara dot bc.ca
2014-07-19 18:54 ` bugdal at aerifal dot cx
2014-07-20 18:15 ` sstewartgallus00 at mylangara dot bc.ca
2014-07-20 18:41 ` bugdal at aerifal dot cx
2014-08-19 14:08 ` azanella at linux dot vnet.ibm.com
2014-08-28 15:02 ` carlos at redhat dot com
2015-01-15 13:20 ` dan at censornet dot com
2015-01-15 13:31 ` bugdal at aerifal dot cx
2015-01-15 14:01 ` dan at censornet dot com
2020-06-08 14:04 ` fweimer at redhat dot com

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=bug-12683-131-2CLDJzFSnY@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@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).