Skip to content

Commit 567dd8f

Browse files
Mikulas PatockaMike Snitzer
authored andcommitted
dm crypt: make printing of the key constant-time
The device mapper dm-crypt target is using scnprintf("%02x", cc->key[i]) to report the current key to userspace. However, this is not a constant-time operation and it may leak information about the key via timing, via cache access patterns or via the branch predictor. Change dm-crypt's key printing to use "%c" instead of "%02x". Also introduce hex2asc() that carefully avoids any branching or memory accesses when converting a number in the range 0 ... 15 to an ascii character. Cc: [email protected] Signed-off-by: Mikulas Patocka <[email protected]> Tested-by: Milan Broz <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent d3f2a14 commit 567dd8f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

drivers/md/dm-crypt.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3439,6 +3439,11 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
34393439
return DM_MAPIO_SUBMITTED;
34403440
}
34413441

3442+
static char hex2asc(unsigned char c)
3443+
{
3444+
return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27);
3445+
}
3446+
34423447
static void crypt_status(struct dm_target *ti, status_type_t type,
34433448
unsigned status_flags, char *result, unsigned maxlen)
34443449
{
@@ -3457,9 +3462,12 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
34573462
if (cc->key_size > 0) {
34583463
if (cc->key_string)
34593464
DMEMIT(":%u:%s", cc->key_size, cc->key_string);
3460-
else
3461-
for (i = 0; i < cc->key_size; i++)
3462-
DMEMIT("%02x", cc->key[i]);
3465+
else {
3466+
for (i = 0; i < cc->key_size; i++) {
3467+
DMEMIT("%c%c", hex2asc(cc->key[i] >> 4),
3468+
hex2asc(cc->key[i] & 0xf));
3469+
}
3470+
}
34633471
} else
34643472
DMEMIT("-");
34653473

0 commit comments

Comments
 (0)