Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] jffs2: wait for INO_STATE_CLEARING in jffs2_do_read_inode()
@ 2026-12-04  5:13 Shuai Liu
  2026-12-04  5:36 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-12-04  5:13 UTC (permalink / raw)
  To: linux-mtd
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	David Woodhouse, syzbot+40179a6b1f86287d2848

A concurrent jffs2_do_read_inode() can observe an inocache in
INO_STATE_CLEARING and hit the BUG() at the end of the state switch.

jffs2_do_clear_inode() sets INO_STATE_CLEARING on the inocache before
releasing the metadata node, the fragtree and the dirents, and only
restores INO_STATE_CHECKEDABSENT afterwards.  During that window the
inocache stays on the inocache hash list, so a concurrent reader can
still find it.  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.  jffs2_gc_fetch_inode() then goes through
jffs2_iget() -> jffs2_do_read_inode(), which finds the inocache in
INO_STATE_CLEARING -- a state with no case in the switch -- and calls
BUG().

  kernel BUG at fs/jffs2/readinode.c:1365
  RIP: 0010:jffs2_do_read_inode+0x699/0x6a0
  Call Trace:
   jffs2_iget+0x228/0xcc0
   jffs2_gc_fetch_inode+0xd9/0x340
   jffs2_garbage_collect_pass+0x1a69/0x2170
   jffs2_garbage_collect_thread+0x693/0x720

The clearing state is transient and always ends in INO_STATE_CHECKEDABSENT
(jffs2_set_inocache_state() wakes the inocache wait queue on every
transition), so a reader can simply wait for it, exactly like it already
waits for INO_STATE_CHECKING and INO_STATE_GC.  If the inocache is removed
while waiting, the retry loop re-reads the hash and fails with -ENOENT as
it already does for a nonexistent inode.

Fixes: 67e345d17ff8 ("[JFFS2] Prevent ino cache removal for inodes in use")
Reported-by: syzbot+40179a6b1f86287d2848@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=40179a6b1f86287d2848
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 fs/jffs2/readinode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

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);
-- 
2.43.0


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

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

* Re: [PATCH] jffs2: wait for INO_STATE_CLEARING in jffs2_do_read_inode()
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04  5:36 UTC (permalink / raw)
  To: Shuai Liu, linux-mtd
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	David Woodhouse, syzbot+40179a6b1f86287d2848

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/

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

end of thread, other threads:[~2026-12-04  5:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox