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