xref: /xnu-12377.81.4/doc/allocators/read-only.md (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions# The Read-Only Allocator
2*043036a2SApple OSS Distributions
3*043036a2SApple OSS DistributionsAllocating read-only data in xnu.
4*043036a2SApple OSS Distributions
5*043036a2SApple OSS Distributions## Introduction
6*043036a2SApple OSS Distributions
7*043036a2SApple OSS DistributionsThe Read-Only Allocator is an extension of the zone allocator that facilitates
8*043036a2SApple OSS Distributions"read-only" allocations.  Data allocated from a read-only zone can only be
9*043036a2SApple OSS Distributionsmodified programmatically through the `zalloc_ro_mut` function.
10*043036a2SApple OSS Distributions
11*043036a2SApple OSS DistributionsRead-only zones are intended for very specific use cases where the data being
12*043036a2SApple OSS Distributionsmanaged directly affects runtime security decisions.
13*043036a2SApple OSS Distributions
14*043036a2SApple OSS Distributions## Discussion
15*043036a2SApple OSS Distributions
16*043036a2SApple OSS DistributionsThe purpose of the Read-Only Allocator is to protect security-
17*043036a2SApple OSS Distributionssensitive data from being targeted by memory corruption vulnerabilities.
18*043036a2SApple OSS Distributions
19*043036a2SApple OSS DistributionsWhile, historically, the modus operandi for an advanced attacker is to seize
20*043036a2SApple OSS Distributionscontrol of kernel execution, advances in control flow integrity defenses, such
21*043036a2SApple OSS Distributionsas PAC, means that today's attacker favors data-only attacks to achieve
22*043036a2SApple OSS Distributionscompromise.  Typically this involves using a controlled write primitive to
23*043036a2SApple OSS Distributionstarget data structures in the kernel's memory that effectively disables or
24*043036a2SApple OSS Distributionsbypasses obstacles standing in the way of the desired data.
25*043036a2SApple OSS Distributions
26*043036a2SApple OSS DistributionsBy necessity, we store lots of data on the heap that informs the various
27*043036a2SApple OSS Distributionssecurity mechanisms on our platforms.  The heap traditionally dispenses
28*043036a2SApple OSS Distributionsdirectly mutable allocations because this fits what we need the memory for:
29*043036a2SApple OSS Distributionsfrequent, fast and easy read/write access to memory.  Unfortunately, these are
30*043036a2SApple OSS Distributionsalso the requirements for an attacker looking to exploit a controllable write
31*043036a2SApple OSS Distributionsinto kernel memory.
32*043036a2SApple OSS Distributions
33*043036a2SApple OSS DistributionsFor globals, `SECURITY_READ_ONLY_(EARLY|LATE)` provides an elegant protection
34*043036a2SApple OSS Distributionsmechanism, but unfortunately that doesn't cater for dynamic runtime
35*043036a2SApple OSS Distributionsallocations.
36*043036a2SApple OSS Distributions
37*043036a2SApple OSS DistributionsThis is where the Read-Only Allocator provides its defense: we observe that
38*043036a2SApple OSS Distributionsthe majority of security-sensitive data that we allocate on the heap tends to
39*043036a2SApple OSS Distributionsbe written into memory once and seldom changed thereafter.  We can therefore
40*043036a2SApple OSS Distributionstrade some of this ease of access in exchange for stronger guarantees on the
41*043036a2SApple OSS Distributionsintegrity of the data.
42*043036a2SApple OSS Distributions
43*043036a2SApple OSS DistributionsData under the control of the Read-Only Allocator can be read from just as
44*043036a2SApple OSS Distributionscheaply and easily as other data, but writing to it must be done through the
45*043036a2SApple OSS Distributionsrelatively expensive `zalloc_ro_mut` function.  By insisting that data be
46*043036a2SApple OSS Distributionswritten programmatically (i.e. through calling a function), we raise the cost
47*043036a2SApple OSS Distributionsof targeting that data towards the cost of seizing control of kernel
48*043036a2SApple OSS Distributionsexecution.
49*043036a2SApple OSS Distributions
50*043036a2SApple OSS Distributions
51*043036a2SApple OSS Distributions## Data Structure Strategies
52*043036a2SApple OSS Distributions
53*043036a2SApple OSS DistributionsTo make best use of the Read-Only Allocator, some simple advice should be
54*043036a2SApple OSS Distributionsfollowed:
55*043036a2SApple OSS Distributions
56*043036a2SApple OSS Distributions1. Pointers to read-only elements should either reside in read-only memory
57*043036a2SApple OSS Distributions   themselves, or be protected by PAC.
58*043036a2SApple OSS Distributions2. Where there is a 1:1 mapping between read/write and read-only elements, the
59*043036a2SApple OSS Distributions   read-only element should include a pointer back to the read/write side (a
60*043036a2SApple OSS Distributions   "back reference") that is validated when traversing from read/write to
61*043036a2SApple OSS Distributions   read-only.
62*043036a2SApple OSS Distributions
63*043036a2SApple OSS DistributionsOn Point 1: data structures are typically stored through chains of pointers --
64*043036a2SApple OSS Distributionse.g. a thread points to its task, which points to its proc, which points to
65*043036a2SApple OSS Distributionsits credential.  The principle here is to ensure the integrity of the entire
66*043036a2SApple OSS Distributionschain from source pointer (e.g. thread) to destination data (e.g. credential).
67*043036a2SApple OSS Distributions
68*043036a2SApple OSS DistributionsOn Point 2: by storing a back reference on the read-only side of 1:1
69*043036a2SApple OSS Distributionsrelationships, we can validate the ownership invariant that we expect to hold.
70*043036a2SApple OSS DistributionsIf this is violated, it suggests that a use-after-free has happened -- perhaps
71*043036a2SApple OSS Distributionsthrough a genuine bug, or perhaps by an attacker targeting the zone allocator
72*043036a2SApple OSS Distributionsitself.
73*043036a2SApple OSS Distributions
74*043036a2SApple OSS Distributions## Should I Use the Read-Only Allocator?
75*043036a2SApple OSS Distributions
76*043036a2SApple OSS DistributionsThe Read-Only Allocator is intended to protect data from very specific
77*043036a2SApple OSS Distributionsthreats.  This means that for most data, it simply doesn't make sense to use
78*043036a2SApple OSS Distributionsit.  Its use is primarily geared toward allocations supporting security
79*043036a2SApple OSS Distributionsboundaries such as labels, sandboxing, audit tokens, etc.
80*043036a2SApple OSS Distributions
81*043036a2SApple OSS Distributions
82*043036a2SApple OSS Distributions## API
83*043036a2SApple OSS Distributions
84*043036a2SApple OSS DistributionsRead-only zones cannot be created after lockdown.  To create a new read-only
85*043036a2SApple OSS Distributionszone, a new identifier must be added to the `zone_reserved_id_t` enumeration
86*043036a2SApple OSS Distributionsand it must be created by passing `ZC_READONLY` through either `ZONE_INIT` or
87*043036a2SApple OSS Distributions`zone_create_ext`.
88*043036a2SApple OSS Distributions
89*043036a2SApple OSS DistributionsWe require identifiers for read-only zones for two reasons: firstly to ensure
90*043036a2SApple OSS Distributionsthat we're making conscious, considered choices over which zones are made
91*043036a2SApple OSS Distributionsread-only, and secondly to allow for more stringent validation at the API
92*043036a2SApple OSS Distributionsboundary.
93*043036a2SApple OSS Distributions
94*043036a2SApple OSS DistributionsOnce a read-only zone is created, the API for using it is small and simple.
95*043036a2SApple OSS DistributionsThe key functions are:
96*043036a2SApple OSS Distributions
97*043036a2SApple OSS Distributions- `zalloc_ro`: Allocate an element from a read-only zone.
98*043036a2SApple OSS Distributions- `zfree_ro`: Free an element back to a read-only zone.  Note that this is a
99*043036a2SApple OSS Distributions  macro that automatically zeroes the pointer after freeing.
100*043036a2SApple OSS Distributions- `zone_require_ro`: Verify that an element belongs to a given read-only zone
101*043036a2SApple OSS Distributions  and panic if it doesn't.
102*043036a2SApple OSS Distributions- `zalloc_ro_mut`: Modify part of an element allocated from a read-only zone.
103*043036a2SApple OSS Distributions  Think of this as a special `memcpy` to write into your elements.
104*043036a2SApple OSS Distributions- `zalloc_ro_update_elem`: A convenience function for calling `zalloc_ro_mut`
105*043036a2SApple OSS Distributions  over the entirety of an element: simply passes an offset of zero and size
106*043036a2SApple OSS Distributions  equal to the size of the elements in the zone.
107*043036a2SApple OSS Distributions
108*043036a2SApple OSS DistributionsNote that `zfree_ro`, `zalloc_ro_mut` and `zalloc_ro_update_elem` will
109*043036a2SApple OSS Distributionsperform a `zone_require_ro` on the element themselves; there's no need to do
110*043036a2SApple OSS Distributionsthis manually beforehand.
111