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