From owner-freebsd-bugs@freebsd.org Wed Nov 18 01:18:09 2020 Return-Path: Delivered-To: freebsd-bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 55CFB474B52 for ; Wed, 18 Nov 2020 01:18:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mailman.nyi.freebsd.org (mailman.nyi.freebsd.org [IPv6:2610:1c1:1:606c::50:13]) by mx1.freebsd.org (Postfix) with ESMTP id 4CbQ0x1pXYz4SLF for ; Wed, 18 Nov 2020 01:18:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: by mailman.nyi.freebsd.org (Postfix) id 3DFD8474B51; Wed, 18 Nov 2020 01:18:09 +0000 (UTC) Delivered-To: bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3DC50474D3E for ; Wed, 18 Nov 2020 01:18:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CbQ0x1DYWz4S28 for ; Wed, 18 Nov 2020 01:18:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1D4B7164A for ; Wed, 18 Nov 2020 01:18:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 0AI1I98A010170 for ; Wed, 18 Nov 2020 01:18:09 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 0AI1I8Vg010169 for bugs@FreeBSD.org; Wed, 18 Nov 2020 01:18:08 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 251227] setpgid sometimes returns ESRCH instead of EACCES Date: Wed, 18 Nov 2020 01:18:09 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: 12.2-RELEASE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Many People X-Bugzilla-Who: mqudsi@neosmart.net X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Nov 2020 01:18:09 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D251227 Bug ID: 251227 Summary: setpgid sometimes returns ESRCH instead of EACCES Product: Base System Version: 12.2-RELEASE Hardware: Any OS: Any Status: New Severity: Affects Many People Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: mqudsi@neosmart.net setpgid(2) says > [ESRCH] The requested process does not exist. > [ESRCH] The target process is not the calling process or a > child of the calling process. > [EACCES] The requested process is a child of the calling > process, but it has performed an exec(3) operatio= n. However, as demonstrated by the following test, FreeBSD is incorrectly returning ESRCH when setpgid(2) is called on a child that has called both fork(2) and execv(3) by the time the parent calls setpgid(2). ``` #include #include #include #include int main() { pid_t pid =3D vfork(); if (pid =3D=3D 0) { // Child process char * const argv[] =3D { "env", "true", NULL }; execv("/usr/bin/env", argv); assert(0 && "child returned after execv!"); } // Parent process sleep(1); int result =3D setpgid(pid, pid); assert(result !=3D 0); printf("error code: %d\n", errno); assert(errno =3D=3D EACCES && "incorrect error code returned!"); return 0; } ``` The test above fails on FreeBSD 11.2 and FreeBSD 12.2; it passes on Linux. The underlying issue appears to be some sort of race condition: the implementation of sys_setpgid in sys/kern/kern_prot.c correctly checks for = the P_EXEC flag and sets errno to EACCES if present. You'll notice the use of vfork(2) in the test above, which, though not guaranteed, in practice blocks execution of the parent process until exec(3) has been called in the child. If the sleep(1) call is omitted from the test, the test passes (EACCES is returned because execv(3) has been called by the child process). With or without the call to sleep(1), the unreaped child process remains present until the parent terminates (i.e. the referenced ta= rget pid does exist). Fundamentally, the behavior of the parent (and in particul= ar, the return code from its call to setpgid) should be the same both with and without the call to sleep(1), given the semantics of vfork(3). (The behavior does not change if /usr/bin/true is spawned directly instead = of via /usr/bin/env, i.e. this is not an issue with child vs grandchild.) --=20 You are receiving this mail because: You are the assignee for the bug.=