From owner-svn-src-stable@freebsd.org Sat Oct 22 13:20:04 2016 Return-Path: Delivered-To: svn-src-stable@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 04BADC1C417; Sat, 22 Oct 2016 13:20:04 +0000 (UTC) (envelope-from bapt@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 AE704883; Sat, 22 Oct 2016 13:20:03 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u9MDK29X039998; Sat, 22 Oct 2016 13:20:02 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u9MDK2iL039997; Sat, 22 Oct 2016 13:20:02 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201610221320.u9MDK2iL039997@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Sat, 22 Oct 2016 13:20:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r307773 - stable/11/usr.bin/tee X-SVN-Group: stable-11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Oct 2016 13:20:04 -0000 Author: bapt Date: Sat Oct 22 13:20:02 2016 New Revision: 307773 URL: https://svnweb.freebsd.org/changeset/base/307773 Log: MFC: 305937 Capsicum-ize tee(1) Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D7940 Modified: stable/11/usr.bin/tee/tee.c Directory Properties: stable/11/ (props changed) Modified: stable/11/usr.bin/tee/tee.c ============================================================================== --- stable/11/usr.bin/tee/tee.c Sat Oct 22 13:15:19 2016 (r307772) +++ stable/11/usr.bin/tee/tee.c Sat Oct 22 13:20:02 2016 (r307773) @@ -41,14 +41,18 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include +#include #include +#include + #include +#include #include #include #include #include #include +#include #include typedef struct _list { @@ -69,6 +73,8 @@ main(int argc, char *argv[]) char *bp; int append, ch, exitval; char *buf; + cap_rights_t rights; + unsigned long cmd; #define BSIZE (8 * 1024) append = 0; @@ -90,6 +96,16 @@ main(int argc, char *argv[]) if ((buf = malloc(BSIZE)) == NULL) err(1, "malloc"); + cap_rights_init(&rights, CAP_READ, CAP_FSTAT); + if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to limit rights for stdin"); + cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL); + if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to limit rights for stderr"); + cmd = TIOCGETA; + if (cap_ioctls_limit(STDERR_FILENO, &cmd, 1) < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to limit ioctls for stderr"); + add(STDOUT_FILENO, "stdout"); for (exitval = 0; *argv; ++argv) @@ -100,6 +116,8 @@ main(int argc, char *argv[]) } else add(fd, *argv); + if (cap_enter() < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to enter capability mode"); while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0) for (p = head; p; p = p->next) { n = rval; @@ -129,6 +147,21 @@ static void add(int fd, const char *name) { LIST *p; + cap_rights_t rights; + unsigned long cmd; + + if (fd == STDOUT_FILENO) + cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL); + else + cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT); + if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to limit rights"); + + if (fd == STDOUT_FILENO) { + cmd = TIOCGETA; + if (cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to limit ioctls for stdout"); + } if ((p = malloc(sizeof(LIST))) == NULL) err(1, "malloc");