Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp103174lqp; Tue, 21 May 2024 20:51:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVvEWWoFz3uUeEIx65yzN10poF/YE/TwNkmwX1CQEgVZVtukZHdAKGNbTfVx3XYxb+5KVmsHvqt23g6uIi9MqIsN7ng4pmOHQ361cYMzw== X-Google-Smtp-Source: AGHT+IGCG70SrqAkY6u5kWrFNnhVc/55GluK3tcT6DMVotmfKIK9Fph/SW1JY+bdnSi5NUrDKa99 X-Received: by 2002:a05:6214:c6b:b0:6a0:9607:a441 with SMTP id 6a1803df08f44-6a843e69b60mr196846586d6.28.1716349883869; Tue, 21 May 2024 20:51:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716349883; cv=pass; d=google.com; s=arc-20160816; b=gb5O3/M42IEWlFI1xS07GjkL3fYdrh3fHDv7aieER3S7murWjP0z36U01ItLRjEvUz PV3GmmwB6S5l0B0djV1xn9hX/VIGs3T+aqH88e7zhkxBqiTphyjS+FlAQyKBMh0eltDv nbBHsGR03Jib7nJbdUMsj81c6ntfHg4Dcvab6r8978Q+JXkHmntXoR5cgvmRiJMkXzKa hd7dqtpjdidgs2yw7Yynhb5YqkAuhDFIbXQgERMTp+wy+d2G1B9WMLokOQ/AAqupntC2 lrA3IwqsisyS2twlMVMcZnSjJ506QkrRid1DKjrrBXU0Ro/pMWI9gfSoYsPWQy/0wSCn 0CqQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=DQcFkT/J/JJ50n6enoAcftIjNXTvRDxb+qug3E4g9gE=; fh=dfpuzL54LAMf7GQmHoqmub+qZCPWKUPNTVFHnj8Vxqk=; b=S8tx1BDUJIJvcF4oP2Tguq51OVpoN/9OHaYxMoGoXcG0siaP9lYOKpoGnYU16kBKao pMSgftripONMVtvG5x6mRiy0ezUlRxMcEofmfgZvZTLiEwB+dtUv4zSpue7VS4BCGijs znCN/Q9p8Dn1nmOHaOlLwtpYBjzsJ72PEpMlDrOrAjI9xnhow+3Fft4lr3dpDW6QTBP1 GxtkprcUsPEAYCPeUBfT1v5FsoJR1k1pBEPGj+HHNC1Af4ZSpFPMQeWB+rvR+o3SbWLH dO2kJMdQg2SeTty1L+yD9XwqnMGlDmzx2DcqWQiWAcZq6qYfou74RBi49MqTUmXeqbLo WTkA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-185738-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185738-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43e3cca84absi39199411cf.79.2024.05.21.20.51.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 20:51:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185738-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-185738-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185738-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 996281C219B5 for ; Wed, 22 May 2024 03:51:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 212BD7D401; Wed, 22 May 2024 03:50:07 +0000 (UTC) Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72BAD78B50; Wed, 22 May 2024 03:50:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716349806; cv=none; b=kVT2qofBW2zLiEYqQKB6bz+Gcg9RLewl8cq2+lMYLgwoI/CACsOGt+FRtvwrHAVUGaSjN46nbOk7i+WZSOFZT3V8Xo7l9+dwXbDnP6TO/xf1VIbR6nRPBF7yg0hIoq+c6+f1mHmK5vte+71gvSP2A1VTl9RaexKNYSK2ul3dsBI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716349806; c=relaxed/simple; bh=KGhLjDE6mjYa1JN2Te7dwSeDKaLR124ro7acF+e5Tec=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DI2bCA/ez2G7OmASU7+d9GEU8fn4AsTjC2pxZGPZgrQ8D59cdRC5WmE6ylKCC+gVgkyXpMuQBAdS+RkkKY86hdug+3qw4ITeSRgke+CQPWYSGc0ycCj4Lrs6UdVZKDOqqUYNpwfZLlNDVrVzL3r9BYPsOD9utB95nBQMgqpupuQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Vkckk16vNz4f3jkP; Wed, 22 May 2024 11:49:50 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id EA4141A0199; Wed, 22 May 2024 11:49:55 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgBHGBFea01mxlBXNQ--.57627S6; Wed, 22 May 2024 11:49:55 +0800 (CST) From: libaokun@huaweicloud.com To: netfs@lists.linux.dev, dhowells@redhat.com, jlayton@kernel.org Cc: hsiangkao@linux.alibaba.com, jefflexu@linux.alibaba.com, zhujia.zj@bytedance.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, libaokun@huaweicloud.com, yangerkun@huawei.com, houtao1@huawei.com, yukuai3@huawei.com, wozizhi@huawei.com, Baokun Li Subject: [PATCH v3 02/12] cachefiles: remove requests from xarray during flushing requests Date: Wed, 22 May 2024 19:42:58 +0800 Message-Id: <20240522114308.2402121-3-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240522114308.2402121-1-libaokun@huaweicloud.com> References: <20240522114308.2402121-1-libaokun@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgBHGBFea01mxlBXNQ--.57627S6 X-Coremail-Antispam: 1UD129KBjvJXoW7Zr1rJr1fKr43CrW8urykKrg_yoW8Ar4xpF WSk3W7KFykWr4jkr1kAFyrJryxJ348XF17XF17Xr1kZrn8Zrn5Zr4Sy340gr98ArWktFW3 tr1UCF9rX34Ut3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQ014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2jI8I6cxK62vIxIIY0VWUZVW8XwA2048vs2IY02 0E87I2jVAFwI0_Jryl82xGYIkIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2 F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjx v20xvEc7CjxVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2 z280aVCY1x0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0V AKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1l Ox8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErc IFxwACI402YVCY1x02628vn2kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l 4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67 AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8I cVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI 8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v2 6r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjTRtb18UUUUU X-CM-SenderInfo: 5olet0hnxqqx5xdzvxpfor3voofrz/ From: Baokun Li Even with CACHEFILES_DEAD set, we can still read the requests, so in the following concurrency the request may be used after it has been freed: mount | daemon_thread1 | daemon_thread2 ------------------------------------------------------------ cachefiles_ondemand_init_object cachefiles_ondemand_send_req REQ_A = kzalloc(sizeof(*req) + data_len) wait_for_completion(&REQ_A->done) cachefiles_daemon_read cachefiles_ondemand_daemon_read // close dev fd cachefiles_flush_reqs complete(&REQ_A->done) kfree(REQ_A) xa_lock(&cache->reqs); cachefiles_ondemand_select_req req->msg.opcode != CACHEFILES_OP_READ // req use-after-free !!! xa_unlock(&cache->reqs); xa_destroy(&cache->reqs) Hence remove requests from cache->reqs when flushing them to avoid accessing freed requests. Fixes: c8383054506c ("cachefiles: notify the user daemon when looking up cookie") Signed-off-by: Baokun Li Acked-by: Jeff Layton Reviewed-by: Jia Zhu Reviewed-by: Gao Xiang Reviewed-by: Jingbo Xu --- fs/cachefiles/daemon.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index 6465e2574230..ccb7b707ea4b 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -159,6 +159,7 @@ static void cachefiles_flush_reqs(struct cachefiles_cache *cache) xa_for_each(xa, index, req) { req->error = -EIO; complete(&req->done); + __xa_erase(xa, index); } xa_unlock(xa); -- 2.39.2