From owner-svn-src-all@freebsd.org Sat Jan 27 18:24:13 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id ADE8EED0033; Sat, 27 Jan 2018 18:24:13 +0000 (UTC) (envelope-from pfg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6382773EEB; Sat, 27 Jan 2018 18:24:13 +0000 (UTC) (envelope-from pfg@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 5E6CC63F8; Sat, 27 Jan 2018 18:24:13 +0000 (UTC) (envelope-from pfg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w0RIODcc042565; Sat, 27 Jan 2018 18:24:13 GMT (envelope-from pfg@FreeBSD.org) Received: (from pfg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w0RIODQc042564; Sat, 27 Jan 2018 18:24:13 GMT (envelope-from pfg@FreeBSD.org) Message-Id: <201801271824.w0RIODQc042564@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: pfg set sender to pfg@FreeBSD.org using -f From: "Pedro F. Giffuni" Date: Sat, 27 Jan 2018 18:24:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r328487 - head/bin/pax X-SVN-Group: head X-SVN-Commit-Author: pfg X-SVN-Commit-Paths: head/bin/pax X-SVN-Commit-Revision: 328487 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Jan 2018 18:24:13 -0000 Author: pfg Date: Sat Jan 27 18:24:13 2018 New Revision: 328487 URL: https://svnweb.freebsd.org/changeset/base/328487 Log: pax(1): Honour the restrict in sigaction(). Use a setup_sig() helper and make it fail when either of sigaction fails. While there, do not leak fds for "." + minor cleanup. Obtained from: OpenBSD (through DragonFly git eca362d0f9bd086cc56d6b5bc4f03f09e040b9db) Modified: head/bin/pax/pax.c Modified: head/bin/pax/pax.c ============================================================================== --- head/bin/pax/pax.c Sat Jan 27 17:43:09 2018 (r328486) +++ head/bin/pax/pax.c Sat Jan 27 18:24:13 2018 (r328487) @@ -109,7 +109,7 @@ char *tempbase; /* basename of tempfile to use for mk /* * PAX - Portable Archive Interchange * - * A utility to read, write, and write lists of the members of archive + * A utility to read, write, and write lists of the members of archive * files and copy directory hierarchies. A variety of archive formats * are supported (some are described in POSIX 1003.1 10.1): * @@ -237,7 +237,7 @@ main(int argc, char *argv[]) /* * Keep a reference to cwd, so we can always come back home. */ - cwdfd = open(".", O_RDONLY); + cwdfd = open(".", O_RDONLY | O_CLOEXEC); if (cwdfd < 0) { syswarn(0, errno, "Can't open current working directory."); return(exit_val); @@ -249,7 +249,7 @@ main(int argc, char *argv[]) if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') tmpdir = _PATH_TMP; tdlen = strlen(tmpdir); - while(tdlen > 0 && tmpdir[tdlen - 1] == '/') + while (tdlen > 0 && tmpdir[tdlen - 1] == '/') tdlen--; tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE)); if (tempfile == NULL) { @@ -271,7 +271,7 @@ main(int argc, char *argv[]) /* * select a primary operation mode */ - switch(act) { + switch (act) { case EXTRACT: extract(); break; @@ -325,6 +325,25 @@ sig_cleanup(int which_sig) } /* + * setup_sig() + * set a signal to be caught, but only if it isn't being ignored already + */ + +static int +setup_sig(int sig, const struct sigaction *n_hand) +{ + struct sigaction o_hand; + + if (sigaction(sig, NULL, &o_hand) < 0) + return (-1); + + if (o_hand.sa_handler == SIG_IGN) + return (0); + + return (sigaction(sig, n_hand, NULL)); +} + +/* * gen_init() * general setup routines. Not all are required, but they really help * when dealing with a medium to large sized archives. @@ -335,7 +354,6 @@ gen_init(void) { struct rlimit reslimit; struct sigaction n_hand; - struct sigaction o_hand; /* * Really needed to handle large archives. We can run out of memory for @@ -389,34 +407,16 @@ gen_init(void) n_hand.sa_flags = 0; n_hand.sa_handler = sig_cleanup; - if ((sigaction(SIGHUP, &n_hand, &o_hand) < 0) && - (o_hand.sa_handler == SIG_IGN) && - (sigaction(SIGHUP, &o_hand, &o_hand) < 0)) + if (setup_sig(SIGHUP, &n_hand) || + setup_sig(SIGTERM, &n_hand) || + setup_sig(SIGINT, &n_hand) || + setup_sig(SIGQUIT, &n_hand) || + setup_sig(SIGXCPU, &n_hand)) goto out; - if ((sigaction(SIGTERM, &n_hand, &o_hand) < 0) && - (o_hand.sa_handler == SIG_IGN) && - (sigaction(SIGTERM, &o_hand, &o_hand) < 0)) - goto out; - - if ((sigaction(SIGINT, &n_hand, &o_hand) < 0) && - (o_hand.sa_handler == SIG_IGN) && - (sigaction(SIGINT, &o_hand, &o_hand) < 0)) - goto out; - - if ((sigaction(SIGQUIT, &n_hand, &o_hand) < 0) && - (o_hand.sa_handler == SIG_IGN) && - (sigaction(SIGQUIT, &o_hand, &o_hand) < 0)) - goto out; - - if ((sigaction(SIGXCPU, &n_hand, &o_hand) < 0) && - (o_hand.sa_handler == SIG_IGN) && - (sigaction(SIGXCPU, &o_hand, &o_hand) < 0)) - goto out; - n_hand.sa_handler = SIG_IGN; - if ((sigaction(SIGPIPE, &n_hand, &o_hand) < 0) || - (sigaction(SIGXFSZ, &n_hand, &o_hand) < 0)) + if ((sigaction(SIGPIPE, &n_hand, NULL) < 0) || + (sigaction(SIGXFSZ, &n_hand, NULL) < 0)) goto out; return(0);