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 jail_sysvipc_allowed = 0; 43 SYSCTL_INT(_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW, 44 &jail_sysvipc_allowed, 0, 45 "Processes in jail can use System V IPC primitives"); 46 47 int 48 jail(p, uap) 49 struct proc *p; 50 struct jail_args /* { 51 syscallarg(struct jail *) jail; 52 } */ *uap; 53 { 54 int error; 55 struct prison *pr; 56 struct jail j; 57 struct chroot_args ca; 58 59 error = suser(p); 60 if (error) 61 return (error); 62 error = copyin(uap->jail, &j, sizeof j); 63 if (error) 64 return (error); 65 if (j.version != 0) 66 return (EINVAL); 67 MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK | M_ZERO); 68 error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0); 69 if (error) 70 goto bail; 71 pr->pr_ip = j.ip_number; 72 73 ca.path = j.path; 74 error = chroot(p, &ca); 75 if (error) 76 goto bail; 77 78 pr->pr_ref++; 79 p->p_prison = pr; 80 p->p_flag |= P_JAILED; 81 return (0); 82 83 bail: 84 FREE(pr, M_PRISON); 85 return (error); 86 } 87 88 int 89 prison_ip(struct proc *p, int flag, u_int32_t *ip) 90 { 91 u_int32_t tmp; 92 93 if (!p->p_prison) 94 return (0); 95 if (flag) 96 tmp = *ip; 97 else 98 tmp = ntohl(*ip); 99 if (tmp == INADDR_ANY) { 100 if (flag) 101 *ip = p->p_prison->pr_ip; 102 else 103 *ip = htonl(p->p_prison->pr_ip); 104 return (0); 105 } 106 if (p->p_prison->pr_ip != tmp) 107 return (1); 108 return (0); 109 } 110 111 void 112 prison_remote_ip(struct proc *p, int flag, u_int32_t *ip) 113 { 114 u_int32_t tmp; 115 116 if (!p || !p->p_prison) 117 return; 118 if (flag) 119 tmp = *ip; 120 else 121 tmp = ntohl(*ip); 122 if (tmp == 0x7f000001) { 123 if (flag) 124 *ip = p->p_prison->pr_ip; 125 else 126 *ip = htonl(p->p_prison->pr_ip); 127 return; 128 } 129 return; 130 } 131 132 int 133 prison_if(struct proc *p, struct sockaddr *sa) 134 { 135 struct sockaddr_in *sai = (struct sockaddr_in*) sa; 136 int ok; 137 138 if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only) 139 ok = 1; 140 else if (sai->sin_family != AF_INET) 141 ok = 0; 142 else if (p->p_prison->pr_ip != ntohl(sai->sin_addr.s_addr)) 143 ok = 1; 144 else 145 ok = 0; 146 return (ok); 147 } 148