Fix crash with fscrypt + Landlock
Eric Biggers
maintainercover Fix crash with fscrypt + Landlock
This series fixes a bug, found by syzbot, where when a filesystem was being unmounted, the fscrypt keyring was destroyed before inodes have been released by the Landlock LSM.
Eric Biggers (3):
fscrypt: destroy keyring after security_sb_delete()
fscrypt: improve fscrypt_destroy_keyring() documentation
fscrypt: check for NULL keyring in fscrypt_put_master_key_activeref()
fs/crypto/keyring.c | 23 +++++++++++++---------- fs/super.c | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 13 deletions(-)
base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6
Signature · 1 line
2.39.2
Eric Biggers
maintainerPATCH 1/3 fscrypt: destroy keyring after security_sb_delete()
From: Eric Biggers <[email protected]>
fscrypt_destroy_keyring() must be called after all potentially-encrypted inodes were evicted; otherwise it cannot safely destroy the keyring. Since inodes that are in-use by the Landlock LSM don't get evicted until security_sb_delete(), this means that fscrypt_destroy_keyring() must be called *after* security_sb_delete().
This fixes a WARN_ON followed by a NULL dereference, only possible if Landlock was being used on encrypted files.
--- fs/super.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
fs/super.c +12 -3
@@ -475,13 +475,22 @@ void generic_shutdown_super(struct super_block *sb)cgroup_writeback_umount (); -/* evict all inodes with zero refcount */ +/* Evict all inodes with zero refcount. */ evict_inodes (sb); -/* only nonzero refcount inodes can have marks */ + +/* +* Clean up and evict any inodes that still have references due +* to fsnotify or the security policy. +*/ fsnotify_sb_delete (sb); -fscrypt_destroy_keyring (sb);security_sb_delete (sb); +/* +* Now that all potentially-encrypted inodes have been evicted, +* the fscrypt keyring can be destroyed. +*/ +fscrypt_destroy_keyring (sb); +if (sb->s_dio_done_wq) {destroy_workqueue (sb->s_dio_done_wq); sb->s_dio_done_wq =NULL ;
Signature · 1 line
2.39.2
Re: [PATCH 1/3] fscrypt: destroy keyring after security_sb_delete()
Quoted 11 lines · Eric Biggers
From: Eric Biggers <[email protected]>
fscrypt_destroy_keyring()must be called after all potentially-encrypted inodes were evicted; otherwise it cannot safely destroy the keyring. Since inodes that are in-use by the Landlock LSM don't get evicted untilsecurity_sb_delete(), this means thatfscrypt_destroy_keyring()must be called *after*security_sb_delete().This fixes a
WARN_ONfollowed by a NULL dereference, only possible if Landlock was being used on encrypted files.
Quoted 6 lines
Fixes d7e7b9af104c ("fscrypt: stop using keyrings subsystem for fscrypt_master_key")Reported-by [email protected]Signed-off-by Eric Biggers <[email protected]>---
Looks good,
Eric Biggers
maintainerPATCH 2/3 fscrypt: improve fscrypt_destroy_keyring() documentation
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Document that it must be called after all potentially-encrypted inodes have been evicted.
--- fs/crypto/keyring.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
fs/crypto/keyring.c +11 -10
@@ -207,10 +207,11 @@ static int allocate_filesystem_keyring(struct super_block *sb)* Release all encryption keys that have been added to the filesystem, along * with the keyring that contains them. * -* This is called at unmount time. The filesystem's underlying block device(s) -* are still available at this time; this is important because after user file -* accesses have been allowed, this function may need to evict keys from the -* keyslots of an inline crypto engine, which requires the block device(s). +* This is called at unmount time, after all potentially-encrypted inodes have +* been evicted. The filesystem's underlying block device(s) are still +* available at this time; this is important because after user file accesses +* have been allowed, this function may need to evict keys from the keyslots of +* an inline crypto engine, which requires the block device(s). */ void fscrypt_destroy_keyring (struct super_block *sb) {
@@ -227,12 +228,12 @@ void fscrypt_destroy_keyring(struct super_block *sb)hlist_for_each_entry_safe (mk, tmp, bucket, mk_node) {/* -* Since all inodes were already evicted, every key -* remaining in the keyring should have an empty inode -* list, and should only still be in the keyring due to -* the single active ref associated with ->mk_secret. -* There should be no structural refs beyond the one -* associated with the active ref. +* Since all potentially-encrypted inodes were already +* evicted, every key remaining in the keyring should +* have an empty inode list, and should only still be in +* the keyring due to the single active ref associated +* with ->mk_secret. There should be no structural refs +* beyond the one associated with the active ref. */ WARN_ON (refcount_read (&mk->mk_active_refs) !=1 );WARN_ON (refcount_read (&mk->mk_struct_refs) !=1 );
Signature · 1 line
2.39.2
Eric Biggers
maintainerPATCH 3/3 fscrypt: check for NULL keyring in fscrypt_put_master_key_activeref()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
It is a bug for fscrypt_put_master_key_activeref() to see a NULL keyring. But it used to be possible due to the bug, now fixed, where fscrypt_destroy_keyring() was called before security_sb_delete(). To be consistent with how fscrypt_destroy_keyring() uses WARN_ON for the same issue, WARN and leak the fscrypt_master_key if the keyring is NULL instead of dereferencing the NULL pointer.
This is a robustness improvement, not a fix.
--- fs/crypto/keyring.c | 2 ++ 1 file changed, 2 insertions(+)
fs/crypto/keyring.c +2 -0
@@ -92,6 +92,8 @@ void fscrypt_put_master_key_activeref(struct super_block *sb,* destroying any subkeys embedded in it. */ +if (WARN_ON (!sb->s_master_keys)) +return ;spin_lock (&sb->s_master_keys->lock);hlist_del_rcu (&mk->mk_node);spin_unlock (&sb->s_master_keys->lock);
Signature · 1 line
2.39.2