From owner-svn-src-all@freebsd.org Thu Aug 25 21:33:40 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 33BFCBC686E; Thu, 25 Aug 2016 21:33:40 +0000 (UTC) (envelope-from emaste@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 mx1.freebsd.org (Postfix) with ESMTPS id 0098C1DFF; Thu, 25 Aug 2016 21:33:39 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u7PLXdcQ067905; Thu, 25 Aug 2016 21:33:39 GMT (envelope-from emaste@FreeBSD.org) Received: (from emaste@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u7PLXd5v067904; Thu, 25 Aug 2016 21:33:39 GMT (envelope-from emaste@FreeBSD.org) Message-Id: <201608252133.u7PLXd5v067904@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: emaste set sender to emaste@FreeBSD.org using -f From: Ed Maste Date: Thu, 25 Aug 2016 21:33:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r304821 - head/usr.bin/bsdiff/bspatch X-SVN-Group: head 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.22 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: Thu, 25 Aug 2016 21:33:40 -0000 Author: emaste Date: Thu Aug 25 21:33:39 2016 New Revision: 304821 URL: https://svnweb.freebsd.org/changeset/base/304821 Log: bspatch: remove output file in the case of error Reviewed by: oshogbo Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D7644 Modified: head/usr.bin/bsdiff/bspatch/bspatch.c Modified: head/usr.bin/bsdiff/bspatch/bspatch.c ============================================================================== --- head/usr.bin/bsdiff/bspatch/bspatch.c Thu Aug 25 21:29:16 2016 (r304820) +++ head/usr.bin/bsdiff/bspatch/bspatch.c Thu Aug 25 21:33:39 2016 (r304821) @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -51,6 +52,18 @@ __FBSDID("$FreeBSD$"); #define O_BINARY 0 #endif +static char *newfile; +static int dirfd = -1; + +static void +exit_cleanup(void) +{ + + if (dirfd != -1 && newfile != NULL) + if (unlinkat(dirfd, newfile, 0)) + warn("unlinkat"); +} + static off_t offtin(u_char *buf) { off_t y; @@ -82,6 +95,7 @@ int main(int argc, char *argv[]) { FILE *f, *cpf, *dpf, *epf; BZFILE *cpfbz2, *dpfbz2, *epfbz2; + char *directory, *namebuf; int cbz2err, dbz2err, ebz2err; int newfd, oldfd; ssize_t oldsize, newsize; @@ -93,7 +107,7 @@ int main(int argc, char *argv[]) off_t lenread; off_t i; #ifdef HAVE_CAPSICUM - cap_rights_t rights_ro, rights_wr; + cap_rights_t rights_dir, rights_ro, rights_wr; #endif if (argc != 4) @@ -114,10 +128,19 @@ int main(int argc, char *argv[]) /* open oldfile */ if ((oldfd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0) err(1, "open(%s)", argv[1]); + /* open directory where we'll write newfile */ + if ((namebuf = strdup(argv[2])) == NULL || + (directory = dirname(namebuf)) == NULL || + (dirfd = open(directory, O_DIRECTORY)) < 0) + err(1, "open %s", argv[2]); + free(namebuf); + if ((newfile = basename(argv[2])) == NULL) + err(1, "basename"); /* open newfile */ - if ((newfd = open(argv[2], O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, - 0666)) < 0) + if ((newfd = openat(dirfd, newfile, + O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) < 0) err(1, "open(%s)", argv[2]); + atexit(exit_cleanup); #ifdef HAVE_CAPSICUM if (cap_enter() < 0) { @@ -128,13 +151,15 @@ int main(int argc, char *argv[]) /* Capsicum Available */ cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK); cap_rights_init(&rights_wr, CAP_WRITE); - + cap_rights_init(&rights_dir, CAP_UNLINKAT); + if (cap_rights_limit(fileno(f), &rights_ro) < 0 || cap_rights_limit(fileno(cpf), &rights_ro) < 0 || cap_rights_limit(fileno(dpf), &rights_ro) < 0 || cap_rights_limit(fileno(epf), &rights_ro) < 0 || cap_rights_limit(oldfd, &rights_ro) < 0 || - cap_rights_limit(newfd, &rights_wr) < 0) + cap_rights_limit(newfd, &rights_wr) < 0 || + cap_rights_limit(dirfd, &rights_dir) < 0) err(1, "cap_rights_limit() failed, could not restrict" " capabilities"); } @@ -260,6 +285,8 @@ int main(int argc, char *argv[]) /* Write the new file */ if (write(newfd, new, newsize) != newsize || close(newfd) == -1) err(1, "%s", argv[2]); + /* Disable atexit cleanup */ + newfile = NULL; free(new); free(old);