NetBSD Problem Report #56424

From Clay.Mayers@kioxia.com  Thu Sep 30 00:09:05 2021
Return-Path: <Clay.Mayers@kioxia.com>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id AA37E1A921F
	for <gnats-bugs@gnats.NetBSD.org>; Thu, 30 Sep 2021 00:09:05 +0000 (UTC)
Message-Id: <a871ab88fe0344378f264bef28728f2a@kioxia.com>
Date: Wed, 29 Sep 2021 22:53:57 +0000
From: Clay Mayers <Clay.Mayers@kioxia.com>
Reply-To:
To: "gnats-bugs@NetBSD.org" <gnats-bugs@NetBSD.org>
Subject: recvfrom() is not a cancelation point

>Number:         56424
>Category:       lib
>Synopsis:       recvfrom() is not a cancelation point as documented in pthread_setcanceltype.3
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    lib-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 30 00:10:00 +0000 2021
>Closed-Date:    Wed Oct 20 08:01:51 +0000 2021
>Last-Modified:  Wed Oct 20 08:01:51 +0000 2021
>Originator:     Clay Mayers <Clay.Mayers@kioxia.com>
>Release:        
>Organization:
Kioxia

>Environment:
 System: NetBSD arm64 9.99.88 NetBSD 9.99.88 (GENERIC64) #0: Fri Aug 13 21:04:44 UTC 2021 mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/evbarm/compile/GENERIC64 evbarm
 Architecture: aarch64eb
 Machine: evbarm

>Description:
 When pthread cancel type is deferred and cancel is enabled, recvfrom() does not act like a
               cancelation point when entering or while executing.  If it is executing when pthread_cancel()
               is called, it returns -1 and sets errno to EINTR instead.  If pthread_cancel() was already called
               before recvfrom() is called, it will block.

               You can see this in the disassembly that recvfrom() is simply a svc #0x1d with no checks
               of TLS for being canceled.

 How-To-Repeat:

 #include <pthread.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <sys/socket.h>
 #include <errno.h>

 int gSock;

 void * reader(void *unused)
 {
    char buff[32];
    ssize_t ret;

    printf("Thread waiting for data\n");
    while (1)
    {
        // Fixed by adding a cancelation point.
        // pthread_testcancel();
        ret =3D recvfrom(gSock, buff, 16, 0, NULL, NULL);
        // recvfrom() returns -1/EINTR instead of canceling.
        if (ret =3D=3D -1 && errno =3D=3D EINTR)
           continue;
        break;
    }
    printf("reader exiting\n");
    return (void*) ret;
 }

 int main()
 {
     pthread_t read_thread;
     void *ret;

     gSock =3D socket(PF_LOCAL, SOCK_DGRAM, 0);
     if (gSock < 0)
     {
         printf("Socket system call failed\n");
         return 1;
     }

     pthread_create(&read_thread, NULL, reader, NULL);

     printf("sleeping 2\n");
     sleep(2);
     printf("cancelled %d\n", pthread_cancel(read_thread));
     printf("joined %d\n", pthread_join(read_thread,&ret));
     printf("ret %p\n", ret);
     return 0;
 }

 arm64# gcc -pthread -g -o testit t.c
 arm64# ./testit
 sleeping 2
 Thread waiting for data
 slept
 cancelled 0

 Uncomment call to pthread_testcancel() so there actually is a cancelation point
 In the loop and it works.

 arm64# ./testit
 sleeping 2
 Thread waiting for data
 slept
 cancelled 0
 joined 0
 ret 0x1

 Fix:
 recvfrom() likely needs to test cancel before and after the sys call like read() does.
                The work around is have a cancelation point before recvfrom() and when it sets errno to EINTR.

>How-To-Repeat:

>Fix:

Unknown
>Release-Note:

>Audit-Trail:

Responsible-Changed-From-To: gnats-admin->lib-bug-people
Responsible-Changed-By: spz@NetBSD.org
Responsible-Changed-When: Fri, 01 Oct 2021 05:37:29 +0000
Responsible-Changed-Why:
unmangled the submission and rescued it from pending


From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56424 CVS commit: src/lib
Date: Fri, 1 Oct 2021 13:13:44 -0400

 Module Name:	src
 Committed By:	christos
 Date:		Fri Oct  1 17:13:44 UTC 2021

 Modified Files:
 	src/lib/libc/sys: Makefile.inc
 	src/lib/libpthread: pthread_cancelstub.c

 Log Message:
 PR/56424: Clay Mayers: recvfrom() is not a cancelation point as documented
 in pthread_setcanceltype.3


 To generate a diff of this commit:
 cvs rdiff -u -r1.246 -r1.247 src/lib/libc/sys/Makefile.inc
 cvs rdiff -u -r1.38 -r1.39 src/lib/libpthread/pthread_cancelstub.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->feedback
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sun, 03 Oct 2021 22:16:42 +0000
State-Changed-Why:
Christos's change should have fixed it; please give it a try when you can to
make sure it works as expected.
(or if that's too much trouble, we can just close the report)


From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56424 CVS commit: [netbsd-9] src/lib
Date: Tue, 5 Oct 2021 11:02:18 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Tue Oct  5 11:02:18 UTC 2021

 Modified Files:
 	src/lib/libc/sys [netbsd-9]: Makefile.inc
 	src/lib/libpthread [netbsd-9]: pthread_cancelstub.c

 Log Message:
 Pull up following revision(s) (requested by christos in ticket #1355):

 	lib/libpthread/pthread_cancelstub.c: revision 1.39
 	lib/libc/sys/Makefile.inc: revision 1.247
 	lib/libpthread/pthread_cancelstub.c: revision 1.40

 PR/56424: Clay Mayers: recvfrom() is not a cancelation point as documented
 in pthread_setcanceltype.3

 remove parameter names from decls.


 To generate a diff of this commit:
 cvs rdiff -u -r1.240 -r1.240.2.1 src/lib/libc/sys/Makefile.inc
 cvs rdiff -u -r1.38 -r1.38.32.1 src/lib/libpthread/pthread_cancelstub.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Clay Mayers <Clay.Mayers@kioxia.com>
To: "gnats-bugs@netbsd.org" <gnats-bugs@netbsd.org>,
	"lib-bug-people@netbsd.org" <lib-bug-people@netbsd.org>,
	"netbsd-bugs@netbsd.org" <netbsd-bugs@netbsd.org>, "gnats-admin@netbsd.org"
	<gnats-admin@netbsd.org>
Cc: 
Subject: RE: lib/56424 (recvfrom() is not a cancelation point as documented in
 pthread_setcanceltype.3)
Date: Tue, 5 Oct 2021 22:36:07 +0000

 We have verified the bug is fixed on our x86 installation of NetBSD.

 > -----Original Message-----
 > From: dholland@NetBSD.org <dholland@NetBSD.org>
 > Sent: Sunday, October 3, 2021 3:17 PM
 > To: lib-bug-people@netbsd.org; netbsd-bugs@netbsd.org; gnats-
 > admin@netbsd.org; dholland@NetBSD.org; Clay Mayers
 > <Clay.Mayers@kioxia.com>
 > Subject: Re: lib/56424 (recvfrom() is not a cancelation point as document=
 ed
 > in pthread_setcanceltype.3)
 >=20
 > Synopsis: recvfrom() is not a cancelation point as documented in
 > pthread_setcanceltype.3
 >=20
 > State-Changed-From-To: open->feedback
 > State-Changed-By: dholland@NetBSD.org
 > State-Changed-When: Sun, 03 Oct 2021 22:16:42 +0000
 > State-Changed-Why:
 > Christos's change should have fixed it; please give it a try when you can=
  to
 > make sure it works as expected.
 > (or if that's too much trouble, we can just close the report)
 >=20

State-Changed-From-To: feedback->closed
State-Changed-By: spz@NetBSD.org
State-Changed-When: Wed, 20 Oct 2021 08:01:51 +0000
State-Changed-Why:
verified fixed


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.