linux/fs/crypto
Eric Biggers 6fe4e4b825 fscrypt: Avoid dynamic allocation in fscrypt_get_devices()
When a blk_crypto_key starts being used or is evicted, fs/crypto/ calls
fscrypt_get_devices() to get the filesystem's list of block devices,
then iterates over them and calls blk_crypto_config_supported(),
blk_crypto_start_using_key(), or blk_crypto_evict_key() on each one.

Currently, the block device pointers are placed in a dynamically
allocated array.  This dynamic allocation is problematic because:

- It can fail, especially at the fscrypt_destroy_inline_crypt_key() call
  site when it's invoked for inode eviction under direct reclaim.

- fscrypt_destroy_inline_crypt_key() doesn't handle the failure.  It
  just zeroizes and frees the blk_crypto_key without calling
  blk_crypto_evict_key().  That causes a use-after-free.

For now, let's fix this in the straightforward and easily-backportable
way by switching to an on-stack array.  Currently the fscrypt
multi-device functionality is used only by f2fs, which has a hardcoded
limit of 8 block devices.  An on-stack array works fine for that.

(Of course, this solution won't scale up to large number of block
devices.  For that we'd need a different solution, like moving the block
device iteration into the filesystem.  Or in the case of btrfs, which
will only support blk-crypto-fallback, we should make it just call
blk-crypto-fallback directly, so the block devices won't be needed.)

Fixes: 22e9947a4b ("fscrypt: stop holding extra request_queue references")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260713023708.9245-1-ebiggers%40kernel.org
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://patch.msgid.link/20260719055602.78828-1-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
2026-07-20 10:39:24 -07:00
..
bio.c fscrypt: pass a real sector_t to fscrypt_zeroout_range 2026-03-09 13:34:21 -07:00
crypto.c treewide: change inode->i_ino from unsigned long to u64 2026-03-06 14:31:28 +01:00
fname.c fs: add helpers name_is_dot{,dot,_dotdot} 2026-01-29 10:06:59 +01:00
fscrypt_private.h fscrypt: Replace mk_users keyring with simple list 2026-06-22 12:12:11 -07:00
hkdf.c fscrypt: use HMAC-SHA512 library for HKDF 2025-09-05 21:01:51 -07:00
hooks.c treewide: change inode->i_ino from unsigned long to u64 2026-03-06 14:31:28 +01:00
inline_crypt.c fscrypt: Avoid dynamic allocation in fscrypt_get_devices() 2026-07-20 10:39:24 -07:00
Kconfig fscrypt: use AES library for v1 key derivation 2026-03-25 12:06:33 -07:00
keyring.c fscrypt: Replace mk_users keyring with simple list 2026-06-22 12:12:11 -07:00
keysetup_v1.c fscrypt: Add missing superblock check in find_or_insert_direct_key() 2026-07-20 10:39:24 -07:00
keysetup.c fscrypt: Fix key setup in edge case with multiple data unit sizes 2026-06-22 12:12:11 -07:00
Makefile fscrypt: add inline encryption support 2020-07-08 10:29:30 -07:00
policy.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00