1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Privilege Separation for dhcpcd 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 #ifndef PRIVSEP_H 30 #define PRIVSEP_H 31 32 //#define PRIVSEP_DEBUG 33 34 /* Start flags */ 35 #define PSF_DROPPRIVS 0x01 36 37 /* Commands */ 38 #define PS_BOOTP 0x01 39 #define PS_ND 0x02 40 #define PS_DHCP6 0x03 41 #define PS_BPF_BOOTP 0x04 42 #define PS_BPF_ARP 0x05 43 #define PS_BPF_ARP_ADDR 0x06 44 45 #define PS_IOCTL 0x10 46 #define PS_ROUTE 0x11 /* Also used for NETLINK */ 47 #define PS_SCRIPT 0x12 48 #define PS_UNLINK 0x13 49 #define PS_COPY 0x14 50 51 /* BSD Commands */ 52 #define PS_IOCTLLINK 0x15 53 #define PS_IOCTL6 0x16 54 55 /* Linux commands */ 56 #define PS_WRITEPATHUINT 0x17 57 58 #define PS_DELETE 0x20 59 #define PS_START 0x40 60 #define PS_STOP 0x80 61 62 /* Max INET message size + meta data for IPC */ 63 #define PS_BUFLEN ((64 * 1024) + \ 64 sizeof(struct ps_msghdr) + \ 65 sizeof(struct msghdr) + \ 66 CMSG_SPACE(sizeof(struct in6_pktinfo) + \ 67 sizeof(int))) 68 69 /* Handy macro to work out if in the privsep engine or not. */ 70 #define IN_PRIVSEP(ctx) \ 71 ((ctx)->options & DHCPCD_PRIVSEP) 72 #define IN_PRIVSEP_SE(ctx) \ 73 (((ctx)->options & (DHCPCD_PRIVSEP | DHCPCD_FORKED)) == DHCPCD_PRIVSEP) 74 75 #include "config.h" 76 #include "arp.h" 77 #include "dhcp.h" 78 #include "dhcpcd.h" 79 80 struct ps_addr { 81 sa_family_t psa_family; 82 uint8_t psa_pad[4 - sizeof(sa_family_t)]; 83 union { 84 struct in_addr psau_in_addr; 85 struct in6_addr psau_in6_addr; 86 } psa_u; 87 #define psa_in_addr psa_u.psau_in_addr 88 #define psa_in6_addr psa_u.psau_in6_addr 89 }; 90 91 /* Uniquely identify a process */ 92 struct ps_id { 93 struct ps_addr psi_addr; 94 unsigned int psi_ifindex; 95 uint8_t psi_cmd; 96 uint8_t psi_pad[3]; 97 }; 98 99 struct ps_msghdr { 100 uint8_t ps_cmd; 101 uint8_t ps_pad[sizeof(unsigned long) - 1]; 102 unsigned long ps_flags; 103 struct ps_id ps_id; 104 socklen_t ps_namelen; 105 socklen_t ps_controllen; 106 uint8_t ps_pad2[sizeof(size_t) - sizeof(socklen_t)]; 107 size_t ps_datalen; 108 }; 109 110 struct ps_msg { 111 struct ps_msghdr psm_hdr; 112 uint8_t psm_data[PS_BUFLEN]; 113 }; 114 115 struct ps_process { 116 TAILQ_ENTRY(ps_process) next; 117 struct dhcpcd_ctx *psp_ctx; 118 struct ps_id psp_id; 119 pid_t psp_pid; 120 int psp_fd; 121 int psp_work_fd; 122 unsigned int psp_ifindex; 123 char psp_ifname[IF_NAMESIZE]; 124 uint16_t psp_proto; 125 const char *psp_protostr; 126 127 #ifdef INET 128 int (*psp_filter)(struct interface *, int); 129 struct interface psp_ifp; /* Move BPF gubbins elsewhere */ 130 #endif 131 }; 132 TAILQ_HEAD(ps_process_head, ps_process); 133 134 #include "privsep-inet.h" 135 #include "privsep-root.h" 136 #ifdef INET 137 #include "privsep-bpf.h" 138 #endif 139 140 int ps_mkdir(char *); 141 int ps_init(struct dhcpcd_ctx *); 142 int ps_dropprivs(struct dhcpcd_ctx *); 143 int ps_start(struct dhcpcd_ctx *); 144 int ps_stop(struct dhcpcd_ctx *); 145 146 int ps_unrollmsg(struct msghdr *, struct ps_msghdr *, const void *, size_t); 147 ssize_t ps_sendpsmmsg(struct dhcpcd_ctx *, int, 148 struct ps_msghdr *, const struct msghdr *); 149 ssize_t ps_sendpsmdata(struct dhcpcd_ctx *, int, 150 struct ps_msghdr *, const void *, size_t); 151 ssize_t ps_sendmsg(struct dhcpcd_ctx *, int, uint8_t, unsigned long, 152 const struct msghdr *); 153 ssize_t ps_sendcmd(struct dhcpcd_ctx *, int, uint8_t, unsigned long, 154 const void *data, size_t len); 155 ssize_t ps_recvmsg(struct dhcpcd_ctx *, int, uint8_t, int); 156 ssize_t ps_recvpsmsg(struct dhcpcd_ctx *, int, 157 ssize_t (*callback)(void *, struct ps_msghdr *, struct msghdr *), void *); 158 159 /* Internal privsep functions. */ 160 pid_t ps_dostart(struct dhcpcd_ctx * ctx, 161 pid_t *priv_pid, int *priv_fd, 162 void (*recv_msg)(void *), void (*recv_unpriv_msg), 163 void *recv_ctx, int (*callback)(void *), void (*)(int, void *), 164 unsigned int); 165 int ps_dostop(struct dhcpcd_ctx *ctx, pid_t *pid, int *fd); 166 167 struct ps_process *ps_findprocess(struct dhcpcd_ctx *, struct ps_id *); 168 struct ps_process *ps_newprocess(struct dhcpcd_ctx *, struct ps_id *); 169 void ps_freeprocess(struct ps_process *); 170 void ps_freeprocesses(struct dhcpcd_ctx *, struct ps_process *); 171 #endif 172