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