xref: /freebsd/sys/sys/jaildesc.h (revision d81b337d690c971d60c731494795ee4b81fb929e)
1851dc7f8SJamie Gritton /*-
2851dc7f8SJamie Gritton  * SPDX-License-Identifier: BSD-2-Clause
3851dc7f8SJamie Gritton  *
4851dc7f8SJamie Gritton  * Copyright (c) 2025 James Gritton.
5851dc7f8SJamie Gritton  * All rights reserved.
6851dc7f8SJamie Gritton  *
7851dc7f8SJamie Gritton  * This software was developed at the University of Cambridge Computer
8851dc7f8SJamie Gritton  * Laboratory with support from a grant from Google, Inc.
9851dc7f8SJamie Gritton  *
10851dc7f8SJamie Gritton  * Redistribution and use in source and binary forms, with or without
11851dc7f8SJamie Gritton  * modification, are permitted provided that the following conditions
12851dc7f8SJamie Gritton  * are met:
13851dc7f8SJamie Gritton  * 1. Redistributions of source code must retain the above copyright
14851dc7f8SJamie Gritton  *    notice, this list of conditions and the following disclaimer.
15851dc7f8SJamie Gritton  * 2. Redistributions in binary form must reproduce the above copyright
16851dc7f8SJamie Gritton  *    notice, this list of conditions and the following disclaimer in the
17851dc7f8SJamie Gritton  *    documentation and/or other materials provided with the distribution.
18851dc7f8SJamie Gritton  *
19851dc7f8SJamie Gritton  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20851dc7f8SJamie Gritton  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21851dc7f8SJamie Gritton  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22851dc7f8SJamie Gritton  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23851dc7f8SJamie Gritton  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24851dc7f8SJamie Gritton  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25851dc7f8SJamie Gritton  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26851dc7f8SJamie Gritton  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27851dc7f8SJamie Gritton  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28851dc7f8SJamie Gritton  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29851dc7f8SJamie Gritton  * SUCH DAMAGE.
30851dc7f8SJamie Gritton  */
31851dc7f8SJamie Gritton 
32851dc7f8SJamie Gritton #ifndef _SYS_JAILDESC_H_
33851dc7f8SJamie Gritton #define	_SYS_JAILDESC_H_
34851dc7f8SJamie Gritton 
35851dc7f8SJamie Gritton #ifdef _KERNEL
36851dc7f8SJamie Gritton 
37851dc7f8SJamie Gritton #include <sys/queue.h>
38851dc7f8SJamie Gritton #include <sys/_lock.h>
39851dc7f8SJamie Gritton #include <sys/_mutex.h>
40851dc7f8SJamie Gritton #include <sys/_types.h>
41851dc7f8SJamie Gritton 
42851dc7f8SJamie Gritton struct prison;
43851dc7f8SJamie Gritton 
44851dc7f8SJamie Gritton /*-
45851dc7f8SJamie Gritton  * struct jaildesc describes a jail descriptor, which points to a struct
46851dc7f8SJamie Gritton  * prison.  struct prison in turn has a linked list of struct jaildesc.
47851dc7f8SJamie Gritton  *
48851dc7f8SJamie Gritton  * Locking key:
49851dc7f8SJamie Gritton  *   (c) set on creation, remains unchanged
50851dc7f8SJamie Gritton  *   (d) jd_lock
51851dc7f8SJamie Gritton  *   (p) jd_prison->pr_mtx
52851dc7f8SJamie Gritton  */
53851dc7f8SJamie Gritton struct jaildesc {
54851dc7f8SJamie Gritton 	LIST_ENTRY(jaildesc) jd_list;	/* (d,p) this prison's descs */
55851dc7f8SJamie Gritton 	struct prison	*jd_prison;	/* (d) the prison */
56851dc7f8SJamie Gritton 	struct mtx	 jd_lock;
57851dc7f8SJamie Gritton 	unsigned	 jd_flags;	/* (d) JDF_* flags */
58851dc7f8SJamie Gritton };
59851dc7f8SJamie Gritton 
60851dc7f8SJamie Gritton /*
61851dc7f8SJamie Gritton  * Locking macros for the jaildesc.
62851dc7f8SJamie Gritton  */
63851dc7f8SJamie Gritton #define	JAILDESC_LOCK_DESTROY(jd)	mtx_destroy(&(jd)->jd_lock)
64851dc7f8SJamie Gritton #define	JAILDESC_LOCK_INIT(jd)		mtx_init(&(jd)->jd_lock, "jaildesc", \
65851dc7f8SJamie Gritton 					    NULL, MTX_DEF)
66851dc7f8SJamie Gritton #define	JAILDESC_LOCK(jd)		mtx_lock(&(jd)->jd_lock)
67851dc7f8SJamie Gritton #define	JAILDESC_UNLOCK(jd)		mtx_unlock(&(jd)->jd_lock)
68851dc7f8SJamie Gritton 
69851dc7f8SJamie Gritton /*
70851dc7f8SJamie Gritton  * Flags for the jd_flags field
71851dc7f8SJamie Gritton  */
72851dc7f8SJamie Gritton #define	JDF_REMOVED	0x00000002	/* jail was removed */
73*d81b337dSJamie Gritton #define	JDF_OWNING	0x00000004	/* closing descriptor removes jail */
74851dc7f8SJamie Gritton 
75*d81b337dSJamie Gritton int jaildesc_find(struct thread *td, int fd, struct prison **prp,
76*d81b337dSJamie Gritton     struct ucred **ucredp);
77851dc7f8SJamie Gritton int jaildesc_alloc(struct thread *td, struct file **fpp, int *fdp, int owning);
78851dc7f8SJamie Gritton void jaildesc_set_prison(struct file *jd, struct prison *pr);
79851dc7f8SJamie Gritton void jaildesc_prison_cleanup(struct prison *pr);
80851dc7f8SJamie Gritton 
81851dc7f8SJamie Gritton #endif /* _KERNEL */
82851dc7f8SJamie Gritton 
83851dc7f8SJamie Gritton #endif /* !_SYS_JAILDESC_H_ */
84