1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Privilege Separation for dhcpcd
4  * Copyright (c) 2006-2021 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 /* Protocols */
38 #define	PS_BOOTP		0x0001
39 #define	PS_ND			0x0002
40 #define	PS_DHCP6		0x0003
41 #define	PS_BPF_BOOTP		0x0004
42 #define	PS_BPF_ARP		0x0005
43 
44 /* Generic commands */
45 #define	PS_IOCTL		0x0010
46 #define	PS_ROUTE		0x0011	/* Also used for NETLINK */
47 #define	PS_SCRIPT		0x0012
48 #define	PS_UNLINK		0x0013
49 #define	PS_READFILE		0x0014
50 #define	PS_WRITEFILE		0x0015
51 #define	PS_FILEMTIME		0x0016
52 #define	PS_AUTH_MONORDM		0x0017
53 #define	PS_CTL			0x0018
54 #define	PS_CTL_EOF		0x0019
55 #define	PS_LOGREOPEN		0x0020
56 
57 /* BSD Commands */
58 #define	PS_IOCTLLINK		0x0101
59 #define	PS_IOCTL6		0x0102
60 #define	PS_IOCTLINDIRECT	0x0103
61 #define	PS_IP6FORWARDING	0x0104
62 #define	PS_GETIFADDRS		0x0105
63 #define	PS_IFIGNOREGRP		0x0106
64 
65 /* Dev Commands */
66 #define	PS_DEV_LISTENING	0x1001
67 #define	PS_DEV_INITTED		0x1002
68 #define	PS_DEV_IFCMD		0x1003
69 
70 /* Dev Interface Commands (via flags) */
71 #define	PS_DEV_IFADDED		0x0001
72 #define	PS_DEV_IFREMOVED	0x0002
73 #define	PS_DEV_IFUPDATED	0x0003
74 
75 /* Control Type (via flags) */
76 #define	PS_CTL_PRIV		0x0004
77 #define	PS_CTL_UNPRIV		0x0005
78 
79 /* Process commands */
80 #define	PS_START		0x4000
81 #define	PS_STOP			0x8000
82 
83 /* Max INET message size + meta data for IPC */
84 #define	PS_BUFLEN		((64 * 1024) +			\
85 				 sizeof(struct ps_msghdr) +	\
86 				 sizeof(struct msghdr) +	\
87 				 CMSG_SPACE(sizeof(struct in6_pktinfo) + \
88 					    sizeof(int)))
89 
90 /* Handy macro to work out if in the privsep engine or not. */
91 #define	IN_PRIVSEP(ctx)	\
92 	((ctx)->options & DHCPCD_PRIVSEP)
93 #define	IN_PRIVSEP_SE(ctx)	\
94 	(((ctx)->options & (DHCPCD_PRIVSEP | DHCPCD_FORKED)) == DHCPCD_PRIVSEP)
95 
96 #if defined(PRIVSEP) && defined(HAVE_CAPSICUM)
97 #define PRIVSEP_RIGHTS
98 #endif
99 
100 #ifdef __linux__
101 # include <linux/version.h>
102 # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
103 #  define HAVE_SECCOMP
104 # endif
105 #endif
106 
107 #include "config.h"
108 #include "arp.h"
109 #include "dhcp.h"
110 #include "dhcpcd.h"
111 
112 struct ps_addr {
113 	sa_family_t psa_family;
114 	uint8_t psa_pad[4 - sizeof(sa_family_t)];
115 	union {
116 		struct in_addr psau_in_addr;
117 		struct in6_addr psau_in6_addr;
118 	} psa_u;
119 #define	psa_in_addr	psa_u.psau_in_addr
120 #define	psa_in6_addr	psa_u.psau_in6_addr
121 };
122 
123 /* Uniquely identify a process */
124 struct ps_id {
125 	struct ps_addr psi_addr;
126 	unsigned int psi_ifindex;
127 	uint16_t psi_cmd;
128 	uint8_t psi_pad[2];
129 };
130 
131 struct ps_msghdr {
132 	uint16_t ps_cmd;
133 	uint8_t ps_pad[sizeof(unsigned long) - sizeof(uint16_t)];
134 	unsigned long ps_flags;
135 	struct ps_id ps_id;
136 	socklen_t ps_namelen;
137 	socklen_t ps_controllen;
138 	uint8_t ps_pad2[sizeof(size_t) - sizeof(socklen_t)];
139 	size_t ps_datalen;
140 };
141 
142 struct ps_msg {
143 	struct ps_msghdr psm_hdr;
144 	uint8_t psm_data[PS_BUFLEN];
145 };
146 
147 struct bpf;
148 struct ps_process {
149 	TAILQ_ENTRY(ps_process) next;
150 	struct dhcpcd_ctx *psp_ctx;
151 	struct ps_id psp_id;
152 	pid_t psp_pid;
153 	int psp_fd;
154 	int psp_work_fd;
155 	unsigned int psp_ifindex;
156 	char psp_ifname[IF_NAMESIZE];
157 	uint16_t psp_proto;
158 	const char *psp_protostr;
159 
160 #ifdef INET
161 	int (*psp_filter)(const struct bpf *, const struct in_addr *);
162 	struct interface psp_ifp; /* Move BPF gubbins elsewhere */
163 	struct bpf *psp_bpf;
164 #endif
165 };
166 TAILQ_HEAD(ps_process_head, ps_process);
167 
168 #include "privsep-control.h"
169 #include "privsep-inet.h"
170 #include "privsep-root.h"
171 #ifdef INET
172 #include "privsep-bpf.h"
173 #endif
174 
175 int ps_init(struct dhcpcd_ctx *);
176 int ps_start(struct dhcpcd_ctx *);
177 int ps_stop(struct dhcpcd_ctx *);
178 int ps_entersandbox(const char *, const char **);
179 int ps_managersandbox(struct dhcpcd_ctx *, const char *);
180 
181 int ps_unrollmsg(struct msghdr *, struct ps_msghdr *, const void *, size_t);
182 ssize_t ps_sendpsmmsg(struct dhcpcd_ctx *, int,
183     struct ps_msghdr *, const struct msghdr *);
184 ssize_t ps_sendpsmdata(struct dhcpcd_ctx *, int,
185     struct ps_msghdr *, const void *, size_t);
186 ssize_t ps_sendmsg(struct dhcpcd_ctx *, int, uint16_t, unsigned long,
187     const struct msghdr *);
188 ssize_t ps_sendcmd(struct dhcpcd_ctx *, int, uint16_t, unsigned long,
189     const void *data, size_t len);
190 ssize_t ps_recvmsg(struct dhcpcd_ctx *, int, uint16_t, int);
191 ssize_t ps_recvpsmsg(struct dhcpcd_ctx *, int,
192     ssize_t (*callback)(void *, struct ps_msghdr *, struct msghdr *), void *);
193 
194 /* Internal privsep functions. */
195 int ps_setbuf_fdpair(int []);
196 
197 #ifdef PRIVSEP_RIGHTS
198 int ps_rights_limit_ioctl(int);
199 int ps_rights_limit_fd_fctnl(int);
200 int ps_rights_limit_fd_rdonly(int);
201 int ps_rights_limit_fd_sockopt(int);
202 int ps_rights_limit_fd(int);
203 int ps_rights_limit_fdpair(int []);
204 #endif
205 
206 #ifdef HAVE_SECCOMP
207 int ps_seccomp_enter(void);
208 #endif
209 
210 pid_t ps_dostart(struct dhcpcd_ctx * ctx,
211     pid_t *priv_pid, int *priv_fd,
212     void (*recv_msg)(void *), void (*recv_unpriv_msg),
213     void *recv_ctx, int (*callback)(void *), void (*)(int, void *),
214     unsigned int);
215 int ps_dostop(struct dhcpcd_ctx *ctx, pid_t *pid, int *fd);
216 
217 struct ps_process *ps_findprocess(struct dhcpcd_ctx *, struct ps_id *);
218 struct ps_process *ps_newprocess(struct dhcpcd_ctx *, struct ps_id *);
219 void ps_freeprocess(struct ps_process *);
220 void ps_freeprocesses(struct dhcpcd_ctx *, struct ps_process *);
221 #endif
222