Skip to content

Commit 8717627

Browse files
committed
random: document crng_fast_key_erasure() destination possibility
This reverts 35a33ff ("random: use memmove instead of memcpy for remaining 32 bytes"), which was made on a totally bogus basis. The thing it was worried about overlapping came from the stack, not from one of its arguments, as Eric pointed out. But the fact that this confusion even happened draws attention to the fact that it's a bit non-obvious that the random_data parameter can alias chacha_state, and in fact should do so when the caller can't rely on the stack being cleared in a timely manner. So this commit documents that. Reported-by: Eric Biggers <[email protected]> Reviewed-by: Eric Biggers <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent af2d861 commit 8717627

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

drivers/char/random.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ static void crng_reseed(bool force)
318318
* the resultant ChaCha state to the user, along with the second
319319
* half of the block containing 32 bytes of random data that may
320320
* be used; random_data_len may not be greater than 32.
321+
*
322+
* The returned ChaCha state contains within it a copy of the old
323+
* key value, at index 4, so the state should always be zeroed out
324+
* immediately after using in order to maintain forward secrecy.
325+
* If the state cannot be erased in a timely manner, then it is
326+
* safer to set the random_data parameter to &chacha_state[4] so
327+
* that this function overwrites it before returning.
321328
*/
322329
static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
323330
u32 chacha_state[CHACHA_STATE_WORDS],
@@ -333,7 +340,7 @@ static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
333340
chacha20_block(chacha_state, first_block);
334341

335342
memcpy(key, first_block, CHACHA_KEY_SIZE);
336-
memmove(random_data, first_block + CHACHA_KEY_SIZE, random_data_len);
343+
memcpy(random_data, first_block + CHACHA_KEY_SIZE, random_data_len);
337344
memzero_explicit(first_block, sizeof(first_block));
338345
}
339346

0 commit comments

Comments
 (0)