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 @@ -1,114 +1,115 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1990, 1993, 1994 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __SCCSID("@(#)pty.c 8.3 (Berkeley) 5/16/94"); #include #include #include #include #include #include #include #include #include #include #include int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp) { const char *slavename; int master, slave; master = posix_openpt(O_RDWR|O_NOCTTY); if (master == -1) return (-1); if (grantpt(master) == -1) goto bad; if (unlockpt(master) == -1) goto bad; slavename = ptsname(master); if (slavename == NULL) goto bad; slave = open(slavename, O_RDWR); if (slave == -1) goto bad; *amaster = master; *aslave = slave; if (name) strcpy(name, slavename); if (termp) tcsetattr(slave, TCSAFLUSH, termp); if (winp) ioctl(slave, TIOCSWINSZ, (char *)winp); return (0); bad: close(master); return (-1); } int forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) { int master, slave, pid; if (openpty(&master, &slave, name, termp, winp) == -1) return (-1); switch (pid = fork()) { case -1: + (void)close(master); (void)close(slave); return (-1); case 0: /* * child */ (void)close(master); login_tty(slave); return (0); } /* * parent */ *amaster = master; (void) close(slave); return (pid); } 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 @@ -1,12 +1,13 @@ TAP_TESTS_C+= flopen_test TAP_TESTS_C+= grp_test 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 .include 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()); +}