Home | History | Annotate | Line # | Download | only in src
      1 /* SPDX-License-Identifier: BSD-2-Clause */
      2 /*
      3  * Privilege Separation for dhcpcd
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 #define	PSF_ELOOP		0x02
     37 
     38 /* Protocols */
     39 #define	PS_BOOTP		0x0001
     40 #define	PS_ND			0x0002
     41 #define	PS_DHCP6		0x0003
     42 #define	PS_BPF_BOOTP		0x0004
     43 #define	PS_BPF_ARP		0x0005
     44 
     45 /* Generic commands */
     46 #define	PS_IOCTL		0x0010
     47 #define	PS_ROUTE		0x0011	/* Also used for NETLINK */
     48 #define	PS_SCRIPT		0x0012
     49 #define	PS_UNLINK		0x0013
     50 #define	PS_READFILE		0x0014
     51 #define	PS_WRITEFILE		0x0015
     52 #define	PS_FILEMTIME		0x0016
     53 #define	PS_AUTH_MONORDM		0x0017
     54 #define	PS_CTL			0x0018
     55 #define	PS_CTL_EOF		0x0019
     56 #define	PS_LOGREOPEN		0x0020
     57 #define	PS_STOPPROCS		0x0021
     58 #define	PS_DAEMONISED		0x0022
     59 
     60 /* Domains */
     61 #define	PS_ROOT			0x0101
     62 #define	PS_INET			0x0102
     63 #define	PS_CONTROL		0x0103
     64 
     65 /* BSD Commands */
     66 #define	PS_IOCTLLINK		0x0201
     67 #define	PS_IOCTL6		0x0202
     68 #define	PS_IOCTLINDIRECT	0x0203
     69 #define	PS_IP6FORWARDING	0x0204
     70 #define	PS_GETIFADDRS		0x0205
     71 #define	PS_IFIGNOREGRP		0x0206
     72 #define	PS_SYSCTL		0x0207
     73 
     74 /* Dev Commands */
     75 #define	PS_DEV_LISTENING	0x1001
     76 #define	PS_DEV_INITTED		0x1002
     77 #define	PS_DEV_IFCMD		0x1003
     78 
     79 /* Dev Interface Commands (via flags) */
     80 #define	PS_DEV_IFADDED		0x0001
     81 #define	PS_DEV_IFREMOVED	0x0002
     82 #define	PS_DEV_IFUPDATED	0x0003
     83 
     84 /* Control Type (via flags) */
     85 #define	PS_CTL_PRIV		0x0004
     86 #define	PS_CTL_UNPRIV		0x0005
     87 
     88 /* Sysctl Needs (via flags) */
     89 #define	PS_SYSCTL_OLEN		0x0001
     90 #define	PS_SYSCTL_ODATA		0x0002
     91 
     92 /* Process commands */
     93 #define	PS_START		0x4000
     94 #define	PS_STOP			0x8000
     95 
     96 /* Max INET message size + meta data for IPC */
     97 #define	PS_BUFLEN		((64 * 1024) +			\
     98 				 sizeof(struct ps_msghdr) +	\
     99 				 sizeof(struct msghdr) +	\
    100 				 CMSG_SPACE(sizeof(struct in6_pktinfo) + \
    101 					    sizeof(int)))
    102 
    103 #define	PSP_NAMESIZE		16 + INET_MAX_ADDRSTRLEN
    104 
    105 /* Handy macro to work out if in the privsep engine or not. */
    106 #define	IN_PRIVSEP(ctx)	\
    107 	((ctx)->options & DHCPCD_PRIVSEP)
    108 #define	IN_PRIVSEP_SE(ctx)	\
    109 	(((ctx)->options & (DHCPCD_PRIVSEP | DHCPCD_FORKED)) == DHCPCD_PRIVSEP)
    110 
    111 #define	PS_PROCESS_TIMEOUT	5	/* seconds to stop all processes */
    112 
    113 #ifdef PRIVSEP
    114 # ifdef HAVE_CAPSICUM
    115 #  define PRIVSEP_RIGHTS
    116 # endif
    117 /* Pledge and Capsicum deny nearly all sysctls.
    118  * Linux needs directory access to sysctls. */
    119 # if defined(HAVE_CAPSICUM) ||defined(HAVE_PLEDGE) || defined(__linux__)
    120 #  define PRIVSEP_SYSCTL
    121 # endif
    122 #endif
    123 
    124 #define PS_ROOT_FD(ctx) ((ctx)->ps_root ? (ctx)->ps_root->psp_fd : -1)
    125 
    126 #if !defined(DISABLE_SECCOMP) && defined(__linux__)
    127 # include <linux/version.h>
    128 # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
    129 #  define HAVE_SECCOMP
    130 # endif
    131 #endif
    132 
    133 #include "config.h"
    134 #include "arp.h"
    135 #include "dhcp.h"
    136 #include "dhcpcd.h"
    137 
    138 struct ps_addr {
    139 	sa_family_t psa_family;
    140 	uint8_t psa_pad[4 - sizeof(sa_family_t)];
    141 	union {
    142 		struct in_addr psau_in_addr;
    143 		struct in6_addr psau_in6_addr;
    144 	} psa_u;
    145 #define	psa_in_addr	psa_u.psau_in_addr
    146 #define	psa_in6_addr	psa_u.psau_in6_addr
    147 };
    148 
    149 /* Uniquely identify a process */
    150 struct ps_id {
    151 	struct ps_addr psi_addr;
    152 	unsigned int psi_ifindex;
    153 	uint16_t psi_cmd;
    154 	uint8_t psi_pad[2];
    155 };
    156 
    157 struct ps_msghdr {
    158 	uint16_t ps_cmd;
    159 	uint8_t ps_pad[sizeof(unsigned long) - sizeof(uint16_t)];
    160 	unsigned long ps_flags;
    161 	struct ps_id ps_id;
    162 	socklen_t ps_namelen;
    163 	socklen_t ps_controllen;
    164 	uint8_t ps_pad2[sizeof(size_t) - sizeof(socklen_t)];
    165 	size_t ps_datalen;
    166 };
    167 
    168 struct ps_msg {
    169 	struct ps_msghdr psm_hdr;
    170 	uint8_t psm_data[PS_BUFLEN];
    171 };
    172 
    173 struct bpf;
    174 
    175 struct ps_process {
    176 	TAILQ_ENTRY(ps_process) next;
    177 	struct dhcpcd_ctx *psp_ctx;
    178 	struct ps_id psp_id;
    179 	pid_t psp_pid;
    180 	int psp_fd;
    181 	int psp_work_fd;
    182 	unsigned int psp_ifindex;
    183 	char psp_ifname[IF_NAMESIZE];
    184 	char psp_name[PSP_NAMESIZE];
    185 	uint16_t psp_proto;
    186 	const char *psp_protostr;
    187 	bool psp_started;
    188 
    189 #ifdef INET
    190 	int (*psp_filter)(const struct bpf *, const struct in_addr *);
    191 	struct interface psp_ifp; /* Move BPF gubbins elsewhere */
    192 	struct bpf *psp_bpf;
    193 #endif
    194 
    195 #ifdef HAVE_CAPSICUM
    196 	int psp_pfd;
    197 #endif
    198 };
    199 TAILQ_HEAD(ps_process_head, ps_process);
    200 
    201 #include "privsep-control.h"
    202 #include "privsep-inet.h"
    203 #include "privsep-root.h"
    204 #ifdef INET
    205 #include "privsep-bpf.h"
    206 #endif
    207 
    208 int ps_init(struct dhcpcd_ctx *);
    209 int ps_start(struct dhcpcd_ctx *);
    210 int ps_stop(struct dhcpcd_ctx *);
    211 int ps_stopwait(struct dhcpcd_ctx *);
    212 int ps_entersandbox(const char *, const char **);
    213 int ps_managersandbox(struct dhcpcd_ctx *, const char *);
    214 ssize_t ps_daemonised(struct dhcpcd_ctx *);
    215 
    216 int ps_unrollmsg(struct msghdr *, struct ps_msghdr *, const void *, size_t);
    217 ssize_t ps_sendpsmmsg(struct dhcpcd_ctx *, int,
    218     struct ps_msghdr *, const struct msghdr *);
    219 ssize_t ps_sendpsmdata(struct dhcpcd_ctx *, int,
    220     struct ps_msghdr *, const void *, size_t);
    221 ssize_t ps_sendmsg(struct dhcpcd_ctx *, int, uint16_t, unsigned long,
    222     const struct msghdr *);
    223 ssize_t ps_sendcmd(struct dhcpcd_ctx *, int, uint16_t, unsigned long,
    224     const void *data, size_t len);
    225 ssize_t ps_sendcmdmsg(struct dhcpcd_ctx *, int fd, uint16_t cmd,
    226     unsigned long flags, const struct msghdr *msg);
    227 ssize_t ps_recvmsg(int, unsigned short, uint16_t, int);
    228 ssize_t ps_recvpsmsg(struct dhcpcd_ctx *, int, unsigned short,
    229     ssize_t (*callback)(void *, struct ps_msghdr *, struct msghdr *), void *);
    230 
    231 /* Internal privsep functions. */
    232 int ps_setbuf_fdpair(int []);
    233 
    234 #ifdef PRIVSEP_RIGHTS
    235 int ps_rights_limit_ioctl(int);
    236 int ps_rights_limit_fd_fctnl(int);
    237 int ps_rights_limit_fd_rdonly(int);
    238 int ps_rights_limit_fd_sockopt(int);
    239 int ps_rights_limit_fd(int);
    240 int ps_rights_limit_fdpair(int []);
    241 #endif
    242 
    243 #ifdef HAVE_SECCOMP
    244 int ps_seccomp_enter(void);
    245 #endif
    246 
    247 pid_t ps_startprocess(struct ps_process *,
    248     void (*recv_msg)(void *, unsigned short),
    249     void (*recv_unpriv_msg)(void *, unsigned short),
    250     int (*callback)(struct ps_process *),
    251     unsigned int);
    252 int ps_stopprocess(struct ps_process *);
    253 struct ps_process *ps_findprocess(struct dhcpcd_ctx *, struct ps_id *);
    254 struct ps_process *ps_findprocesspid(struct dhcpcd_ctx *, pid_t);
    255 struct ps_process *ps_newprocess(struct dhcpcd_ctx *, struct ps_id *);
    256 bool ps_waitforprocs(struct dhcpcd_ctx *ctx);
    257 void ps_process_timeout(void *);
    258 void ps_freeprocess(struct ps_process *);
    259 void ps_freeprocesses(struct dhcpcd_ctx *, struct ps_process *);
    260 #endif
    261