Archive-only list for syzbot
 help / color / mirror / Atom feed
From: Krystian Kaniewski <krystianmkaniewski@gmail.com>
To: syzbot <syzbot@kernel.org>,
	syzkaller-upstream-moderation@googlegroups.com
Cc: syzbot@lists.linux.dev
Subject: Re: [PATCH RFC] ima: annotate iint->mutex per filesystem type and stacking depth
Date: Thu, 26 Nov 2026 16:46:50 +0100	[thread overview]
Message-ID: <699282ee-e579-4eae-8a32-77610fdf9fe0@gmail.com> (raw)
In-Reply-To: <3579fbbf-56d8-4607-b372-3ef99fd7b51a@mail.kernel.org>

On 11/24/26 19:23, syzbot wrote:
> ima_iint_lockdep_annotate() currently assigns a global lockdep class
> (`ima_iint_mutex_key[depth]`) indexed solely by
> `inode->i_sb->s_stack_depth`. Because of this, all non-stacked filesystems
> (`s_stack_depth == 0`) share the exact same lockdep class
> `&ima_iint_mutex_key[0]` for `ima_iint_cache::mutex`, allowing lockdep to
> falsely bridge lock dependencies between unrelated filesystems such as ext4
> and sysfs.
> 
> Specifically, creating an entry in an overlayfs directory acquires the
> overlayfs directory `inode->i_rwsem` (`&ovl_i_mutex_dir_key[depth]#2`) and
> then `sb_writers#4` on the upper ext4 superblock in `ovl_create_object()`.
> Opening an existing regular file on ext4 with `O_TRUNC` acquires
> `sb_writers#4` via `mnt_want_write()` in `do_open()` before calling
> `security_file_post_open()` -> `ima_file_check()` ->
> `process_measurement()`, which locks `iint->mutex`
> (`&ima_iint_mutex_key[0]`). When a custom IMA policy allows measuring sysfs
> files, opening `/sys/power/resume` with `O_RDWR` causes
> `process_measurement()` to lock `iint->mutex` (also classified as
> `&ima_iint_mutex_key[0]`) and read the sysfs file via
> `integrity_kernel_read()` -> `seq_read_iter()` -> `kernfs_seq_start()`,
> establishing `&ima_iint_mutex_key[0]` -> `&p->lock` -> `&of->mutex` (using
> the writable `of->mutex` lockdep class due to `FMODE_WRITE`). Writing an
> uncached path on the overlayfs mount to `/sys/power/resume` then holds
> `&of->mutex` in `kernfs_fop_write_iter()` while `resume_store()` ->
> `lookup_bdev()` -> `kern_path()` -> `lookup_slow()` acquires
> `&ovl_i_mutex_dir_key[depth]#2`, completing the false lockdep cycle:
> 
> WARNING: possible circular locking dependency detected
> is trying to acquire lock:
> ffff888076a38c50 (&ovl_i_mutex_dir_key[depth]#2){++++}-{4:4}, at:
> lookup_slow+0x46/0x80 fs/namei.c:1941
> but task is already holding lock:
> ffff88807a4fa480 (&of->mutex){+.+.}-{4:4}, at:
> kernfs_fop_write_iter+0x1d8/0x540 fs/kernfs/file.c:336
> which lock already depends on the new lock.
> 
> the existing dependency chain (in reverse order) is:
> -> #4 (&of->mutex){+.+.}-{4:4}:
>        kernfs_seq_start+0x5d/0x420 fs/kernfs/file.c:165
>        seq_read_iter+0x37e/0xca0 fs/seq_file.c:226
>        __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+0xdc/0x130 security/integrity/ima/ima_main.c:686
>        security_file_post_open+0xb3/0x260 security/security.c:2777
> -> #3 (&p->lock){+.+.}-{4:4}:
>        seq_read_iter+0xa4/0xca0 fs/seq_file.c:183
>        ...
> -> #2 (&ima_iint_mutex_key[depth]){+.+.}-{4:4}:
>        process_measurement+0x7d0/0x1c10
>        security/integrity/ima/ima_main.c:319
>        ima_file_check+0xdc/0x130 security/integrity/ima/ima_main.c:686
>        security_file_post_open+0xb3/0x260 security/security.c:2777
> -> #1 (sb_writers#4){.+.+}-{0:0}:
>        sb_start_write+0x48/0x1c0 include/linux/fs/super.h:125
>        mnt_want_write+0x41/0x90 fs/namespace.c:531
>        ovl_create_object+0x10d/0x3b0 fs/overlayfs/dir.c:717
>        lookup_open+0x102d/0x1ae0 fs/namei.c:4575
> -> #0 (&ovl_i_mutex_dir_key[depth]#2){++++}-{4:4}:
>        down_read+0x45/0x320 kernel/locking/rwsem.c:1574
>        lookup_slow+0x46/0x80 fs/namei.c:1941
>        lookup_bdev+0xc8/0x2b0 block/bdev.c:1268
>        resume_store+0x181/0x500 kernel/power/hibernate.c:1286
>        kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
> 
> Fix this by adding a per-filesystem-type array
> `i_ima_iint_key[FILESYSTEM_MAX_STACK_DEPTH + 1]` (guarded by `CONFIG_IMA`)
> to `struct file_system_type`, similar to the existing per-filesystem-type
> inode lockdep keys, and updating `ima_iint_lockdep_annotate()` to set the
> class of `iint->mutex` to `&sb->s_type->i_ima_iint_key[depth]`. This
> isolates `iint->mutex` across unrelated filesystems while preserving
> distinct lockdep classes for each stacking layer (including nested
> overlayfs mounts) and avoiding collisions at `FILESYSTEM_MAX_STACK_DEPTH`
> with non-stacked pseudo-filesystems that set `s_stack_depth =
> FILESYSTEM_MAX_STACK_DEPTH`.
> 
> 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+da1d15f707a556f6e48c@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=da1d15f707a556f6e48c
> Link: https://syzkaller.appspot.com/ai_job?id=a9eee2d2-a95f-4524-a3f7-213929fe8238
> To: "Christian Brauner" <brauner@kernel.org>
> To: "Dmitry Kasatkin" <dmitry.kasatkin@gmail.com>
> To: "James Morris" <jmorris@namei.org>
> To: <linux-fsdevel@vger.kernel.org>
> To: <linux-integrity@vger.kernel.org>
> To: <linux-security-module@vger.kernel.org>
> To: "Paul Moore" <paul@paul-moore.com>
> To: "Roberto Sassu" <roberto.sassu@huawei.com>
> To: "Serge E. Hallyn" <serge@hallyn.com>
> To: "Alexander Viro" <viro@zeniv.linux.org.uk>
> To: "Mimi Zohar" <zohar@linux.ibm.com>
> To: "Amir Goldstein" <amir73il@gmail.com>
> Cc: "Eric Snowberg" <eric.snowberg@oracle.com>
> Cc: "Jan Kara" <jack@suse.cz>
> 
> ---
> 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..9372a2eec 100644
> --- a/security/integrity/ima/ima_iint.c
> +++ b/security/integrity/ima/ima_iint.c
> @@ -32,26 +32,26 @@ 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.
> - * See ovl_lockdep_annotate_inode_mutex_key() for more details.
> + * Annotate the iint mutex per filesystem type and stacking depth to avoid
> + * lockdep false positives across unrelated filesystems as well as between
> + * stacked filesystems (e.g. overlayfs, including nested overlayfs) and their
> + * underlying filesystems.
> + * See lockdep_annotate_inode_mutex_key() and
> + * 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;
> +	struct file_system_type *type = sb->s_type;
> +	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, &type->i_ima_iint_key[depth]);
>  #endif
>  }
>  
> 
> 
> base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Please shorten the commit message and drop the Fixes tag. Keep the code
diff exactly as it is, both hunks are fine.

The message walks through the whole lock chain and then quotes about
forty lines of a lockdep report with addresses and offsets. That report
closes the cycle in resume_store() through ovl_create_object(), while
the syzbot report closes it in ovl_copy_up_start(). Both come from the
same class merge, but a maintainer needs the cause and the reason why
the new keys remove the false cycle, not a full trace of either run.
Plain text without Markdown backticks also reads better in git log.

The Fixes tag names e044374a8a0a ("ima: annotate iint mutex to avoid
lockdep false positive warnings"). That commit did not introduce the
problem. Before it, every iint mutex was in one lockdep class, so ext4
and sysfs inodes were joined already. That commit also went to stable,
so the tag would ask for backports to trees that do not have
security/integrity/ima/ima_iint.c. No single commit introduced this
false cycle, so please leave the tag out.

A message along these lines would be enough:

    ima: annotate iint->mutex per filesystem type and stacking depth

    ima_iint_lockdep_annotate() gives iint->mutex one lockdep class per
    stacking depth only. All non-stacked filesystems share that class,
    so lockdep joins lock chains of unrelated filesystems through it.

    syzbot hit such a false cycle with a custom IMA policy that
    measures FILE_CHECK. An O_TRUNC open of an ext4 file takes
    iint->mutex under sb_writers of ext4. An O_RDWR open of
    /sys/power/resume takes the iint->mutex of the sysfs inode and
    reads the file, which takes the kernfs of->mutex. A write of an
    overlayfs path to /sys/power/resume looks the path up under
    of->mutex, and overlayfs with an ext4 upper layer takes sb_writers
    of ext4 under its own inode locks. Lockdep reports a possible
    deadlock, although the two iint mutexes belong to inodes of
    different filesystems.

    Add lockdep keys for the iint mutex to struct file_system_type,
    next to the per filesystem type keys of the inode locks, and select
    the key by filesystem type and stacking depth.
-- 
Krystian Kaniewski

      reply	other threads:[~2026-11-26 15:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 18:23 [PATCH RFC] ima: annotate iint->mutex per filesystem type and stacking depth syzbot
2026-11-26 15:46 ` Krystian Kaniewski [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=699282ee-e579-4eae-8a32-77610fdf9fe0@gmail.com \
    --to=krystianmkaniewski@gmail.com \
    --cc=syzbot@kernel.org \
    --cc=syzbot@lists.linux.dev \
    --cc=syzkaller-upstream-moderation@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox