From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 1320 invoked from network); 30 Jun 2020 09:27:05 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 30 Jun 2020 09:27:05 -0000 Received: (qmail 20472 invoked by uid 550); 30 Jun 2020 09:27:00 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 20454 invoked from network); 30 Jun 2020 09:26:59 -0000 Date: Tue, 30 Jun 2020 05:26:46 -0400 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20200630092644.GE6430@brightrain.aerifal.cx> References: <0217b8838100175725993b0ed0114ee7@thelig.ht> <20200630044323.GD6430@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Potential deadlock in pthread_kill() On Mon, Jun 29, 2020 at 11:19:39PM -0700, Hydro Flask wrote: > >> int pthread_kill(pthread_t t, int sig) > >> { > >> int r; > >> LOCK(t->killlock); > >> r = t->tid ? -__syscall(SYS_tkill, t->tid, sig) > >> : (sig+0U >= _NSIG ? EINVAL : 0); > >> UNLOCK(t->killlock); > >> return r; > >> } > >> > >>Thank you for your attention. > > > >Thanks. It looks like this case was overlooked in the pthread_cancel > >fix that was commit 060ed9367337cbbd59a9e5e638a1c2f460192f25. The > >possibility of blocking signals was even mentioned there but deemed > >unnecessary. > > > >A simpler/lighter fix might be, before the lock, > > > > if (t==__pthread_self()) > > return -__syscall(SYS_tkill, t->tid, sig); > > > >since no lock is needed if targeting self; t->tid is necessarily valid > >in that case. > > Just to be clear, this doesn't only occur when calling > pthread_kill() and using pthread_self() as the target, it can be any > target thread, as long as it's the same target thread is used in the > signal handler and in the synchronous context. How so? If the target is different, the rest of the pthread_kill, including the unlock, will proceed concurrently with the signal handler. However you may be able to construct mutual-signaling deadlock cases. > Looking at the commit message you references, I think the only fix > for all cases is to block signals before taking the killlock. If This might be the case. > there is a way to avoid the killlock altogether that would also fix > it. Thanks again for confirming the issue. It can't be removed without replacing it with something else to synchronize against possible thread exit. Rich