Skip to content

Commit e3b1078

Browse files
committed
fscrypt: add support for IV_INO_LBLK_32 policies
The eMMC inline crypto standard will only specify 32 DUN bits (a.k.a. IV bits), unlike UFS's 64. IV_INO_LBLK_64 is therefore not applicable, but an encryption format which uses one key per policy and permits the moving of encrypted file contents (as f2fs's garbage collector requires) is still desirable. To support such hardware, add a new encryption format IV_INO_LBLK_32 that makes the best use of the 32 bits: the IV is set to 'SipHash-2-4(inode_number) + file_logical_block_number mod 2^32', where the SipHash key is derived from the fscrypt master key. We hash only the inode number and not also the block number, because we need to maintain contiguity of DUNs to merge bios. Unlike with IV_INO_LBLK_64, with this format IV reuse is possible; this is unavoidable given the size of the DUN. This means this format should only be used where the requirements of the first paragraph apply. However, the hash spreads out the IVs in the whole usable range, and the use of a keyed hash makes it difficult for an attacker to determine which files use which IVs. Besides the above differences, this flag works like IV_INO_LBLK_64 in that on ext4 it is only allowed if the stable_inodes feature has been enabled to prevent inode numbers and the filesystem UUID from changing. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Theodore Ts'o <[email protected]> Reviewed-by: Paul Crowley <[email protected]> Signed-off-by: Eric Biggers <[email protected]>
1 parent 0ca2ddb commit e3b1078

File tree

7 files changed

+157
-46
lines changed

7 files changed

+157
-46
lines changed

Documentation/filesystems/fscrypt.rst

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,22 @@ files' data differently, inode numbers are included in the IVs.
292292
Consequently, shrinking the filesystem may not be allowed.
293293

294294
This format is optimized for use with inline encryption hardware
295-
compliant with the UFS or eMMC standards, which support only 64 IV
296-
bits per I/O request and may have only a small number of keyslots.
295+
compliant with the UFS standard, which supports only 64 IV bits per
296+
I/O request and may have only a small number of keyslots.
297+
298+
IV_INO_LBLK_32 policies
299+
-----------------------
300+
301+
IV_INO_LBLK_32 policies work like IV_INO_LBLK_64, except that for
302+
IV_INO_LBLK_32, the inode number is hashed with SipHash-2-4 (where the
303+
SipHash key is derived from the master key) and added to the file
304+
logical block number mod 2^32 to produce a 32-bit IV.
305+
306+
This format is optimized for use with inline encryption hardware
307+
compliant with the eMMC v5.2 standard, which supports only 32 IV bits
308+
per I/O request and may have only a small number of keyslots. This
309+
format results in some level of IV reuse, so it should only be used
310+
when necessary due to hardware limitations.
297311

298312
Key identifiers
299313
---------------
@@ -369,6 +383,10 @@ a little endian number, except that:
369383
to 32 bits and is placed in bits 0-31 of the IV. The inode number
370384
(which is also limited to 32 bits) is placed in bits 32-63.
371385

386+
- With `IV_INO_LBLK_32 policies`_, the logical block number is limited
387+
to 32 bits and is placed in bits 0-31 of the IV. The inode number
388+
is then hashed and added mod 2^32.
389+
372390
Note that because file logical block numbers are included in the IVs,
373391
filesystems must enforce that blocks are never shifted around within
374392
encrypted files, e.g. via "collapse range" or "insert range".
@@ -465,8 +483,15 @@ This structure must be initialized as follows:
465483
(0x3).
466484
- FSCRYPT_POLICY_FLAG_DIRECT_KEY: See `DIRECT_KEY policies`_.
467485
- FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64: See `IV_INO_LBLK_64
468-
policies`_. This is mutually exclusive with DIRECT_KEY and is not
469-
supported on v1 policies.
486+
policies`_.
487+
- FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32: See `IV_INO_LBLK_32
488+
policies`_.
489+
490+
v1 encryption policies only support the PAD_* and DIRECT_KEY flags.
491+
The other flags are only supported by v2 encryption policies.
492+
493+
The DIRECT_KEY, IV_INO_LBLK_64, and IV_INO_LBLK_32 flags are
494+
mutually exclusive.
470495

471496
- For v2 encryption policies, ``__reserved`` must be zeroed.
472497

fs/crypto/crypto.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
7777
memset(iv, 0, ci->ci_mode->ivsize);
7878

7979
if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
80-
WARN_ON_ONCE((u32)lblk_num != lblk_num);
80+
WARN_ON_ONCE(lblk_num > U32_MAX);
81+
WARN_ON_ONCE(ci->ci_inode->i_ino > U32_MAX);
8182
lblk_num |= (u64)ci->ci_inode->i_ino << 32;
83+
} else if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
84+
WARN_ON_ONCE(lblk_num > U32_MAX);
85+
lblk_num = (u32)(ci->ci_hashed_ino + lblk_num);
8286
} else if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
8387
memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
8488
}

fs/crypto/fscrypt_private.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ struct fscrypt_info {
222222

223223
/* This inode's nonce, copied from the fscrypt_context */
224224
u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
225+
226+
/* Hashed inode number. Only set for IV_INO_LBLK_32 */
227+
u32 ci_hashed_ino;
225228
};
226229

227230
typedef enum {
@@ -290,6 +293,8 @@ int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
290293
#define HKDF_CONTEXT_DIRECT_KEY 3
291294
#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4
292295
#define HKDF_CONTEXT_DIRHASH_KEY 5
296+
#define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6
297+
#define HKDF_CONTEXT_INODE_HASH_KEY 7
293298

294299
int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
295300
const u8 *info, unsigned int infolen,
@@ -386,14 +391,17 @@ struct fscrypt_master_key {
386391
struct list_head mk_decrypted_inodes;
387392
spinlock_t mk_decrypted_inodes_lock;
388393

389-
/* Crypto API transforms for DIRECT_KEY policies, allocated on-demand */
390-
struct crypto_skcipher *mk_direct_tfms[__FSCRYPT_MODE_MAX + 1];
391-
392394
/*
393-
* Crypto API transforms for filesystem-layer implementation of
394-
* IV_INO_LBLK_64 policies, allocated on-demand.
395+
* Per-mode encryption keys for the various types of encryption policies
396+
* that use them. Allocated and derived on-demand.
395397
*/
396-
struct crypto_skcipher *mk_iv_ino_lblk_64_tfms[__FSCRYPT_MODE_MAX + 1];
398+
struct crypto_skcipher *mk_direct_keys[__FSCRYPT_MODE_MAX + 1];
399+
struct crypto_skcipher *mk_iv_ino_lblk_64_keys[__FSCRYPT_MODE_MAX + 1];
400+
struct crypto_skcipher *mk_iv_ino_lblk_32_keys[__FSCRYPT_MODE_MAX + 1];
401+
402+
/* Hash key for inode numbers. Initialized only when needed. */
403+
siphash_key_t mk_ino_hash_key;
404+
bool mk_ino_hash_key_initialized;
397405

398406
} __randomize_layout;
399407

fs/crypto/keyring.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ static void free_master_key(struct fscrypt_master_key *mk)
4545
wipe_master_key_secret(&mk->mk_secret);
4646

4747
for (i = 0; i <= __FSCRYPT_MODE_MAX; i++) {
48-
crypto_free_skcipher(mk->mk_direct_tfms[i]);
49-
crypto_free_skcipher(mk->mk_iv_ino_lblk_64_tfms[i]);
48+
crypto_free_skcipher(mk->mk_direct_keys[i]);
49+
crypto_free_skcipher(mk->mk_iv_ino_lblk_64_keys[i]);
50+
crypto_free_skcipher(mk->mk_iv_ino_lblk_32_keys[i]);
5051
}
5152

5253
key_put(mk->mk_users);

fs/crypto/keysetup.c

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct fscrypt_mode fscrypt_modes[] = {
4646
},
4747
};
4848

49+
static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex);
50+
4951
static struct fscrypt_mode *
5052
select_encryption_mode(const union fscrypt_policy *policy,
5153
const struct inode *inode)
@@ -130,7 +132,7 @@ static int setup_per_mode_enc_key(struct fscrypt_info *ci,
130132
const struct super_block *sb = inode->i_sb;
131133
struct fscrypt_mode *mode = ci->ci_mode;
132134
const u8 mode_num = mode - fscrypt_modes;
133-
struct crypto_skcipher *tfm, *prev_tfm;
135+
struct crypto_skcipher *tfm;
134136
u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
135137
u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
136138
unsigned int hkdf_infolen = 0;
@@ -139,10 +141,17 @@ static int setup_per_mode_enc_key(struct fscrypt_info *ci,
139141
if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX))
140142
return -EINVAL;
141143

142-
/* pairs with cmpxchg() below */
144+
/* pairs with smp_store_release() below */
143145
tfm = READ_ONCE(tfms[mode_num]);
144-
if (likely(tfm != NULL))
145-
goto done;
146+
if (likely(tfm != NULL)) {
147+
ci->ci_ctfm = tfm;
148+
return 0;
149+
}
150+
151+
mutex_lock(&fscrypt_mode_key_setup_mutex);
152+
153+
if (tfms[mode_num])
154+
goto done_unlock;
146155

147156
BUILD_BUG_ON(sizeof(mode_num) != 1);
148157
BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
@@ -157,21 +166,21 @@ static int setup_per_mode_enc_key(struct fscrypt_info *ci,
157166
hkdf_context, hkdf_info, hkdf_infolen,
158167
mode_key, mode->keysize);
159168
if (err)
160-
return err;
169+
goto out_unlock;
161170
tfm = fscrypt_allocate_skcipher(mode, mode_key, inode);
162171
memzero_explicit(mode_key, mode->keysize);
163-
if (IS_ERR(tfm))
164-
return PTR_ERR(tfm);
165-
166-
/* pairs with READ_ONCE() above */
167-
prev_tfm = cmpxchg(&tfms[mode_num], NULL, tfm);
168-
if (prev_tfm != NULL) {
169-
crypto_free_skcipher(tfm);
170-
tfm = prev_tfm;
172+
if (IS_ERR(tfm)) {
173+
err = PTR_ERR(tfm);
174+
goto out_unlock;
171175
}
172-
done:
176+
/* pairs with READ_ONCE() above */
177+
smp_store_release(&tfms[mode_num], tfm);
178+
done_unlock:
173179
ci->ci_ctfm = tfm;
174-
return 0;
180+
err = 0;
181+
out_unlock:
182+
mutex_unlock(&fscrypt_mode_key_setup_mutex);
183+
return err;
175184
}
176185

177186
int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
@@ -189,6 +198,43 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
189198
return 0;
190199
}
191200

201+
static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
202+
struct fscrypt_master_key *mk)
203+
{
204+
int err;
205+
206+
err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys,
207+
HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true);
208+
if (err)
209+
return err;
210+
211+
/* pairs with smp_store_release() below */
212+
if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) {
213+
214+
mutex_lock(&fscrypt_mode_key_setup_mutex);
215+
216+
if (mk->mk_ino_hash_key_initialized)
217+
goto unlock;
218+
219+
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
220+
HKDF_CONTEXT_INODE_HASH_KEY, NULL, 0,
221+
(u8 *)&mk->mk_ino_hash_key,
222+
sizeof(mk->mk_ino_hash_key));
223+
if (err)
224+
goto unlock;
225+
/* pairs with smp_load_acquire() above */
226+
smp_store_release(&mk->mk_ino_hash_key_initialized, true);
227+
unlock:
228+
mutex_unlock(&fscrypt_mode_key_setup_mutex);
229+
if (err)
230+
return err;
231+
}
232+
233+
ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
234+
&mk->mk_ino_hash_key);
235+
return 0;
236+
}
237+
192238
static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
193239
struct fscrypt_master_key *mk)
194240
{
@@ -203,19 +249,22 @@ static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
203249
* encryption key. This ensures that the master key is
204250
* consistently used only for HKDF, avoiding key reuse issues.
205251
*/
206-
err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_tfms,
252+
err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys,
207253
HKDF_CONTEXT_DIRECT_KEY, false);
208254
} else if (ci->ci_policy.v2.flags &
209255
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
210256
/*
211257
* IV_INO_LBLK_64: encryption keys are derived from (master_key,
212258
* mode_num, filesystem_uuid), and inode number is included in
213259
* the IVs. This format is optimized for use with inline
214-
* encryption hardware compliant with the UFS or eMMC standards.
260+
* encryption hardware compliant with the UFS standard.
215261
*/
216-
err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
262+
err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys,
217263
HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
218264
true);
265+
} else if (ci->ci_policy.v2.flags &
266+
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
267+
err = fscrypt_setup_iv_ino_lblk_32_key(ci, mk);
219268
} else {
220269
u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
221270

fs/crypto/policy.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,37 @@ static bool supported_direct_key_modes(const struct inode *inode,
6969
return true;
7070
}
7171

72-
static bool supported_iv_ino_lblk_64_policy(
73-
const struct fscrypt_policy_v2 *policy,
74-
const struct inode *inode)
72+
static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
73+
const struct inode *inode,
74+
const char *type,
75+
int max_ino_bits, int max_lblk_bits)
7576
{
7677
struct super_block *sb = inode->i_sb;
7778
int ino_bits = 64, lblk_bits = 64;
7879

79-
if (policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
80-
fscrypt_warn(inode,
81-
"The DIRECT_KEY and IV_INO_LBLK_64 flags are mutually exclusive");
82-
return false;
83-
}
8480
/*
8581
* It's unsafe to include inode numbers in the IVs if the filesystem can
8682
* potentially renumber inodes, e.g. via filesystem shrinking.
8783
*/
8884
if (!sb->s_cop->has_stable_inodes ||
8985
!sb->s_cop->has_stable_inodes(sb)) {
9086
fscrypt_warn(inode,
91-
"Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't have stable inode numbers",
92-
sb->s_id);
87+
"Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
88+
type, sb->s_id);
9389
return false;
9490
}
9591
if (sb->s_cop->get_ino_and_lblk_bits)
9692
sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
97-
if (ino_bits > 32 || lblk_bits > 32) {
93+
if (ino_bits > max_ino_bits) {
94+
fscrypt_warn(inode,
95+
"Can't use %s policy on filesystem '%s' because its inode numbers are too long",
96+
type, sb->s_id);
97+
return false;
98+
}
99+
if (lblk_bits > max_lblk_bits) {
98100
fscrypt_warn(inode,
99-
"Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't use 32-bit inode and block numbers",
100-
sb->s_id);
101+
"Can't use %s policy on filesystem '%s' because its block numbers are too long",
102+
type, sb->s_id);
101103
return false;
102104
}
103105
return true;
@@ -140,6 +142,8 @@ static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
140142
static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
141143
const struct inode *inode)
142144
{
145+
int count = 0;
146+
143147
if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
144148
policy->filenames_encryption_mode)) {
145149
fscrypt_warn(inode,
@@ -155,13 +159,29 @@ static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
155159
return false;
156160
}
157161

162+
count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
163+
count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
164+
count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
165+
if (count > 1) {
166+
fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
167+
policy->flags);
168+
return false;
169+
}
170+
158171
if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
159172
!supported_direct_key_modes(inode, policy->contents_encryption_mode,
160173
policy->filenames_encryption_mode))
161174
return false;
162175

163176
if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
164-
!supported_iv_ino_lblk_64_policy(policy, inode))
177+
!supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
178+
32, 32))
179+
return false;
180+
181+
if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
182+
/* This uses hashed inode numbers, so ino_bits doesn't matter. */
183+
!supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
184+
INT_MAX, 32))
165185
return false;
166186

167187
if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
@@ -366,6 +386,9 @@ static int set_encryption_policy(struct inode *inode,
366386
policy->v2.master_key_identifier);
367387
if (err)
368388
return err;
389+
if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
390+
pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n",
391+
current->comm, current->pid);
369392
break;
370393
default:
371394
WARN_ON(1);

include/uapi/linux/fscrypt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#define FSCRYPT_POLICY_FLAGS_PAD_MASK 0x03
2020
#define FSCRYPT_POLICY_FLAG_DIRECT_KEY 0x04
2121
#define FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 0x08
22-
#define FSCRYPT_POLICY_FLAGS_VALID 0x0F
22+
#define FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 0x10
23+
#define FSCRYPT_POLICY_FLAGS_VALID 0x1F
2324

2425
/* Encryption algorithms */
2526
#define FSCRYPT_MODE_AES_256_XTS 1

0 commit comments

Comments
 (0)