changeset 5445:2070a61faddd draft

privsep: Dump leases from stdin in a limited sandbox
author Roy Marples <roy@marples.name>
date Sun, 06 Sep 2020 12:20:40 +0100
parents d861892268ff
children 560e67bb9f6f
files src/dhcpcd.c src/privsep.c
diffstat 2 files changed, 12 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/dhcpcd.c	Sun Sep 06 11:58:29 2020 +0100
+++ b/src/dhcpcd.c	Sun Sep 06 12:20:40 2020 +0100
@@ -2135,11 +2135,20 @@
 	}
 #endif
 
+#ifdef PRIVSEP
+	ps_init(&ctx);
+#endif
+
 #ifndef SMALL
 	if (ctx.options & DHCPCD_DUMPLEASE &&
 	    ioctl(fileno(stdin), FIONREAD, &i, sizeof(i)) == 0 &&
 	    i > 0)
 	{
+		ctx.options |= DHCPCD_FORKED; /* pretend child process */
+#ifdef PRIVSEP
+		if (IN_PRIVSEP(&ctx) && ps_mastersandbox(&ctx) == -1)
+			goto exit_failure;
+#endif
 		ifp = calloc(1, sizeof(*ifp));
 		if (ifp == NULL) {
 			logerr(__func__);
@@ -2241,11 +2250,6 @@
 	if (freopen(_PATH_DEVNULL, "r", stdin) == NULL)
 		logerr("%s: freopen stdin", __func__);
 
-
-#ifdef PRIVSEP
-	ps_init(&ctx);
-#endif
-
 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK)
 	if (xsocketpair(AF_UNIX, SOCK_DGRAM | SOCK_CXNB, 0, fork_fd) == -1 ||
 	    xsocketpair(AF_UNIX, SOCK_DGRAM | SOCK_CXNB, 0, stderr_fd) == -1)
--- a/src/privsep.c	Sun Sep 06 11:58:29 2020 +0100
+++ b/src/privsep.c	Sun Sep 06 12:20:40 2020 +0100
@@ -480,8 +480,9 @@
 	}
 
 #ifdef PRIVSEP_RIGHTS
-	if (ps_rights_limit_ioctl(ctx->pf_inet_fd) == -1 ||
-	     ps_rights_limit_fd(ctx->link_fd) == -1 ||
+	if ((ctx->pf_inet_fd != -1 &&
+	    ps_rights_limit_ioctl(ctx->pf_inet_fd) == -1) ||
+	    (ctx->link_fd != -1 && ps_rights_limit_fd(ctx->link_fd) == -1) ||
 	     ps_rights_limit_stdio() == -1)
 	{
 		logerr("%s: cap_rights_limit", __func__);