git: 1fb1008822c6 - stable/13 - forkpty: Avoid fd leak if fork() fails.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 07 Sep 2023 17:30:47 UTC
The branch stable/13 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=1fb1008822c64611535d3cb8f872b66a4e70acf1
commit 1fb1008822c64611535d3cb8f872b66a4e70acf1
Author: Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-08-17 13:48:42 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-09-07 17:26:58 +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
(cherry picked from commit a4aaee2120ce0a121f86e39e214c2fabe82f2762)
---
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 dcbcb0ab4461..e096021fbb76 100644
--- a/lib/libutil/tests/Makefile
+++ b/lib/libutil/tests/Makefile
@@ -5,6 +5,7 @@ TAP_TESTS_C+= humanize_number_test
TAP_TESTS_C+= pidfile_test
TAP_TESTS_C+= trimdomain_test
TAP_TESTS_C+= trimdomain-nodomain_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 <sys/resource.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <libutil.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+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());
+}