Skip to content

Commit afdb0f2

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
Pull fscrypt updates from Eric Biggers: - Add the IV_INO_LBLK_32 encryption policy flag which modifies the encryption to be optimized for eMMC inline encryption hardware. - Make the test_dummy_encryption mount option for ext4 and f2fs support v2 encryption policies. - Fix kerneldoc warnings and some coding style inconsistencies. * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt: fscrypt: add support for IV_INO_LBLK_32 policies fscrypt: make test_dummy_encryption use v2 by default fscrypt: support test_dummy_encryption=v2 fscrypt: add fscrypt_add_test_dummy_key() linux/parser.h: add include guards fscrypt: remove unnecessary extern keywords fscrypt: name all function parameters fscrypt: fix all kerneldoc warnings
2 parents 829f3b9 + e3b1078 commit afdb0f2

File tree

18 files changed

+737
-302
lines changed

18 files changed

+737
-302
lines changed

Documentation/filesystems/f2fs.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,12 @@ fsync_mode=%s Control the policy of fsync. Currently supports "posix",
225225
pass, but the performance will regress. "nobarrier" is
226226
based on "posix", but doesn't issue flush command for
227227
non-atomic files likewise "nobarrier" mount option.
228-
test_dummy_encryption Enable dummy encryption, which provides a fake fscrypt
228+
test_dummy_encryption
229+
test_dummy_encryption=%s
230+
Enable dummy encryption, which provides a fake fscrypt
229231
context. The fake fscrypt context is used by xfstests.
232+
The argument may be either "v1" or "v2", in order to
233+
select the corresponding fscrypt policy version.
230234
checkpoint=%s[:%u[%]] Set to "disable" to turn off checkpointing. Set to "enable"
231235
to reenable checkpointing. Is enabled by default. While
232236
disabled, any unmounting or unexpected shutdowns will cause

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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags)
5454

5555
/**
5656
* fscrypt_free_bounce_page() - free a ciphertext bounce page
57+
* @bounce_page: the bounce page to free, or NULL
5758
*
5859
* Free a bounce page that was allocated by fscrypt_encrypt_pagecache_blocks(),
5960
* or by fscrypt_alloc_bounce_page() directly.
@@ -76,8 +77,12 @@ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
7677
memset(iv, 0, ci->ci_mode->ivsize);
7778

7879
if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
79-
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);
8082
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);
8186
} else if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
8287
memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
8388
}
@@ -132,7 +137,8 @@ int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
132137
}
133138

134139
/**
135-
* fscrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a pagecache page
140+
* fscrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a
141+
* pagecache page
136142
* @page: The locked pagecache page containing the block(s) to encrypt
137143
* @len: Total size of the block(s) to encrypt. Must be a nonzero
138144
* multiple of the filesystem's block size.
@@ -222,7 +228,8 @@ int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
222228
EXPORT_SYMBOL(fscrypt_encrypt_block_inplace);
223229

224230
/**
225-
* fscrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a pagecache page
231+
* fscrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a
232+
* pagecache page
226233
* @page: The locked pagecache page containing the block(s) to decrypt
227234
* @len: Total size of the block(s) to decrypt. Must be a nonzero
228235
* multiple of the filesystem's block size.
@@ -346,6 +353,8 @@ void fscrypt_msg(const struct inode *inode, const char *level,
346353

347354
/**
348355
* fscrypt_init() - Set up for fs encryption.
356+
*
357+
* Return: 0 on success; -errno on failure
349358
*/
350359
static int __init fscrypt_init(void)
351360
{

fs/crypto/fname.c

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <crypto/skcipher.h>
1919
#include "fscrypt_private.h"
2020

21-
/**
21+
/*
2222
* struct fscrypt_nokey_name - identifier for directory entry when key is absent
2323
*
2424
* When userspace lists an encrypted directory without access to the key, the
@@ -100,9 +100,12 @@ static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
100100

101101
/**
102102
* fscrypt_fname_encrypt() - encrypt a filename
103-
*
104-
* The output buffer must be at least as large as the input buffer.
105-
* Any extra space is filled with NUL padding before encryption.
103+
* @inode: inode of the parent directory (for regular filenames)
104+
* or of the symlink (for symlink targets)
105+
* @iname: the filename to encrypt
106+
* @out: (output) the encrypted filename
107+
* @olen: size of the encrypted filename. It must be at least @iname->len.
108+
* Any extra space is filled with NUL padding before encryption.
106109
*
107110
* Return: 0 on success, -errno on failure
108111
*/
@@ -152,8 +155,11 @@ int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
152155

153156
/**
154157
* fname_decrypt() - decrypt a filename
155-
*
156-
* The caller must have allocated sufficient memory for the @oname string.
158+
* @inode: inode of the parent directory (for regular filenames)
159+
* or of the symlink (for symlink targets)
160+
* @iname: the encrypted filename to decrypt
161+
* @oname: (output) the decrypted filename. The caller must have allocated
162+
* enough space for this, e.g. using fscrypt_fname_alloc_buffer().
157163
*
158164
* Return: 0 on success, -errno on failure
159165
*/
@@ -201,7 +207,10 @@ static const char lookup_table[65] =
201207
#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
202208

203209
/**
204-
* base64_encode() -
210+
* base64_encode() - base64-encode some bytes
211+
* @src: the bytes to encode
212+
* @len: number of bytes to encode
213+
* @dst: (output) the base64-encoded string. Not NUL-terminated.
205214
*
206215
* Encodes the input string using characters from the set [A-Za-z0-9+,].
207216
* The encoded string is roughly 4/3 times the size of the input string.
@@ -267,7 +276,12 @@ bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
267276
}
268277

269278
/**
270-
* fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames
279+
* fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames
280+
* @inode: inode of the parent directory (for regular filenames)
281+
* or of the symlink (for symlink targets)
282+
* @max_encrypted_len: maximum length of encrypted filenames the buffer will be
283+
* used to present
284+
* @crypto_str: (output) buffer to allocate
271285
*
272286
* Allocate a buffer that is large enough to hold any decrypted or encoded
273287
* filename (null-terminated), for the given maximum encrypted filename length.
@@ -292,9 +306,10 @@ int fscrypt_fname_alloc_buffer(const struct inode *inode,
292306
EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
293307

294308
/**
295-
* fscrypt_fname_free_buffer - free the buffer for presented filenames
309+
* fscrypt_fname_free_buffer() - free a buffer for presented filenames
310+
* @crypto_str: the buffer to free
296311
*
297-
* Free the buffer allocated by fscrypt_fname_alloc_buffer().
312+
* Free a buffer that was allocated by fscrypt_fname_alloc_buffer().
298313
*/
299314
void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
300315
{
@@ -306,10 +321,19 @@ void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
306321
EXPORT_SYMBOL(fscrypt_fname_free_buffer);
307322

308323
/**
309-
* fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
310-
* space
311-
*
312-
* The caller must have allocated sufficient memory for the @oname string.
324+
* fscrypt_fname_disk_to_usr() - convert an encrypted filename to
325+
* user-presentable form
326+
* @inode: inode of the parent directory (for regular filenames)
327+
* or of the symlink (for symlink targets)
328+
* @hash: first part of the name's dirhash, if applicable. This only needs to
329+
* be provided if the filename is located in an indexed directory whose
330+
* encryption key may be unavailable. Not needed for symlink targets.
331+
* @minor_hash: second part of the name's dirhash, if applicable
332+
* @iname: encrypted filename to convert. May also be "." or "..", which
333+
* aren't actually encrypted.
334+
* @oname: output buffer for the user-presentable filename. The caller must
335+
* have allocated enough space for this, e.g. using
336+
* fscrypt_fname_alloc_buffer().
313337
*
314338
* If the key is available, we'll decrypt the disk name. Otherwise, we'll
315339
* encode it for presentation in fscrypt_nokey_name format.

0 commit comments

Comments
 (0)