kern_jail.c revision 113630
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: head/sys/kern/kern_jail.c 113630 2003-04-17 22:26:53Z jhb $
10 *
11 */
12
13#include <sys/param.h>
14#include <sys/types.h>
15#include <sys/kernel.h>
16#include <sys/systm.h>
17#include <sys/errno.h>
18#include <sys/sysproto.h>
19#include <sys/malloc.h>
20#include <sys/proc.h>
21#include <sys/jail.h>
22#include <sys/lock.h>
23#include <sys/mutex.h>
24#include <sys/namei.h>
25#include <sys/queue.h>
26#include <sys/socket.h>
27#include <sys/syscallsubr.h>
28#include <sys/sysctl.h>
29#include <sys/vnode.h>
30#include <net/if.h>
31#include <netinet/in.h>
32
33MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
34
35SYSCTL_DECL(_security);
36SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
37    "Jail rules");
38
39mp_fixme("these variables need a lock")
40
41int	jail_set_hostname_allowed = 1;
42SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
43    &jail_set_hostname_allowed, 0,
44    "Processes in jail can set their hostnames");
45
46int	jail_socket_unixiproute_only = 1;
47SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
48    &jail_socket_unixiproute_only, 0,
49    "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
50
51int	jail_sysvipc_allowed = 0;
52SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
53    &jail_sysvipc_allowed, 0,
54    "Processes in jail can use System V IPC primitives");
55
56/* allprison, lastprid, and prisoncount are protected by allprison_mtx. */
57struct	prisonlist allprison;
58struct	mtx allprison_mtx;
59int	lastprid = 0;
60int	prisoncount = 0;
61
62static void		 init_prison(void *);
63static struct prison	*prison_find(int);
64static int		 sysctl_jail_list(SYSCTL_HANDLER_ARGS);
65
66static void
67init_prison(void *data __unused)
68{
69
70	mtx_init(&allprison_mtx, "allprison", NULL, MTX_DEF);
71	LIST_INIT(&allprison);
72}
73
74SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
75
76/*
77 * MPSAFE
78 */
79int
80jail(td, uap)
81	struct thread *td;
82	struct jail_args /* {
83		struct jail *jail;
84	} */ *uap;
85{
86	struct nameidata nd;
87	struct prison *pr, *tpr;
88	struct jail j;
89	struct jail_attach_args jaa;
90	int error, tryprid;
91
92	error = copyin(uap->jail, &j, sizeof j);
93	if (error)
94		return (error);
95	if (j.version != 0)
96		return (EINVAL);
97
98	MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK | M_ZERO);
99	mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
100	pr->pr_ref = 1;
101	error = copyinstr(j.path, &pr->pr_path, sizeof pr->pr_path, 0);
102	if (error)
103		goto e_killmtx;
104	mtx_lock(&Giant);
105	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, pr->pr_path, td);
106	error = namei(&nd);
107	if (error) {
108		mtx_unlock(&Giant);
109		goto e_killmtx;
110	}
111	pr->pr_root = nd.ni_vp;
112	VOP_UNLOCK(nd.ni_vp, 0, td);
113	NDFREE(&nd, NDF_ONLY_PNBUF);
114	mtx_unlock(&Giant);
115	error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
116	if (error)
117		goto e_dropvnref;
118	pr->pr_ip = j.ip_number;
119	pr->pr_linux = NULL;
120	pr->pr_securelevel = securelevel;
121
122	/* Determine next pr_id and add prison to allprison list. */
123	mtx_lock(&allprison_mtx);
124	tryprid = lastprid + 1;
125	if (tryprid == JAIL_MAX)
126		tryprid = 1;
127next:
128	LIST_FOREACH(tpr, &allprison, pr_list) {
129		if (tpr->pr_id == tryprid) {
130			tryprid++;
131			if (tryprid == JAIL_MAX) {
132				mtx_unlock(&allprison_mtx);
133				error = EAGAIN;
134				goto e_dropvnref;
135			}
136			goto next;
137		}
138	}
139	pr->pr_id = jaa.jid = lastprid = tryprid;
140	LIST_INSERT_HEAD(&allprison, pr, pr_list);
141	prisoncount++;
142	mtx_unlock(&allprison_mtx);
143
144	error = jail_attach(td, &jaa);
145	if (error)
146		goto e_dropprref;
147	mtx_lock(&pr->pr_mtx);
148	pr->pr_ref--;
149	mtx_unlock(&pr->pr_mtx);
150	td->td_retval[0] = jaa.jid;
151	return (0);
152e_dropprref:
153	mtx_lock(&allprison_mtx);
154	LIST_REMOVE(pr, pr_list);
155	prisoncount--;
156	mtx_unlock(&allprison_mtx);
157e_dropvnref:
158	mtx_lock(&Giant);
159	vrele(pr->pr_root);
160	mtx_unlock(&Giant);
161e_killmtx:
162	mtx_destroy(&pr->pr_mtx);
163	FREE(pr, M_PRISON);
164	return (error);
165}
166
167/*
168 * MPSAFE
169 */
170int
171jail_attach(td, uap)
172	struct thread *td;
173	struct jail_attach_args /* {
174		int jid;
175	} */ *uap;
176{
177	struct proc *p;
178	struct ucred *newcred, *oldcred;
179	struct prison *pr;
180	int error;
181
182	p = td->td_proc;
183
184	mtx_lock(&allprison_mtx);
185	pr = prison_find(uap->jid);
186	if (pr == NULL) {
187		mtx_unlock(&allprison_mtx);
188		return (EINVAL);
189	}
190	pr->pr_ref++;
191	mtx_unlock(&pr->pr_mtx);
192	mtx_unlock(&allprison_mtx);
193
194	error = suser_cred(td->td_ucred, PRISON_ROOT);
195	if (error)
196		goto e_dropref;
197	mtx_lock(&Giant);
198	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td);
199	if ((error = change_dir(pr->pr_root, td)) != 0)
200		goto e_unlock;
201#ifdef MAC
202	if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root)))
203		goto e_unlock;
204#endif
205	VOP_UNLOCK(pr->pr_root, 0, td);
206	change_root(pr->pr_root, td);
207	mtx_unlock(&Giant);
208
209	newcred = crget();
210	PROC_LOCK(p);
211	/* Implicitly fail if already in jail.  */
212	error = suser_cred(p->p_ucred, 0);
213	if (error) {
214		PROC_UNLOCK(p);
215		crfree(newcred);
216		goto e_dropref;
217	}
218	oldcred = p->p_ucred;
219	setsugid(p);
220	crcopy(newcred, oldcred);
221	newcred->cr_prison = pr;
222	p->p_ucred = newcred;
223	PROC_UNLOCK(p);
224	crfree(oldcred);
225	return (0);
226e_unlock:
227	VOP_UNLOCK(pr->pr_root, 0, td);
228	mtx_unlock(&Giant);
229e_dropref:
230	mtx_lock(&pr->pr_mtx);
231	pr->pr_ref--;
232	mtx_unlock(&pr->pr_mtx);
233	return (error);
234}
235
236/*
237 * Returns a locked prison instance, or NULL on failure.
238 */
239static struct prison *
240prison_find(int prid)
241{
242	struct prison *pr;
243
244	mtx_assert(&allprison_mtx, MA_OWNED);
245	LIST_FOREACH(pr, &allprison, pr_list) {
246		if (pr->pr_id == prid) {
247			mtx_lock(&pr->pr_mtx);
248			return (pr);
249		}
250	}
251	return (NULL);
252}
253
254void
255prison_free(struct prison *pr)
256{
257
258	mtx_assert(&Giant, MA_OWNED);
259	mtx_lock(&allprison_mtx);
260	mtx_lock(&pr->pr_mtx);
261	pr->pr_ref--;
262	if (pr->pr_ref == 0) {
263		LIST_REMOVE(pr, pr_list);
264		mtx_unlock(&pr->pr_mtx);
265		prisoncount--;
266		mtx_unlock(&allprison_mtx);
267		vrele(pr->pr_root);
268		mtx_destroy(&pr->pr_mtx);
269		if (pr->pr_linux != NULL)
270			FREE(pr->pr_linux, M_PRISON);
271		FREE(pr, M_PRISON);
272		return;
273	}
274	mtx_unlock(&pr->pr_mtx);
275	mtx_unlock(&allprison_mtx);
276}
277
278void
279prison_hold(struct prison *pr)
280{
281
282	mtx_lock(&pr->pr_mtx);
283	pr->pr_ref++;
284	mtx_unlock(&pr->pr_mtx);
285}
286
287u_int32_t
288prison_getip(struct ucred *cred)
289{
290
291	return (cred->cr_prison->pr_ip);
292}
293
294int
295prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
296{
297	u_int32_t tmp;
298
299	if (!jailed(cred))
300		return (0);
301	if (flag)
302		tmp = *ip;
303	else
304		tmp = ntohl(*ip);
305	if (tmp == INADDR_ANY) {
306		if (flag)
307			*ip = cred->cr_prison->pr_ip;
308		else
309			*ip = htonl(cred->cr_prison->pr_ip);
310		return (0);
311	}
312	if (tmp == INADDR_LOOPBACK) {
313		if (flag)
314			*ip = cred->cr_prison->pr_ip;
315		else
316			*ip = htonl(cred->cr_prison->pr_ip);
317		return (0);
318	}
319	if (cred->cr_prison->pr_ip != tmp)
320		return (1);
321	return (0);
322}
323
324void
325prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
326{
327	u_int32_t tmp;
328
329	if (!jailed(cred))
330		return;
331	if (flag)
332		tmp = *ip;
333	else
334		tmp = ntohl(*ip);
335	if (tmp == INADDR_LOOPBACK) {
336		if (flag)
337			*ip = cred->cr_prison->pr_ip;
338		else
339			*ip = htonl(cred->cr_prison->pr_ip);
340		return;
341	}
342	return;
343}
344
345int
346prison_if(struct ucred *cred, struct sockaddr *sa)
347{
348	struct sockaddr_in *sai = (struct sockaddr_in*) sa;
349	int ok;
350
351	if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
352		ok = 1;
353	else if (sai->sin_family != AF_INET)
354		ok = 0;
355	else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
356		ok = 1;
357	else
358		ok = 0;
359	return (ok);
360}
361
362/*
363 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
364 */
365int
366prison_check(cred1, cred2)
367	struct ucred *cred1, *cred2;
368{
369
370	if (jailed(cred1)) {
371		if (!jailed(cred2))
372			return (ESRCH);
373		if (cred2->cr_prison != cred1->cr_prison)
374			return (ESRCH);
375	}
376
377	return (0);
378}
379
380/*
381 * Return 1 if the passed credential is in a jail, otherwise 0.
382 */
383int
384jailed(cred)
385	struct ucred *cred;
386{
387
388	return (cred->cr_prison != NULL);
389}
390
391/*
392 * Return the correct hostname for the passed credential.
393 */
394void
395getcredhostname(cred, buf, size)
396	struct ucred *cred;
397	char *buf;
398	size_t size;
399{
400
401	if (jailed(cred)) {
402		mtx_lock(&cred->cr_prison->pr_mtx);
403		strlcpy(buf, cred->cr_prison->pr_host, size);
404		mtx_unlock(&cred->cr_prison->pr_mtx);
405	}
406	else
407		strlcpy(buf, hostname, size);
408}
409
410static int
411sysctl_jail_list(SYSCTL_HANDLER_ARGS)
412{
413	struct xprison *xp, *sxp;
414	struct prison *pr;
415	int count, error;
416
417	mtx_assert(&Giant, MA_OWNED);
418retry:
419	mtx_lock(&allprison_mtx);
420	count = prisoncount;
421	mtx_unlock(&allprison_mtx);
422
423	if (count == 0)
424		return (0);
425
426	sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
427	mtx_lock(&allprison_mtx);
428	if (count != prisoncount) {
429		mtx_unlock(&allprison_mtx);
430		free(sxp, M_TEMP);
431		goto retry;
432	}
433
434	LIST_FOREACH(pr, &allprison, pr_list) {
435		mtx_lock(&pr->pr_mtx);
436		xp->pr_version = XPRISON_VERSION;
437		xp->pr_id = pr->pr_id;
438		strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
439		strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
440		xp->pr_ip = pr->pr_ip;
441		mtx_unlock(&pr->pr_mtx);
442		xp++;
443	}
444	mtx_unlock(&allprison_mtx);
445
446	error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
447	free(sxp, M_TEMP);
448	if (error)
449		return (error);
450	return (0);
451}
452
453SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
454    NULL, 0, sysctl_jail_list, "S", "List of active jails");
455