From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13289 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH v2] fix race condition in file locking Date: Tue, 18 Sep 2018 11:55:08 -0400 Message-ID: <20180918155508.GV17995@brightrain.aerifal.cx> References: <20180918070327.32154-1-kaarle.ritvanen@datakunkku.fi> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1537286000 29667 195.159.176.226 (18 Sep 2018 15:53:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 18 Sep 2018 15:53:20 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-13305-gllmg-musl=m.gmane.org@lists.openwall.com Tue Sep 18 17:53:16 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1g2IJN-0007Uv-3D for gllmg-musl@m.gmane.org; Tue, 18 Sep 2018 17:53:13 +0200 Original-Received: (qmail 21515 invoked by uid 550); 18 Sep 2018 15:55:21 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 20469 invoked from network); 18 Sep 2018 15:55:21 -0000 Content-Disposition: inline In-Reply-To: <20180918070327.32154-1-kaarle.ritvanen@datakunkku.fi> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:13289 Archived-At: On Tue, Sep 18, 2018 at 10:03:27AM +0300, Kaarle Ritvanen wrote: > The condition occurs when > - thread #1 is holding the lock > - thread #2 is waiting for it on __futexwait > - thread #1 is about to release the lock and performs a_swap > - thread #3 enters the __lockfile function and manages to grab the lock > before thread #1 calls __wake, resetting the MAYBE_WAITERS flag > - thread #1 calls __wake > - thread #2 wakes up but goes again to __futexwait as the lock is > held by thread #3 > - thread #3 releases the lock but does not call __wake as the > MAYBE_WAITERS flag is not set > > This condition results in thread #2 not being woken up. This patch fixes > the problem by making the woken up thread ensure that the flag is > properly set before going to sleep again. > --- > src/stdio/__lockfile.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/src/stdio/__lockfile.c b/src/stdio/__lockfile.c > index 2ff75d8a..0dcb2a42 100644 > --- a/src/stdio/__lockfile.c > +++ b/src/stdio/__lockfile.c > @@ -8,13 +8,13 @@ int __lockfile(FILE *f) > int owner = f->lock, tid = __pthread_self()->tid; > if ((owner & ~MAYBE_WAITERS) == tid) > return 0; > - for (;;) { > - owner = a_cas(&f->lock, 0, tid); > - if (!owner) return 1; > - if (a_cas(&f->lock, owner, owner|MAYBE_WAITERS)==owner) break; > + owner = a_cas(&f->lock, 0, tid); > + if (!owner) return 1; > + while ((owner = a_cas(&f->lock, 0, tid|MAYBE_WAITERS))) { > + if ((owner & MAYBE_WAITERS) || > + a_cas(&f->lock, owner, owner|MAYBE_WAITERS)==owner) > + __futexwait(&f->lock, owner|MAYBE_WAITERS, 1); > } > - while ((owner = a_cas(&f->lock, 0, tid|MAYBE_WAITERS))) > - __futexwait(&f->lock, owner, 1); > return 1; > } > > -- > 2.14.4 Thanks! I'm applying this with a note added to the commit message about the source of the regression. I hit the same race today working on a test case for something else, so I can confirm it occurs in practice. Distros using musl 1.1.20 should apply this patch. Rich