Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3178794lqo; Wed, 15 May 2024 01:58:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXPY6kgxMEtqvukrM+Zj8cIqJUfjwEdFJDBaCxzXG/l0ALJijtutnquNKJUgCWYotL5iA1NhcXMgGCEsheKqAHl8zego7t7u8Gp6iu6Rw== X-Google-Smtp-Source: AGHT+IGdRhCFyxS1KfLdnJkUA9M4iC+Hp4BKIWUQSpammQLhJVfAvttTJZg5x+oLsacFjrxm8WLE X-Received: by 2002:a05:6808:912:b0:3c9:6a50:95f5 with SMTP id 5614622812f47-3c9970d1a09mr15604391b6e.56.1715763517661; Wed, 15 May 2024 01:58:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715763517; cv=pass; d=google.com; s=arc-20160816; b=W1MAupGTOPQjyh95TdgStXqi65y+g23apeWguvunM8lvi9T0iLlvdwiUfb9dOTCGYV 36Kv1DgGJutKHuDhul3uZgu4zfQYb/lMfWIo8Vify9H8APUi2bpDE//k0qt9svBmR+wh uPNYY5Ny03lu4OCvCfSJCVeCX4QeFw8ZmfZcFrCFJkkBRtbjhLSKg+fbF+At0YPomRFq UwJJkzqjdsJ3jonlMO7TFWeaxD9LqnOIKj2W8EOdG1GoBUeOAaDlgNFcAYUfG0aIx0Iu m75RDyZgt6LJFhNLdISzKMgZb/jlnRjDecgTYDM+TCQ6JBsNDAOBayYJCepPIYLBGBah IXFw== 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=Y9JXLwr1LwOiwZ8dGbV6pgUhVXVBIDCSzyrlukyN4+o=; fh=dfpuzL54LAMf7GQmHoqmub+qZCPWKUPNTVFHnj8Vxqk=; b=pk+e8CBVjXsNhUYZuyil1cD4dmy8gZT7FRC3HNShtDgwOiSejO19EHCF5ZgGfS+LHq obWQvycvarMbYlkpQ9B1pWrhjEmLvZRbwzM0JpzvEqOZe1C+6x8KeKGgTA0S0IAY+q2J Zlb91uphyWyOOfLp5bdhOchbV0khmin0Et4CkFTlt/5eM34o45rjiiixZ8abZbTYfxuq rOYaSqe6lKFvdlPjj1nVUe64LtCbjaucBMkaY8wKb42XctzkjgxpLZDFby6unH3EDuH0 ChrVirnY/EsoxhiVCnJOVLaKOiV/JKo24q11gCP7peDXcwZHb2T7HpLcmVd0bkdY+ya+ lfGA==; 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-179673-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179673-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-634103f6af9si13587732a12.337.2024.05.15.01.58.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 01:58:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179673-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-179673-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179673-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 0A8D2B22C48 for ; Wed, 15 May 2024 08:57:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AAA4B5A0E1; Wed, 15 May 2024 08:56:37 +0000 (UTC) Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [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 ED37D53368; Wed, 15 May 2024 08:56:33 +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=1715763397; cv=none; b=l9c+T749ZmVCjSL/6ONVPZJkceKDPvwQSSAkbxaszlwBqvzyxTk8GRV/kihUl9gHBnAkY1KGu78i/o9iHgsmhyScHX9wIgwKqGxHoRa8RCuSXTJ81x0Oqrp/0vqEGD7sLuD1b/3jHmZCK98uGKp/+HtoLNgRpBo4Xqe/lahNm9Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715763397; c=relaxed/simple; bh=YchpI8ImUUy79SyzomMMcU/1PwDg4DZ77Ixp8gSuUD8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fchsTcUFuNXPMcyRjBkv0E9ZKuRDTVC7LiTu7YGMPMBAeywiMxekcdY11rYT9tkZwcYZlVOIZKwzrheE+PByslDSrGwr7P1+jTCPQfo4hpLRawWzc2gcyooz5sU5Mm3p8/EHQAebJd/eAS/iHkmdwUkAU9xwKjDuu1nJKd52DJY= 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.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4VfRsc4BGjz4f3mHk; Wed, 15 May 2024 16:56:20 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id BA97C1A016E; Wed, 15 May 2024 16:56:30 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgDHlxC7eERm68LgMg--.42328S5; Wed, 15 May 2024 16:56:30 +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 v2 01/12] cachefiles: remove request from xarry during flush requests Date: Wed, 15 May 2024 16:45:50 +0800 Message-Id: <20240515084601.3240503-2-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240515084601.3240503-1-libaokun@huaweicloud.com> References: <20240515084601.3240503-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:cCh0CgDHlxC7eERm68LgMg--.42328S5 X-Coremail-Antispam: 1UD129KBjvJXoW7Zr1rJr1fKr43CrW8urykKrg_yoW8XFWkpF WSka47KFykWr4qkr1kAFyrJryfJ348XF17XF17Xw1kArn8Zrs5Zr4xt340gry5ArWktFW3 Kr1qkF9rX34jy3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPG14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWUuVWrJwAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l x2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14 v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IY x2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87 Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIF yTuYvjfU8UDGUUUUU 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 Reviewed-by: Jia Zhu --- 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