Skip to content

Commit 4617778

Browse files
Mikulas Patockagregkh
authored andcommitted
dm crypt: make printing of the key constant-time
commit 567dd8f upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bb64957 commit 4617778

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
@@ -3404,6 +3404,11 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
34043404
return DM_MAPIO_SUBMITTED;
34053405
}
34063406

3407+
static char hex2asc(unsigned char c)
3408+
{
3409+
return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27);
3410+
}
3411+
34073412
static void crypt_status(struct dm_target *ti, status_type_t type,
34083413
unsigned status_flags, char *result, unsigned maxlen)
34093414
{
@@ -3422,9 +3427,12 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
34223427
if (cc->key_size > 0) {
34233428
if (cc->key_string)
34243429
DMEMIT(":%u:%s", cc->key_size, cc->key_string);
3425-
else
3426-
for (i = 0; i < cc->key_size; i++)
3427-
DMEMIT("%02x", cc->key[i]);
3430+
else {
3431+
for (i = 0; i < cc->key_size; i++) {
3432+
DMEMIT("%c%c", hex2asc(cc->key[i] >> 4),
3433+
hex2asc(cc->key[i] & 0xf));
3434+
}
3435+
}
34283436
} else
34293437
DMEMIT("-");
34303438

0 commit comments

Comments
 (0)