From owner-svn-src-all@freebsd.org Fri Dec 16 01:59:29 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 69C49C82F59; Fri, 16 Dec 2016 01:59:29 +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 2A983106D; Fri, 16 Dec 2016 01:59:29 +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 uBG1xSLX021134; Fri, 16 Dec 2016 01:59:28 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uBG1xSW8021133; Fri, 16 Dec 2016 01:59:28 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201612160159.uBG1xSW8021133@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 01:59:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r310142 - head/usr.bin/ktrdump 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 01:59:29 -0000 Author: cem Date: Fri Dec 16 01:59:28 2016 New Revision: 310142 URL: https://svnweb.freebsd.org/changeset/base/310142 Log: ktrdump(8): Capsicumify We restrict the (optional) input file and output files. It would be nice to restrict the KVM files, but that's up to libkvm. We wait until after kvm_nlist() is invoked to cap_enter() because kldsym() isn't supported in the Capsicum sandbox. Feedback from: emaste@ (earlier versions) Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D7921 Modified: head/usr.bin/ktrdump/ktrdump.c Modified: head/usr.bin/ktrdump/ktrdump.c ============================================================================== --- head/usr.bin/ktrdump/ktrdump.c Fri Dec 16 01:51:12 2016 (r310141) +++ head/usr.bin/ktrdump/ktrdump.c Fri Dec 16 01:59:28 2016 (r310142) @@ -29,11 +29,14 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include +#include #include +#include #include #include #include @@ -70,6 +73,7 @@ static int hflag; static char corefile[PATH_MAX]; static char execfile[PATH_MAX]; +static char outfile[PATH_MAX] = "stdout"; static char desc[SBUFLEN]; static char errbuf[_POSIX2_LINE_MAX]; @@ -87,6 +91,7 @@ main(int ac, char **av) struct ktr_entry *buf; uintmax_t tlast, tnow; unsigned long bufptr; + cap_rights_t rights; struct stat sb; kvm_t *kd; FILE *out; @@ -122,6 +127,11 @@ main(int ac, char **av) iflag = 1; if ((in = open(optarg, O_RDONLY)) == -1) err(1, "%s", optarg); + cap_rights_init(&rights, CAP_FSTAT, CAP_MMAP_R); + if (cap_rights_limit(in, &rights) < 0 && + errno != ENOSYS) + err(1, "unable to limit rights for %s", + optarg); break; case 'M': case 'm': @@ -133,6 +143,7 @@ main(int ac, char **av) case 'o': if ((out = fopen(optarg, "w")) == NULL) err(1, "%s", optarg); + strlcpy(outfile, optarg, sizeof(outfile)); break; case 'q': qflag++; @@ -155,6 +166,10 @@ main(int ac, char **av) if (ac != 0) usage(); + cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE); + if (cap_rights_limit(fileno(out), &rights) < 0 && errno != ENOSYS) + err(1, "unable to limit rights for %s", outfile); + /* * Open our execfile and corefile, resolve needed symbols and read in * the trace buffer. @@ -162,11 +177,28 @@ main(int ac, char **av) if ((kd = kvm_openfiles(Nflag ? execfile : NULL, Mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL) errx(1, "%s", errbuf); + + /* + * Cache NLS data, for strerror, for err(3), before entering capability + * mode. + */ + caph_cache_catpages(); + if (kvm_nlist(kd, nl) != 0 || kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1) errx(1, "%s", kvm_geterr(kd)); if (version != KTR_VERSION) errx(1, "ktr version mismatch"); + + /* + * Enter Capsicum sandbox. + * + * kvm_nlist() above uses kldsym(2) for native kernels, and that isn't + * allowed in the sandbox. + */ + if (cap_enter() < 0 && errno != ENOSYS) + err(1, "unable to enter capability mode"); + if (iflag) { if (fstat(in, &sb) == -1) errx(1, "stat");