1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Privilege Separation BPF Initiator 4 * Copyright (c) 2006-2020 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 <sys/socket.h> 30 #include <sys/types.h> 31 32 /* Need these headers just for if_ether on some OS. */ 33 #ifndef __NetBSD__ 34 #include <net/if.h> 35 #include <net/if_arp.h> 36 #include <netinet/in.h> 37 #endif 38 #include <netinet/if_ether.h> 39 40 #include <assert.h> 41 #include <pwd.h> 42 #include <errno.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #include "arp.h" 48 #include "bpf.h" 49 #include "dhcp.h" 50 #include "dhcp6.h" 51 #include "eloop.h" 52 #include "ipv6nd.h" 53 #include "logerr.h" 54 #include "privsep.h" 55 56 static void 57 ps_bpf_recvbpf(void *arg) 58 { 59 struct ps_process *psp = arg; 60 struct bpf *bpf = psp->psp_bpf; 61 uint8_t buf[FRAMELEN_MAX]; 62 ssize_t len; 63 struct ps_msghdr psm = { 64 .ps_id = psp->psp_id, 65 .ps_cmd = psp->psp_id.psi_cmd, 66 }; 67 68 bpf->bpf_flags &= ~BPF_EOF; 69 /* A BPF read can read more than one filtered packet at time. 70 * This mechanism allows us to read each packet from the buffer. */ 71 while (!(bpf->bpf_flags & BPF_EOF)) { 72 len = bpf_read(bpf, buf, sizeof(buf)); 73 if (len == -1) { 74 int error = errno; 75 76 if (errno != ENETDOWN) 77 logerr("%s: %s", psp->psp_ifname, __func__); 78 if (error != ENXIO) 79 break; 80 /* If the interface has departed, close the BPF 81 * socket. This stops log spam if RTM_IFANNOUNCE is 82 * delayed in announcing the departing interface. */ 83 eloop_event_delete(psp->psp_ctx->eloop, bpf->bpf_fd); 84 bpf_close(bpf); 85 psp->psp_bpf = NULL; 86 break; 87 } 88 if (len == 0) 89 break; 90 psm.ps_flags = bpf->bpf_flags; 91 len = ps_sendpsmdata(psp->psp_ctx, psp->psp_ctx->ps_data_fd, 92 &psm, buf, (size_t)len); 93 if (len == -1) 94 logerr(__func__); 95 if (len == -1 || len == 0) 96 break; 97 } 98 } 99 100 static ssize_t 101 ps_bpf_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) 102 { 103 struct ps_process *psp = arg; 104 struct iovec *iov = msg->msg_iov; 105 106 #ifdef PRIVSEP_DEBUG 107 logerrx("%s: IN cmd %x, psp %p", __func__, psm->ps_cmd, psp); 108 #endif 109 110 switch(psm->ps_cmd) { 111 #ifdef ARP 112 case PS_BPF_ARP: /* FALLTHROUGH */ 113 #endif 114 case PS_BPF_BOOTP: 115 break; 116 default: 117 /* IPC failure, we should not be processing any commands 118 * at this point!/ */ 119 errno = EINVAL; 120 return -1; 121 } 122 123 /* We might have had an earlier ENXIO error. */ 124 if (psp->psp_bpf == NULL) { 125 errno = ENXIO; 126 return -1; 127 } 128 129 return bpf_send(psp->psp_bpf, psp->psp_proto, 130 iov->iov_base, iov->iov_len); 131 } 132 133 static void 134 ps_bpf_recvmsg(void *arg) 135 { 136 struct ps_process *psp = arg; 137 138 if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, 139 ps_bpf_recvmsgcb, arg) == -1) 140 logerr(__func__); 141 } 142 143 static int 144 ps_bpf_start_bpf(void *arg) 145 { 146 struct ps_process *psp = arg; 147 struct dhcpcd_ctx *ctx = psp->psp_ctx; 148 char *addr; 149 struct in_addr *ia = &psp->psp_id.psi_addr.psa_in_addr; 150 151 if (ia->s_addr == INADDR_ANY) { 152 ia = NULL; 153 addr = NULL; 154 } else 155 addr = inet_ntoa(*ia); 156 setproctitle("[BPF %s] %s%s%s", psp->psp_protostr, psp->psp_ifname, 157 addr != NULL ? " " : "", addr != NULL ? addr : ""); 158 ps_freeprocesses(ctx, psp); 159 160 psp->psp_bpf = bpf_open(&psp->psp_ifp, psp->psp_filter, ia); 161 if (psp->psp_bpf == NULL) 162 logerr("%s: bpf_open",__func__); 163 #ifdef PRIVSEP_RIGHTS 164 else if (ps_rights_limit_fd(psp->psp_bpf->bpf_fd) == -1) 165 logerr("%s: ps_rights_limit_fd", __func__); 166 #endif 167 else if (eloop_event_add(ctx->eloop, 168 psp->psp_bpf->bpf_fd, ps_bpf_recvbpf, psp) == -1) 169 logerr("%s: eloop_event_add", __func__); 170 else { 171 psp->psp_work_fd = psp->psp_bpf->bpf_fd; 172 return 0; 173 } 174 175 eloop_exit(ctx->eloop, EXIT_FAILURE); 176 return -1; 177 } 178 179 ssize_t 180 ps_bpf_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) 181 { 182 uint16_t cmd; 183 struct ps_process *psp; 184 pid_t start; 185 struct iovec *iov = msg->msg_iov; 186 struct interface *ifp; 187 188 cmd = (uint16_t)(psm->ps_cmd & ~(PS_START | PS_STOP)); 189 psp = ps_findprocess(ctx, &psm->ps_id); 190 191 #ifdef PRIVSEP_DEBUG 192 logerrx("%s: IN cmd %x, psp %p", __func__, psm->ps_cmd, psp); 193 #endif 194 195 switch (cmd) { 196 #ifdef ARP 197 case PS_BPF_ARP: /* FALLTHROUGH */ 198 #endif 199 case PS_BPF_BOOTP: 200 break; 201 default: 202 logerrx("%s: unknown command %x", __func__, psm->ps_cmd); 203 errno = ENOTSUP; 204 return -1; 205 } 206 207 if (!(psm->ps_cmd & PS_START)) { 208 errno = EINVAL; 209 return -1; 210 } 211 212 if (psp != NULL) 213 return 1; 214 215 psp = ps_newprocess(ctx, &psm->ps_id); 216 if (psp == NULL) 217 return -1; 218 219 ifp = &psp->psp_ifp; 220 assert(msg->msg_iovlen == 1); 221 assert(iov->iov_len == sizeof(*ifp)); 222 memcpy(ifp, iov->iov_base, sizeof(*ifp)); 223 ifp->ctx = psp->psp_ctx; 224 ifp->options = NULL; 225 memset(ifp->if_data, 0, sizeof(ifp->if_data)); 226 227 memcpy(psp->psp_ifname, ifp->name, sizeof(psp->psp_ifname)); 228 229 switch (cmd) { 230 #ifdef ARP 231 case PS_BPF_ARP: 232 psp->psp_proto = ETHERTYPE_ARP; 233 psp->psp_protostr = "ARP"; 234 psp->psp_filter = bpf_arp; 235 break; 236 #endif 237 case PS_BPF_BOOTP: 238 psp->psp_proto = ETHERTYPE_IP; 239 psp->psp_protostr = "BOOTP"; 240 psp->psp_filter = bpf_bootp; 241 break; 242 } 243 244 start = ps_dostart(ctx, 245 &psp->psp_pid, &psp->psp_fd, 246 ps_bpf_recvmsg, NULL, psp, 247 ps_bpf_start_bpf, NULL, 248 PSF_DROPPRIVS); 249 switch (start) { 250 case -1: 251 ps_freeprocess(psp); 252 return -1; 253 case 0: 254 ps_entersandbox("stdio", NULL); 255 break; 256 default: 257 #ifdef PRIVSEP_DEBUG 258 logdebugx("%s: spawned BPF %s on PID %d", 259 psp->psp_ifname, psp->psp_protostr, start); 260 #endif 261 break; 262 } 263 return start; 264 } 265 266 ssize_t 267 ps_bpf_dispatch(struct dhcpcd_ctx *ctx, 268 struct ps_msghdr *psm, struct msghdr *msg) 269 { 270 struct iovec *iov = msg->msg_iov; 271 struct interface *ifp; 272 uint8_t *bpf; 273 size_t bpf_len; 274 275 switch (psm->ps_cmd) { 276 #ifdef ARP 277 case PS_BPF_ARP: 278 #endif 279 case PS_BPF_BOOTP: 280 break; 281 default: 282 errno = ENOTSUP; 283 return -1; 284 } 285 286 ifp = if_findindex(ctx->ifaces, psm->ps_id.psi_ifindex); 287 /* interface may have departed .... */ 288 if (ifp == NULL) 289 return -1; 290 291 bpf = iov->iov_base; 292 bpf_len = iov->iov_len; 293 294 switch (psm->ps_cmd) { 295 #ifdef ARP 296 case PS_BPF_ARP: 297 arp_packet(ifp, bpf, bpf_len, (unsigned int)psm->ps_flags); 298 break; 299 #endif 300 case PS_BPF_BOOTP: 301 dhcp_packet(ifp, bpf, bpf_len, (unsigned int)psm->ps_flags); 302 break; 303 } 304 305 return 1; 306 } 307 308 static ssize_t 309 ps_bpf_send(const struct interface *ifp, const struct in_addr *ia, 310 uint16_t cmd, const void *data, size_t len) 311 { 312 struct dhcpcd_ctx *ctx = ifp->ctx; 313 struct ps_msghdr psm = { 314 .ps_cmd = cmd, 315 .ps_id = { 316 .psi_ifindex = ifp->index, 317 .psi_cmd = (uint8_t)(cmd & ~(PS_START | PS_STOP)), 318 }, 319 }; 320 321 if (ia != NULL) 322 psm.ps_id.psi_addr.psa_in_addr = *ia; 323 324 return ps_sendpsmdata(ctx, ctx->ps_root_fd, &psm, data, len); 325 } 326 327 #ifdef ARP 328 ssize_t 329 ps_bpf_openarp(const struct interface *ifp, const struct in_addr *ia) 330 { 331 332 assert(ia != NULL); 333 return ps_bpf_send(ifp, ia, PS_BPF_ARP | PS_START, 334 ifp, sizeof(*ifp)); 335 } 336 337 ssize_t 338 ps_bpf_closearp(const struct interface *ifp, const struct in_addr *ia) 339 { 340 341 return ps_bpf_send(ifp, ia, PS_BPF_ARP | PS_STOP, NULL, 0); 342 } 343 344 ssize_t 345 ps_bpf_sendarp(const struct interface *ifp, const struct in_addr *ia, 346 const void *data, size_t len) 347 { 348 349 assert(ia != NULL); 350 return ps_bpf_send(ifp, ia, PS_BPF_ARP, data, len); 351 } 352 #endif 353 354 ssize_t 355 ps_bpf_openbootp(const struct interface *ifp) 356 { 357 358 return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP | PS_START, 359 ifp, sizeof(*ifp)); 360 } 361 362 ssize_t 363 ps_bpf_closebootp(const struct interface *ifp) 364 { 365 366 return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP | PS_STOP, NULL, 0); 367 } 368 369 ssize_t 370 ps_bpf_sendbootp(const struct interface *ifp, const void *data, size_t len) 371 { 372 373 return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP, data, len); 374 } 375