From 290514978463c1489e8a1914541f6babc3b49f8a Mon Sep 17 00:00:00 2001 From: kevans Date: Fri, 13 Apr 2018 03:30:10 +0000 Subject: MFC r308432, r308657: Capsicumize some trivial stdio programs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit r308432: Capsicumize some trivial stdio programs Trivially capsicumize some simple programs that just interact with stdio. This list of programs uses 'pledge("stdio")' in OpenBSD. r308657: fold(1): Revert incorrect r308432 As Jean-Sébastien notes, fold(1) requires handling argv-supplied files. That will require a slightly more sophisticated approach. --- usr.bin/basename/basename.c | 4 ++++ usr.bin/dc/dc.c | 26 +++++++++++++++++++++----- usr.bin/dirname/dirname.c | 4 ++++ usr.bin/getopt/getopt.c | 6 ++++++ usr.bin/locate/bigram/locate.bigram.c | 6 ++++++ usr.bin/logname/logname.c | 4 ++++ usr.bin/printenv/printenv.c | 5 +++++ usr.bin/yes/yes.c | 5 +++++ 8 files changed, 55 insertions(+), 5 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/basename/basename.c b/usr.bin/basename/basename.c index 4c1023f..e82dec5 100644 --- a/usr.bin/basename/basename.c +++ b/usr.bin/basename/basename.c @@ -42,6 +42,7 @@ static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95"; #include __FBSDID("$FreeBSD$"); +#include #include #include #include @@ -64,6 +65,9 @@ main(int argc, char **argv) setlocale(LC_ALL, ""); + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + aflag = 0; suffix = NULL; suffixlen = 0; diff --git a/usr.bin/dc/dc.c b/usr.bin/dc/dc.c index f6968a5..a2cf8c9 100644 --- a/usr.bin/dc/dc.c +++ b/usr.bin/dc/dc.c @@ -22,9 +22,11 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include +#include #include #include #include @@ -58,11 +60,11 @@ usage(void) } static void -procfile(char *fname) { +procfd(int fd, char *fname) { struct stat st; FILE *file; - file = fopen(fname, "r"); + file = fdopen(fd, "r"); if (file == NULL) err(1, "cannot open file %s", fname); if (fstat(fileno(file), &st) == -1) @@ -80,7 +82,7 @@ procfile(char *fname) { int main(int argc, char *argv[]) { - int ch; + int ch, fd; bool extended_regs = false, preproc_done = false; /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */ @@ -97,7 +99,10 @@ main(int argc, char *argv[]) case 'f': if (!preproc_done) init_bmachine(extended_regs); - procfile(optarg); + fd = open(optarg, O_RDONLY); + if (fd < 0) + err(1, "cannot open file %s", optarg); + procfd(fd, optarg); preproc_done = true; break; case 'x': @@ -126,12 +131,23 @@ main(int argc, char *argv[]) if (argc > 1) usage(); if (argc == 1) { - procfile(argv[0]); + fd = open(argv[0], O_RDONLY); + if (fd < 0) + err(1, "cannot open file %s", argv[0]); + + if (caph_limit_stream(fd, CAPH_READ) < 0 || + caph_limit_stdio() < 0 || + (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + + procfd(fd, argv[0]); preproc_done = true; } if (preproc_done) return (0); + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); src_setstream(&src, stdin); reset_bmachine(&src); eval(); diff --git a/usr.bin/dirname/dirname.c b/usr.bin/dirname/dirname.c index c176995..0036c60 100644 --- a/usr.bin/dirname/dirname.c +++ b/usr.bin/dirname/dirname.c @@ -39,6 +39,7 @@ static const char sccsid[] = "@(#)dirname.c 8.4 (Berkeley) 5/4/95"; #include __FBSDID("$FreeBSD$"); +#include #include #include #include @@ -53,6 +54,9 @@ main(int argc, char **argv) char *p; int ch; + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + while ((ch = getopt(argc, argv, "")) != -1) switch(ch) { case '?': diff --git a/usr.bin/getopt/getopt.c b/usr.bin/getopt/getopt.c index d1671a7..3f55a34 100644 --- a/usr.bin/getopt/getopt.c +++ b/usr.bin/getopt/getopt.c @@ -6,6 +6,9 @@ __FBSDID("$FreeBSD$"); * into the public domain and is thus not subject to any copyright. */ +#include +#include +#include #include #include #include @@ -16,6 +19,9 @@ main(int argc, char *argv[]) int c; int status = 0; + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + optind = 2; /* Past the program name and the option letters. */ while ((c = getopt(argc, argv, argv[1])) != -1) switch (c) { diff --git a/usr.bin/locate/bigram/locate.bigram.c b/usr.bin/locate/bigram/locate.bigram.c index 6fa2d31..90111e7 100644 --- a/usr.bin/locate/bigram/locate.bigram.c +++ b/usr.bin/locate/bigram/locate.bigram.c @@ -57,6 +57,9 @@ static char sccsid[] = "@(#)locate.bigram.c 8.1 (Berkeley) 6/6/93"; * Use 'code' to encode a file using this output. */ +#include +#include +#include #include #include #include /* for MAXPATHLEN */ @@ -73,6 +76,9 @@ main(void) u_char *oldpath = buf1, *path = buf2; u_int i, j; + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + while (fgets(path, sizeof(buf2), stdin) != NULL) { /* diff --git a/usr.bin/logname/logname.c b/usr.bin/logname/logname.c index 8a251ae..05aa881 100644 --- a/usr.bin/logname/logname.c +++ b/usr.bin/logname/logname.c @@ -39,6 +39,7 @@ static const char sccsid[] = "@(#)logname.c 8.2 (Berkeley) 4/3/94"; #include __FBSDID("$FreeBSD$"); +#include #include #include #include @@ -51,6 +52,9 @@ main(int argc, char *argv[] __unused) { char *p; + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + if (argc != 1) usage(); if ((p = getlogin()) == NULL) diff --git a/usr.bin/printenv/printenv.c b/usr.bin/printenv/printenv.c index 27882ab..4ca91ec 100644 --- a/usr.bin/printenv/printenv.c +++ b/usr.bin/printenv/printenv.c @@ -44,6 +44,8 @@ __FBSDID("$FreeBSD$"); #include +#include +#include #include #include #include @@ -65,6 +67,9 @@ main(int argc, char *argv[]) size_t len; int ch; + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + while ((ch = getopt(argc, argv, "")) != -1) switch(ch) { case '?': diff --git a/usr.bin/yes/yes.c b/usr.bin/yes/yes.c index ef98b4d..2336d3f 100644 --- a/usr.bin/yes/yes.c +++ b/usr.bin/yes/yes.c @@ -41,12 +41,17 @@ static const char rcsid[] = "$FreeBSD$"; #endif #endif /* not lint */ +#include #include #include int main(int argc, char **argv) { + + if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) + err(1, "capsicum"); + if (argc > 1) while (puts(argv[1]) != EOF) ; -- cgit v1.1