1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Privilege Separation for dhcpcd, control proxy 4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name> 5 * All rights reserved 6 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <errno.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 #include "dhcpcd.h" 34 #include "control.h" 35 #include "eloop.h" 36 #include "logerr.h" 37 #include "privsep.h" 38 39 static int 40 ps_ctl_startcb(struct ps_process *psp) 41 { 42 struct dhcpcd_ctx *ctx = psp->psp_ctx; 43 sa_family_t af; 44 45 if (ctx->options & DHCPCD_MANAGER) { 46 setproctitle("[control proxy]"); 47 af = AF_UNSPEC; 48 } else { 49 setproctitle("[control proxy] %s%s%s", 50 ctx->ifv[0], 51 ctx->options & DHCPCD_IPV4 ? " [ip4]" : "", 52 ctx->options & DHCPCD_IPV6 ? " [ip6]" : ""); 53 if ((ctx->options & 54 (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV4) 55 af = AF_INET; 56 else if ((ctx->options & 57 (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV6) 58 af = AF_INET6; 59 else 60 af = AF_UNSPEC; 61 } 62 63 return control_start(ctx, 64 ctx->options & DHCPCD_MANAGER ? NULL : *ctx->ifv, af); 65 } 66 67 static void 68 ps_ctl_recvmsg(void *arg, unsigned short events) 69 { 70 struct ps_process *psp = arg; 71 72 if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, 73 NULL, psp->psp_ctx) == -1) 74 logerr(__func__); 75 } 76 77 ssize_t 78 ps_ctl_handleargs(struct fd_list *fd, char *data, size_t len) 79 { 80 81 /* Make any change here in dhcpcd.c as well. */ 82 if (strncmp(data, "--version", 83 MIN(strlen("--version"), len)) == 0) { 84 return control_queue(fd, UNCONST(VERSION), 85 strlen(VERSION) + 1); 86 } else if (strncmp(data, "--getconfigfile", 87 MIN(strlen("--getconfigfile"), len)) == 0) { 88 return control_queue(fd, UNCONST(fd->ctx->cffile), 89 strlen(fd->ctx->cffile) + 1); 90 } else if (strncmp(data, "--listen", 91 MIN(strlen("--listen"), len)) == 0) { 92 fd->flags |= FD_LISTEN; 93 return 0; 94 } 95 96 if (fd->ctx->ps_control_client != NULL && 97 fd->ctx->ps_control_client != fd) 98 { 99 logerrx("%s: cannot handle another client", __func__); 100 return 0; 101 } 102 return 1; 103 } 104 105 static ssize_t 106 ps_ctl_dispatch(void *arg, struct ps_msghdr *psm, struct msghdr *msg) 107 { 108 struct dhcpcd_ctx *ctx = arg; 109 struct iovec *iov = msg->msg_iov; 110 struct fd_list *fd; 111 unsigned int fd_flags = FD_SENDLEN; 112 113 switch (psm->ps_flags) { 114 case PS_CTL_PRIV: 115 break; 116 case PS_CTL_UNPRIV: 117 fd_flags |= FD_UNPRIV; 118 break; 119 } 120 121 switch (psm->ps_cmd) { 122 case PS_CTL: 123 if (msg->msg_iovlen != 1) { 124 errno = EINVAL; 125 return -1; 126 } 127 if (ctx->ps_control_client != NULL) { 128 logerrx("%s: cannot handle another client", __func__); 129 return 0; 130 } 131 fd = control_new(ctx, ctx->ps_ctl->psp_work_fd, fd_flags); 132 if (fd == NULL) 133 return -1; 134 ctx->ps_control_client = fd; 135 control_recvdata(fd, iov->iov_base, iov->iov_len); 136 break; 137 case PS_CTL_EOF: 138 ctx->ps_control_client = NULL; 139 break; 140 default: 141 errno = ENOTSUP; 142 return -1; 143 } 144 return 0; 145 } 146 147 static void 148 ps_ctl_dodispatch(void *arg, unsigned short events) 149 { 150 struct ps_process *psp = arg; 151 152 if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, 153 ps_ctl_dispatch, psp->psp_ctx) == -1) 154 logerr(__func__); 155 } 156 157 static void 158 ps_ctl_recv(void *arg, unsigned short events) 159 { 160 struct dhcpcd_ctx *ctx = arg; 161 char buf[BUFSIZ]; 162 ssize_t len; 163 164 if (!(events & (ELE_READ | ELE_HANGUP))) 165 logerrx("%s: unexpected event 0x%04x", __func__, events); 166 167 if (events & ELE_READ) { 168 len = read(ctx->ps_ctl->psp_work_fd, buf, sizeof(buf)); 169 if (len == -1) 170 logerr("%s: read", __func__); 171 else if (len == 0) 172 // FIXME: Why does this happen? 173 ; 174 else if (ctx->ps_control_client == NULL) 175 logerrx("%s: clientfd #%d disconnected (len=%zd)", 176 __func__, ctx->ps_ctl->psp_work_fd, len); 177 else { 178 errno = 0; 179 if (control_queue(ctx->ps_control_client, 180 buf, (size_t)len) == -1) 181 logerr("%s: control_queue", __func__); 182 } 183 } 184 } 185 186 static void 187 ps_ctl_listen(void *arg, unsigned short events) 188 { 189 struct dhcpcd_ctx *ctx = arg; 190 char buf[BUFSIZ]; 191 ssize_t len; 192 struct fd_list *fd; 193 194 if (!(events & ELE_READ)) 195 logerrx("%s: unexpected event 0x%04x", __func__, events); 196 197 len = read(ctx->ps_control->fd, buf, sizeof(buf)); 198 if (len == -1) { 199 logerr("%s: read", __func__); 200 eloop_exit(ctx->eloop, EXIT_FAILURE); 201 return; 202 } 203 204 /* Send to our listeners */ 205 TAILQ_FOREACH(fd, &ctx->control_fds, next) { 206 if (!(fd->flags & FD_LISTEN)) 207 continue; 208 if (control_queue(fd, buf, (size_t)len)== -1) 209 logerr("%s: control_queue", __func__); 210 } 211 } 212 213 pid_t 214 ps_ctl_start(struct dhcpcd_ctx *ctx) 215 { 216 struct ps_id id = { 217 .psi_ifindex = 0, 218 .psi_cmd = PS_CTL, 219 }; 220 struct ps_process *psp; 221 int data_fd[2], listen_fd[2]; 222 pid_t pid; 223 224 if (xsocketpair(AF_UNIX, SOCK_STREAM | SOCK_CXNB, 0, data_fd) == -1 || 225 xsocketpair(AF_UNIX, SOCK_STREAM | SOCK_CXNB, 0, listen_fd) == -1) 226 return -1; 227 #ifdef PRIVSEP_RIGHTS 228 if (ps_rights_limit_fdpair(data_fd) == -1 || 229 ps_rights_limit_fdpair(listen_fd) == -1) 230 return -1; 231 #endif 232 233 psp = ctx->ps_ctl = ps_newprocess(ctx, &id); 234 strlcpy(psp->psp_name, "control proxy", sizeof(psp->psp_name)); 235 pid = ps_startprocess(psp, ps_ctl_recvmsg, ps_ctl_dodispatch, 236 ps_ctl_startcb, NULL, PSF_DROPPRIVS); 237 238 if (pid == -1) 239 return -1; 240 else if (pid != 0) { 241 psp->psp_work_fd = data_fd[1]; 242 close(data_fd[0]); 243 ctx->ps_control = control_new(ctx, 244 listen_fd[1], FD_SENDLEN | FD_LISTEN); 245 if (ctx->ps_control == NULL) 246 return -1; 247 close(listen_fd[0]); 248 return pid; 249 } 250 251 psp->psp_work_fd = data_fd[0]; 252 close(data_fd[1]); 253 if (eloop_event_add(ctx->eloop, psp->psp_work_fd, ELE_READ, 254 ps_ctl_recv, ctx) == -1) 255 return -1; 256 257 ctx->ps_control = control_new(ctx, listen_fd[0], 0); 258 close(listen_fd[1]); 259 if (ctx->ps_control == NULL) 260 return -1; 261 if (eloop_event_add(ctx->eloop, ctx->ps_control->fd, ELE_READ, 262 ps_ctl_listen, ctx) == -1) 263 return -1; 264 265 ps_entersandbox("stdio inet", NULL); 266 return 0; 267 } 268 269 int 270 ps_ctl_stop(struct dhcpcd_ctx *ctx) 271 { 272 273 return ps_stopprocess(ctx->ps_ctl); 274 } 275 276 ssize_t 277 ps_ctl_sendargs(struct fd_list *fd, void *data, size_t len) 278 { 279 struct dhcpcd_ctx *ctx = fd->ctx; 280 281 if (ctx->ps_control_client != NULL && ctx->ps_control_client != fd) 282 logerrx("%s: cannot deal with another client", __func__); 283 ctx->ps_control_client = fd; 284 return ps_sendcmd(ctx, ctx->ps_ctl->psp_fd, PS_CTL, 285 fd->flags & FD_UNPRIV ? PS_CTL_UNPRIV : PS_CTL_PRIV, 286 data, len); 287 } 288 289 ssize_t 290 ps_ctl_sendeof(struct fd_list *fd) 291 { 292 struct dhcpcd_ctx *ctx = fd->ctx; 293 294 return ps_sendcmd(ctx, ctx->ps_ctl->psp_fd, PS_CTL_EOF, 0, NULL, 0); 295 } 296