From nobody Thu Aug 17 13:49:10 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RRRF24br6z4qhH1; Thu, 17 Aug 2023 13:49:10 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RRRF23swMz4lN6; Thu, 17 Aug 2023 13:49:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692280150; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rqKx0S6wZU8L/mUTx+xjKKnAr1nj2sZg066hre6/lEA=; b=MrKBlCA4jjBH4UJWsp4vI7qHLw5ose72QCZ8QFStAwsVYng9fh2s6lkBPRQFHU+a0ClwLQ EWaBuMnrgt4N/sFDvpicqh36lm8QgFLJhvS9m1S4XBcYLhFRbngQbi62VYuSVe2PS/IAV8 D4NHbKgilmKNji+KY5ImGRpBtaPBXZdMpz0+4dWxSzcbph97UVg9/+E39Nr8wkwSaViQau dj7J3gXlcoAU7DmGGgHgna9Zuy6XwNWdkEztH/T5XuSYBLI5OTPuJ0wc09wPphMiYatdLq HkYRQsE82oQpr8LwPnlo17eKTB0ffN2syqNYLy7ki3b13RWGTtS0fO79hY/+kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692280150; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rqKx0S6wZU8L/mUTx+xjKKnAr1nj2sZg066hre6/lEA=; b=Yo3hyl4Mw74z1UHB/RyN4mPZNDS4F47tTVKCM1jPu+JVnA0cBu/rxd3x/MFNkxYNWgy9DX OTBJme9K6DU5zJzuQ5VO3dDzoE3SaH3rWMuJRLg/wMjvll0EHX34jpL4QViClKlcDGv5pd whDaGx4w2FwomlRC3PRIPLd7IS/I0M0RhedmDS+ciA0OEALmGUSCQqSjOI4j5Q4snD1V9P 9oNkX+PELFcRoKExARX4isk+Ft4v8B5K8RCzIimaQ2b7tCvTpB9mXqsq5aq6z5Xy5odugV 5kEMCg7/2rZIWEEWFx7/YIuqwerdkky3qJohxF5yzamZ8iLyU9dvH4qMozCqJA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1692280150; a=rsa-sha256; cv=none; b=KQOnidhlu62HFE9Lmy2ApFkIRscuO8+BzlmmLDLva5RAfaeTJlRsyPLb+SDWjtTE+Bn5+R 8Xne6pyK6abDr+sUkokDpU1BFQtlZc0l62OOz6GQGNDx9ImQk6FzxEYhHTJvYVv0Ru53z7 ailouzT+Y/OuKY3lW/IcDbgPfylG8r9IwydvWb9sUBOlkxgOd5qf2V/ZkU7hbMW64hZBis 6wCd9nBih5nvywbQg2RXcIlWEL3Kl4trMD8N1MMYuZWkaAk82J9sQUEsYALKvHltQiK7Kg rh/C0EpsssPOLxBFmqLCzmAGW0CQsR1oWWNS4LfuFeuzy7j5XPUVEnORr3yn+w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 4RRRF22dq4zblm; Thu, 17 Aug 2023 13:49:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37HDnAKB063887; Thu, 17 Aug 2023 13:49:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37HDnAq7063884; Thu, 17 Aug 2023 13:49:10 GMT (envelope-from git) Date: Thu, 17 Aug 2023 13:49:10 GMT Message-Id: <202308171349.37HDnAq7063884@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: a4aaee2120ce - main - forkpty: Avoid fd leak if fork() fails. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a4aaee2120ce0a121f86e39e214c2fabe82f2762 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=a4aaee2120ce0a121f86e39e214c2fabe82f2762 commit a4aaee2120ce0a121f86e39e214c2fabe82f2762 Author: Dag-Erling Smørgrav AuthorDate: 2023-08-17 13:48:42 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-08-17 13:48:42 +0000 forkpty: Avoid fd leak if fork() fails. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D41491 --- lib/libutil/pty.c | 1 + lib/libutil/tests/Makefile | 1 + lib/libutil/tests/forkpty_test.c | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/lib/libutil/pty.c b/lib/libutil/pty.c index f52407608e9a..e5b42a666c7f 100644 --- a/lib/libutil/pty.c +++ b/lib/libutil/pty.c @@ -95,6 +95,7 @@ forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) return (-1); switch (pid = fork()) { case -1: + (void)close(master); (void)close(slave); return (-1); case 0: diff --git a/lib/libutil/tests/Makefile b/lib/libutil/tests/Makefile index 0c67747aeb3f..d29045d78a10 100644 --- a/lib/libutil/tests/Makefile +++ b/lib/libutil/tests/Makefile @@ -7,6 +7,7 @@ TAP_TESTS_C+= trimdomain_test TAP_TESTS_C+= trimdomain-nodomain_test ATF_TESTS_C+= cpuset_test ATF_TESTS_C+= expand_number_test +ATF_TESTS_C+= forkpty_test WARNS?= 2 LIBADD+= util diff --git a/lib/libutil/tests/forkpty_test.c b/lib/libutil/tests/forkpty_test.c new file mode 100644 index 000000000000..3e54cf310150 --- /dev/null +++ b/lib/libutil/tests/forkpty_test.c @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2023 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#include +#include +#include + +#include + +ATF_TC(forkfail); +ATF_TC_HEAD(forkfail, tc) +{ + atf_tc_set_md_var(tc, "descr", "Check for fd leak when fork() fails"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} + +ATF_TC_BODY(forkfail, tc) +{ + struct rlimit orl, nrl; + pid_t pid; + int prevfd, fd, pty; + + /* set process limit to 1 so fork() will fail */ + ATF_REQUIRE_EQ(0, getrlimit(RLIMIT_NPROC, &orl)); + nrl = orl; + nrl.rlim_cur = 1; + ATF_REQUIRE_EQ(0, setrlimit(RLIMIT_NPROC, &nrl)); + /* check first free fd */ + ATF_REQUIRE((fd = dup(0)) > 0); + ATF_REQUIRE_EQ(0, close(fd)); + /* attempt forkpty() */ + pid = forkpty(&pty, NULL, NULL, NULL); + if (pid == 0) { + /* child - fork() unexpectedly succeeded */ + _exit(0); + } + ATF_CHECK_ERRNO(EAGAIN, pid < 0); + if (pid > 0) { + /* parent - fork() unexpectedly succeeded */ + (void)waitpid(pid, NULL, 0); + } + /* check that first free fd hasn't changed */ + prevfd = fd; + ATF_REQUIRE((fd = dup(0)) > 0); + ATF_CHECK_EQ(prevfd, fd); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, forkfail); + return (atf_no_error()); +}