#ifndef _SYS_JAIL_H_
#define _SYS_JAIL_H_
#ifdef _KERNEL
struct jail_v0 {
u_int32_t version;
char *path;
char *hostname;
u_int32_t ip_number;
};
#endif
struct jail {
uint32_t version;
char *path;
char *hostname;
char *jailname;
uint32_t ip4s;
uint32_t ip6s;
struct in_addr *ip4;
struct in6_addr *ip6;
};
#define JAIL_API_VERSION 2
#ifndef _KERNEL
struct xprison_v1 {
int pr_version;
int pr_id;
char pr_path[MAXPATHLEN];
char pr_host[MAXHOSTNAMELEN];
u_int32_t pr_ip;
};
#endif
struct xprison {
int pr_version;
int pr_id;
int pr_state;
cpusetid_t pr_cpusetid;
char pr_path[MAXPATHLEN];
char pr_host[MAXHOSTNAMELEN];
char pr_name[MAXHOSTNAMELEN];
uint32_t pr_ip4s;
uint32_t pr_ip6s;
#if 0
struct in_addr pr_ip4[];
struct in6_addr pr_ip6[];
#endif
};
#define XPRISON_VERSION 3
enum prison_state {
PRISON_STATE_INVALID = 0,
PRISON_STATE_ALIVE,
PRISON_STATE_DYING
};
#define JAIL_CREATE 0x01
#define JAIL_UPDATE 0x02
#define JAIL_ATTACH 0x04
#define JAIL_DYING 0x08
#define JAIL_USE_DESC 0x10
#define JAIL_AT_DESC 0x20
#define JAIL_GET_DESC 0x40
#define JAIL_OWN_DESC 0x80
#define JAIL_SET_MASK 0xff
#define JAIL_GET_MASK 0xf8
#define JAIL_SYS_DISABLE 0
#define JAIL_SYS_NEW 1
#define JAIL_SYS_INHERIT 2
#ifndef _KERNEL
struct iovec;
__BEGIN_DECLS
int jail(struct jail *);
int jail_set(struct iovec *, unsigned int, int);
int jail_get(struct iovec *, unsigned int, int);
int jail_attach(int);
int jail_attach_jd(int);
int jail_remove(int);
int jail_remove_jd(int);
__END_DECLS
#else
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/_task.h>
#define JAIL_MAX 999999
#ifdef MALLOC_DECLARE
MALLOC_DECLARE(M_PRISON);
#endif
#endif
#if defined(_KERNEL) || defined(_WANT_PRISON)
#include <sys/osd.h>
#define HOSTUUIDLEN 64
#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
#define OSRELEASELEN 32
#define JAIL_META_PRIVATE "meta"
#define JAIL_META_SHARED "env"
struct jaildesc;
struct knlist;
struct racct;
struct prison_racct;
typedef enum {
PR_INET = 0,
PR_INET6 = 1,
PR_FAMILY_MAX = 2,
} pr_family_t;
struct prison {
TAILQ_ENTRY(prison) pr_list;
int pr_id;
volatile u_int pr_ref;
volatile u_int pr_uref;
unsigned pr_flags;
LIST_HEAD(, prison) pr_children;
LIST_HEAD(, proc) pr_proclist;
LIST_ENTRY(prison) pr_sibling;
struct prison *pr_parent;
struct mtx pr_mtx;
struct task pr_task;
struct osd pr_osd;
struct cpuset *pr_cpuset;
struct vnet *pr_vnet;
struct vnode *pr_root;
struct prison_ip *pr_addrs[PR_FAMILY_MAX];
struct prison_racct *pr_prison_racct;
struct knlist *pr_klist;
struct label *pr_label;
LIST_HEAD(, jaildesc) pr_descs;
void *pr_sparep;
int pr_childcount;
int pr_childmax;
unsigned pr_allow;
int pr_securelevel;
int pr_enforce_statfs;
int pr_devfs_rsnum;
enum prison_state pr_state;
volatile int pr_exportcnt;
int pr_spare;
int pr_osreldate;
unsigned long pr_hostid;
char pr_name[MAXHOSTNAMELEN];
char pr_path[MAXPATHLEN];
char pr_hostname[MAXHOSTNAMELEN];
char pr_domainname[MAXHOSTNAMELEN];
char pr_hostuuid[HOSTUUIDLEN];
char pr_osrelease[OSRELEASELEN];
};
struct prison_racct {
LIST_ENTRY(prison_racct) prr_next;
char prr_name[MAXHOSTNAMELEN];
u_int prr_refcount;
struct racct *prr_racct;
};
#endif
#ifdef _KERNEL
#define PR_PERSIST 0x00000001
#define PR_HOST 0x00000002
#define PR_IP4_USER 0x00000004
#define PR_IP6_USER 0x00000008
#define PR_VNET 0x00000010
#define PR_IP4_SADDRSEL 0x00000080
#define PR_IP6_SADDRSEL 0x00000100
#define PR_REMOVE 0x01000000
#define PR_IP4 0x02000000
#define PR_IP6 0x04000000
#define PR_COMPLETE_PROC 0x08000000
#define PR_ALLOW_SET_HOSTNAME 0x00000001
#define PR_ALLOW_SYSVIPC 0x00000002
#define PR_ALLOW_RAW_SOCKETS 0x00000004
#define PR_ALLOW_CHFLAGS 0x00000008
#define PR_ALLOW_MOUNT 0x00000010
#define PR_ALLOW_QUOTAS 0x00000020
#define PR_ALLOW_SOCKET_AF 0x00000040
#define PR_ALLOW_MLOCK 0x00000080
#define PR_ALLOW_READ_MSGBUF 0x00000100
#define PR_ALLOW_UNPRIV_DEBUG 0x00000200
#define PR_ALLOW_SUSER 0x00000400
#define PR_ALLOW_RESERVED_PORTS 0x00008000
#define PR_ALLOW_KMEM_ACCESS 0x00010000
#define PR_ALLOW_NFSD 0x00020000
#define PR_ALLOW_EXTATTR 0x00040000
#define PR_ALLOW_ADJTIME 0x00080000
#define PR_ALLOW_SETTIME 0x00100000
#define PR_ALLOW_ROUTING 0x00200000
#define PR_ALLOW_UNPRIV_PARENT_TAMPER 0x00400000
#define PR_ALLOW_SETAUDIT 0x00800000
#define PR_ALLOW_ALL_STATIC 0x00ff87ff
#define PR_ALLOW_PRISON0 \
(PR_ALLOW_ALL_STATIC & ~(PR_ALLOW_UNPRIV_PARENT_TAMPER))
#define PR_ALLOW_DIFFERENCES \
(PR_ALLOW_UNPRIV_DEBUG | PR_ALLOW_UNPRIV_PARENT_TAMPER)
#define PR_METHOD_CREATE 0
#define PR_METHOD_GET 1
#define PR_METHOD_SET 2
#define PR_METHOD_CHECK 3
#define PR_METHOD_ATTACH 4
#define PR_METHOD_REMOVE 5
#define PR_MAXMETHOD 6
static __inline void
prison_lock(struct prison *pr)
{
mtx_lock(&pr->pr_mtx);
}
static __inline void
prison_unlock(struct prison *pr)
{
mtx_unlock(&pr->pr_mtx);
}
#define FOREACH_PRISON_CHILD(ppr, cpr) \
LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
#define FOREACH_PRISON_DESCENDANT(ppr, cpr, descend) \
for ((cpr) = (ppr), (descend) = 1; \
((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
? LIST_FIRST(&(cpr)->pr_children) \
: ((cpr) == (ppr) \
? NULL \
: (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
? LIST_NEXT(cpr, pr_sibling) \
: (cpr)->pr_parent))));) \
if (!(descend)) \
; \
else
#define FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend) \
for ((cpr) = (ppr), (descend) = 1; \
((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
? LIST_FIRST(&(cpr)->pr_children) \
: ((cpr) == (ppr) \
? NULL \
: ((prison_unlock(cpr), \
(descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
? LIST_NEXT(cpr, pr_sibling) \
: (cpr)->pr_parent))));) \
if ((descend) ? (prison_lock(cpr), 0) : 1) \
; \
else
#define FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
for ((cpr) = (ppr), (descend) = 1, (level) = 0; \
((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
? (level++, LIST_FIRST(&(cpr)->pr_children)) \
: ((cpr) == (ppr) \
? NULL \
: ((prison_unlock(cpr), \
(descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
? LIST_NEXT(cpr, pr_sibling) \
: (level--, (cpr)->pr_parent)))));) \
if ((descend) ? (prison_lock(cpr), 0) : 1) \
; \
else
#define FOREACH_PRISON_DESCENDANT_PRE_POST(ppr, cpr, descend) \
for ((cpr) = (ppr), (descend) = 1; \
((cpr) = (descend) \
? ((descend) = !LIST_EMPTY(&(cpr)->pr_children)) \
? LIST_FIRST(&(cpr)->pr_children) \
: (cpr) \
: ((descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
? LIST_NEXT(cpr, pr_sibling) \
: cpr->pr_parent) != (ppr);)
extern struct prison prison0;
TAILQ_HEAD(prisonlist, prison);
extern struct prisonlist allprison;
extern struct sx allprison_lock;
SYSCTL_DECL(_security_jail);
SYSCTL_DECL(_security_jail_param);
#define SYSCTL_JAIL_PARAM_DECL(name) \
SYSCTL_DECL(_security_jail_param_##name)
#define SYSCTL_JAIL_PARAM(module, param, type, fmt, descr) \
SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
(type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
#define SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr) \
SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len, \
sysctl_jail_param, "A", descr)
#define SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr) \
SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len, \
sysctl_jail_param, fmt, descr)
#define SYSCTL_JAIL_PARAM_NODE(module, descr) \
SYSCTL_NODE(_security_jail_param, OID_AUTO, module, CTLFLAG_MPSAFE, \
0, descr)
#define SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr) \
SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module, \
CTLFLAG_MPSAFE, 0, descr)
#define SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr) \
SYSCTL_JAIL_PARAM_NODE(module, descr); \
SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys", \
descr)
#define SYSCTL_JAIL_PARAM_SYS_SUBNODE(parent, module, access, descr) \
SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr); \
SYSCTL_JAIL_PARAM(_##parent##_##module, , CTLTYPE_INT | (access), \
"E,jailsys", descr)
struct knote;
struct mount;
struct sockaddr;
struct statfs;
struct ucred;
struct vfsconf;
#define jailed(cred) (cred->cr_prison != &prison0)
bool jailed_without_vnet(struct ucred *);
void getcredhostname(struct ucred *, char *, size_t);
void getcreddomainname(struct ucred *, char *, size_t);
void getcredhostuuid(struct ucred *, char *, size_t);
void getcredhostid(struct ucred *, unsigned long *);
void getjailname(struct ucred *cred, char *name, size_t len);
void prison0_init(void);
bool prison_allow(struct ucred *, unsigned);
int prison_check(struct ucred *cred1, struct ucred *cred2);
bool prison_check_nfsd(struct ucred *cred);
bool prison_owns_vnet(struct prison *pr);
int prison_canseemount(struct ucred *cred, struct mount *mp);
void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
struct statfs *sp);
struct prison *prison_find(int prid);
struct prison *prison_find_child(struct prison *, int);
struct prison *prison_find_name(struct prison *, const char *);
bool prison_flag(struct ucred *, unsigned);
void prison_free(struct prison *pr);
void prison_free_locked(struct prison *pr);
void prison_hold(struct prison *pr);
void prison_hold_locked(struct prison *pr);
void prison_proc_hold(struct prison *);
void prison_proc_free(struct prison *);
void prison_proc_link(struct prison *, struct proc *);
void prison_proc_unlink(struct prison *, struct proc *);
void prison_proc_iterate(struct prison *, void (*)(struct proc *, void *), void *);
void prison_remove(struct prison *);
void prison_set_allow(struct ucred *cred, unsigned flag, int enable);
bool prison_ischild(struct prison *, struct prison *);
bool prison_isalive(const struct prison *);
bool prison_isvalid(struct prison *);
#if defined(INET) || defined(INET6)
int prison_ip_check(const struct prison *, const pr_family_t, const void *);
const void *prison_ip_get0(const struct prison *, const pr_family_t);
u_int prison_ip_cnt(const struct prison *, const pr_family_t);
#endif
#ifdef INET
bool prison_equal_ip4(struct prison *, struct prison *);
int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
int prison_check_ip4(const struct ucred *, const struct in_addr *);
int prison_check_ip4_locked(const struct prison *, const struct in_addr *);
bool prison_saddrsel_ip4(struct ucred *, struct in_addr *);
int prison_qcmp_v4(const void *, const void *);
bool prison_valid_v4(const void *);
#endif
#ifdef INET6
bool prison_equal_ip6(struct prison *, struct prison *);
int prison_get_ip6(struct ucred *, struct in6_addr *);
int prison_local_ip6(struct ucred *, struct in6_addr *, int);
int prison_remote_ip6(struct ucred *, struct in6_addr *);
int prison_check_ip6(const struct ucred *, const struct in6_addr *);
int prison_check_ip6_locked(const struct prison *, const struct in6_addr *);
bool prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
int prison_qcmp_v6(const void *, const void *);
bool prison_valid_v6(const void *);
#endif
int prison_check_af(struct ucred *cred, int af);
int prison_if(struct ucred *cred, const struct sockaddr *sa);
char *prison_name(struct prison *, struct prison *);
int prison_priv_check(struct ucred *cred, int priv);
int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
unsigned prison_add_allow(const char *prefix, const char *name,
const char *prefix_descr, const char *descr);
void prison_add_vfs(struct vfsconf *vfsp);
void prison_racct_foreach(void (*callback)(struct racct *racct,
void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
void *arg2, void *arg3);
struct prison_racct *prison_racct_find(const char *name);
void prison_racct_hold(struct prison_racct *prr);
void prison_racct_free(struct prison_racct *prr);
#endif
#endif