blob: 1ddb1a8fbe592483aad5606d30ca6e81a698b0a5 [file] [log] [blame] [view]
Gilles Peskine41d03342022-02-14 23:55:59 +01001Thread safety of the PSA subsystem
Gilles Peskinea42a8de2021-11-03 12:18:41 +01002==================================
3
Gilles Peskine41d03342022-02-14 23:55:59 +01004## Requirements
5
6### Backward compatibility requirement
7
8Code that is currently working must keep working. There can be an exception for code that uses features that are advertised as experimental; for example, it would be annoying but ok to add extra requirements for drivers.
9
10In particular, if you build with `MBEDTLS_PSA_CRYPTO_C` and `MBEDTLS_THREADING_C` and you either protect all PSA calls with a mutex, or only ever call PSA functions from a single thread, your application works.
11
12As a consequence, we must not add a new platform requirement beyond mutexes for the base case. It would be ok to add new platform requirements if they're only needed for PSA drivers, or if they're only performance improvements.
13
14Tempting platform requirements that we cannot add to the default `MBEDTLS_THREADING_C` include:
15
16* Releasing a mutex from a different thread than the one that acquired it. This isn't even guaranteed to work with pthreads.
17* New primitives such as semaphores or condition variables.
18
19### Correctness out of the box
20
21If you build with `MBEDTLS_PSA_CRYPTO_C` and `MBEDTLS_THREADING_C`, the code must be functionally correct: no race conditions, deadlocks or livelocks.
22
23The [PSA Crypto API specification](https://armmbed.github.io/mbed-crypto/html/overview/conventions.html#concurrent-calls) defines minimum expectations for concurrent calls. They must work as if they had been executed one at a time, except that the following cases have undefined behavior:
24
25* Destroying a key while it's in use.
26* Concurrent calls using the same operation object. (An operation object may not be used by more than one thread at a time. But it can move from one thread to another between calls.)
27* Overlap of an output buffer with an input or output of a concurrent call.
28* Modification of an input buffer during a call.
29
30Note that while the specification does not define the behavior in such cases, Mbed TLS can be used as a crypto service. It's acceptable if an application can mess itself up, but it is not acceptable if an application can mess up the crypto service. As a consequence, destroying a key while it's in use may violate the security property that all key material is erased as soon as `psa_destroy_key` returns, but it may not cause data corruption or read-after-free inside the key store.
31
32### No spinning
33
34The code must not spin on a potentially non-blocking task. For example, this is proscribed:
35```
36lock(m);
37while (!its_my_turn) {
38 unlock(m);
39 lock(m);
40}
41```
42
43Rationale: this can cause battery drain, and can even be a livelock (spinning forever), e.g. if the thread that might unblock this one has a lower priority.
44
45### Driver requirements
46
47At the time of writing, the driver interface specification does not consider multithreaded environments.
48
49We need to define clear policies so that driver implementers know what to expect. Here are two possible policies at two ends of the spectrum; what is desirable is probably somewhere in between.
50
51* Driver entry points may be called concurrently from multiple threads, even if they're using the same key, and even including destroying a key while an operation is in progress on it.
52* At most one driver entry point is active at any given time.
53
54A more reasonable policy could be:
55
56* By default, each driver only has at most one entry point active at any given time. In other words, each driver has its own exclusive lock.
57* Drivers have an optional `"thread_safe"` boolean property. If true, it allows concurrent calls to this driver.
58* Even with a thread-safe driver, the core never starts the destruction of a key while there are operations in progress on it, and never performs concurrent calls on the same multipart operation.
59
60### Long-term performance requirements
61
62In the short term, correctness is the important thing. We can start with a global lock.
63
64In the medium to long term, performing a slow or blocking operation (for example, a driver call, or an RSA decryption) should not block other threads, even if they're calling the same driver or using the same key object.
65
66We may want to go directly to a more sophisticated approach because when a system works with a global lock, it's typically hard to get rid of it to get more fine-grained concurrency.
67
68### Key destruction long-term requirements
69
70As noted above in [“Correctness out of the box”](#correctness-out-of-the-box), when a key is destroyed, it's ok if `psa_destroy_key` allows copies of the key to live until ongoing operations using the key return. In the long term, it would be good to guarantee that `psa_destroy_key` wipes all copies of the key material.
Gilles Peskinea42a8de2021-11-03 12:18:41 +010071
72## Resources to protect
73
Gilles Peskine41d03342022-02-14 23:55:59 +010074Analysis of the behavior of the PSA key store as of Mbed TLS 9202ba37b19d3ea25c8451fd8597fce69eaa6867.
75
Gilles Peskinea42a8de2021-11-03 12:18:41 +010076### Global variables
77
78* `psa_crypto_slot_management::global_data.key_slots[i]`: see [“Key slots”](#key-slots).
79
80* `psa_crypto_slot_management::global_data.key_slots_initialized`:
81 * `psa_initialize_key_slots`: modification.
82 * `psa_wipe_all_key_slots`: modification.
83 * `psa_get_empty_key_slot`: read.
84 * `psa_get_and_lock_key_slot`: read.
85
86* `psa_crypto::global_data.rng`: depends on the RNG implementation. See [“Random generator”](#random-generator).
87 * `psa_generate_random`: query.
88 * `mbedtls_psa_crypto_configure_entropy_sources` (only if `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` is enabled): setup. Only called from `psa_crypto_init` via `mbedtls_psa_random_init`, or from test code.
89 * `mbedtls_psa_crypto_free`: deinit.
90 * `psa_crypto_init`: seed (via `mbedtls_psa_random_seed`); setup via `mbedtls_psa_crypto_configure_entropy_sources.
91
92* `psa_crypto::global_data.{initialized,rng_state}`: these are bit-fields and cannot be modified independently so they must be protected by the same mutex. The following functions access these fields:
93 * `mbedtls_psa_crypto_configure_entropy_sources` [`rng_state`] (only if `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` is enabled): read. Only called from `psa_crypto_init` via `mbedtls_psa_random_init`, or from test code.
94 * `mbedtls_psa_crypto_free`: modification.
95 * `psa_crypto_init`: modification.
96 * Many functions via `GUARD_MODULE_INITIALIZED`: read.
97
98### Key slots
99
100#### Key slot array traversal
101
102“Occupied key slot” is determined by `psa_is_key_slot_occupied` based on `slot->attr.type`.
103
104The following functions traverse the key slot array:
105
106* `psa_get_and_lock_key_slot_in_memory`: reads `slot->attr.id`.
107* `psa_get_and_lock_key_slot_in_memory`: calls `psa_lock_key_slot` on one occupied slot.
108* `psa_get_empty_key_slot`: calls `psa_is_key_slot_occupied`.
109* `psa_get_empty_key_slot`: calls `psa_wipe_key_slot` and more modifications on one occupied slot with no active user.
110* `psa_get_empty_key_slot`: calls `psa_lock_key_slot` and more modification on one unoccupied slot.
111* `psa_wipe_all_key_slots`: writes to all slots.
112* `mbedtls_psa_get_stats`: reads from all slots.
113
114#### Key slot state
115
116The following functions modify a slot's usage state:
117
118* `psa_lock_key_slot`: writes to `slot->lock_count`.
119* `psa_unlock_key_slot`: writes to `slot->lock_count`.
120* `psa_wipe_key_slot`: writes to `slot->lock_count`.
121* `psa_destroy_key`: reads `slot->lock_count`, calls `psa_lock_key_slot`.
122* `psa_wipe_all_key_slots`: writes to all slots.
123* `psa_get_empty_key_slot`: writes to `slot->lock_count` and calls `psa_wipe_key_slot` and `psa_lock_key_slot` on one occupied slot with no active user; calls `psa_lock_key_slot` on one unoccupied slot.
124* `psa_close_key`: reads `slot->lock_count`; calls `psa_get_and_lock_key_slot_in_memory`, `psa_wipe_key_slot` and `psa_unlock_key_slot`.
125* `psa_purge_key`: reads `slot->lock_count`; calls `psa_get_and_lock_key_slot_in_memory`, `psa_wipe_key_slot` and `psa_unlock_key_slot`.
126
Andrzej Kurekeec6b2c2021-11-08 14:09:29 +0100127**slot->attr access:**
128`psa_crypto_core.h`:
129* `psa_key_slot_set_flags` - writes to attr.flags
130* `psa_key_slot_set_bits_in_flags` - writes to attr.flags
131* `psa_key_slot_clear_bits` - writes to attr.flags
132* `psa_is_key_slot_occupied` - reads attr.type
133* `psa_key_slot_get_flags` - reads attr.flags
134
135`psa_crypto_slot_management.c`:
136* `psa_get_and_lock_key_slot_in_memory` - reads attr.id
137* `psa_get_empty_key_slot` - reads attr.lifetime
138* `psa_load_persistent_key_into_slot` - passes attr pointer to psa_load_persistent_key
139* `psa_load_persistent_key` - reads attr.id and passes pointer to psa_parse_key_data_from_storage
140* `psa_parse_key_data_from_storage` - writes to many attributes
141* `psa_get_and_lock_key_slot` - writes to attr.id, attr.lifetime, and attr.policy.usage
142* `psa_purge_key` - reads attr.lifetime, calls psa_wipe_key_slot
143* `mbedtls_psa_get_stats` - reads attr.lifetime, attr.id
144
145`psa_crypto.c`:
146* `psa_get_and_lock_key_slot_with_policy` - reads attr.type, attr.policy.
147* `psa_get_and_lock_transparent_key_slot_with_policy` - reads attr.lifetime
148* `psa_destroy_key` - reads attr.lifetime, attr.id
149* `psa_get_key_attributes` - copies all publicly available attributes of a key
150* `psa_export_key` - copies attributes
151* `psa_export_public_key` - reads attr.type, copies attributes
152* `psa_start_key_creation` - writes to the whole attr structure
153* `psa_validate_optional_attributes` - reads attr.type, attr.bits
154* `psa_import_key` - reads attr.bits
155* `psa_copy_key` - reads attr.bits, attr.type, attr.lifetime, attr.policy
156* `psa_mac_setup` - copies whole attr structure
157* `psa_mac_compute_internal` - copies whole attr structure
158* `psa_verify_internal` - copies whole attr structure
159* `psa_sign_internal` - copies whole attr structure, reads attr.type
160* `psa_assymmetric_encrypt` - reads attr.type
161* `psa_assymetric_decrypt` - reads attr.type
162* `psa_cipher_setup` - copies whole attr structure, reads attr.type
163* `psa_cipher_encrypt` - copies whole attr structure, reads attr.type
164* `psa_cipher_decrypt` - copies whole attr structure, reads attr.type
165* `psa_aead_encrypt` - copies whole attr structure
166* `psa_aead_decrypt` - copies whole attr structure
167* `psa_aead_setup` - copies whole attr structure
168* `psa_generate_derived_key_internal` - reads attr.type, writes to and reads from attr.bits, copies whole attr structure
169* `psa_key_derivation_input_key` - reads attr.type
170* `psa_key_agreement_raw_internal` - reads attr.type and attr.bits
Gilles Peskinea42a8de2021-11-03 12:18:41 +0100171
172TODO: change `psa_is_key_slot_occupied` to checking the id?
173
174#### Key slot content
175
176Other than what is used to determine the [“key slot state”](#key-slot-state), the contents of a key slot are only accessed as follows:
177
178* Modification during key creation (between `psa_start_key_creation` and `psa_finish_key_creation` or `psa_fail_key_creation`).
179* Destruction in `psa_wipe_key_slot`.
180* Read in many functions, between calls to `psa_lock_key_slot` and `psa_unlock_key_slot`.
181
Andrzej Kurekeec6b2c2021-11-08 14:09:29 +0100182**slot->key access:**
183* `psa_allocate_buffer_to_slot` - allocates key.data, sets key.bytes;
184* `psa_copy_key_material_into_slot` - writes to key.data
185* `psa_remove_key_data_from_memory` - writes and reads to/from key data
186* `psa_get_key_attributes` - reads from key data
187* `psa_export_key` - passes key data to psa_driver_wrapper_export_key
188* `psa_export_public_key` - passes key data to psa_driver_wrapper_export_public_key
189* `psa_finish_key_creation` - passes key data to psa_save_persistent_key
190* `psa_validate_optional_attributes` - passes key data and bytes to mbedtls_psa_rsa_load_representation
191* `psa_import_key` - passes key data to psa_driver_wrapper_import_key
192* `psa_copy_key` - passes key data to psa_driver_wrapper_copy_key, psa_copy_key_material_into_slot
193* `psa_mac_setup` - passes key data to psa_driver_wrapper_mac_sign_setup, psa_driver_wrapper_mac_verify_setup
194* `psa_mac_compute_internal` - passes key data to psa_driver_wrapper_mac_compute
195* `psa_sign_internal` - passes key data to psa_driver_wrapper_sign_message, psa_driver_wrapper_sign_hash
196* `psa_verify_internal` - passes key data to psa_driver_wrapper_verify_message, psa_driver_wrapper_verify_hash
197* `psa_asymmetric_encrypt` - passes key data to mbedtls_psa_rsa_load_representation
198* `psa_asymmetric_decrypt` - passes key data to mbedtls_psa_rsa_load_representation
199* `psa_cipher_setup ` - passes key data to psa_driver_wrapper_cipher_encrypt_setup and psa_driver_wrapper_cipher_decrypt_setup
200* `psa_cipher_encrypt` - passes key data to psa_driver_wrapper_cipher_encrypt
201* `psa_cipher_decrypt` - passes key data to psa_driver_wrapper_cipher_decrypt
202* `psa_aead_encrypt` - passes key data to psa_driver_wrapper_aead_encrypt
203* `psa_aead_decrypt` - passes key data to psa_driver_wrapper_aead_decrypt
204* `psa_aead_setup` - passes key data to psa_driver_wrapper_aead_encrypt_setup and psa_driver_wrapper_aead_decrypt_setup
205* `psa_generate_derived_key_internal` - passes key data to psa_driver_wrapper_import_key
206* `psa_key_derivation_input_key` - passes key data to psa_key_derivation_input_internal
207* `psa_key_agreement_raw_internal` - passes key data to mbedtls_psa_ecp_load_representation
208* `psa_generate_key` - passes key data to psa_driver_wrapper_generate_key
209
Gilles Peskinea42a8de2021-11-03 12:18:41 +0100210### Random generator
211
212The PSA RNG can be accessed both from various PSA functions, and from application code via `mbedtls_psa_get_random`.
213
214With the built-in RNG implementations using `mbedtls_ctr_drbg_context` or `mbedtls_hmac_drbg_context`, querying the RNG with `mbedtls_xxx_drbg_random()` is thread-safe (protected by a mutex inside the RNG implementation), but other operations (init, free, seed) are not.
215
216When `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` is enabled, thread safety depends on the implementation.
217
218### Driver resources
219
220Depends on the driver. The PSA driver interface specification does not discuss whether drivers must support concurrent calls.
221
222## Simple global lock strategy
223
224Have a single mutex protecting all accesses to the key store and other global variables. In practice, this means every PSA API function needs to take the lock on entry and release on exit, except for:
225
226* Hash function.
227* Accessors for key attributes and other local structures.
228
229Note that operation functions do need to take the lock, since they need to prevent the destruction of the key.
230
231Note that this does not protect access to the RNG via `mbedtls_psa_get_random`, which is guaranteed to be thread-safe when `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` is disabled.
232
233This approach is conceptually simple, but requires extra instrumentation to every function and has bad performance in a multithreaded environment since a slow operation in one thread blocks unrelated operations on other threads.
234
235## Global lock excluding slot content
236
237Have a single mutex protecting all accesses to the key store and other global variables, except that it's ok to access the content of a key slot without taking the lock if one of the following conditions holds:
238
239* The key slot is in a state that guarantees that the thread has exclusive access.
240* The key slot is in a state that guarantees that no other thread can modify the slot content, and the accessing thread is only reading the slot.
241
242Note that a thread must hold the global mutex when it reads or changes a slot's state.
243
244### Slot states
245
246For concurrency purposes, a slot can be in one of three states:
247
248* UNUSED: no thread is currently accessing the slot. It may be occupied by a volatile key or a cached key.
249* WRITING: a thread has exclusive access to the slot. This can only happen in specific circumstances as detailed below.
250* READING: any thread may read from the slot.
251
252A high-level view of state transitions:
253
254* `psa_get_empty_key_slot`: UNUSED → WRITING.
255* `psa_get_and_lock_key_slot_in_memory`: UNUSED or READING → READING. This function only accepts slots in the UNUSED or READING state. A slot with the correct id but in the WRITING state is considered free.
256* `psa_unlock_key_slot`: READING → UNUSED or READING.
257* `psa_finish_key_creation`: WRITING → READING.
258* `psa_fail_key_creation`: WRITING → UNUSED.
259* `psa_wipe_key_slot`: any → UNUSED. If the slot is READING or WRITING on entry, this function must wait until the writer or all readers have finished. (By the way, the WRITING state is possible if `mbedtls_psa_crypto_free` is called while a key creation is in progress.) See [“Destruction of a key in use”](#destruction of a key in use).
260
261The current `state->lock_count` corresponds to the difference between UNUSED and READING: a slot is in use iff its lock count is nonzero, so `lock_count == 0` corresponds to UNUSED and `lock_count != 0` corresponds to READING.
262
263There is currently no indication of when a slot is in the WRITING state. This only happens between a call to `psa_start_key_creation` and a call to one of `psa_finish_key_creation` or `psa_fail_key_creation`. This new state can be conveyed by a new boolean flag, or by setting `lock_count` to `~0`.
264
265### Destruction of a key in use
266
267Problem: a key slot is destroyed (by `psa_wipe_key_slot`) while it's in use (READING or WRITING).
268
269TODO: how do we ensure that? This needs something more sophisticated than mutexes (concurrency number >2)! Even a per-slot mutex isn't enough (we'd need a reader-writer lock).