xref: /xnu-11417.140.69/bsd/sys/guarded.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2018 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions #ifndef _SYS_GUARDED_H_
30*43a90889SApple OSS Distributions #define _SYS_GUARDED_H_
31*43a90889SApple OSS Distributions 
32*43a90889SApple OSS Distributions #include <sys/types.h>
33*43a90889SApple OSS Distributions #include <sys/cdefs.h>
34*43a90889SApple OSS Distributions 
35*43a90889SApple OSS Distributions #include <sys/_types/_iovec_t.h>
36*43a90889SApple OSS Distributions #ifdef PRIVATE
37*43a90889SApple OSS Distributions 
38*43a90889SApple OSS Distributions __BEGIN_DECLS
39*43a90889SApple OSS Distributions 
40*43a90889SApple OSS Distributions #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
41*43a90889SApple OSS Distributions 
42*43a90889SApple OSS Distributions #ifndef _GUARDID_T
43*43a90889SApple OSS Distributions #define _GUARDID_T
44*43a90889SApple OSS Distributions typedef __uint64_t guardid_t;
45*43a90889SApple OSS Distributions #endif /* _GUARDID_T */
46*43a90889SApple OSS Distributions 
47*43a90889SApple OSS Distributions #if !defined(KERNEL)
48*43a90889SApple OSS Distributions extern int guarded_open_np(const char *path,
49*43a90889SApple OSS Distributions     const guardid_t *guard, u_int guardflags, int flags, ...);
50*43a90889SApple OSS Distributions extern int guarded_open_dprotected_np(const char *path,
51*43a90889SApple OSS Distributions     const guardid_t *guard, u_int guardflags, int flags,
52*43a90889SApple OSS Distributions     int dpclass, int dpflags, ...);
53*43a90889SApple OSS Distributions extern int guarded_kqueue_np(const guardid_t *guard, u_int guardflags);
54*43a90889SApple OSS Distributions extern int guarded_close_np(int fd, const guardid_t *guard);
55*43a90889SApple OSS Distributions extern int change_fdguard_np(int fd, const guardid_t *guard, u_int guardflags,
56*43a90889SApple OSS Distributions     const guardid_t *nguard, u_int nguardflags, int *fdflagsp);
57*43a90889SApple OSS Distributions extern ssize_t guarded_write_np(int fd, const guardid_t *guard, const void *buf, size_t nbyte);
58*43a90889SApple OSS Distributions extern ssize_t guarded_pwrite_np(int fd, const guardid_t *guard, const void *buf, size_t nbyte, off_t offset);
59*43a90889SApple OSS Distributions extern ssize_t guarded_writev_np(int fd, const guardid_t *guard, const struct iovec *iovp, int iovcnt);
60*43a90889SApple OSS Distributions #endif /* KERNEL */
61*43a90889SApple OSS Distributions 
62*43a90889SApple OSS Distributions #ifndef GUARD_TYPE_FD
63*43a90889SApple OSS Distributions /* temporary source compat: use <kern/exc_guard.h> instead */
64*43a90889SApple OSS Distributions #define GUARD_TYPE_FD           0x2
65*43a90889SApple OSS Distributions #endif
66*43a90889SApple OSS Distributions 
67*43a90889SApple OSS Distributions /*
68*43a90889SApple OSS Distributions  * File descriptor guard flavors.
69*43a90889SApple OSS Distributions  */
70*43a90889SApple OSS Distributions 
71*43a90889SApple OSS Distributions /*
72*43a90889SApple OSS Distributions  * Forbid close(2), and the implicit close() that a dup2(2) may do.
73*43a90889SApple OSS Distributions  * Forces close-on-fork to be set immutably too.
74*43a90889SApple OSS Distributions  */
75*43a90889SApple OSS Distributions #define GUARD_CLOSE             (1u << 0)
76*43a90889SApple OSS Distributions 
77*43a90889SApple OSS Distributions /*
78*43a90889SApple OSS Distributions  * Forbid dup(2), dup2(2), and fcntl(2) subcodes F_DUPFD, F_DUPFD_CLOEXEC
79*43a90889SApple OSS Distributions  * on a guarded fd. Also forbids open's of a guarded fd via /dev/fd/
80*43a90889SApple OSS Distributions  * (an implicit dup.)
81*43a90889SApple OSS Distributions  */
82*43a90889SApple OSS Distributions #define GUARD_DUP               (1u << 1)
83*43a90889SApple OSS Distributions 
84*43a90889SApple OSS Distributions /*
85*43a90889SApple OSS Distributions  * Forbid sending a guarded fd via a socket
86*43a90889SApple OSS Distributions  */
87*43a90889SApple OSS Distributions #define GUARD_SOCKET_IPC        (1u << 2)
88*43a90889SApple OSS Distributions 
89*43a90889SApple OSS Distributions /*
90*43a90889SApple OSS Distributions  * Forbid creating a fileport from a guarded fd
91*43a90889SApple OSS Distributions  */
92*43a90889SApple OSS Distributions #define GUARD_FILEPORT          (1u << 3)
93*43a90889SApple OSS Distributions 
94*43a90889SApple OSS Distributions /*
95*43a90889SApple OSS Distributions  * Forbid writes on a guarded fd
96*43a90889SApple OSS Distributions  */
97*43a90889SApple OSS Distributions #define GUARD_WRITE             (1u << 4)
98*43a90889SApple OSS Distributions 
99*43a90889SApple OSS Distributions /*
100*43a90889SApple OSS Distributions  * Violating a guard results in an error (EPERM), and potentially
101*43a90889SApple OSS Distributions  * an exception with one or more of the following bits set.
102*43a90889SApple OSS Distributions  */
103*43a90889SApple OSS Distributions enum guard_fd_exception_codes {
104*43a90889SApple OSS Distributions 	kGUARD_EXC_CLOSE        = 1u << 0,      /* close of a guarded fd */
105*43a90889SApple OSS Distributions 	kGUARD_EXC_DUP          = 1u << 1,      /* dup of a guarded fd */
106*43a90889SApple OSS Distributions 	kGUARD_EXC_NOCLOEXEC    = 1u << 2,      /* clear close-on-exec */
107*43a90889SApple OSS Distributions 	kGUARD_EXC_SOCKET_IPC   = 1u << 3,      /* sendmsg of a guarded fd */
108*43a90889SApple OSS Distributions 	kGUARD_EXC_FILEPORT     = 1u << 4,      /* fileport_makeport .. */
109*43a90889SApple OSS Distributions 	kGUARD_EXC_MISMATCH     = 1u << 5,      /* wrong guard for guarded fd */
110*43a90889SApple OSS Distributions 	kGUARD_EXC_WRITE        = 1u << 6       /* write on a guarded fd */
111*43a90889SApple OSS Distributions };
112*43a90889SApple OSS Distributions 
113*43a90889SApple OSS Distributions /*
114*43a90889SApple OSS Distributions  * Experimental guarded vnode support
115*43a90889SApple OSS Distributions  */
116*43a90889SApple OSS Distributions #define VNG_RENAME_TO           (1u << 0)
117*43a90889SApple OSS Distributions #define VNG_RENAME_FROM         (1u << 1)
118*43a90889SApple OSS Distributions #define VNG_UNLINK              (1u << 2)
119*43a90889SApple OSS Distributions #define VNG_WRITE_OTHER         (1u << 3)
120*43a90889SApple OSS Distributions #define VNG_TRUNC_OTHER         (1u << 4)
121*43a90889SApple OSS Distributions #define VNG_LINK                (1u << 5)
122*43a90889SApple OSS Distributions #define VNG_EXCHDATA            (1u << 6)
123*43a90889SApple OSS Distributions #define VNG_PERMISSIONS         (1u << 7)
124*43a90889SApple OSS Distributions 
125*43a90889SApple OSS Distributions #define VNG_ALL \
126*43a90889SApple OSS Distributions 	(VNG_RENAME_TO | VNG_RENAME_FROM | VNG_UNLINK | VNG_LINK | \
127*43a90889SApple OSS Distributions 	 VNG_WRITE_OTHER | VNG_TRUNC_OTHER | VNG_EXCHDATA | VNG_PERMISSIONS)
128*43a90889SApple OSS Distributions 
129*43a90889SApple OSS Distributions struct vnguard_set {
130*43a90889SApple OSS Distributions 	int vns_fd;
131*43a90889SApple OSS Distributions 	unsigned vns_attrs;
132*43a90889SApple OSS Distributions 	guardid_t vns_guard;
133*43a90889SApple OSS Distributions };
134*43a90889SApple OSS Distributions 
135*43a90889SApple OSS Distributions struct vnguard_getattr {
136*43a90889SApple OSS Distributions 	int vga_fd;             /* in */
137*43a90889SApple OSS Distributions 	unsigned vga_attrs;     /* out */
138*43a90889SApple OSS Distributions 	guardid_t vga_guard;    /* in */
139*43a90889SApple OSS Distributions };
140*43a90889SApple OSS Distributions 
141*43a90889SApple OSS Distributions #define VNG_SYSC_PING           0
142*43a90889SApple OSS Distributions #define VNG_SYSC_SET_GUARD      1
143*43a90889SApple OSS Distributions #define VNG_SYSC_GET_ATTR       2
144*43a90889SApple OSS Distributions 
145*43a90889SApple OSS Distributions #define VNG_POLICY_NAME         "vnguard"
146*43a90889SApple OSS Distributions 
147*43a90889SApple OSS Distributions /*
148*43a90889SApple OSS Distributions  * Violating a guard may result in an error (EPERM), and potentially
149*43a90889SApple OSS Distributions  * an exception with one or more of the following bits set.
150*43a90889SApple OSS Distributions  */
151*43a90889SApple OSS Distributions enum guard_vn_exception_codes {
152*43a90889SApple OSS Distributions 	kGUARD_EXC_RENAME_TO    = VNG_RENAME_TO,
153*43a90889SApple OSS Distributions 	kGUARD_EXC_RENAME_FROM  = VNG_RENAME_FROM,
154*43a90889SApple OSS Distributions 	kGUARD_EXC_UNLINK       = VNG_UNLINK,
155*43a90889SApple OSS Distributions 	kGUARD_EXC_WRITE_OTHER  = VNG_WRITE_OTHER,
156*43a90889SApple OSS Distributions 	kGUARD_EXC_TRUNC_OTHER  = VNG_TRUNC_OTHER,
157*43a90889SApple OSS Distributions 	kGUARD_EXC_LINK         = VNG_LINK,
158*43a90889SApple OSS Distributions 	kGUARD_EXC_EXCHDATA     = VNG_EXCHDATA,
159*43a90889SApple OSS Distributions };
160*43a90889SApple OSS Distributions 
161*43a90889SApple OSS Distributions /* Guard violation behaviors: not all combinations make sense */
162*43a90889SApple OSS Distributions 
163*43a90889SApple OSS Distributions #define kVNG_POLICY_LOGMSG      (1u << 0)
164*43a90889SApple OSS Distributions #define kVNG_POLICY_EPERM       (1u << 1)
165*43a90889SApple OSS Distributions #define kVNG_POLICY_EXC         (1u << 2)
166*43a90889SApple OSS Distributions #define kVNG_POLICY_EXC_CORPSE  (1u << 3)
167*43a90889SApple OSS Distributions #define kVNG_POLICY_SIGKILL     (1u << 4)
168*43a90889SApple OSS Distributions #define kVNG_POLICY_UPRINTMSG   (1u << 5)
169*43a90889SApple OSS Distributions 
170*43a90889SApple OSS Distributions #if BSD_KERNEL_PRIVATE
171*43a90889SApple OSS Distributions struct fileglob;
172*43a90889SApple OSS Distributions extern int vnguard_exceptions_active(void);
173*43a90889SApple OSS Distributions extern void vnguard_policy_init(void);
174*43a90889SApple OSS Distributions #if CONFIG_MACF && CONFIG_VNGUARD
175*43a90889SApple OSS Distributions extern void vng_file_label_destroy(struct fileglob *fg);
176*43a90889SApple OSS Distributions #endif /* CONFIG_MACF && CONFIG_VNGUARD */
177*43a90889SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */
178*43a90889SApple OSS Distributions 
179*43a90889SApple OSS Distributions #endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
180*43a90889SApple OSS Distributions 
181*43a90889SApple OSS Distributions __END_DECLS
182*43a90889SApple OSS Distributions 
183*43a90889SApple OSS Distributions #endif /* PRIVATE */
184*43a90889SApple OSS Distributions 
185*43a90889SApple OSS Distributions #endif /* !_SYS_GUARDED_H_ */
186