tmpfs: fix race on handling dquot rbtree
authorCarlos Maiolino <cem@kernel.org>
Wed, 20 Mar 2024 12:39:59 +0000 (13:39 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 26 Mar 2024 18:07:23 +0000 (11:07 -0700)
commit0a69b6b3a026543bc215ccc866d0aea5579e6ce2
tree261159c40ad9a7b4ff98dc0041c06c9204013cd3
parent105840ebd76d8dbc1a7d734748ae320076f3201e
tmpfs: fix race on handling dquot rbtree

A syzkaller reproducer found a race while attempting to remove dquot
information from the rb tree.

Fetching the rb_tree root node must also be protected by the
dqopt->dqio_sem, otherwise, giving the right timing, shmem_release_dquot()
will trigger a warning because it couldn't find a node in the tree, when
the real reason was the root node changing before the search starts:

Thread 1 Thread 2
- shmem_release_dquot() - shmem_{acquire,release}_dquot()

- fetch ROOT - Fetch ROOT

- acquire dqio_sem
- wait dqio_sem

- do something, triger a tree rebalance
- release dqio_sem

- acquire dqio_sem
- start searching for the node, but
  from the wrong location, missing
  the node, and triggering a warning.

Link: https://lkml.kernel.org/r/20240320124011.398847-1-cem@kernel.org
Fixes: eafc474e2029 ("shmem: prepare shmem quota infrastructure")
Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
Reported-by: Ubisectech Sirius <bugreport@ubisectech.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Hugh Dickins <hughd@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/shmem_quota.c