]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_jail.c
Various style and comment fixes.
[FreeBSD/FreeBSD.git] / sys / kern / kern_jail.c
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
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
12
13 #include "opt_mac.h"
14
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/kernel.h>
18 #include <sys/systm.h>
19 #include <sys/errno.h>
20 #include <sys/sysproto.h>
21 #include <sys/mac.h>
22 #include <sys/malloc.h>
23 #include <sys/proc.h>
24 #include <sys/taskqueue.h>
25 #include <sys/jail.h>
26 #include <sys/lock.h>
27 #include <sys/mutex.h>
28 #include <sys/namei.h>
29 #include <sys/mount.h>
30 #include <sys/queue.h>
31 #include <sys/socket.h>
32 #include <sys/syscallsubr.h>
33 #include <sys/sysctl.h>
34 #include <sys/vnode.h>
35 #include <net/if.h>
36 #include <netinet/in.h>
37
38 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
39
40 SYSCTL_DECL(_security);
41 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
42     "Jail rules");
43
44 int     jail_set_hostname_allowed = 1;
45 SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
46     &jail_set_hostname_allowed, 0,
47     "Processes in jail can set their hostnames");
48
49 int     jail_socket_unixiproute_only = 1;
50 SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
51     &jail_socket_unixiproute_only, 0,
52     "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
53
54 int     jail_sysvipc_allowed = 0;
55 SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
56     &jail_sysvipc_allowed, 0,
57     "Processes in jail can use System V IPC primitives");
58
59 static int jail_enforce_statfs = 2;
60 SYSCTL_INT(_security_jail, OID_AUTO, enforce_statfs, CTLFLAG_RW,
61     &jail_enforce_statfs, 0,
62     "Processes in jail cannot see all mounted file systems");
63
64 int     jail_allow_raw_sockets = 0;
65 SYSCTL_INT(_security_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
66     &jail_allow_raw_sockets, 0,
67     "Prison root can create raw sockets");
68
69 int     jail_chflags_allowed = 0;
70 SYSCTL_INT(_security_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
71     &jail_chflags_allowed, 0,
72     "Processes in jail can alter system file flags");
73
74 /* allprison, lastprid, and prisoncount are protected by allprison_mtx. */
75 struct  prisonlist allprison;
76 struct  mtx allprison_mtx;
77 int     lastprid = 0;
78 int     prisoncount = 0;
79
80 static void              init_prison(void *);
81 static void              prison_complete(void *context, int pending);
82 static struct prison    *prison_find(int);
83 static int               sysctl_jail_list(SYSCTL_HANDLER_ARGS);
84
85 static void
86 init_prison(void *data __unused)
87 {
88
89         mtx_init(&allprison_mtx, "allprison", NULL, MTX_DEF);
90         LIST_INIT(&allprison);
91 }
92
93 SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
94
95 /*
96  * MPSAFE
97  *
98  * struct jail_args {
99  *      struct jail *jail;
100  * };
101  */
102 int
103 jail(struct thread *td, struct jail_args *uap)
104 {
105         struct nameidata nd;
106         struct prison *pr, *tpr;
107         struct jail j;
108         struct jail_attach_args jaa;
109         int vfslocked, error, tryprid;
110
111         error = copyin(uap->jail, &j, sizeof(j));
112         if (error)
113                 return (error);
114         if (j.version != 0)
115                 return (EINVAL);
116
117         MALLOC(pr, struct prison *, sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
118         mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
119         pr->pr_ref = 1;
120         error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0);
121         if (error)
122                 goto e_killmtx;
123         NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE,
124             pr->pr_path, td);
125         error = namei(&nd);
126         if (error)
127                 goto e_killmtx;
128         vfslocked = NDHASGIANT(&nd);
129         pr->pr_root = nd.ni_vp;
130         VOP_UNLOCK(nd.ni_vp, 0, td);
131         NDFREE(&nd, NDF_ONLY_PNBUF);
132         VFS_UNLOCK_GIANT(vfslocked);
133         error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
134         if (error)
135                 goto e_dropvnref;
136         pr->pr_ip = j.ip_number;
137         pr->pr_linux = NULL;
138         pr->pr_securelevel = securelevel;
139
140         /* Determine next pr_id and add prison to allprison list. */
141         mtx_lock(&allprison_mtx);
142         tryprid = lastprid + 1;
143         if (tryprid == JAIL_MAX)
144                 tryprid = 1;
145 next:
146         LIST_FOREACH(tpr, &allprison, pr_list) {
147                 if (tpr->pr_id == tryprid) {
148                         tryprid++;
149                         if (tryprid == JAIL_MAX) {
150                                 mtx_unlock(&allprison_mtx);
151                                 error = EAGAIN;
152                                 goto e_dropvnref;
153                         }
154                         goto next;
155                 }
156         }
157         pr->pr_id = jaa.jid = lastprid = tryprid;
158         LIST_INSERT_HEAD(&allprison, pr, pr_list);
159         prisoncount++;
160         mtx_unlock(&allprison_mtx);
161
162         error = jail_attach(td, &jaa);
163         if (error)
164                 goto e_dropprref;
165         mtx_lock(&pr->pr_mtx);
166         pr->pr_ref--;
167         mtx_unlock(&pr->pr_mtx);
168         td->td_retval[0] = jaa.jid;
169         return (0);
170 e_dropprref:
171         mtx_lock(&allprison_mtx);
172         LIST_REMOVE(pr, pr_list);
173         prisoncount--;
174         mtx_unlock(&allprison_mtx);
175 e_dropvnref:
176         vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
177         vrele(pr->pr_root);
178         VFS_UNLOCK_GIANT(vfslocked);
179 e_killmtx:
180         mtx_destroy(&pr->pr_mtx);
181         FREE(pr, M_PRISON);
182         return (error);
183 }
184
185 /*
186  * MPSAFE
187  *
188  * struct jail_attach_args {
189  *      int jid;
190  * };
191  */
192 int
193 jail_attach(struct thread *td, struct jail_attach_args *uap)
194 {
195         struct proc *p;
196         struct ucred *newcred, *oldcred;
197         struct prison *pr;
198         int vfslocked, error;
199         
200         /*
201          * XXX: Note that there is a slight race here if two threads
202          * in the same privileged process attempt to attach to two
203          * different jails at the same time.  It is important for
204          * user processes not to do this, or they might end up with
205          * a process root from one prison, but attached to the jail
206          * of another.
207          */
208         error = suser(td);
209         if (error)
210                 return (error);
211
212         p = td->td_proc;
213         mtx_lock(&allprison_mtx);
214         pr = prison_find(uap->jid);
215         if (pr == NULL) {
216                 mtx_unlock(&allprison_mtx);
217                 return (EINVAL);
218         }
219         pr->pr_ref++;
220         mtx_unlock(&pr->pr_mtx);
221         mtx_unlock(&allprison_mtx);
222
223         vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
224         vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td);
225         if ((error = change_dir(pr->pr_root, td)) != 0)
226                 goto e_unlock;
227 #ifdef MAC
228         if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root)))
229                 goto e_unlock;
230 #endif
231         VOP_UNLOCK(pr->pr_root, 0, td);
232         change_root(pr->pr_root, td);
233         VFS_UNLOCK_GIANT(vfslocked);
234
235         newcred = crget();
236         PROC_LOCK(p);
237         oldcred = p->p_ucred;
238         setsugid(p);
239         crcopy(newcred, oldcred);
240         newcred->cr_prison = pr;
241         p->p_ucred = newcred;
242         PROC_UNLOCK(p);
243         crfree(oldcred);
244         return (0);
245 e_unlock:
246         VOP_UNLOCK(pr->pr_root, 0, td);
247         VFS_UNLOCK_GIANT(vfslocked);
248         mtx_lock(&pr->pr_mtx);
249         pr->pr_ref--;
250         mtx_unlock(&pr->pr_mtx);
251         return (error);
252 }
253
254 /*
255  * Returns a locked prison instance, or NULL on failure.
256  */
257 static struct prison *
258 prison_find(int prid)
259 {
260         struct prison *pr;
261
262         mtx_assert(&allprison_mtx, MA_OWNED);
263         LIST_FOREACH(pr, &allprison, pr_list) {
264                 if (pr->pr_id == prid) {
265                         mtx_lock(&pr->pr_mtx);
266                         return (pr);
267                 }
268         }
269         return (NULL);
270 }
271
272 void
273 prison_free(struct prison *pr)
274 {
275
276         mtx_lock(&allprison_mtx);
277         mtx_lock(&pr->pr_mtx);
278         pr->pr_ref--;
279         if (pr->pr_ref == 0) {
280                 LIST_REMOVE(pr, pr_list);
281                 mtx_unlock(&pr->pr_mtx);
282                 prisoncount--;
283                 mtx_unlock(&allprison_mtx);
284
285                 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
286                 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
287                 return;
288         }
289         mtx_unlock(&pr->pr_mtx);
290         mtx_unlock(&allprison_mtx);
291 }
292
293 static void
294 prison_complete(void *context, int pending)
295 {
296         struct prison *pr;
297         int vfslocked;
298
299         pr = (struct prison *)context;
300
301         vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
302         vrele(pr->pr_root);
303         VFS_UNLOCK_GIANT(vfslocked);
304
305         mtx_destroy(&pr->pr_mtx);
306         if (pr->pr_linux != NULL)
307                 FREE(pr->pr_linux, M_PRISON);
308         FREE(pr, M_PRISON);
309 }
310
311 void
312 prison_hold(struct prison *pr)
313 {
314
315         mtx_lock(&pr->pr_mtx);
316         pr->pr_ref++;
317         mtx_unlock(&pr->pr_mtx);
318 }
319
320 u_int32_t
321 prison_getip(struct ucred *cred)
322 {
323
324         return (cred->cr_prison->pr_ip);
325 }
326
327 int
328 prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
329 {
330         u_int32_t tmp;
331
332         if (!jailed(cred))
333                 return (0);
334         if (flag) 
335                 tmp = *ip;
336         else
337                 tmp = ntohl(*ip);
338         if (tmp == INADDR_ANY) {
339                 if (flag) 
340                         *ip = cred->cr_prison->pr_ip;
341                 else
342                         *ip = htonl(cred->cr_prison->pr_ip);
343                 return (0);
344         }
345         if (tmp == INADDR_LOOPBACK) {
346                 if (flag)
347                         *ip = cred->cr_prison->pr_ip;
348                 else
349                         *ip = htonl(cred->cr_prison->pr_ip);
350                 return (0);
351         }
352         if (cred->cr_prison->pr_ip != tmp)
353                 return (1);
354         return (0);
355 }
356
357 void
358 prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
359 {
360         u_int32_t tmp;
361
362         if (!jailed(cred))
363                 return;
364         if (flag)
365                 tmp = *ip;
366         else
367                 tmp = ntohl(*ip);
368         if (tmp == INADDR_LOOPBACK) {
369                 if (flag)
370                         *ip = cred->cr_prison->pr_ip;
371                 else
372                         *ip = htonl(cred->cr_prison->pr_ip);
373                 return;
374         }
375         return;
376 }
377
378 int
379 prison_if(struct ucred *cred, struct sockaddr *sa)
380 {
381         struct sockaddr_in *sai;
382         int ok;
383
384         sai = (struct sockaddr_in *)sa;
385         if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
386                 ok = 1;
387         else if (sai->sin_family != AF_INET)
388                 ok = 0;
389         else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
390                 ok = 1;
391         else
392                 ok = 0;
393         return (ok);
394 }
395
396 /*
397  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
398  */
399 int
400 prison_check(struct ucred *cred1, struct ucred *cred2)
401 {
402
403         if (jailed(cred1)) {
404                 if (!jailed(cred2))
405                         return (ESRCH);
406                 if (cred2->cr_prison != cred1->cr_prison)
407                         return (ESRCH);
408         }
409
410         return (0);
411 }
412
413 /*
414  * Return 1 if the passed credential is in a jail, otherwise 0.
415  */
416 int
417 jailed(struct ucred *cred)
418 {
419
420         return (cred->cr_prison != NULL);
421 }
422
423 /*
424  * Return the correct hostname for the passed credential.
425  */
426 void
427 getcredhostname(struct ucred *cred, char *buf, size_t size)
428 {
429
430         if (jailed(cred)) {
431                 mtx_lock(&cred->cr_prison->pr_mtx);
432                 strlcpy(buf, cred->cr_prison->pr_host, size);
433                 mtx_unlock(&cred->cr_prison->pr_mtx);
434         } else
435                 strlcpy(buf, hostname, size);
436 }
437
438 /*
439  * Determine whether the subject represented by cred can "see"
440  * status of a mount point.
441  * Returns: 0 for permitted, ENOENT otherwise.
442  * XXX: This function should be called cr_canseemount() and should be
443  *      placed in kern_prot.c.
444  */
445 int
446 prison_canseemount(struct ucred *cred, struct mount *mp)
447 {
448         struct prison *pr;
449         struct statfs *sp;
450         size_t len;
451
452         if (!jailed(cred) || jail_enforce_statfs == 0)
453                 return (0);
454         pr = cred->cr_prison;
455         if (pr->pr_root->v_mount == mp)
456                 return (0);
457         if (jail_enforce_statfs == 2)
458                 return (ENOENT);
459         /*
460          * If jail's chroot directory is set to "/" we should be able to see
461          * all mount-points from inside a jail.
462          * This is ugly check, but this is the only situation when jail's
463          * directory ends with '/'.
464          */
465         if (strcmp(pr->pr_path, "/") == 0)
466                 return (0);
467         len = strlen(pr->pr_path);
468         sp = &mp->mnt_stat;
469         if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
470                 return (ENOENT);
471         /*
472          * Be sure that we don't have situation where jail's root directory
473          * is "/some/path" and mount point is "/some/pathpath".
474          */
475         if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
476                 return (ENOENT);
477         return (0);
478 }
479
480 void
481 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
482 {
483         char jpath[MAXPATHLEN];
484         struct prison *pr;
485         size_t len;
486
487         if (!jailed(cred) || jail_enforce_statfs == 0)
488                 return;
489         pr = cred->cr_prison;
490         if (prison_canseemount(cred, mp) != 0) {
491                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
492                 strlcpy(sp->f_mntonname, "[restricted]",
493                     sizeof(sp->f_mntonname));
494                 return;
495         }
496         if (pr->pr_root->v_mount == mp) {
497                 /*
498                  * Clear current buffer data, so we are sure nothing from
499                  * the valid path left there.
500                  */
501                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
502                 *sp->f_mntonname = '/';
503                 return;
504         }
505         /*
506          * If jail's chroot directory is set to "/" we should be able to see
507          * all mount-points from inside a jail.
508          */
509         if (strcmp(pr->pr_path, "/") == 0)
510                 return;
511         len = strlen(pr->pr_path);
512         strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
513         /*
514          * Clear current buffer data, so we are sure nothing from
515          * the valid path left there.
516          */
517         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
518         if (*jpath == '\0') {
519                 /* Should never happen. */
520                 *sp->f_mntonname = '/';
521         } else {
522                 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
523         }
524 }
525
526 static int
527 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
528 {
529         struct xprison *xp, *sxp;
530         struct prison *pr;
531         int count, error;
532
533         if (jailed(req->td->td_ucred))
534                 return (0);
535 retry:
536         mtx_lock(&allprison_mtx);
537         count = prisoncount;
538         mtx_unlock(&allprison_mtx);
539
540         if (count == 0)
541                 return (0);
542
543         sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
544         mtx_lock(&allprison_mtx);
545         if (count != prisoncount) {
546                 mtx_unlock(&allprison_mtx);
547                 free(sxp, M_TEMP);
548                 goto retry;
549         }
550         
551         LIST_FOREACH(pr, &allprison, pr_list) {
552                 mtx_lock(&pr->pr_mtx);
553                 xp->pr_version = XPRISON_VERSION;
554                 xp->pr_id = pr->pr_id;
555                 strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
556                 strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
557                 xp->pr_ip = pr->pr_ip;
558                 mtx_unlock(&pr->pr_mtx);
559                 xp++;
560         }
561         mtx_unlock(&allprison_mtx);
562
563         error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
564         free(sxp, M_TEMP);
565         if (error)
566                 return (error);
567         return (0);
568 }
569
570 SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
571     NULL, 0, sysctl_jail_list, "S", "List of active jails");
572
573 static int
574 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
575 {
576         int error, injail;
577
578         injail = jailed(req->td->td_ucred);
579         error = SYSCTL_OUT(req, &injail, sizeof(injail));
580
581         return (error);
582 }
583 SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD,
584     NULL, 0, sysctl_jail_jailed, "I", "Process in jail?");