From patchwork Wed Mar 20 12:39:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13597838 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03D00C54E58 for ; Wed, 20 Mar 2024 12:40:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75DA56B0092; Wed, 20 Mar 2024 08:40:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70E4B6B0093; Wed, 20 Mar 2024 08:40:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D60D6B0095; Wed, 20 Mar 2024 08:40:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4BBF36B0092 for ; Wed, 20 Mar 2024 08:40:34 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DFAF912135D for ; Wed, 20 Mar 2024 12:40:33 +0000 (UTC) X-FDA: 81917375946.16.C917F21 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf26.hostedemail.com (Postfix) with ESMTP id B1BE214001C for ; Wed, 20 Mar 2024 12:40:31 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qeoINsTh; spf=pass (imf26.hostedemail.com: domain of cem@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710938432; a=rsa-sha256; cv=none; b=NDoe1qqKTf4hr5nt07iMRdEg28OZYo/dFSyqYa5nIkU9khH7vd05eFRlx09h81RkbGLjVE bAojwptg/By3UjFadJ27yUpICuVH+/+YMoh1hpE4Orjzyh8ed2EmUJAM1c5XA8laisiYNK ADRCGPMux/EOa42IVcwbye0CSSa0v2M= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qeoINsTh; spf=pass (imf26.hostedemail.com: domain of cem@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710938432; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=5rNgv1CyHPTtjKU4mOhbG6lw17X8C1PeBtXBB/tSiKE=; b=BTkKTFpCFA1N3mqHY3nedbTpJB4N+5UwFeggvuHXC8d7AZsF/sh5D2e3T0SH2ZAYsK91MR u+pZG4KerQzwEol7zbGkXL5jGGiukuDq7ko2NLmpFvPKXAGbLZZVJauitUpEXGNcyHr7uC /RpFWKgSEZvz/Rzq1iQqq7ch/6GK8ak= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 02993CE108C; Wed, 20 Mar 2024 12:40:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB54AC433F1; Wed, 20 Mar 2024 12:40:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710938426; bh=L8vG9X0aLpduj2IMjd1lFAKHMHqMxxJSPbXUMsD9Vz0=; h=From:To:Cc:Subject:Date:From; b=qeoINsThEOWj9Hc0GsyoJQB+8JGsYzJG3bv+N2UstanrJLF5KoXOMJMLEsh08Ute0 QxeI5GjP4B2v4bDHFUe0moI6QPD8zhTSfodctZgVlCVj6mreRVCuvh+f28ul1bQHmd oirHvAQTq4Q0SIzmadHqjiKIlhkE0KkyQZajt3aT7G6MOHFS84yG6/UPGcnZVervY4 mKEzDAszC9112Agddbg1U5U5wGqBDYSnKpzS4uiSMBNXFWKKTCiZ53c/sbpm+gojMV IFaHEPnKBMLa5wd7HviizOA2TlVLvyhQzSIYIZZkV8KfXEujqLrvKZgQ4ukWr9ccrl fPXb69zXGhARw== From: cem@kernel.org To: akpm@linux-foundation.org Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, hughd@google.com Subject: [PATCH RESEND] tmpfs: Fix race on handling dquot rbtree Date: Wed, 20 Mar 2024 13:39:59 +0100 Message-ID: <20240320124011.398847-1-cem@kernel.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: B1BE214001C X-Stat-Signature: 4znegxqimhjkf3hkdxugjwr4iqdf9dos X-Rspam-User: X-HE-Tag: 1710938431-561595 X-HE-Meta: U2FsdGVkX18QLnTJ8nfYjQilK73mu+qRIOs3mT5hno+aaM8D/l1QvcGwAm/pYdblgn5flVe5HyuufxuXZtgqpzne6wZ6O9VbmCMLyCnd/EzOz+7wx8OErTBdEWSksHmLjBQPLGipWWwngIrSuDAKAawKKEv7lJN+ml1pnQuYG6bwm9NG0xVqcsBcdEEZLuNxeqlPRJlQGABK4vKALraywJk+bm6r7/PWRHxCo7Lih8WFgdgd9C+R6/wYmWwmEduNNDrCTE3e7EzCiODvkdsOwfgjm9cSIF85qvBYSKnZjwMRz8IErP0tDNTRsrDpOt3vyu4tsdEkX4HBViC2Bmto6D7pXEKog+sCrwsgOx7ky7c+IxHOa9O5wUcmCeWWAsxD6ixL12VzBiQ8dxjPy0eqBSvvh+EjTf8Dnx5opB5SQp+lnqesEv8sZFuMsCuhFH3H7e6If7W6C+g8sqgJ718lNAjZ6esfD8Y+2NcZJDRIbR60wxKeRY5VWiGDHer5n6NLLMydG+ka43Wa7uf9XcUbyOyNW2WEZ0yGUx9JtG1hXBvIZoCTgwRNo2tsFE7fszHOEA0TGshSK7sFm8MKv/zzu5Yo/dSs2oZ+1qAL6OzQ9tzkUez9MeaP8E3pB/gvPLvZ6mMJ+KybF9is6kLMXPJOPyOH3BKm7VailON9a8xHIRhift2cYPMRvKpiogH3CsB317NMVeaetPUZclJYxf9JQ/Eou2g/52QHzhfbBxNHr5lLPslFccGgcHcDAESqhFXqydgLxQLJUolBFj54sN4En/F9KSGTkVVqR9nXFMbPN4gpD0iruSGAqqxuTIJMBrUhO9sxKL25OQmL6tCc2mQ0hVX7L0apFzj7lEfavA2Atw/rj7Ad3w7lQUT5OVQLSJxzs7i3gFY8Ip6US1PTSExpoh4KxOiFpFXADW2F75TikfI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Carlos Maiolino 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. Fixes: eafc474e202978 ("shmem: prepare shmem quota infrastructure") Reported-by: Ubisectech Sirius Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- Hi Andrew, the original series has been pushed through your tree, do you think you can pull this patch too? Thanks, Carlos mm/shmem_quota.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/shmem_quota.c b/mm/shmem_quota.c index 062d1c1097ae3..ce514e700d2f6 100644 --- a/mm/shmem_quota.c +++ b/mm/shmem_quota.c @@ -116,7 +116,7 @@ static int shmem_free_file_info(struct super_block *sb, int type) static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) { struct mem_dqinfo *info = sb_dqinfo(sb, qid->type); - struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node *node; qid_t id = from_kqid(&init_user_ns, *qid); struct quota_info *dqopt = sb_dqopt(sb); struct quota_id *entry = NULL; @@ -126,6 +126,7 @@ static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) return -ESRCH; down_read(&dqopt->dqio_sem); + node = ((struct rb_root *)info->dqi_priv)->rb_node; while (node) { entry = rb_entry(node, struct quota_id, node); @@ -165,7 +166,7 @@ static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) static int shmem_acquire_dquot(struct dquot *dquot) { struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); - struct rb_node **n = &((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node **n; struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info; struct rb_node *parent = NULL, *new_node = NULL; struct quota_id *new_entry, *entry; @@ -176,6 +177,8 @@ static int shmem_acquire_dquot(struct dquot *dquot) mutex_lock(&dquot->dq_lock); down_write(&dqopt->dqio_sem); + n = &((struct rb_root *)info->dqi_priv)->rb_node; + while (*n) { parent = *n; entry = rb_entry(parent, struct quota_id, node); @@ -264,7 +267,7 @@ static bool shmem_is_empty_dquot(struct dquot *dquot) static int shmem_release_dquot(struct dquot *dquot) { struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); - struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node *node; qid_t id = from_kqid(&init_user_ns, dquot->dq_id); struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); struct quota_id *entry = NULL; @@ -275,6 +278,7 @@ static int shmem_release_dquot(struct dquot *dquot) goto out_dqlock; down_write(&dqopt->dqio_sem); + node = ((struct rb_root *)info->dqi_priv)->rb_node; while (node) { entry = rb_entry(node, struct quota_id, node);