Skip to content

Commit 2fc2b43

Browse files
committed
fscrypt: fix derivation of SipHash keys on big endian CPUs
Typically, the cryptographic APIs that fscrypt uses take keys as byte arrays, which avoids endianness issues. However, siphash_key_t is an exception. It is defined as 'u64 key[2];', i.e. the 128-bit key is expected to be given directly as two 64-bit words in CPU endianness. fscrypt_derive_dirhash_key() and fscrypt_setup_iv_ino_lblk_32_key() forgot to take this into account. Therefore, the SipHash keys used to index encrypted+casefolded directories differ on big endian vs. little endian platforms, as do the SipHash keys used to hash inode numbers for IV_INO_LBLK_32-encrypted directories. This makes such directories non-portable between these platforms. Fix this by always using the little endian order. This is a breaking change for big endian platforms, but this should be fine in practice since these features (encrypt+casefold support, and the IV_INO_LBLK_32 flag) aren't known to actually be used on any big endian platforms yet. Fixes: aa408f8 ("fscrypt: derive dirhash key for casefolded directories") Fixes: e3b1078 ("fscrypt: add support for IV_INO_LBLK_32 policies") Cc: <[email protected]> # v5.6+ Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 77f30bf commit 2fc2b43

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

fs/crypto/keysetup.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,40 @@ static int setup_per_mode_enc_key(struct fscrypt_info *ci,
210210
return err;
211211
}
212212

213+
/*
214+
* Derive a SipHash key from the given fscrypt master key and the given
215+
* application-specific information string.
216+
*
217+
* Note that the KDF produces a byte array, but the SipHash APIs expect the key
218+
* as a pair of 64-bit words. Therefore, on big endian CPUs we have to do an
219+
* endianness swap in order to get the same results as on little endian CPUs.
220+
*/
221+
static int fscrypt_derive_siphash_key(const struct fscrypt_master_key *mk,
222+
u8 context, const u8 *info,
223+
unsigned int infolen, siphash_key_t *key)
224+
{
225+
int err;
226+
227+
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen,
228+
(u8 *)key, sizeof(*key));
229+
if (err)
230+
return err;
231+
232+
BUILD_BUG_ON(sizeof(*key) != 16);
233+
BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2);
234+
le64_to_cpus(&key->key[0]);
235+
le64_to_cpus(&key->key[1]);
236+
return 0;
237+
}
238+
213239
int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
214240
const struct fscrypt_master_key *mk)
215241
{
216242
int err;
217243

218-
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, HKDF_CONTEXT_DIRHASH_KEY,
219-
ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
220-
(u8 *)&ci->ci_dirhash_key,
221-
sizeof(ci->ci_dirhash_key));
244+
err = fscrypt_derive_siphash_key(mk, HKDF_CONTEXT_DIRHASH_KEY,
245+
ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
246+
&ci->ci_dirhash_key);
222247
if (err)
223248
return err;
224249
ci->ci_dirhash_key_initialized = true;
@@ -253,10 +278,9 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
253278
if (mk->mk_ino_hash_key_initialized)
254279
goto unlock;
255280

256-
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
257-
HKDF_CONTEXT_INODE_HASH_KEY, NULL, 0,
258-
(u8 *)&mk->mk_ino_hash_key,
259-
sizeof(mk->mk_ino_hash_key));
281+
err = fscrypt_derive_siphash_key(mk,
282+
HKDF_CONTEXT_INODE_HASH_KEY,
283+
NULL, 0, &mk->mk_ino_hash_key);
260284
if (err)
261285
goto unlock;
262286
/* pairs with smp_load_acquire() above */

0 commit comments

Comments
 (0)