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