]> git.ipfire.org Git - people/ms/dhcpcd.git/commitdiff
privsep: Fold capsicum and pledge entry points into ps_entersandbox
authorRoy Marples <roy@marples.name>
Sat, 19 Sep 2020 17:58:52 +0000 (18:58 +0100)
committerRoy Marples <roy@marples.name>
Sat, 19 Sep 2020 17:58:52 +0000 (18:58 +0100)
src/privsep-bpf.c
src/privsep-control.c
src/privsep-inet.c
src/privsep.c
src/privsep.h

index 3025fda8914a97a8c52e2f9405783ddf1eb0bd20..9009083e5c01d261e2af864c3a2438ca8e6413e0 100644 (file)
 #include "logerr.h"
 #include "privsep.h"
 
-#ifdef HAVE_CAPSICUM
-#include <sys/capsicum.h>
-#endif
-
 static void
 ps_bpf_recvbpf(void *arg)
 {
@@ -244,14 +240,7 @@ ps_bpf_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg)
                ps_freeprocess(psp);
                return -1;
        case 0:
-#ifdef HAVE_CAPSICUM
-               if (cap_enter() == -1 && errno != ENOSYS)
-                       logerr("%s: cap_enter", __func__);
-#endif
-#ifdef HAVE_PLEDGE
-               if (pledge("stdio", NULL) == -1)
-                       logerr("%s: pledge", __func__);
-#endif
+               ps_entersandbox("stdio");
                break;
        default:
 #ifdef PRIVSEP_DEBUG
index 01a8acd744dd588e3f65cab5fd6fcbde63aaa557..8d8534dcb15c59d53f3122c367187e6b91dded1f 100644 (file)
 #include "logerr.h"
 #include "privsep.h"
 
-#ifdef HAVE_CAPSICUM
-#include <sys/capsicum.h>
-#endif
-
 static int
 ps_ctl_startcb(void *arg)
 {
@@ -267,14 +263,7 @@ ps_ctl_start(struct dhcpcd_ctx *ctx)
            ps_ctl_listen, ctx) == -1)
                return -1;
 
-#ifdef HAVE_CAPSICUM
-       if (cap_enter() == -1 && errno != ENOSYS)
-               logerr("%s: cap_enter", __func__);
-#endif
-#ifdef HAVE_PLEDGE
-       if (pledge("stdio inet", NULL) == -1)
-               logerr("%s: pledge", __func__);
-#endif
+       ps_entersandbox("stdio inet");
        return 0;
 }
 
index 89ba79e039ae0ac434342ce22384c6b70dc590e7..bac3a7b11f0335f615a139938279d74487c4e921 100644 (file)
 #include "logerr.h"
 #include "privsep.h"
 
-#ifdef HAVE_CAPSICUM
-#include <sys/capsicum.h>
-#endif
-
 #ifdef INET
 static void
 ps_inet_recvbootp(void *arg)
@@ -337,14 +333,8 @@ ps_inet_start(struct dhcpcd_ctx *ctx)
            ps_inet_startcb, NULL,
            PSF_DROPPRIVS);
 
-#ifdef HAVE_CAPSICUM
-       if (pid == 0 && cap_enter() == -1 && errno != ENOSYS)
-               logerr("%s: cap_enter", __func__);
-#endif
-#ifdef HAVE_PLEDGE
-       if (pid == 0 && pledge("stdio", NULL) == -1)
-               logerr("%s: pledge", __func__);
-#endif
+       if (pid == 0)
+               ps_entersandbox("stdio");
 
        return pid;
 }
@@ -570,14 +560,7 @@ ps_inet_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg)
                ps_freeprocess(psp);
                return -1;
        case 0:
-#ifdef HAVE_CAPSICUM
-               if (cap_enter() == -1 && errno != ENOSYS)
-                       logerr("%s: cap_enter", __func__);
-#endif
-#ifdef HAVE_PLEDGE
-               if (pledge("stdio", NULL) == -1)
-                       logerr("%s: pledge", __func__);
-#endif
+               ps_entersandbox("stdio");
                break;
        default:
                break;
index f92ef45b2eaa1a714071d2496ec39c0fddd10323..1841fb36b65c79c328bc88825a77bd343f6b243a 100644 (file)
@@ -489,6 +489,28 @@ started_net:
        return 1;
 }
 
+int
+ps_entersandbox(const char *_pledge)
+{
+
+#ifdef HAVE_CAPSICUM
+       if (cap_enter() == -1 && errno != ENOSYS) {
+               logerr("%s: cap_enter", __func__);
+               return -1;
+       }
+#endif
+#ifdef HAVE_PLEDGE
+       if (pledge(_pledge, NULL) == -1) {
+               logerr("%s: pledge", __func__);
+               return -1;
+       }
+#else
+       UNUSED(_pledge);
+#endif
+
+       return 0;
+}
+
 int
 ps_mastersandbox(struct dhcpcd_ctx *ctx)
 {
@@ -508,20 +530,8 @@ ps_mastersandbox(struct dhcpcd_ctx *ctx)
                return -1;
        }
 #endif
-#ifdef HAVE_CAPSICUM
-       if (cap_enter() == -1 && errno != ENOSYS) {
-               logerr("%s: cap_enter", __func__);
-               return -1;
-       }
-#endif
-#ifdef HAVE_PLEDGE
-       if (pledge("stdio route", NULL) == -1) {
-               logerr("%s: pledge", __func__);
-               return -1;
-       }
-#endif
 
-       return 0;
+       return ps_entersandbox("stdio route");
 }
 
 int
index c789543282fc0cebb32c70fb579745ca4aa2767d..8d73af0e8a2bb5a953537a85d95936225da8b470 100644 (file)
@@ -92,7 +92,6 @@
 #define        IN_PRIVSEP_SE(ctx)      \
        (((ctx)->options & (DHCPCD_PRIVSEP | DHCPCD_FORKED)) == DHCPCD_PRIVSEP)
 
-
 #if defined(PRIVSEP) && defined(HAVE_CAPSICUM)
 #define PRIVSEP_RIGHTS
 #endif
@@ -168,6 +167,7 @@ TAILQ_HEAD(ps_process_head, ps_process);
 int ps_init(struct dhcpcd_ctx *);
 int ps_start(struct dhcpcd_ctx *);
 int ps_stop(struct dhcpcd_ctx *);
+int ps_entersandbox(const char *);
 int ps_mastersandbox(struct dhcpcd_ctx *);
 
 int ps_unrollmsg(struct msghdr *, struct ps_msghdr *, const void *, size_t);
@@ -185,6 +185,7 @@ ssize_t ps_recvpsmsg(struct dhcpcd_ctx *, int,
 
 /* Internal privsep functions. */
 int ps_setbuf_fdpair(int []);
+
 #ifdef PRIVSEP_RIGHTS
 int ps_rights_limit_ioctl(int);
 int ps_rights_limit_fd_fctnl(int);
@@ -192,6 +193,7 @@ int ps_rights_limit_fd_rdonly(int);
 int ps_rights_limit_fd(int);
 int ps_rights_limit_fdpair(int []);
 #endif
+
 pid_t ps_dostart(struct dhcpcd_ctx * ctx,
     pid_t *priv_pid, int *priv_fd,
     void (*recv_msg)(void *), void (*recv_unpriv_msg),