All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shuai Liu" <847807867@qq.com>, linux-mtd@lists.infradead.org
Cc: "Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	syzbot+40179a6b1f86287d2848@syzkaller.appspotmail.com
Subject: Re: [PATCH] jffs2: wait for INO_STATE_CLEARING in jffs2_do_read_inode()
Date: Fri, 04 Dec 2026 05:36:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-249154@kernel.org> (raw)
In-Reply-To: <tencent_874E70B9634A17B0E8C86B351DE39288450A@qq.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] jffs2: UAF and double-free of inocache on wakeup from
  INO_STATE_CLEARING
- [Low] jffs2: commit message misattributes race to normal final iput()
  eviction
--

commit 01069a7558247104973c1e812d3b69b5afe1be6b
Author: Shuai Liu <847807867@qq.com>

jffs2: wait for INO_STATE_CLEARING in jffs2_do_read_inode()

This commit adds INO_STATE_CLEARING to the wait cases in
jffs2_do_read_inode() so concurrent readers sleep on inocache_wq and retry
instead of hitting BUG().

> Eviction is not serialized against the garbage
> collector: jffs2_garbage_collect_pass() holds alloc_sem, but the final
> iput() -> jffs2_evict_inode() -> jffs2_do_clear_inode() path needs no
> alloc_sem, so the GC thread can iget() an inode whose in-core copy is
> being cleared at that moment.

[Severity: Low]
Is this description accurate in attributing the race to normal final iput()
eviction?

During normal eviction, evict() keeps the inode on inode_hashtable with
I_FREEING set until jffs2_evict_inode() -> jffs2_do_clear_inode() finishes,
and only calls remove_inode_hash() afterwards.  A concurrent jffs2_iget() ->
iget_locked() -> find_inode_fast() therefore waits in
__wait_on_freeing_inode() until clearing completes.

Entering jffs2_do_read_inode() concurrently with INO_STATE_CLEARING requires
an inode that was already unhashed before eviction, such as via
iget_failed() -> make_bad_inode() -> remove_inode_hash().

[ ... ]

> diff --git a/fs/jffs2/readinode.c b/fs/jffs2/readinode.c
> index 1caabff9dc910..70df024789bdf 100644
> --- a/fs/jffs2/readinode.c
> +++ b/fs/jffs2/readinode.c
> @@ -1344,7 +1344,8 @@ int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
>  
>  		case INO_STATE_CHECKING:
>  		case INO_STATE_GC:
> -			/* If it's in either of these states, we need
> +		case INO_STATE_CLEARING:
> +			/* If it's in any of these states, we need
>  			   to wait for whoever's got it to finish and
>  			   put it back. */
>  			dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);

[Severity: High]
Can waking up from INO_STATE_CLEARING cause a use-after-free and double free
of struct jffs2_inode_cache?

jffs2_do_clear_inode() calls jffs2_set_inocache_state(), which sets
INO_STATE_CHECKEDABSENT, wakes c->inocache_wq, and drops c->inocache_lock
before checking whether the inocache is empty and removing it:

fs/jffs2/readinode.c:jffs2_do_clear_inode() {
    ...
	if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
		jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
		if (f->inocache->nodes == (void *)f->inocache)
			jffs2_del_ino_cache(c, f->inocache);
	}
    ...
}

When a failing creation path such as jffs2_create() calls iget_failed(), the
inode is unhashed while its inocache remains on c->inocache_list with
nodes == (void *)ic.  A concurrent jffs2_iget() can sleep on
INO_STATE_CLEARING and race with the deletion:

CPU0                              CPU1
jffs2_create() -> iget_failed()
  jffs2_do_clear_inode(c, f1)
    state = INO_STATE_CLEARING
                                  jffs2_iget() -> jffs2_do_read_inode()
                                    sleep_on_spinunlock(&c->inocache_wq)
    jffs2_set_inocache_state(
      INO_STATE_CHECKEDABSENT)
      wake_up(&c->inocache_wq)
                                    ic = jffs2_get_ino_cache()
                                    ic->state = INO_STATE_READING
                                    jffs2_do_read_inode_internal()
                                      /* !rii.fds -> -EIO */
                                      state = INO_STATE_CHECKEDABSENT
    if (ic->nodes == (void *)ic)
      jffs2_del_ino_cache(c, ic)
        jffs2_free_inode_cache()
                                    iget_failed(inode2)
                                      jffs2_do_clear_inode(c, f2)
                                        f2->inocache  <-- use-after-free

Because jffs2_do_read_inode_internal() leaves f2->inocache set on -EIO,
CPU1 then calls jffs2_do_clear_inode() and jffs2_del_ino_cache() on the
freed inocache, causing a use-after-free and double free.

If CPU1 frees the inocache first, CPU0 hits a use-after-free reading
f->inocache->nodes and a double free in jffs2_del_ino_cache().  If CPU0
reaches jffs2_del_ino_cache() while CPU1 is still in INO_STATE_READING, it
unlinks the active inocache from c->inocache_list.

jffs2_do_clear_inode() should update the state and remove any empty inocache
under c->inocache_lock before waking c->inocache_wq.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_874E70B9634A17B0E8C86B351DE39288450A@qq.com?part=1

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

      reply	other threads:[~2026-12-04  5:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  5:13 [PATCH] jffs2: wait for INO_STATE_CLEARING in jffs2_do_read_inode() Shuai Liu
2026-12-04  5:36 ` sashiko-bot [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=sashiko-outbox-249154@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=847807867@qq.com \
    --cc=dwmw2@infradead.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=syzbot+40179a6b1f86287d2848@syzkaller.appspotmail.com \
    --cc=vigneshr@ti.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.