Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp176597lqd; Tue, 23 Apr 2024 20:44:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV63Fb/0sTTB5pDXNo8UtALCl3WAwnVGluks77ELxxBg+OvxIC900iT23jvk+b2P0vhsFGuBwanmfgjDXFMu58Ldd++5LKfd2BGHDdl+w== X-Google-Smtp-Source: AGHT+IHd7qra6ll04OSlNxWWkOeBEB90eG+on+doRZBFZWqGFI//EY6C0GdXaYUVH3kLKV0LwT6p X-Received: by 2002:ac8:5896:0:b0:436:8356:bf5 with SMTP id t22-20020ac85896000000b0043683560bf5mr1771759qta.60.1713930247577; Tue, 23 Apr 2024 20:44:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713930247; cv=pass; d=google.com; s=arc-20160816; b=Hf0pk1P5VvMzHwbT6USWK5oGwIvK9rVDXic480D8XuLFyknptF5+lY0cNGGu60a/tH Ua3RcGyl68EA8mNtygkLtiPStcw5jWR6u/rGkRNXrjFyy3uewh5cCMvOsllL27OLRcak urHmfxfQ2iP1Y8q1+2Tp4P2wC5tfHAHcbvy4prYuPv7Z9jFxvV7Q6Gtt9qm3ZxQ0nOrL 5uWimDvUI9CxLoD9Cvv8IVpkQEx/v33yCvhRuB2PZ4wY+3s9vAbkHBb3UMlROEwGv0De kwHDf/Tdte838d/bERDnQo3H+5iTGW/qq66yKJkcxA3uRjpmGt8rw6l5NLKCCzhcnTkn zxpw== 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=CnKvZdUqH0v2TxJbFecDI1iS0nMiZRhVBVFMLbUU5aE=; fh=vAuxh9e/tGDX2+vbWBL1ISpgGqspPCY5Xp6+pLa1oNs=; b=x5MfZpKwtGhEQAFWGaVQynW53MF0lY5RXFDQ5isarTV5kQ1j4w4ENdU9cng0RAOVYV v2vPkUpPgoSWKkeNRrhzTo4AsxA0al1klIyhWQl8D8lO3JnUTzyVNPTB1KX7o/cK1ViF gPr5RmwbVCCbb+KpOCQW8xAKtQ21OFRQ72wqxUIEsTtEufD3cWQ3D6ZL1eWcOUAAzTn/ sSG5BiyK31+dSz01DFN5WQdvG25a1fFSpvj3SHmo7GtGzBYq2CrlpqAbuAI9/224+73A VJ664C8L4iGqtAuXDerUzEmTXyIwO278kZnQqkc0c/iy3os5TkRGaD6cORGErCFF4nUY kzaw==; 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-156227-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156227-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 d2-20020ac85ac2000000b0043a18d4588bsi883686qtd.749.2024.04.23.20.44.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 20:44:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-156227-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-156227-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156227-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 4F61B1C2345F for ; Wed, 24 Apr 2024 03:44:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 60E8D143C70; Wed, 24 Apr 2024 03:43:30 +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 3E42213B2BF; Wed, 24 Apr 2024 03:43:27 +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=1713930209; cv=none; b=odfhzyzpPYHd6TVjfDaH6ydeAW163QEHt39Rw2qeRMRrzGT6CR7FsEFRNG/GvSxF+r7LaRhZdwvQoq87I74N0zo3Ajssnyty1RwIksgNqdcVn1bH/sOZD48ZL787XoOzMqtmxpqn9a/4PkJbVUDz/LAdfEm8IjCh+hpg2Qor528= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713930209; c=relaxed/simple; bh=A5BHeR7uHc2t+fnupZkgPH4mwvlVSQRTKeIrm4tSUbU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TAiVBXSGGvkAsYIiXh5k+zYsW5RhffpdjS4fwf4mi47kvC06FAECczY8QIXVPOJAPCNaTL0nnx4BKEk917kTSo/SiMFq8dqRqqw+iuQ0fUxpIwTg3nRcNcJGhD3ZtgJJy47xD570wqlk/EGjaLf5uTNFAFHpOoVq0+NEwr3gj5k= 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.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4VPPw40bslz4f3p0v; Wed, 24 Apr 2024 11:43:16 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 464BC1A0572; Wed, 24 Apr 2024 11:43:25 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgBHGBHafyhmuSA4Kw--.57541S7; Wed, 24 Apr 2024 11:43:25 +0800 (CST) From: libaokun@huaweicloud.com To: netfs@lists.linux.dev Cc: dhowells@redhat.com, jlayton@kernel.org, zhujia.zj@bytedance.com, jefflexu@linux.alibaba.com, linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, libaokun@huaweicloud.com, Hou Tao , Baokun Li Subject: [PATCH 3/5] cachefiles: flush ondemand_object_worker during clean object Date: Wed, 24 Apr 2024 11:34:07 +0800 Message-Id: <20240424033409.2735257-4-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240424033409.2735257-1-libaokun@huaweicloud.com> References: <20240424033409.2735257-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:cCh0CgBHGBHafyhmuSA4Kw--.57541S7 X-Coremail-Antispam: 1UD129KBjvJXoW7Ww4kur4fJF1DXFy7Zw1UKFg_yoW8Cw4rpF WakFy7KrWxWF4DCrWkZFs5JryrK3ykuFnFgFyYq398Ar90qr4rZr12y3ZxXF15Jw4SgrZr tr4UCr9xt34qy3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQY14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWUWVWUuwAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwAKzVCY07xG64k0F24lc7CjxVAKzI0EY4vE52x082I5MxAIw28IcxkI7VAKI48JMxC2 0s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI 0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE 14v26r1I6r4UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI 8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v2 6r4UJVWxJrUvcSsGvfC2KfnxnUUI43ZEXa7VUjfHUDUUUUU== X-CM-SenderInfo: 5olet0hnxqqx5xdzvxpfor3voofrz/ From: Hou Tao When queuing ondemand_object_worker() to re-open the object, cachefiles_object is not pinned. The cachefiles_object may be freed when the pending read request is completed intentionally and the related erofs is umounted. If ondemand_object_worker() runs after the object is freed, it will incur use-after-free problem as shown below. process A processs B process C process D cachefiles_ondemand_send_req() // send a read req X // wait for its completion // close ondemand fd cachefiles_ondemand_fd_release() // set object as CLOSE cachefiles_ondemand_daemon_read() // set object as REOPENING queue_work(fscache_wq, &info->ondemand_work) // close /dev/cachefiles cachefiles_daemon_release cachefiles_flush_reqs complete(&req->done) // read req X is completed // umount the erofs fs cachefiles_put_object() // object will be freed cachefiles_ondemand_deinit_obj_info() kmem_cache_free(object) // both info and object are freed ondemand_object_worker() When dropping an object, it is no longer necessary to reopen the object, so use cancel_work_sync() to cancel or wait for ondemand_object_worker() to complete. Signed-off-by: Hou Tao Signed-off-by: Baokun Li --- fs/cachefiles/ondemand.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index d24bff43499b..f6440b3e7368 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -589,6 +589,9 @@ void cachefiles_ondemand_clean_object(struct cachefiles_object *object) } } xa_unlock(&cache->reqs); + + /* Wait for ondemand_object_worker() to finish to avoid UAF. */ + cancel_work_sync(&object->ondemand->ondemand_work); } int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, -- 2.39.2