ima: key iint mutex lockdep class per filesystem type
Subsystems (6)
PATCH 1/1 ima: key iint mutex lockdep class per filesystem type
syzbot reports a circular locking dependency between the iint mutex, a 9p inode's i_rwsem and the sb_writers of the cachefiles backing fs:
sb_writers#12 --> &ima_iint_mutex_key[depth] --> &sb->s_type->i_mutex_key#25
The three edges are:
backing fs O_TRUNC open: sb_writers(backing) -> iint(backing inode) 9p open + IMA hash: iint(9p inode) -> i_rwsem(9p inode) 9p truncate/writeback: i_rwsem(9p inode) -> sb_writers(backing)
The last one is inherent to netfs: it writes to its cachefiles backing file under its own inode lock. The cycle only closes because the iint mutex is keyed by s_stack_depth alone, so the 9p and backing fs iints, both at depth 0, share one lockdep class. They are different inodes and nothing waits on a netfs iint while holding the backing fs's sb_writers, so this is a false positive.
Key the iint mutex per filesystem type as well as depth, as is done for i_rwsem with s_type->i_mutex_key. The overlayfs case covered by the depth annotation is unchanged.
---
Tested in QEMU on v7.4-rc4 (syzbot config, ima_policy=tcb): cachefiles bound on an ext4 disk, 9p over virtio with cache=fscache, then 50 rounds of write + O_RDONLY|O_TRUNC open on 9p and on the backing fs, followed by 20 IMA-measured O_TRUNC opens through an overlayfs on the backing fs.
unpatched: "possible circular locking dependency detected", same chain
patched: no lockdep report, debug_locks still 1; the syzbot C repro
also ran 90s with no report
include/linux/fs.h | 3 +++ security/integrity/ima/ima_iint.c | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
include/linux/fs.h +3 -0
@@ -2325,6 +2325,9 @@ struct file_system_type {struct lock_class_key i_mutex_dir_key;struct lock_class_key d_lookup_key; +#ifdef CONFIG_IMA +struct lock_class_key ima_iint_key[FILESYSTEM_MAX_STACK_DEPTH +1 ]; +#endif };#define MODULE_ALIAS_FS (NAME ) MODULE_ALIAS("fs-" NAME)
security/integrity/ima/ima_iint.c +7 -4
@@ -39,19 +39,22 @@ struct ima_iint_cache *ima_iint_find(struct inode *inode)* files both on overlayfs and on underlying fs, we need to annotate the iint * mutex to avoid lockdep false positives related to IMA + overlayfs. * See ovl_lockdep_annotate_inode_mutex_key() for more details. +* +* Depth alone is not enough: a netfs inode writes to its cachefiles backing +* fs under its own i_rwsem, and both are at depth 0. So, like i_rwsem, key +* the mutex per filesystem type too. */ static inline void ima_iint_lockdep_annotate (struct ima_iint_cache *iint,struct inode *inode) {#ifdef CONFIG_LOCKDEP -static struct lock_class_key ima_iint_mutex_key[IMA_MAX_NESTING ]; - -int depth = inode->i_sb->s_stack_depth; +struct super_block *sb = inode->i_sb; +int depth = sb->s_stack_depth;if (WARN_ON_ONCE (depth <0 || depth >=IMA_MAX_NESTING )) depth =0 ; -lockdep_set_class (&iint->mutex, &ima_iint_mutex_key[depth]); +lockdep_set_class (&iint->mutex, &sb->s_type->ima_iint_key[depth]);#endif }
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 18 lines
syzbot reports a circular locking dependency between the iint mutex, a 9p inode's
i_rwsemand thesb_writersof the cachefiles backing fs:sb_writers#12 --> &ima_iint_mutex_key[depth] --> &sb->s_type->i_mutex_key#25
The three edges are:
backing fs O_TRUNC open: sb_writers(backing) -> iint(backing inode) 9p open + IMA hash: iint(9p inode) -> i_rwsem(9p inode) 9p truncate/writeback: i_rwsem(9p inode) -> sb_writers(backing)The last one is inherent to netfs: it writes to its cachefiles backing file under its own inode lock. The cycle only closes because the iint mutex is keyed by
s_stack_depthalone, so the 9p and backing fs iints, both at depth 0, share one lockdep class. They are different inodes and nothing waits on a netfs iint while holding the backing fs'ssb_writers, so this is a false positive.
Quoted 8 lines
Key the iint mutex per filesystem type as well as depth, as is done for
i_rwsemwiths_type->i_mutex_key. The overlayfs case covered by the depth annotation is unchanged.Fixes e044374a8a0a ("ima: annotate iint mutex to avoid lockdep false positive warnings")Reported-by [email protected]Signed-off-by Nguyen Ngoc Thang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Mimi Zohar
maintainerQuoted 32 lines · Nguyen Ngoc Thang
syzbot reports a circular locking dependency between the iint mutex, a 9p inode's
i_rwsemand thesb_writersof the cachefiles backing fs:sb_writers#12 --> &ima_iint_mutex_key[depth] --> &sb->s_type->i_mutex_key#25
The three edges are:
backing fs O_TRUNC open: sb_writers(backing) -> iint(backing inode) 9p open + IMA hash: iint(9p inode) -> i_rwsem(9p inode) 9p truncate/writeback: i_rwsem(9p inode) -> sb_writers(backing)The last one is inherent to netfs: it writes to its cachefiles backing file under its own inode lock. The cycle only closes because the iint mutex is keyed by
s_stack_depthalone, so the 9p and backing fs iints, both at depth 0, share one lockdep class. They are different inodes and nothing waits on a netfs iint while holding the backing fs'ssb_writers, so this is a false positive.Key the iint mutex per filesystem type as well as depth, as is done for
i_rwsemwiths_type->i_mutex_key. The overlayfs case covered by the depth annotation is unchanged.Fixes e044374a8a0a ("ima: annotate iint mutex to avoid lockdep false positive warnings")Reported-by [email protected]Signed-off-by Nguyen Ngoc Thang <[email protected]>---
Tested in QEMU on v7.4-rc4 (syzbot config, ima_policy=tcb): cachefiles bound on an ext4 disk, 9p over virtio with cache=fscache, then 50 rounds of write + O_RDONLY|O_TRUNC open on 9p and on the backing fs, followed by 20 IMA-measuredO_TRUNCopens through an overlayfs on the backing fs.
Quoted 7 lines
unpatched: "possible circular locking dependency detected", same chain
patched: no lockdep report,debug_locksstill 1; the syzbot C repro
also ran 90s with no reportinclude/linux/fs.h | 3 +++ security/integrity/ima/ima_iint.c | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
Hi Nguyen,
Thanks for the patch. Keying the iint mutex per filesystem type, as is done for i_rwsem, makes sense to break the 9p/cachefiles cycle.
One suggestion: instead of an array of lock_class_keys in struct file_system_type, consider using lockdep_set_class_and_subclass() with a single key and pass the stack depth as the subclass:
lockdep_set_class_and_subclass(&iint->mutex,
&sb->s_type->ima_iint_key, depth);A lock_class_key already provides MAX_LOCKDEP_SUBCLASSES subkeys, so this gives the same distinct classes (overlayfs is unaffected) without allocating FILESYSTEM_MAX_STACK_DEPTH + 1 full keys per filesystem type, and it avoids having the array size in fs.h and IMA_MAX_NESTING in ima_iint.c defined independently. A BUILD_BUG_ON(IMA_MAX_NESTING > MAX_LOCKDEP_SUBCLASSES) would keep the bound honest.
thanks,
Mimi
Quoted 45 lines
include/linux/fs.h +3
-0@@ -2325,6 +2325,9 @@ struct file_system_type {struct lock_class_key i_mutex_dir_key;struct lock_class_key d_lookup_key; +#ifdef CONFIG_IMA +struct lock_class_key ima_iint_key[FILESYSTEM_MAX_STACK_DEPTH +1 ]; +#endif };#define MODULE_ALIAS_FS (NAME ) MODULE_ALIAS("fs-" NAME)security/integrity/ima/ima_iint.c +7
-4@@ -39,19 +39,22 @@ struct ima_iint_cache *ima_iint_find(struct inode *inode)* files both on overlayfs and on underlying fs, we need to annotate the iint * mutex to avoid lockdep false positives related to IMA + overlayfs. * See ovl_lockdep_annotate_inode_mutex_key() for more details. +* +* Depth alone is not enough: a netfs inode writes to its cachefiles backing +* fs under its own i_rwsem, and both are at depth 0. So, like i_rwsem, key +* the mutex per filesystem type too. */ static inline void ima_iint_lockdep_annotate (struct ima_iint_cache *iint,struct inode *inode) {#ifdef CONFIG_LOCKDEP -static struct lock_class_key ima_iint_mutex_key[IMA_MAX_NESTING ]; - -int depth = inode->i_sb->s_stack_depth; +struct super_block *sb = inode->i_sb; +int depth = sb->s_stack_depth;if (WARN_ON_ONCE (depth <0 || depth >=IMA_MAX_NESTING )) depth =0 ; -lockdep_set_class (&iint->mutex, &ima_iint_mutex_key[depth]); +lockdep_set_class (&iint->mutex, &sb->s_type->ima_iint_key[depth]);#endif }