blob: 047f74ba952a0f279ddcd048746e93bbe86c8f03 [file] [log] [blame] [view]
Gilles Peskinea42a8de2021-11-03 12:18:41 +01001Thread safety of the PSA key store
2==================================
3
4Analysis of the behavior of the PSA key store as of Mbed TLS 9202ba37b19d3ea25c8451fd8597fce69eaa6867.
5
6## Resources to protect
7
8### Global variables
9
10* `psa_crypto_slot_management::global_data.key_slots[i]`: see [“Key slots”](#key-slots).
11
12* `psa_crypto_slot_management::global_data.key_slots_initialized`:
13 * `psa_initialize_key_slots`: modification.
14 * `psa_wipe_all_key_slots`: modification.
15 * `psa_get_empty_key_slot`: read.
16 * `psa_get_and_lock_key_slot`: read.
17
18* `psa_crypto::global_data.rng`: depends on the RNG implementation. See [“Random generator”](#random-generator).
19 * `psa_generate_random`: query.
20 * `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.
21 * `mbedtls_psa_crypto_free`: deinit.
22 * `psa_crypto_init`: seed (via `mbedtls_psa_random_seed`); setup via `mbedtls_psa_crypto_configure_entropy_sources.
23
24* `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:
25 * `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.
26 * `mbedtls_psa_crypto_free`: modification.
27 * `psa_crypto_init`: modification.
28 * Many functions via `GUARD_MODULE_INITIALIZED`: read.
29
30### Key slots
31
32#### Key slot array traversal
33
34“Occupied key slot” is determined by `psa_is_key_slot_occupied` based on `slot->attr.type`.
35
36The following functions traverse the key slot array:
37
38* `psa_get_and_lock_key_slot_in_memory`: reads `slot->attr.id`.
39* `psa_get_and_lock_key_slot_in_memory`: calls `psa_lock_key_slot` on one occupied slot.
40* `psa_get_empty_key_slot`: calls `psa_is_key_slot_occupied`.
41* `psa_get_empty_key_slot`: calls `psa_wipe_key_slot` and more modifications on one occupied slot with no active user.
42* `psa_get_empty_key_slot`: calls `psa_lock_key_slot` and more modification on one unoccupied slot.
43* `psa_wipe_all_key_slots`: writes to all slots.
44* `mbedtls_psa_get_stats`: reads from all slots.
45
46#### Key slot state
47
48The following functions modify a slot's usage state:
49
50* `psa_lock_key_slot`: writes to `slot->lock_count`.
51* `psa_unlock_key_slot`: writes to `slot->lock_count`.
52* `psa_wipe_key_slot`: writes to `slot->lock_count`.
53* `psa_destroy_key`: reads `slot->lock_count`, calls `psa_lock_key_slot`.
54* `psa_wipe_all_key_slots`: writes to all slots.
55* `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.
56* `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`.
57* `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`.
58
59TODO: modification of `slot->attr.id`, `slot->attr.type`.
60
61TODO: change `psa_is_key_slot_occupied` to checking the id?
62
63#### Key slot content
64
65Other than what is used to determine the [“key slot state”](#key-slot-state), the contents of a key slot are only accessed as follows:
66
67* Modification during key creation (between `psa_start_key_creation` and `psa_finish_key_creation` or `psa_fail_key_creation`).
68* Destruction in `psa_wipe_key_slot`.
69* Read in many functions, between calls to `psa_lock_key_slot` and `psa_unlock_key_slot`.
70
71### Random generator
72
73The PSA RNG can be accessed both from various PSA functions, and from application code via `mbedtls_psa_get_random`.
74
75With 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.
76
77When `MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` is enabled, thread safety depends on the implementation.
78
79### Driver resources
80
81Depends on the driver. The PSA driver interface specification does not discuss whether drivers must support concurrent calls.
82
83## Simple global lock strategy
84
85Have 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:
86
87* Hash function.
88* Accessors for key attributes and other local structures.
89
90Note that operation functions do need to take the lock, since they need to prevent the destruction of the key.
91
92Note 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.
93
94This 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.
95
96## Global lock excluding slot content
97
98Have 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:
99
100* The key slot is in a state that guarantees that the thread has exclusive access.
101* 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.
102
103Note that a thread must hold the global mutex when it reads or changes a slot's state.
104
105### Slot states
106
107For concurrency purposes, a slot can be in one of three states:
108
109* UNUSED: no thread is currently accessing the slot. It may be occupied by a volatile key or a cached key.
110* WRITING: a thread has exclusive access to the slot. This can only happen in specific circumstances as detailed below.
111* READING: any thread may read from the slot.
112
113A high-level view of state transitions:
114
115* `psa_get_empty_key_slot`: UNUSED → WRITING.
116* `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.
117* `psa_unlock_key_slot`: READING → UNUSED or READING.
118* `psa_finish_key_creation`: WRITING → READING.
119* `psa_fail_key_creation`: WRITING → UNUSED.
120* `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).
121
122The 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.
123
124There 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`.
125
126### Destruction of a key in use
127
128Problem: a key slot is destroyed (by `psa_wipe_key_slot`) while it's in use (READING or WRITING).
129
130TODO: 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).