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 3225 invoked from network); 30 Jun 2020 04:43:39 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 30 Jun 2020 04:43:39 -0000 Received: (qmail 28339 invoked by uid 550); 30 Jun 2020 04:43:37 -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 28316 invoked from network); 30 Jun 2020 04:43:36 -0000 Date: Tue, 30 Jun 2020 00:43:23 -0400 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20200630044323.GD6430@brightrain.aerifal.cx> References: <0217b8838100175725993b0ed0114ee7@thelig.ht> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0217b8838100175725993b0ed0114ee7@thelig.ht> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Potential deadlock in pthread_kill() On Mon, Jun 29, 2020 at 09:19:08PM -0700, Hydro Flask wrote: > Hello all, > > Noticed something while reading some code today. pthread_kill() is > specified by POSIX to be async signal safe but I noticed that in > musl's implementation if a signal occurs while the "killlock" is > held and the signal handler calls pthread_kill() on the same target > thread, a deadlock will occur. Is this intentional? > > 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. One concern I just had was interaction with fork (also a nasty AS-safe function), but if fork is called from a signal handler during pthread_kill, it's no different from the signal handler running just before pthread_kill; the result is targeting an invalid (in the child) pthread_t, which thereby has undefined behavior. So, while ugly, I think this is ok. Note that raise() *does* need to block signals here, because there is no explicit pthread_t argument and thus the interaction with fork is well-defined. Rich