xref: /xnu-11417.140.69/bsd/sys/ubc_internal.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 1999-2020 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  *	File:	ubc.h
30*43a90889SApple OSS Distributions  *	Author:	Umesh Vaishampayan [[email protected]]
31*43a90889SApple OSS Distributions  *		05-Aug-1999	umeshv	Created.
32*43a90889SApple OSS Distributions  *
33*43a90889SApple OSS Distributions  *	Header file for Unified Buffer Cache.
34*43a90889SApple OSS Distributions  *
35*43a90889SApple OSS Distributions  */
36*43a90889SApple OSS Distributions 
37*43a90889SApple OSS Distributions #ifndef _SYS_UBC_INTERNAL_H_
38*43a90889SApple OSS Distributions #define _SYS_UBC_INTERNAL_H_
39*43a90889SApple OSS Distributions 
40*43a90889SApple OSS Distributions #include <sys/appleapiopts.h>
41*43a90889SApple OSS Distributions #include <sys/types.h>
42*43a90889SApple OSS Distributions #include <sys/kernel_types.h>
43*43a90889SApple OSS Distributions #include <sys/ucred.h>
44*43a90889SApple OSS Distributions #include <sys/vnode.h>
45*43a90889SApple OSS Distributions #include <sys/ubc.h>
46*43a90889SApple OSS Distributions #include <sys/mman.h>
47*43a90889SApple OSS Distributions #include <sys/codesign.h>
48*43a90889SApple OSS Distributions #include <sys/code_signing.h>
49*43a90889SApple OSS Distributions 
50*43a90889SApple OSS Distributions #include <sys/cdefs.h>
51*43a90889SApple OSS Distributions 
52*43a90889SApple OSS Distributions #include <kern/locks.h>
53*43a90889SApple OSS Distributions #include <mach/memory_object_types.h>
54*43a90889SApple OSS Distributions 
55*43a90889SApple OSS Distributions #include <libkern/ptrauth_utils.h>
56*43a90889SApple OSS Distributions 
57*43a90889SApple OSS Distributions #include <vm/vm_protos.h>
58*43a90889SApple OSS Distributions 
59*43a90889SApple OSS Distributions 
60*43a90889SApple OSS Distributions #define UBC_INFO_NULL   ((struct ubc_info *) 0)
61*43a90889SApple OSS Distributions 
62*43a90889SApple OSS Distributions 
63*43a90889SApple OSS Distributions extern struct zone      *ubc_info_zone;
64*43a90889SApple OSS Distributions 
65*43a90889SApple OSS Distributions /*
66*43a90889SApple OSS Distributions  * Maximum number of vfs clusters per vnode
67*43a90889SApple OSS Distributions  */
68*43a90889SApple OSS Distributions #define MAX_CLUSTERS    CONFIG_MAX_CLUSTERS
69*43a90889SApple OSS Distributions 
70*43a90889SApple OSS Distributions #define SPARSE_PUSH_LIMIT 4     /* limit on number of concurrent sparse pushes outside of the cl_lockw */
71*43a90889SApple OSS Distributions                                 /* once we reach this limit, we'll hold the lock */
72*43a90889SApple OSS Distributions 
73*43a90889SApple OSS Distributions struct cl_extent {
74*43a90889SApple OSS Distributions 	daddr64_t       b_addr;
75*43a90889SApple OSS Distributions 	daddr64_t       e_addr;
76*43a90889SApple OSS Distributions };
77*43a90889SApple OSS Distributions 
78*43a90889SApple OSS Distributions struct cl_wextent {
79*43a90889SApple OSS Distributions 	daddr64_t       b_addr;
80*43a90889SApple OSS Distributions 	daddr64_t       e_addr;
81*43a90889SApple OSS Distributions 	int             io_flags;
82*43a90889SApple OSS Distributions };
83*43a90889SApple OSS Distributions 
84*43a90889SApple OSS Distributions struct cl_readahead {
85*43a90889SApple OSS Distributions 	lck_mtx_t       cl_lockr;
86*43a90889SApple OSS Distributions 	daddr64_t       cl_lastr;                       /* last block read by client */
87*43a90889SApple OSS Distributions 	daddr64_t       cl_maxra;                       /* last block prefetched by the read ahead */
88*43a90889SApple OSS Distributions 	int             cl_ralen;                       /* length of last prefetch */
89*43a90889SApple OSS Distributions };
90*43a90889SApple OSS Distributions 
91*43a90889SApple OSS Distributions struct cl_writebehind {
92*43a90889SApple OSS Distributions 	lck_mtx_t       cl_lockw;
93*43a90889SApple OSS Distributions 	void    *       cl_scmap;                       /* pointer to sparse cluster map */
94*43a90889SApple OSS Distributions 	off_t           cl_last_write;                  /* offset of the end of the last write */
95*43a90889SApple OSS Distributions 	off_t           cl_seq_written;                 /* sequentially written bytes */
96*43a90889SApple OSS Distributions 	int             cl_sparse_pushes;               /* number of pushes outside of the cl_lockw in progress */
97*43a90889SApple OSS Distributions 	int             cl_sparse_wait;                 /* synchronous push is in progress */
98*43a90889SApple OSS Distributions 	int             cl_number;                      /* number of packed write behind clusters currently valid */
99*43a90889SApple OSS Distributions 	struct cl_wextent cl_clusters[MAX_CLUSTERS];    /* packed write behind clusters */
100*43a90889SApple OSS Distributions };
101*43a90889SApple OSS Distributions 
102*43a90889SApple OSS Distributions struct cs_hash;
103*43a90889SApple OSS Distributions 
104*43a90889SApple OSS Distributions uint8_t cs_hash_type(struct cs_hash const *);
105*43a90889SApple OSS Distributions 
106*43a90889SApple OSS Distributions struct cs_blob {
107*43a90889SApple OSS Distributions 	struct cs_blob  *csb_next;
108*43a90889SApple OSS Distributions 	vnode_t         csb_vnode;
109*43a90889SApple OSS Distributions 	void            *csb_ro_addr;
110*43a90889SApple OSS Distributions 	__xnu_struct_group(cs_cpu_info, csb_cpu_info, {
111*43a90889SApple OSS Distributions 		cpu_type_t      csb_cpu_type;
112*43a90889SApple OSS Distributions 		cpu_subtype_t   csb_cpu_subtype;
113*43a90889SApple OSS Distributions 	});
114*43a90889SApple OSS Distributions 	__xnu_struct_group(cs_signer_info, csb_signer_info, {
115*43a90889SApple OSS Distributions 		unsigned int    csb_flags;
116*43a90889SApple OSS Distributions 		unsigned int    csb_signer_type;
117*43a90889SApple OSS Distributions 	});
118*43a90889SApple OSS Distributions 	off_t           csb_base_offset;        /* Offset of Mach-O binary in fat binary */
119*43a90889SApple OSS Distributions 	off_t           csb_start_offset;       /* Blob coverage area start, from csb_base_offset */
120*43a90889SApple OSS Distributions 	off_t           csb_end_offset;         /* Blob coverage area end, from csb_base_offset */
121*43a90889SApple OSS Distributions 	vm_size_t       csb_mem_size;
122*43a90889SApple OSS Distributions 	vm_offset_t     csb_mem_offset;
123*43a90889SApple OSS Distributions 	void            *csb_mem_kaddr;
124*43a90889SApple OSS Distributions 	unsigned char   csb_cdhash[CS_CDHASH_LEN];
125*43a90889SApple OSS Distributions 	const struct cs_hash  *csb_hashtype;
126*43a90889SApple OSS Distributions #if CONFIG_SUPPLEMENTAL_SIGNATURES
127*43a90889SApple OSS Distributions 	unsigned char   csb_linkage[CS_CDHASH_LEN];
128*43a90889SApple OSS Distributions 	const struct cs_hash  *csb_linkage_hashtype;
129*43a90889SApple OSS Distributions #endif
130*43a90889SApple OSS Distributions 	int             csb_hash_pageshift;
131*43a90889SApple OSS Distributions 	int             csb_hash_firstlevel_pageshift;   /* First hash this many bytes, then hash the hashes together */
132*43a90889SApple OSS Distributions 	const CS_CodeDirectory *csb_cd;
133*43a90889SApple OSS Distributions 	const char      *csb_teamid;
134*43a90889SApple OSS Distributions #if CONFIG_SUPPLEMENTAL_SIGNATURES
135*43a90889SApple OSS Distributions 	char            *csb_supplement_teamid;
136*43a90889SApple OSS Distributions #endif
137*43a90889SApple OSS Distributions 	const CS_GenericBlob *csb_entitlements_blob;    /* raw blob, subrange of csb_mem_kaddr */
138*43a90889SApple OSS Distributions 	const CS_GenericBlob *csb_der_entitlements_blob;    /* raw blob, subrange of csb_mem_kaddr */
139*43a90889SApple OSS Distributions 
140*43a90889SApple OSS Distributions 	/*
141*43a90889SApple OSS Distributions 	 * OSEntitlements pointer setup by AMFI. This is PAC signed in addition to the
142*43a90889SApple OSS Distributions 	 * cs_blob being within RO-memory to prevent modifications on the temporary stack
143*43a90889SApple OSS Distributions 	 * variable used to setup the blob.
144*43a90889SApple OSS Distributions 	 */
145*43a90889SApple OSS Distributions 	void *XNU_PTRAUTH_SIGNED_PTR("cs_blob.csb_entitlements") csb_entitlements;
146*43a90889SApple OSS Distributions 
147*43a90889SApple OSS Distributions 	unsigned int    csb_reconstituted;      /* signature has potentially been modified after validation */
148*43a90889SApple OSS Distributions 	__xnu_struct_group(cs_blob_platform_flags, csb_platform_flags, {
149*43a90889SApple OSS Distributions 		/* The following two will be replaced by the csb_signer_type. */
150*43a90889SApple OSS Distributions 		unsigned int    csb_platform_binary:1;
151*43a90889SApple OSS Distributions 		unsigned int    csb_platform_path:1;
152*43a90889SApple OSS Distributions 	});
153*43a90889SApple OSS Distributions 
154*43a90889SApple OSS Distributions 	/* Validation category used for TLE */
155*43a90889SApple OSS Distributions 	unsigned int    csb_validation_category;
156*43a90889SApple OSS Distributions 
157*43a90889SApple OSS Distributions 	/* Auxiliary bit-map for code-signing information */
158*43a90889SApple OSS Distributions 	uint64_t    csb_auxiliary_info;
159*43a90889SApple OSS Distributions 
160*43a90889SApple OSS Distributions #if CODE_SIGNING_MONITOR
161*43a90889SApple OSS Distributions 	void *XNU_PTRAUTH_SIGNED_PTR("cs_blob.csb_csm_obj") csb_csm_obj;
162*43a90889SApple OSS Distributions 	bool csb_csm_managed;
163*43a90889SApple OSS Distributions 	uint32_t csb_csm_trust_level;
164*43a90889SApple OSS Distributions #endif
165*43a90889SApple OSS Distributions };
166*43a90889SApple OSS Distributions 
167*43a90889SApple OSS Distributions /*
168*43a90889SApple OSS Distributions  *	The following data structure keeps the information to associate
169*43a90889SApple OSS Distributions  *	a vnode to the correspondig VM objects.
170*43a90889SApple OSS Distributions  */
171*43a90889SApple OSS Distributions struct ubc_info {
172*43a90889SApple OSS Distributions 	memory_object_t         ui_pager;       /* pager */
173*43a90889SApple OSS Distributions 	memory_object_control_t ui_control;     /* VM control for the pager */
174*43a90889SApple OSS Distributions 	vnode_t                 XNU_PTRAUTH_SIGNED_PTR("ubc_info.ui_vnode") ui_vnode;       /* vnode for this ubc_info */
175*43a90889SApple OSS Distributions 	kauth_cred_t            ui_ucred;       /* holds credentials for NFS paging */
176*43a90889SApple OSS Distributions 	off_t                   ui_size;        /* file size for the vnode */
177*43a90889SApple OSS Distributions 	uint32_t                ui_flags;       /* flags */
178*43a90889SApple OSS Distributions 	uint32_t                cs_add_gen;     /* generation count when csblob was validated */
179*43a90889SApple OSS Distributions 
180*43a90889SApple OSS Distributions 	struct  cl_readahead   *cl_rahead;      /* cluster read ahead context */
181*43a90889SApple OSS Distributions 	struct  cl_writebehind *cl_wbehind;     /* cluster write behind context */
182*43a90889SApple OSS Distributions 
183*43a90889SApple OSS Distributions 	struct timespec         cs_mtime;       /* modify time of file when
184*43a90889SApple OSS Distributions 	                                         *   first cs_blob was loaded */
185*43a90889SApple OSS Distributions 	struct  cs_blob         * XNU_PTRAUTH_SIGNED_PTR("ubc_info.cs_blob") cs_blobs; /* for CODE SIGNING */
186*43a90889SApple OSS Distributions #if CONFIG_SUPPLEMENTAL_SIGNATURES
187*43a90889SApple OSS Distributions 	struct  cs_blob         * cs_blob_supplement;/* supplemental blob (note that there can only be one supplement) */
188*43a90889SApple OSS Distributions #endif
189*43a90889SApple OSS Distributions #if CHECK_CS_VALIDATION_BITMAP
190*43a90889SApple OSS Distributions 	void                    * XNU_PTRAUTH_SIGNED_PTR("ubc_info.cs_valid_bitmap") cs_valid_bitmap;     /* right now: used only for signed files on the read-only root volume */
191*43a90889SApple OSS Distributions 	uint64_t                cs_valid_bitmap_size; /* Save original bitmap size in case the file size changes.
192*43a90889SApple OSS Distributions 	                                               * In the future, we may want to reconsider changing the
193*43a90889SApple OSS Distributions 	                                               * underlying bitmap to reflect the new file size changes.
194*43a90889SApple OSS Distributions 	                                               */
195*43a90889SApple OSS Distributions #endif /* CHECK_CS_VALIDATION_BITMAP */
196*43a90889SApple OSS Distributions };
197*43a90889SApple OSS Distributions 
198*43a90889SApple OSS Distributions /* Defines for ui_flags */
199*43a90889SApple OSS Distributions #define UI_NONE           0x00000000    /* none */
200*43a90889SApple OSS Distributions #define UI_HASPAGER       0x00000001    /* has a pager associated */
201*43a90889SApple OSS Distributions #define UI_INITED         0x00000002    /* newly initialized vnode */
202*43a90889SApple OSS Distributions #define UI_HASOBJREF      0x00000004    /* hold a reference on object */
203*43a90889SApple OSS Distributions #define UI_WASMAPPED      0x00000008    /* vnode was mapped */
204*43a90889SApple OSS Distributions #define UI_ISMAPPED       0x00000010    /* vnode is currently mapped */
205*43a90889SApple OSS Distributions #define UI_MAPBUSY        0x00000020    /* vnode is being mapped or unmapped */
206*43a90889SApple OSS Distributions #define UI_MAPWAITING     0x00000040    /* someone waiting for UI_MAPBUSY */
207*43a90889SApple OSS Distributions #define UI_MAPPEDWRITE    0x00000080    /* it's mapped with PROT_WRITE */
208*43a90889SApple OSS Distributions #define UI_CSBLOBINVALID  0x00000100    /* existing csblobs are invalid */
209*43a90889SApple OSS Distributions #define UI_WASMAPPEDWRITE 0x00000200    /* was mapped writable at some point */
210*43a90889SApple OSS Distributions 
211*43a90889SApple OSS Distributions /*
212*43a90889SApple OSS Distributions  * exported primitives for loadable file systems.
213*43a90889SApple OSS Distributions  */
214*43a90889SApple OSS Distributions 
215*43a90889SApple OSS Distributions __BEGIN_DECLS
216*43a90889SApple OSS Distributions 
217*43a90889SApple OSS Distributions __private_extern__ int  ubc_umount(mount_t mp);
218*43a90889SApple OSS Distributions __private_extern__ void ubc_unmountall(void);
219*43a90889SApple OSS Distributions __private_extern__ memory_object_t ubc_getpager(vnode_t);
220*43a90889SApple OSS Distributions __private_extern__ void ubc_destroy_named(vnode_t vp, vm_object_destroy_reason_t reason);
221*43a90889SApple OSS Distributions 
222*43a90889SApple OSS Distributions /* internal only */
223*43a90889SApple OSS Distributions __private_extern__ void cluster_release(struct ubc_info *);
224*43a90889SApple OSS Distributions __private_extern__ uint32_t cluster_throttle_io_limit(vnode_t, uint32_t *);
225*43a90889SApple OSS Distributions 
226*43a90889SApple OSS Distributions 
227*43a90889SApple OSS Distributions /* Flags for ubc_getobject() */
228*43a90889SApple OSS Distributions #define UBC_FLAGS_NONE          0x0000
229*43a90889SApple OSS Distributions #define UBC_HOLDOBJECT          0x0001
230*43a90889SApple OSS Distributions #define UBC_FOR_PAGEOUT         0x0002
231*43a90889SApple OSS Distributions 
232*43a90889SApple OSS Distributions memory_object_control_t ubc_getobject(vnode_t, int);
233*43a90889SApple OSS Distributions 
234*43a90889SApple OSS Distributions int     ubc_info_init(vnode_t);
235*43a90889SApple OSS Distributions int     ubc_info_init_withsize(vnode_t, off_t);
236*43a90889SApple OSS Distributions void    ubc_info_deallocate(struct ubc_info *);
237*43a90889SApple OSS Distributions 
238*43a90889SApple OSS Distributions int     ubc_isinuse(vnode_t, int);
239*43a90889SApple OSS Distributions int     ubc_isinuse_locked(vnode_t, int, int);
240*43a90889SApple OSS Distributions 
241*43a90889SApple OSS Distributions int     ubc_getcdhash(vnode_t, off_t, unsigned char *);
242*43a90889SApple OSS Distributions 
243*43a90889SApple OSS Distributions /* code signing */
244*43a90889SApple OSS Distributions typedef enum __attribute__((enum_extensibility(closed), flag_enum)) : uint8_t {
245*43a90889SApple OSS Distributions 	CS_BLOB_ADD_ALLOW_MAIN_BINARY = (1 << 0),
246*43a90889SApple OSS Distributions } cs_blob_add_flags_t;
247*43a90889SApple OSS Distributions 
248*43a90889SApple OSS Distributions struct cs_blob;
249*43a90889SApple OSS Distributions void    cs_blob_require(struct cs_blob *, vnode_t);
250*43a90889SApple OSS Distributions int     ubc_cs_blob_add(
251*43a90889SApple OSS Distributions 	vnode_t, uint32_t, cpu_type_t, cpu_subtype_t, off_t,
252*43a90889SApple OSS Distributions 	vm_address_t *, vm_size_t, struct image_params *,
253*43a90889SApple OSS Distributions 	int, struct cs_blob **, cs_blob_add_flags_t);
254*43a90889SApple OSS Distributions #if CONFIG_SUPPLEMENTAL_SIGNATURES
255*43a90889SApple OSS Distributions int     ubc_cs_blob_add_supplement(vnode_t, vnode_t, off_t, vm_address_t *, vm_size_t, struct cs_blob **);
256*43a90889SApple OSS Distributions #endif
257*43a90889SApple OSS Distributions struct cs_blob *ubc_get_cs_blobs(vnode_t);
258*43a90889SApple OSS Distributions #if CONFIG_SUPPLEMENTAL_SIGNATURES
259*43a90889SApple OSS Distributions struct cs_blob *ubc_get_cs_supplement(vnode_t);
260*43a90889SApple OSS Distributions #endif
261*43a90889SApple OSS Distributions void    ubc_get_cs_mtime(vnode_t, struct timespec *);
262*43a90889SApple OSS Distributions int     ubc_cs_getcdhash(vnode_t, off_t, unsigned char *, uint8_t*);
263*43a90889SApple OSS Distributions kern_return_t ubc_cs_blob_allocate(vm_offset_t *, vm_size_t *);
264*43a90889SApple OSS Distributions void ubc_cs_blob_deallocate(vm_offset_t, vm_size_t);
265*43a90889SApple OSS Distributions boolean_t ubc_cs_is_range_codesigned(vnode_t, mach_vm_offset_t, mach_vm_size_t);
266*43a90889SApple OSS Distributions 
267*43a90889SApple OSS Distributions kern_return_t   ubc_cs_validation_bitmap_allocate( vnode_t );
268*43a90889SApple OSS Distributions void            ubc_cs_validation_bitmap_deallocate( struct ubc_info * );
269*43a90889SApple OSS Distributions __END_DECLS
270*43a90889SApple OSS Distributions 
271*43a90889SApple OSS Distributions 
272*43a90889SApple OSS Distributions #endif  /* _SYS_UBC_INTERNAL_H_ */
273