xref: /aosp_15_r20/external/boringssl/src/SANDBOXING.md (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1*8fb009dcSAndroid Build Coastguard Worker# Using BoringSSL in a Sandbox
2*8fb009dcSAndroid Build Coastguard Worker
3*8fb009dcSAndroid Build Coastguard WorkerSandboxes are a valuable tool for securing applications, so BoringSSL aims to
4*8fb009dcSAndroid Build Coastguard Workersupport them. However, it is difficult to make concrete API guarantees with
5*8fb009dcSAndroid Build Coastguard Workersandboxes. Sandboxes remove low-level OS resources and system calls, which
6*8fb009dcSAndroid Build Coastguard Workerbreaks platform abstractions. A syscall-filtering sandbox may, for instance, be
7*8fb009dcSAndroid Build Coastguard Workersensitive to otherwise non-breaking changes to use newer syscalls
8*8fb009dcSAndroid Build Coastguard Workerin either BoringSSL or the C library.
9*8fb009dcSAndroid Build Coastguard Worker
10*8fb009dcSAndroid Build Coastguard WorkerSome functions in BoringSSL, such as `BIO_new_file`, inherently need OS
11*8fb009dcSAndroid Build Coastguard Workerresources like the filesystem. We assume that sandboxed consumers either avoid
12*8fb009dcSAndroid Build Coastguard Workerthose functions or make necessary resources available. Other functions like
13*8fb009dcSAndroid Build Coastguard Worker`RSA_sign` are purely computational, but still have some baseline OS
14*8fb009dcSAndroid Build Coastguard Workerdependencies.
15*8fb009dcSAndroid Build Coastguard Worker
16*8fb009dcSAndroid Build Coastguard WorkerSandboxes which drop privileges partway through a process's lifetime are
17*8fb009dcSAndroid Build Coastguard Workeradditionally sensitive to OS resources retained across the transitions. For
18*8fb009dcSAndroid Build Coastguard Workerinstance, if a library function internally opened and retained a handle to the
19*8fb009dcSAndroid Build Coastguard Workeruser's home directory, and then the application called `chroot`, that handle
20*8fb009dcSAndroid Build Coastguard Workerwould be a sandbox escape.
21*8fb009dcSAndroid Build Coastguard Worker
22*8fb009dcSAndroid Build Coastguard WorkerThis document attempts to describe these baseline OS dependencies and long-lived
23*8fb009dcSAndroid Build Coastguard Workerinternal resources. These dependencies may change over time, but we aim to
24*8fb009dcSAndroid Build Coastguard Worker[work with sandboxed consumers](./BREAKING-CHANGES.md) when they do. However,
25*8fb009dcSAndroid Build Coastguard Workereach sandbox imposes different constraints, so, above all, sandboxed consumers
26*8fb009dcSAndroid Build Coastguard Workermust have ample test coverage to detect issues as they arise.
27*8fb009dcSAndroid Build Coastguard Worker
28*8fb009dcSAndroid Build Coastguard Worker## Baseline dependencies
29*8fb009dcSAndroid Build Coastguard Worker
30*8fb009dcSAndroid Build Coastguard WorkerCallers must assume that any BoringSSL function may perform one of the following
31*8fb009dcSAndroid Build Coastguard Workeroperations:
32*8fb009dcSAndroid Build Coastguard Worker
33*8fb009dcSAndroid Build Coastguard Worker### Memory allocation
34*8fb009dcSAndroid Build Coastguard Worker
35*8fb009dcSAndroid Build Coastguard WorkerAny BoringSSL function may allocate memory via `malloc` and related functions.
36*8fb009dcSAndroid Build Coastguard Worker
37*8fb009dcSAndroid Build Coastguard Worker### Thread synchronization
38*8fb009dcSAndroid Build Coastguard Worker
39*8fb009dcSAndroid Build Coastguard WorkerAny BoringSSL function may call into the platform's thread synchronization
40*8fb009dcSAndroid Build Coastguard Workerprimitives, including read/write locks and the equivalent of `pthread_once`.
41*8fb009dcSAndroid Build Coastguard WorkerThese must succeed, or BoringSSL will abort the process. Callers, however, can
42*8fb009dcSAndroid Build Coastguard Workerassume that BoringSSL functions will not spawn internal threads, unless
43*8fb009dcSAndroid Build Coastguard Workerotherwise documented.
44*8fb009dcSAndroid Build Coastguard Worker
45*8fb009dcSAndroid Build Coastguard WorkerSyscall-filtering sandboxes should note that BoringSSL uses `pthread_rwlock_t`
46*8fb009dcSAndroid Build Coastguard Workeron POSIX systems, which is less common and may not be part of other libraries'
47*8fb009dcSAndroid Build Coastguard Workersyscall surface. Additionally, thread synchronization primitives usually have an
48*8fb009dcSAndroid Build Coastguard Workeratomics-based fast path. If a sandbox blocks a necessary pthreads syscall, it
49*8fb009dcSAndroid Build Coastguard Workermay not show up in testing without lock contention.
50*8fb009dcSAndroid Build Coastguard Worker
51*8fb009dcSAndroid Build Coastguard Worker### Standard error
52*8fb009dcSAndroid Build Coastguard Worker
53*8fb009dcSAndroid Build Coastguard WorkerAny BoringSSL function may write to `stderr` or file descriptor
54*8fb009dcSAndroid Build Coastguard Worker`STDERR_FILENO` (2), either via `FILE` APIs or low-level functions like `write`.
55*8fb009dcSAndroid Build Coastguard WorkerWrites to `stderr` may fail, but there must some file at `STDERR_FILENO` which
56*8fb009dcSAndroid Build Coastguard Workerwill tolerate error messages from BoringSSL. (The file descriptor must be
57*8fb009dcSAndroid Build Coastguard Workerallocated so calls to `open` do not accidentally open something else there.)
58*8fb009dcSAndroid Build Coastguard Worker
59*8fb009dcSAndroid Build Coastguard WorkerNote some C standard library implementations also log to `stderr`, so callers
60*8fb009dcSAndroid Build Coastguard Workershould ensure this regardless.
61*8fb009dcSAndroid Build Coastguard Worker
62*8fb009dcSAndroid Build Coastguard Worker### Entropy
63*8fb009dcSAndroid Build Coastguard Worker
64*8fb009dcSAndroid Build Coastguard WorkerAny BoringSSL function may draw entropy from the OS. On Windows, this uses
65*8fb009dcSAndroid Build Coastguard Worker`RtlGenRandom` and, on POSIX systems, this uses `getrandom`, `getentropy`, or a
66*8fb009dcSAndroid Build Coastguard Worker`read` from a file descriptor to `/dev/urandom`. These operations must succeed
67*8fb009dcSAndroid Build Coastguard Workeror BoringSSL will abort the process. BoringSSL only probes for `getrandom`
68*8fb009dcSAndroid Build Coastguard Workersupport once and assumes support is consistent for the lifetime of the address
69*8fb009dcSAndroid Build Coastguard Workerspace (and any copies made via `fork`). If a syscall-filtering sandbox is
70*8fb009dcSAndroid Build Coastguard Workerenabled partway through this lifetime and changes whether `getrandom` works,
71*8fb009dcSAndroid Build Coastguard WorkerBoringSSL may abort the process. Sandboxes are recommended to allow
72*8fb009dcSAndroid Build Coastguard Worker`getrandom`.
73*8fb009dcSAndroid Build Coastguard Worker
74*8fb009dcSAndroid Build Coastguard WorkerNote even deterministic algorithms may require OS entropy. For example,
75*8fb009dcSAndroid Build Coastguard WorkerRSASSA-PKCS1-v1_5 is deterministic, but BoringSSL draws entropy to implement
76*8fb009dcSAndroid Build Coastguard WorkerRSA blinding.
77*8fb009dcSAndroid Build Coastguard Worker
78*8fb009dcSAndroid Build Coastguard WorkerEntropy gathering additionally has some initialization dependencies described in
79*8fb009dcSAndroid Build Coastguard Workerthe following section.
80*8fb009dcSAndroid Build Coastguard Worker
81*8fb009dcSAndroid Build Coastguard Worker## Initialization
82*8fb009dcSAndroid Build Coastguard Worker
83*8fb009dcSAndroid Build Coastguard WorkerBoringSSL has some uncommon OS dependencies which are only used once to
84*8fb009dcSAndroid Build Coastguard Workerinitialize some state. Sandboxes which drop privileges after some setup work may
85*8fb009dcSAndroid Build Coastguard Workeruse `CRYPTO_pre_sandbox_init` to initialize this state ahead of time. Otherwise,
86*8fb009dcSAndroid Build Coastguard Workercallers must assume any BoringSSL function may depend on these resources, in
87*8fb009dcSAndroid Build Coastguard Workeraddition to the operations above.
88*8fb009dcSAndroid Build Coastguard Worker
89*8fb009dcSAndroid Build Coastguard Worker### CPU capabilities
90*8fb009dcSAndroid Build Coastguard Worker
91*8fb009dcSAndroid Build Coastguard WorkerOn Linux ARM platforms, BoringSSL depends on OS APIs to query CPU capabilities.
92*8fb009dcSAndroid Build Coastguard Worker32-bit and 64-bit ARM both depend on the `getauxval` function. 32-bit ARM, to
93*8fb009dcSAndroid Build Coastguard Workerwork around bugs in older Android devices, may additionally read
94*8fb009dcSAndroid Build Coastguard Worker`/proc/cpuinfo`.
95*8fb009dcSAndroid Build Coastguard Worker
96*8fb009dcSAndroid Build Coastguard WorkerOn 64-bit Apple ARM platforms, BoringSSL needs to query `hw.optional.*` sysctls.
97*8fb009dcSAndroid Build Coastguard Worker
98*8fb009dcSAndroid Build Coastguard WorkerIf querying CPU capabilities fails, BoringSSL will still function, but may not
99*8fb009dcSAndroid Build Coastguard Workerperform as well.
100*8fb009dcSAndroid Build Coastguard Worker
101*8fb009dcSAndroid Build Coastguard Worker### Entropy
102*8fb009dcSAndroid Build Coastguard Worker
103*8fb009dcSAndroid Build Coastguard WorkerOn Linux systems without a working `getrandom`, drawing entropy from the OS
104*8fb009dcSAndroid Build Coastguard Workeradditionally requires opening `/dev/urandom`. If this fails, BoringSSL will
105*8fb009dcSAndroid Build Coastguard Workerabort the process. BoringSSL retains the resulting file descriptor, even across
106*8fb009dcSAndroid Build Coastguard Workerprivilege transitions.
107*8fb009dcSAndroid Build Coastguard Worker
108*8fb009dcSAndroid Build Coastguard Worker### Fork protection
109*8fb009dcSAndroid Build Coastguard Worker
110*8fb009dcSAndroid Build Coastguard WorkerOn Linux, BoringSSL allocates a page and calls `madvise` with `MADV_WIPEONFORK`
111*8fb009dcSAndroid Build Coastguard Workerto protect single-use state from `fork`. This operation must not crash, but if
112*8fb009dcSAndroid Build Coastguard Workerit fails, BoringSSL will use alternate fork-safety strategies, potentially at a
113*8fb009dcSAndroid Build Coastguard Workerperformance cost. If it succeeds, BoringSSL assumes `MADV_WIPEONFORK` is
114*8fb009dcSAndroid Build Coastguard Workerfunctional and relies on it for fork-safety. Sandboxes must not report success
115*8fb009dcSAndroid Build Coastguard Workerif they ignore the `MADV_WIPEONFORK` flag. As of writing, QEMU will ignore
116*8fb009dcSAndroid Build Coastguard Worker`madvise` calls and report success, so BoringSSL detects this by calling
117*8fb009dcSAndroid Build Coastguard Worker`madvise` with -1. Sandboxes must cleanly report an error instead of crashing.
118*8fb009dcSAndroid Build Coastguard Worker
119*8fb009dcSAndroid Build Coastguard WorkerOnce initialized, this mechanism does not require system calls in the steady
120*8fb009dcSAndroid Build Coastguard Workerstate, though note the configured page will be inherited across privilege
121*8fb009dcSAndroid Build Coastguard Workertransitions.
122*8fb009dcSAndroid Build Coastguard Worker
123*8fb009dcSAndroid Build Coastguard Worker## C and C++ standard library
124*8fb009dcSAndroid Build Coastguard Worker
125*8fb009dcSAndroid Build Coastguard WorkerBoringSSL depends on the C and C++ standard libraries which, themselves, do not
126*8fb009dcSAndroid Build Coastguard Workermake any guarantees about sandboxes. If it produces the correct answer and has
127*8fb009dcSAndroid Build Coastguard Workerno observable invalid side effects, it is possible, though unreasonable, for
128*8fb009dcSAndroid Build Coastguard Worker`memcmp` to create and close a socket.
129*8fb009dcSAndroid Build Coastguard Worker
130*8fb009dcSAndroid Build Coastguard WorkerBoringSSL assumes that functions in the C and C++ library only have the platform
131*8fb009dcSAndroid Build Coastguard Workerdependencies which would be "reasonable". For instance, a function in BoringSSL
132*8fb009dcSAndroid Build Coastguard Workerwhich aims not to open files will still freely call any libc memory and
133*8fb009dcSAndroid Build Coastguard Workerstring functions.
134*8fb009dcSAndroid Build Coastguard Worker
135*8fb009dcSAndroid Build Coastguard WorkerNote some C functions, such as `strerror`, may read files relating to the user's
136*8fb009dcSAndroid Build Coastguard Workerlocale. BoringSSL may trigger these paths and assumes the sandbox environment
137*8fb009dcSAndroid Build Coastguard Workerwill tolerate this. BoringSSL additionally cannot make guarantees about which
138*8fb009dcSAndroid Build Coastguard Workersystem calls are used by standard library's syscall wrappers. In some cases, the
139*8fb009dcSAndroid Build Coastguard Workercompiler may add dependencies. (Some C++ language features emit locking code.)
140*8fb009dcSAndroid Build Coastguard WorkerSyscall-filtering sandboxes may need updates as these dependencies change.
141