From owner-svn-src-head@freebsd.org Mon Apr 13 17:55:32 2020 Return-Path: Delivered-To: svn-src-head@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 138D52C4E5E; Mon, 13 Apr 2020 17:55:32 +0000 (UTC) (envelope-from kevans@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) server-signature RSA-PSS (4096 bits) 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 491GVq6qmYz401C; Mon, 13 Apr 2020 17:55:31 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E5A6B1F5E4; Mon, 13 Apr 2020 17:55:31 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 03DHtVrr049555; Mon, 13 Apr 2020 17:55:31 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 03DHtVAC049554; Mon, 13 Apr 2020 17:55:31 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <202004131755.03DHtVAC049554@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Mon, 13 Apr 2020 17:55:31 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r359891 - in head: sys/kern tests/sys/file X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: in head: sys/kern tests/sys/file X-SVN-Commit-Revision: 359891 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Apr 2020 17:55:32 -0000 Author: kevans Date: Mon Apr 13 17:55:31 2020 New Revision: 359891 URL: https://svnweb.freebsd.org/changeset/base/359891 Log: close_range/closefrom: fix regression from close_range introduction close_range will clamp the range between [0, fdp->fd_lastfile], but failed to take into account that fdp->fd_lastfile can become -1 if all fds are closed. =-( In this scenario, just return because there's nothing further we can do at the moment. Add a test case for this, fork() and simply closefrom(0) twice in the child; on the second invocation, fdp->fd_lastfile == -1 and will trigger a panic before this change. X-MFC-With: r359836 Modified: head/sys/kern/kern_descrip.c head/tests/sys/file/closefrom_test.c Modified: head/sys/kern/kern_descrip.c ============================================================================== --- head/sys/kern/kern_descrip.c Mon Apr 13 16:34:21 2020 (r359890) +++ head/sys/kern/kern_descrip.c Mon Apr 13 17:55:31 2020 (r359891) @@ -1333,6 +1333,14 @@ kern_close_range(struct thread *td, u_int lowfd, u_int ret = EINVAL; goto out; } + + /* + * If fdp->fd_lastfile == -1, we're dealing with either a fresh file + * table or one in which every fd has been closed. Just return + * successful; there's nothing left to do. + */ + if (fdp->fd_lastfile == -1) + goto out; /* Clamped to [lowfd, fd_lastfile] */ highfd = MIN(highfd, fdp->fd_lastfile); for (fd = lowfd; fd <= highfd; fd++) { Modified: head/tests/sys/file/closefrom_test.c ============================================================================== --- head/tests/sys/file/closefrom_test.c Mon Apr 13 16:34:21 2020 (r359890) +++ head/tests/sys/file/closefrom_test.c Mon Apr 13 17:55:31 2020 (r359891) @@ -146,7 +146,7 @@ main(void) pid_t pid; int fd, i, start; - printf("1..19\n"); + printf("1..20\n"); /* We better start up with fd's 0, 1, and 2 open. */ start = devnull(); @@ -308,6 +308,22 @@ main(void) if (fd != 3) fail("close_range", "highest fd %d", fd); ok("close_range"); + + /* Fork a child process to test closefrom(0) twice. */ + pid = fork(); + if (pid < 0) + fail_err("fork"); + if (pid == 0) { + /* Child. */ + closefrom(0); + closefrom(0); + cok(info, "closefrom(0)"); + } + if (wait(NULL) < 0) + fail_err("wait"); + if (info->failed) + fail(info->tag, "%s", info->message); + ok(info->tag); return (0); }