From 9490edf2cc1db1c6c10782d325f0baf17b18d234 Mon Sep 17 00:00:00 2001 From: deraadt Date: Sat, 10 Oct 2015 19:28:54 +0000 Subject: [PATCH] Rather than invoking fork/execve of dc(1) on a pipe, compile in the dc(1) code directly and use it as a subfunction. This refactoring allows use of pledge "stdio rpath proc tty" in the main bc(1) process before fork, pledge "stdio rpath tty" after fork, and fully reduced to "stdio" in the dc(1) child. This requires two recent to the kernel code (allowing sigsuspend(), and kill() self as pid 0). ok otto --- usr.bin/bc/Makefile | 9 +++++---- usr.bin/bc/bc.y | 19 ++++++++++++++++--- usr.bin/dc/Makefile | 4 ++-- usr.bin/dc/dc.c | 7 ++----- usr.bin/dc/extern.h | 4 +++- usr.bin/dc/main.c | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 usr.bin/dc/main.c diff --git a/usr.bin/bc/Makefile b/usr.bin/bc/Makefile index 37acb194029..9d5391d0638 100644 --- a/usr.bin/bc/Makefile +++ b/usr.bin/bc/Makefile @@ -1,13 +1,14 @@ -# $OpenBSD: Makefile,v 1.7 2013/09/19 16:12:00 otto Exp $ +# $OpenBSD: Makefile,v 1.8 2015/10/10 19:28:54 deraadt Exp $ PROG= bc -SRCS= bc.y scan.l tty.c +SRCS= bc.y scan.l tty.c dc.c bcode.c inout.c mem.c stack.c CPPFLAGS+= -I. -I${.CURDIR} CFLAGS+= -Wall -Wno-unused YFLAGS+= -LDADD+= -ledit -lcurses -DPADD+= ${LIBEDIT} ${LIBCURSES} +LDADD+= -ledit -lcurses -lcrypto +DPADD+= ${LIBEDIT} ${LIBCURSES} ${LIBCRYPTO} +.PATH: ${.CURDIR}/../dc beforeinstall: install -c -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/bc.library \ diff --git a/usr.bin/bc/bc.y b/usr.bin/bc/bc.y index 735f4158090..2a31a7eaefb 100644 --- a/usr.bin/bc/bc.y +++ b/usr.bin/bc/bc.y @@ -1,5 +1,5 @@ %{ -/* $OpenBSD: bc.y,v 1.47 2014/11/26 18:34:51 millert Exp $ */ +/* $OpenBSD: bc.y,v 1.48 2015/10/10 19:28:54 deraadt Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek @@ -1094,6 +1094,9 @@ main(int argc, char *argv[]) int p[2]; char *q; + if (pledge("stdio rpath proc tty", NULL) == -1) + err(1, "pledge"); + init(); setvbuf(stdout, NULL, _IOLBF, 0); @@ -1144,12 +1147,18 @@ main(int argc, char *argv[]) close(p[0]); close(p[1]); } else { + char *dc_argv[] = { "dc", "-x", NULL }; + extern int dc_main(int, char **); + + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + close(STDIN_FILENO); dup(p[0]); close(p[0]); close(p[1]); - execl(_PATH_DC, "dc", "-x", (char *)NULL); - err(1, "cannot find dc"); + + exit (dc_main(2, dc_argv)); } } if (interactive) { @@ -1165,6 +1174,10 @@ main(int argc, char *argv[]) el_set(el, EL_BIND, "^D", "bc_eof", NULL); el_source(el, NULL); } + + if (pledge("stdio rpath tty", NULL) == -1) + err(1, "pledge"); + yywrap(); return yyparse(); } diff --git a/usr.bin/dc/Makefile b/usr.bin/dc/Makefile index b0a23967a26..d02cc4234aa 100644 --- a/usr.bin/dc/Makefile +++ b/usr.bin/dc/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.2 2006/11/26 11:31:09 deraadt Exp $ +# $OpenBSD: Makefile,v 1.3 2015/10/10 19:28:54 deraadt Exp $ PROG= dc -SRCS= dc.c bcode.c inout.c mem.c stack.c +SRCS= main.c dc.c bcode.c inout.c mem.c stack.c COPTS+= -Wall LDADD= -lcrypto DPADD= ${LIBCRYPTO} diff --git a/usr.bin/dc/dc.c b/usr.bin/dc/dc.c index c50cbca0cd4..f720470681a 100644 --- a/usr.bin/dc/dc.c +++ b/usr.bin/dc/dc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dc.c,v 1.15 2015/10/09 01:37:07 deraadt Exp $ */ +/* $OpenBSD: dc.c,v 1.16 2015/10/10 19:28:54 deraadt Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek @@ -38,7 +38,7 @@ usage(void) } int -main(int argc, char *argv[]) +dc_main(int argc, char *argv[]) { int ch; bool extended_regs = false; @@ -47,9 +47,6 @@ main(int argc, char *argv[]) char *buf, *p; struct stat st; - if (pledge("stdio rpath", NULL) == -1) - err(1, "pledge"); - if ((buf = strdup("")) == NULL) err(1, NULL); /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */ diff --git a/usr.bin/dc/extern.h b/usr.bin/dc/extern.h index 9642dfc5ddf..7f964d64d56 100644 --- a/usr.bin/dc/extern.h +++ b/usr.bin/dc/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.4 2014/12/01 13:13:00 deraadt Exp $ */ +/* $OpenBSD: extern.h,v 1.5 2015/10/10 19:28:54 deraadt Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek @@ -60,3 +60,5 @@ void stack_print(FILE *, const struct stack *, const char *, void frame_assign(struct stack *, size_t, const struct value *); struct value * frame_retrieve(const struct stack *, size_t); /* void frame_free(struct stack *); */ + +int dc_main(int, char **); diff --git a/usr.bin/dc/main.c b/usr.bin/dc/main.c new file mode 100644 index 00000000000..a9e278c6c36 --- /dev/null +++ b/usr.bin/dc/main.c @@ -0,0 +1,34 @@ +/* $OpenBSD: main.c,v 1.1 2015/10/10 19:28:54 deraadt Exp $ */ + +/* + * Copyright (c) 2003, Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "extern.h" + +int +main(int argc, char *argv[]) +{ + setproctitle("dc"); + + if (pledge("stdio rpath", NULL) == -1) + err(1, "pledge"); + + return dc_main(argc, argv); +} -- 2.20.1