Lockdep reports a possible circular locking dependency when a network filesystem (such as 9p using netfs/cachefiles) writes dirty pages to a local backing cache filesystem while truncating a file: WARNING: possible circular locking dependency detected trying to acquire lock: ffff88811ab82478 (sb_writers#5){.+.+}-{0:0}, at: kiocb_start_write include/linux/fs.h:2799 [inline] ffff88811ab82478 (sb_writers#5){.+.+}-{0:0}, at: vfs_iocb_iter_write+0x1cb/0x5e0 fs/read_write.c:929 but task is already holding lock: ffff888123fd3c00 (&sb->s_type->i_mutex_key#25){++++}-{4:4}, at: inode_lock_killable include/linux/fs.h:1032 [inline] ffff888123fd3c00 (&sb->s_type->i_mutex_key#25){++++}-{4:4}, at: do_truncate+0x18f/0x250 fs/open.c:63 Chain exists of: sb_writers#5 --> &ima_iint_mutex_key[depth] --> &sb->s_type->i_mutex_key#25 -> #2 (&sb->s_type->i_mutex_key#25){++++}-{4:4}: down_read_interruptible+0x4b/0x3b0 kernel/locking/rwsem.c:1584 netfs_start_io_read+0x25/0x180 fs/netfs/locking.c:67 netfs_buffered_read_iter+0xd6/0x160 fs/netfs/buffered_read.c:940 __kernel_read+0x4ca/0x960 fs/read_write.c:512 integrity_kernel_read+0x89/0xd0 security/integrity/iint.c:28 ima_calc_file_hash+0x451/0x890 security/integrity/ima/ima_crypto.c:280 ima_collect_measurement+0x51b/0xa00 security/integrity/ima/ima_api.c:300 process_measurement+0x1272/0x1c10 security/integrity/ima/ima_main.c:425 ima_file_check+0xcc/0x110 security/integrity/ima/ima_main.c:686 security_file_post_open+0xb3/0x260 security/security.c:2777 do_open fs/namei.c:4853 [inline] -> #1 (&ima_iint_mutex_key[depth]){+.+.}-{4:4}: __mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821 process_measurement+0x7d0/0x1c10 security/integrity/ima/ima_main.c:319 ima_file_check+0xcc/0x110 security/integrity/ima/ima_main.c:686 security_file_post_open+0xb3/0x260 security/security.c:2777 do_open fs/namei.c:4853 [inline] -> #0 (sb_writers#5){.+.+}-{0:0}: sb_start_write+0x48/0x1c0 include/linux/fs/super.h:125 kiocb_start_write include/linux/fs.h:2799 [inline] vfs_iocb_iter_write+0x1cb/0x5e0 fs/read_write.c:929 __cachefiles_write+0x6c9/0xbd0 fs/cachefiles/io.c:355 netfs_issue_write fs/netfs/write_issue.c:290 [inline] netfs_advance_write+0x328/0x4b0 fs/netfs/write_issue.c:331 netfs_writepages+0x12d3/0x22b0 fs/netfs/write_issue.c:600 do_writepages+0x338/0x560 mm/page-writeback.c:2562 filemap_fdatawrite+0x1e9/0x2f0 mm/filemap.c:417 v9fs_vfs_setattr_dotl+0x4dc/0xab0 fs/9p/vfs_inode_dotl.c:585 notify_change+0xbba/0xea0 fs/attr.c:556 do_truncate+0x1c2/0x250 fs/open.c:68 handle_truncate fs/namei.c:4306 [inline] do_open fs/namei.c:4855 [inline] This is a false positive caused by ima_iint_lockdep_annotate() assigning iint->mutex a lockdep class from a single global ima_iint_mutex_key[] array indexed only by inode->i_sb->s_stack_depth. While VFS per-superblock and per-inode locks (such as sb_writers and i_rwsem) use per-file_system_type lockdep keys, both the upper network filesystem (9p) and the local backing cache filesystem have s_stack_depth == 0 and therefore share &ima_iint_mutex_key[0]. As a result, lockdep merges two independent filesystem lock hierarchies through &ima_iint_mutex_key[0]: opening a file with O_TRUNC on the backing filesystem acquires sb_writers#5 before process_measurement() locks iint->mutex, opening a 9p file acquires iint->mutex before ima_collect_measurement() reads the file under 9p's inode->i_rwsem (&sb->s_type->i_mutex_key#25), and truncating a 9p file with dirty cached pages holds 9p's inode->i_rwsem while flushing via cachefiles to the backing filesystem and acquiring sb_writers#5. At runtime, the two iint->mutex instances belong to distinct inodes on distinct filesystems and cannot deadlock. Fix this by adding a per-file_system_type lockdep key array i_ima_iint_key[FILESYSTEM_MAX_STACK_DEPTH + 1] (guarded by CONFIG_IMA) to struct file_system_type and updating ima_iint_lockdep_annotate() to use &sb->s_type->i_ima_iint_key[depth] instead of a global key array. This gives each filesystem type and stacking depth its own lockdep class for iint->mutex, matching the rest of the VFS lock hierarchy. Fixes: e044374a8a0a ("ima: annotate iint mutex to avoid lockdep false positive warnings") Assisted-by: Gemini:gemini-4-hydrogen Gemini:gemini-4-carbon Gemini:gemini-3.8-flash syzbot Reported-by: syzbot+b8ae5f37f5baa84cb085@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=b8ae5f37f5baa84cb085 Link: https://syzkaller.appspot.com/ai_job?id=a3b01a54-85f8-477a-8590-02aa5c0529b4 To: "Christian Brauner" To: "Dmitry Kasatkin" To: "James Morris" To: To: To: To: "Paul Moore" To: "Roberto Sassu" To: "Serge E. Hallyn" To: "Alexander Viro" To: "Mimi Zohar" To: "Amir Goldstein" Cc: "Eric Snowberg" Cc: "Jan Kara" --- diff --git a/include/linux/fs.h b/include/linux/fs.h index 5b3a100ab..8fd02c303 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2323,6 +2323,9 @@ struct file_system_type { struct lock_class_key i_mutex_key; struct lock_class_key invalidate_lock_key; struct lock_class_key i_mutex_dir_key; +#ifdef CONFIG_IMA + struct lock_class_key i_ima_iint_key[FILESYSTEM_MAX_STACK_DEPTH + 1]; +#endif struct lock_class_key d_lookup_key; }; diff --git a/security/integrity/ima/ima_iint.c b/security/integrity/ima/ima_iint.c index 00b249101..c71fce8f9 100644 --- a/security/integrity/ima/ima_iint.c +++ b/security/integrity/ima/ima_iint.c @@ -32,26 +32,24 @@ struct ima_iint_cache *ima_iint_find(struct inode *inode) return ima_inode_get_iint(inode); } -#define IMA_MAX_NESTING (FILESYSTEM_MAX_STACK_DEPTH + 1) - /* - * It is not clear that IMA should be nested at all, but as long is it measures - * files both on overlayfs and on underlying fs, we need to annotate the iint - * mutex to avoid lockdep false positives related to IMA + overlayfs. + * Give each filesystem type (and stacking depth) its own lockdep class for + * iint->mutex, matching VFS per-filesystem-type locks such as i_rwsem and + * s_writers, to avoid lockdep false positives across stacked or backing + * filesystems (e.g. overlayfs, cachefiles). * See ovl_lockdep_annotate_inode_mutex_key() for more details. */ 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)) + if (WARN_ON_ONCE(depth < 0 || depth > FILESYSTEM_MAX_STACK_DEPTH)) depth = 0; - lockdep_set_class(&iint->mutex, &ima_iint_mutex_key[depth]); + lockdep_set_class(&iint->mutex, &sb->s_type->i_ima_iint_key[depth]); #endif } base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f -- This is an AI-generated patch subject to moderation. Reply with '#syz upstream' to Sign-off the patch as a human author and send it to the upstream kernel mailing lists. Reply with '#syz reject' to reject it ('#syz unreject' to undo). See https://goo.gle/syzbot-ai-patches for information about AI-generated patches. You can comment on the patch as usual, syzbot will try to address the comments and send a new version of the patch if necessary. syzbot engineers can be reached at syzkaller@googlegroups.com.