From owner-svn-src-all@freebsd.org Fri Dec 16 02:09:49 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 B7812C772D4; Fri, 16 Dec 2016 02:09:49 +0000 (UTC) (envelope-from cem@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 6DD55197E; Fri, 16 Dec 2016 02:09:49 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uBG29mPP025099; Fri, 16 Dec 2016 02:09:48 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uBG29mXh025098; Fri, 16 Dec 2016 02:09:48 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201612160209.uBG29mXh025098@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: "Conrad E. Meyer" Date: Fri, 16 Dec 2016 02:09:48 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r310145 - head/usr.bin/ident 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.23 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: Fri, 16 Dec 2016 02:09:49 -0000 Author: cem Date: Fri Dec 16 02:09:48 2016 New Revision: 310145 URL: https://svnweb.freebsd.org/changeset/base/310145 Log: ident(1): Capsicumify Preopen input file list before entering Capsicum capability mode. Feedback by: allanjude@, bapt@, emaste@ (earlier versions) Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D7918 Modified: head/usr.bin/ident/ident.c Modified: head/usr.bin/ident/ident.c ============================================================================== --- head/usr.bin/ident/ident.c Fri Dec 16 02:06:34 2016 (r310144) +++ head/usr.bin/ident/ident.c Fri Dec 16 02:09:48 2016 (r310145) @@ -28,11 +28,14 @@ #include __FBSDID("$FreeBSD$"); +#include #include #include +#include #include #include +#include #include #include #include @@ -202,8 +205,9 @@ int main(int argc, char **argv) { bool quiet = false; - int ch, i; + int ch, i, *fds, fd; int ret = EXIT_SUCCESS; + size_t nfds; FILE *fp; while ((ch = getopt(argc, argv, "qV")) != -1) { @@ -223,17 +227,50 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (argc == 0) - return (scan(stdin, NULL, quiet)); + if (caph_limit_stdio() < 0) + err(EXIT_FAILURE, "unable to limit stdio"); - for (i = 0; i < argc; i++) { - fp = fopen(argv[i], "r"); + if (argc == 0) { + nfds = 1; + fds = malloc(sizeof(*fds)); + if (fds == NULL) + err(EXIT_FAILURE, "unable to allocate fds array"); + fds[0] = STDIN_FILENO; + } else { + nfds = argc; + fds = malloc(sizeof(*fds) * nfds); + if (fds == NULL) + err(EXIT_FAILURE, "unable to allocate fds array"); + + for (i = 0; i < argc; i++) { + fds[i] = fd = open(argv[i], O_RDONLY); + if (fd < 0) { + warn("%s", argv[i]); + ret = EXIT_FAILURE; + continue; + } + if (caph_limit_stream(fd, CAPH_READ) < 0) + err(EXIT_FAILURE, + "unable to limit fcntls/rights for %s", + argv[i]); + } + } + + /* Enter Capsicum sandbox. */ + if (cap_enter() < 0 && errno != ENOSYS) + err(EXIT_FAILURE, "unable to enter capability mode"); + + for (i = 0; i < (int)nfds; i++) { + if (fds[i] < 0) + continue; + + fp = fdopen(fds[i], "r"); if (fp == NULL) { warn("%s", argv[i]); ret = EXIT_FAILURE; continue; } - if (scan(fp, argv[i], quiet) != EXIT_SUCCESS) + if (scan(fp, argc == 0 ? NULL : argv[i], quiet) != EXIT_SUCCESS) ret = EXIT_FAILURE; fclose(fp); }