xref: /freebsd/sys/kern/kern_jail.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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$
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/socket.h>
23 #include <sys/sysctl.h>
24 #include <net/if.h>
25 #include <netinet/in.h>
26 
27 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
28 
29 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
30     "Jail rules");
31 
32 int	jail_set_hostname_allowed = 1;
33 SYSCTL_INT(_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
34     &jail_set_hostname_allowed, 0,
35     "Processes in jail can set their hostnames");
36 
37 int	jail_socket_unixiproute_only = 1;
38 SYSCTL_INT(_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
39     &jail_socket_unixiproute_only, 0,
40     "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
41 
42 int
43 jail(p, uap)
44         struct proc *p;
45         struct jail_args /* {
46                 syscallarg(struct jail *) jail;
47         } */ *uap;
48 {
49 	int error;
50 	struct prison *pr;
51 	struct jail j;
52 	struct chroot_args ca;
53 
54 	error = suser(p);
55 	if (error)
56 		return (error);
57 	error = copyin(uap->jail, &j, sizeof j);
58 	if (error)
59 		return (error);
60 	if (j.version != 0)
61 		return (EINVAL);
62 	MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK);
63 	bzero((caddr_t)pr, sizeof *pr);
64 	error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
65 	if (error)
66 		goto bail;
67 	pr->pr_ip = j.ip_number;
68 
69 	ca.path = j.path;
70 	error = chroot(p, &ca);
71 	if (error)
72 		goto bail;
73 
74 	pr->pr_ref++;
75 	p->p_prison = pr;
76 	p->p_flag |= P_JAILED;
77 	return (0);
78 
79 bail:
80 	FREE(pr, M_PRISON);
81 	return (error);
82 }
83 
84 int
85 prison_ip(struct proc *p, int flag, u_int32_t *ip)
86 {
87 	u_int32_t tmp;
88 
89 	if (!p->p_prison)
90 		return (0);
91 	if (flag)
92 		tmp = *ip;
93 	else
94 		tmp = ntohl(*ip);
95 	if (tmp == INADDR_ANY) {
96 		if (flag)
97 			*ip = p->p_prison->pr_ip;
98 		else
99 			*ip = htonl(p->p_prison->pr_ip);
100 		return (0);
101 	}
102 	if (p->p_prison->pr_ip != tmp)
103 		return (1);
104 	return (0);
105 }
106 
107 void
108 prison_remote_ip(struct proc *p, int flag, u_int32_t *ip)
109 {
110 	u_int32_t tmp;
111 
112 	if (!p || !p->p_prison)
113 		return;
114 	if (flag)
115 		tmp = *ip;
116 	else
117 		tmp = ntohl(*ip);
118 	if (tmp == 0x7f000001) {
119 		if (flag)
120 			*ip = p->p_prison->pr_ip;
121 		else
122 			*ip = htonl(p->p_prison->pr_ip);
123 		return;
124 	}
125 	return;
126 }
127 
128 int
129 prison_if(struct proc *p, struct sockaddr *sa)
130 {
131 	struct sockaddr_in *sai = (struct sockaddr_in*) sa;
132 	int ok;
133 
134 	if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
135 		ok = 1;
136 	else if (sai->sin_family != AF_INET)
137 		ok = 0;
138 	else if (p->p_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
139 		ok = 1;
140 	else
141 		ok = 0;
142 	return (ok);
143 }
144