The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] KEYS: avoid filesystem reclaim while holding keyring->sem
@ 2026-06-14 15:00 Mohammed EL Kadiri
  2026-06-14 21:41 ` Hillf Danton
  2026-06-18 22:32 ` [PATCH] KEYS: avoid filesystem reclaim while holding keyring->sem Eric Biggers
  0 siblings, 2 replies; 4+ messages in thread
From: Mohammed EL Kadiri @ 2026-06-14 15:00 UTC (permalink / raw)
  To: dhowells, jarkko, paul
  Cc: jmorris, serge, ebiggers, keyrings, linux-security-module,
	linux-kernel, stable, syzkaller-bugs, Mohammed EL Kadiri,
	syzbot+f55b043dacf43776b50c

__key_link_begin() runs with keyring->sem held and calls
assoc_array_insert(), which does GFP_KERNEL allocations.  Those
allocations may enter filesystem reclaim, evict an fscrypt-protected
inode, and reach keyring_clear() via fscrypt_put_master_key() --
taking a keyring semaphore of the same lockdep class and closing a
keyring->sem -> fs_reclaim -> keyring->sem cycle reported by syzbot.

Wrap the assoc_array_insert() call with memalloc_nofs_save() /
memalloc_nofs_restore() so reclaim cannot recurse into the keys
subsystem while keyring->sem is held.

Reported-by: syzbot+f55b043dacf43776b50c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f55b043dacf43776b50c
Fixes: d7e7b9af104c ("fscrypt: stop using keyrings subsystem for fscrypt_master_key")
Cc: stable@vger.kernel.org
Signed-off-by: Mohammed EL Kadiri <med08elkadiri@gmail.com>
---
 security/keys/keyring.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 5a9887d6b7be..21bb2e7e7cca 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -12,6 +12,7 @@
 #include <linux/security.h>
 #include <linux/seq_file.h>
 #include <linux/err.h>
+#include <linux/sched/mm.h>
 #include <linux/user_namespace.h>
 #include <linux/nsproxy.h>
 #include <keys/keyring-type.h>
@@ -1298,6 +1299,7 @@ int __key_link_begin(struct key *keyring,
 		     struct assoc_array_edit **_edit)
 {
 	struct assoc_array_edit *edit;
+	unsigned int nofs_flags;
 	int ret;
 
 	kenter("%d,%s,%s,",
@@ -1315,10 +1317,12 @@ int __key_link_begin(struct key *keyring,
 	/* Create an edit script that will insert/replace the key in the
 	 * keyring tree.
 	 */
+	nofs_flags = memalloc_nofs_save();
 	edit = assoc_array_insert(&keyring->keys,
 				  &keyring_assoc_array_ops,
 				  index_key,
 				  NULL);
+	memalloc_nofs_restore(nofs_flags);
 	if (IS_ERR(edit)) {
 		ret = PTR_ERR(edit);
 		goto error;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-18 22:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-14 15:00 [PATCH] KEYS: avoid filesystem reclaim while holding keyring->sem Mohammed EL Kadiri
2026-06-14 21:41 ` Hillf Danton
2026-06-14 22:06   ` [syzbot] [keyrings?] [lsm?] possible deadlock in keyring_clear (3) syzbot
2026-06-18 22:32 ` [PATCH] KEYS: avoid filesystem reclaim while holding keyring->sem Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox