Skip to content

Commit b103fb7

Browse files
committed
fscrypt: add support for IV_INO_LBLK_64 policies
Inline encryption hardware compliant with the UFS v2.1 standard or with the upcoming version of the eMMC standard has the following properties: (1) Per I/O request, the encryption key is specified by a previously loaded keyslot. There might be only a small number of keyslots. (2) Per I/O request, the starting IV is specified by a 64-bit "data unit number" (DUN). IV bits 64-127 are assumed to be 0. The hardware automatically increments the DUN for each "data unit" of configurable size in the request, e.g. for each filesystem block. Property (1) makes it inefficient to use the traditional fscrypt per-file keys. Property (2) precludes the use of the existing DIRECT_KEY fscrypt policy flag, which needs at least 192 IV bits. Therefore, add a new fscrypt policy flag IV_INO_LBLK_64 which causes the encryption to modified as follows: - The encryption keys are derived from the master key, encryption mode number, and filesystem UUID. - The IVs are chosen as (inode_number << 32) | file_logical_block_num. For filenames encryption, file_logical_block_num is 0. Since the file nonces aren't used in the key derivation, many files may share the same encryption key. This is much more efficient on the target hardware. Including the inode number in the IVs and mixing the filesystem UUID into the keys ensures that data in different files is nevertheless still encrypted differently. Additionally, limiting the inode and block numbers to 32 bits and placing the block number in the low bits maintains compatibility with the 64-bit DUN convention (property (2) above). Since this scheme assumes that inode numbers are stable (which may preclude filesystem shrinking) and that inode and file logical block numbers are at most 32-bit, IV_INO_LBLK_64 will only be allowed on filesystems that meet these constraints. These are acceptable limitations for the cases where this format would actually be used. Note that IV_INO_LBLK_64 is an on-disk format, not an implementation. This patch just adds support for it using the existing filesystem layer encryption. A later patch will add support for inline encryption. Reviewed-by: Paul Crowley <paulcrowley@google.com> Co-developed-by: Satya Tangirala <satyat@google.com> Signed-off-by: Satya Tangirala <satyat@google.com> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent ff73c2c commit b103fb7

8 files changed

Lines changed: 146 additions & 41 deletions

File tree

‎Documentation/filesystems/fscrypt.rst‎

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,8 @@ alternative master keys or to support rotating master keys. Instead,
256256
the master keys may be wrapped in userspace, e.g. as is done by the
257257
`fscrypt <https://github.com/google/fscrypt>`_ tool.
258258

259-
Including the inode number in the IVs was considered. However, it was
260-
rejected as it would have prevented ext4 filesystems from being
261-
resized, and by itself still wouldn't have been sufficient to prevent
262-
the same key from being directly reused for both XTS and CTS-CBC.
263-
264-
DIRECT_KEY and per-mode keys
265-
----------------------------
259+
DIRECT_KEY policies
260+
-------------------
266261

267262
The Adiantum encryption mode (see `Encryption modes and usage`_) is
268263
suitable for both contents and filenames encryption, and it accepts
@@ -285,6 +280,21 @@ IV. Moreover:
285280
key derived using the KDF. Users may use the same master key for
286281
other v2 encryption policies.
287282

283+
IV_INO_LBLK_64 policies
284+
-----------------------
285+
286+
When FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 is set in the fscrypt policy,
287+
the encryption keys are derived from the master key, encryption mode
288+
number, and filesystem UUID. This normally results in all files
289+
protected by the same master key sharing a single contents encryption
290+
key and a single filenames encryption key. To still encrypt different
291+
files' data differently, inode numbers are included in the IVs.
292+
Consequently, shrinking the filesystem may not be allowed.
293+
294+
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.
297+
288298
Key identifiers
289299
---------------
290300

@@ -342,10 +352,16 @@ a little endian number, except that:
342352
is encrypted with AES-256 where the AES-256 key is the SHA-256 hash
343353
of the file's data encryption key.
344354

345-
- In the "direct key" configuration (FSCRYPT_POLICY_FLAG_DIRECT_KEY
346-
set in the fscrypt_policy), the file's nonce is also appended to the
347-
IV. Currently this is only allowed with the Adiantum encryption
348-
mode.
355+
- With `DIRECT_KEY policies`_, the file's nonce is appended to the IV.
356+
Currently this is only allowed with the Adiantum encryption mode.
357+
358+
- With `IV_INO_LBLK_64 policies`_, the logical block number is limited
359+
to 32 bits and is placed in bits 0-31 of the IV. The inode number
360+
(which is also limited to 32 bits) is placed in bits 32-63.
361+
362+
Note that because file logical block numbers are included in the IVs,
363+
filesystems must enforce that blocks are never shifted around within
364+
encrypted files, e.g. via "collapse range" or "insert range".
349365

350366
Filenames encryption
351367
--------------------
@@ -355,10 +371,10 @@ the requirements to retain support for efficient directory lookups and
355371
filenames of up to 255 bytes, the same IV is used for every filename
356372
in a directory.
357373

358-
However, each encrypted directory still uses a unique key; or
359-
alternatively (for the "direct key" configuration) has the file's
360-
nonce included in the IVs. Thus, IV reuse is limited to within a
361-
single directory.
374+
However, each encrypted directory still uses a unique key, or
375+
alternatively has the file's nonce (for `DIRECT_KEY policies`_) or
376+
inode number (for `IV_INO_LBLK_64 policies`_) included in the IVs.
377+
Thus, IV reuse is limited to within a single directory.
362378

363379
With CTS-CBC, the IV reuse means that when the plaintext filenames
364380
share a common prefix at least as long as the cipher block size (16
@@ -432,12 +448,15 @@ This structure must be initialized as follows:
432448
(1) for ``contents_encryption_mode`` and FSCRYPT_MODE_AES_256_CTS
433449
(4) for ``filenames_encryption_mode``.
434450

435-
- ``flags`` must contain a value from ``<linux/fscrypt.h>`` which
436-
identifies the amount of NUL-padding to use when encrypting
437-
filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32 (0x3).
438-
Additionally, if the encryption modes are both
439-
FSCRYPT_MODE_ADIANTUM, this can contain
440-
FSCRYPT_POLICY_FLAG_DIRECT_KEY; see `DIRECT_KEY and per-mode keys`_.
451+
- ``flags`` contains optional flags from ``<linux/fscrypt.h>``:
452+
453+
- FSCRYPT_POLICY_FLAGS_PAD_*: The amount of NUL padding to use when
454+
encrypting filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32
455+
(0x3).
456+
- FSCRYPT_POLICY_FLAG_DIRECT_KEY: See `DIRECT_KEY policies`_.
457+
- FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64: See `IV_INO_LBLK_64
458+
policies`_. This is mutually exclusive with DIRECT_KEY and is not
459+
supported on v1 policies.
441460

442461
- For v2 encryption policies, ``__reserved`` must be zeroed.
443462

@@ -1090,7 +1109,7 @@ policy structs (see `Setting an encryption policy`_), except that the
10901109
context structs also contain a nonce. The nonce is randomly generated
10911110
by the kernel and is used as KDF input or as a tweak to cause
10921111
different files to be encrypted differently; see `Per-file keys`_ and
1093-
`DIRECT_KEY and per-mode keys`_.
1112+
`DIRECT_KEY policies`_.
10941113

10951114
Data path changes
10961115
-----------------

‎fs/crypto/crypto.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,17 @@ EXPORT_SYMBOL(fscrypt_free_bounce_page);
7373
void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
7474
const struct fscrypt_info *ci)
7575
{
76+
u8 flags = fscrypt_policy_flags(&ci->ci_policy);
77+
7678
memset(iv, 0, ci->ci_mode->ivsize);
77-
iv->lblk_num = cpu_to_le64(lblk_num);
7879

79-
if (fscrypt_is_direct_key_policy(&ci->ci_policy))
80+
if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
81+
WARN_ON_ONCE((u32)lblk_num != lblk_num);
82+
lblk_num |= (u64)ci->ci_inode->i_ino << 32;
83+
} else if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
8084
memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
85+
}
86+
iv->lblk_num = cpu_to_le64(lblk_num);
8187
}
8288

8389
/* Encrypt or decrypt a single filesystem block of file contents */

‎fs/crypto/fscrypt_private.h‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ struct fscrypt_info {
163163
/* The actual crypto transform used for encryption and decryption */
164164
struct crypto_skcipher *ci_ctfm;
165165

166+
/* True if the key should be freed when this fscrypt_info is freed */
167+
bool ci_owns_key;
168+
166169
/*
167170
* Encryption mode used for this inode. It corresponds to either the
168171
* contents or filenames encryption mode, depending on the inode type.
@@ -281,7 +284,8 @@ extern int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
281284
*/
282285
#define HKDF_CONTEXT_KEY_IDENTIFIER 1
283286
#define HKDF_CONTEXT_PER_FILE_KEY 2
284-
#define HKDF_CONTEXT_PER_MODE_KEY 3
287+
#define HKDF_CONTEXT_DIRECT_KEY 3
288+
#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4
285289

286290
extern int fscrypt_hkdf_expand(struct fscrypt_hkdf *hkdf, u8 context,
287291
const u8 *info, unsigned int infolen,
@@ -378,8 +382,14 @@ struct fscrypt_master_key {
378382
struct list_head mk_decrypted_inodes;
379383
spinlock_t mk_decrypted_inodes_lock;
380384

381-
/* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
382-
struct crypto_skcipher *mk_mode_keys[__FSCRYPT_MODE_MAX + 1];
385+
/* Crypto API transforms for DIRECT_KEY policies, allocated on-demand */
386+
struct crypto_skcipher *mk_direct_tfms[__FSCRYPT_MODE_MAX + 1];
387+
388+
/*
389+
* Crypto API transforms for filesystem-layer implementation of
390+
* IV_INO_LBLK_64 policies, allocated on-demand.
391+
*/
392+
struct crypto_skcipher *mk_iv_ino_lblk_64_tfms[__FSCRYPT_MODE_MAX + 1];
383393

384394
} __randomize_layout;
385395

‎fs/crypto/keyring.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ static void free_master_key(struct fscrypt_master_key *mk)
4343

4444
wipe_master_key_secret(&mk->mk_secret);
4545

46-
for (i = 0; i < ARRAY_SIZE(mk->mk_mode_keys); i++)
47-
crypto_free_skcipher(mk->mk_mode_keys[i]);
46+
for (i = 0; i <= __FSCRYPT_MODE_MAX; i++) {
47+
crypto_free_skcipher(mk->mk_direct_tfms[i]);
48+
crypto_free_skcipher(mk->mk_iv_ino_lblk_64_tfms[i]);
49+
}
4850

4951
key_put(mk->mk_users);
5052
kzfree(mk);

‎fs/crypto/keysetup.c‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,40 +114,54 @@ int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
114114
return PTR_ERR(tfm);
115115

116116
ci->ci_ctfm = tfm;
117+
ci->ci_owns_key = true;
117118
return 0;
118119
}
119120

120121
static int setup_per_mode_key(struct fscrypt_info *ci,
121-
struct fscrypt_master_key *mk)
122+
struct fscrypt_master_key *mk,
123+
struct crypto_skcipher **tfms,
124+
u8 hkdf_context, bool include_fs_uuid)
122125
{
126+
const struct inode *inode = ci->ci_inode;
127+
const struct super_block *sb = inode->i_sb;
123128
struct fscrypt_mode *mode = ci->ci_mode;
124129
u8 mode_num = mode - available_modes;
125130
struct crypto_skcipher *tfm, *prev_tfm;
126131
u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
132+
u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
133+
unsigned int hkdf_infolen = 0;
127134
int err;
128135

129-
if (WARN_ON(mode_num >= ARRAY_SIZE(mk->mk_mode_keys)))
136+
if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX))
130137
return -EINVAL;
131138

132139
/* pairs with cmpxchg() below */
133-
tfm = READ_ONCE(mk->mk_mode_keys[mode_num]);
140+
tfm = READ_ONCE(tfms[mode_num]);
134141
if (likely(tfm != NULL))
135142
goto done;
136143

137144
BUILD_BUG_ON(sizeof(mode_num) != 1);
145+
BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
146+
BUILD_BUG_ON(sizeof(hkdf_info) != 17);
147+
hkdf_info[hkdf_infolen++] = mode_num;
148+
if (include_fs_uuid) {
149+
memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
150+
sizeof(sb->s_uuid));
151+
hkdf_infolen += sizeof(sb->s_uuid);
152+
}
138153
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
139-
HKDF_CONTEXT_PER_MODE_KEY,
140-
&mode_num, sizeof(mode_num),
154+
hkdf_context, hkdf_info, hkdf_infolen,
141155
mode_key, mode->keysize);
142156
if (err)
143157
return err;
144-
tfm = fscrypt_allocate_skcipher(mode, mode_key, ci->ci_inode);
158+
tfm = fscrypt_allocate_skcipher(mode, mode_key, inode);
145159
memzero_explicit(mode_key, mode->keysize);
146160
if (IS_ERR(tfm))
147161
return PTR_ERR(tfm);
148162

149163
/* pairs with READ_ONCE() above */
150-
prev_tfm = cmpxchg(&mk->mk_mode_keys[mode_num], NULL, tfm);
164+
prev_tfm = cmpxchg(&tfms[mode_num], NULL, tfm);
151165
if (prev_tfm != NULL) {
152166
crypto_free_skcipher(tfm);
153167
tfm = prev_tfm;
@@ -178,7 +192,19 @@ static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
178192
ci->ci_mode->friendly_name);
179193
return -EINVAL;
180194
}
181-
return setup_per_mode_key(ci, mk);
195+
return setup_per_mode_key(ci, mk, mk->mk_direct_tfms,
196+
HKDF_CONTEXT_DIRECT_KEY, false);
197+
} else if (ci->ci_policy.v2.flags &
198+
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
199+
/*
200+
* IV_INO_LBLK_64: encryption keys are derived from (master_key,
201+
* mode_num, filesystem_uuid), and inode number is included in
202+
* the IVs. This format is optimized for use with inline
203+
* encryption hardware compliant with the UFS or eMMC standards.
204+
*/
205+
return setup_per_mode_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
206+
HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
207+
true);
182208
}
183209

184210
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
@@ -302,8 +328,7 @@ static void put_crypt_info(struct fscrypt_info *ci)
302328

303329
if (ci->ci_direct_key)
304330
fscrypt_put_direct_key(ci->ci_direct_key);
305-
else if (ci->ci_ctfm != NULL &&
306-
!fscrypt_is_direct_key_policy(&ci->ci_policy))
331+
else if (ci->ci_owns_key)
307332
crypto_free_skcipher(ci->ci_ctfm);
308333

309334
key = ci->ci_master_key;

‎fs/crypto/policy.c‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
2929
return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
3030
}
3131

32+
static bool supported_iv_ino_lblk_64_policy(
33+
const struct fscrypt_policy_v2 *policy,
34+
const struct inode *inode)
35+
{
36+
struct super_block *sb = inode->i_sb;
37+
int ino_bits = 64, lblk_bits = 64;
38+
39+
if (policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
40+
fscrypt_warn(inode,
41+
"The DIRECT_KEY and IV_INO_LBLK_64 flags are mutually exclusive");
42+
return false;
43+
}
44+
/*
45+
* It's unsafe to include inode numbers in the IVs if the filesystem can
46+
* potentially renumber inodes, e.g. via filesystem shrinking.
47+
*/
48+
if (!sb->s_cop->has_stable_inodes ||
49+
!sb->s_cop->has_stable_inodes(sb)) {
50+
fscrypt_warn(inode,
51+
"Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't have stable inode numbers",
52+
sb->s_id);
53+
return false;
54+
}
55+
if (sb->s_cop->get_ino_and_lblk_bits)
56+
sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
57+
if (ino_bits > 32 || lblk_bits > 32) {
58+
fscrypt_warn(inode,
59+
"Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't use 32-bit inode and block numbers",
60+
sb->s_id);
61+
return false;
62+
}
63+
return true;
64+
}
65+
3266
/**
3367
* fscrypt_supported_policy - check whether an encryption policy is supported
3468
*
@@ -55,7 +89,8 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
5589
return false;
5690
}
5791

58-
if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
92+
if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
93+
FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
5994
fscrypt_warn(inode,
6095
"Unsupported encryption flags (0x%02x)",
6196
policy->flags);
@@ -83,6 +118,10 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
83118
return false;
84119
}
85120

121+
if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
122+
!supported_iv_ino_lblk_64_policy(policy, inode))
123+
return false;
124+
86125
if (memchr_inv(policy->__reserved, 0,
87126
sizeof(policy->__reserved))) {
88127
fscrypt_warn(inode,

‎include/linux/fscrypt.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ struct fscrypt_operations {
6161
bool (*dummy_context)(struct inode *);
6262
bool (*empty_dir)(struct inode *);
6363
unsigned int max_namelen;
64+
bool (*has_stable_inodes)(struct super_block *sb);
65+
void (*get_ino_and_lblk_bits)(struct super_block *sb,
66+
int *ino_bits_ret, int *lblk_bits_ret);
6467
};
6568

6669
static inline bool fscrypt_has_encryption_key(const struct inode *inode)

‎include/uapi/linux/fscrypt.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#define FSCRYPT_POLICY_FLAGS_PAD_32 0x03
1818
#define FSCRYPT_POLICY_FLAGS_PAD_MASK 0x03
1919
#define FSCRYPT_POLICY_FLAG_DIRECT_KEY 0x04
20-
#define FSCRYPT_POLICY_FLAGS_VALID 0x07
20+
#define FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 0x08
21+
#define FSCRYPT_POLICY_FLAGS_VALID 0x0F
2122

2223
/* Encryption algorithms */
2324
#define FSCRYPT_MODE_AES_256_XTS 1

0 commit comments

Comments
 (0)