Received: by 2002:ab2:6841:0:b0:1f8:2b01:8fb7 with SMTP id l1csp1987366lqp; Wed, 15 May 2024 06:03:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWZyIqcN7nb0i4Boc7ed39gKYDcnBgaLFMz7+utTleSMX25zMHCqI0tlnly5N5QPCkVTn4YHrvVHm/lVvkI14RmbWVxrjPnjRSgzCyrSA== X-Google-Smtp-Source: AGHT+IEmikfm0c68YQCb0ddwNVQIjXAOlSg87sI58klH0nkh69ik0Y8vDVBTIrVAokOejt6UoZ+x X-Received: by 2002:a05:6512:324a:b0:51d:416e:8434 with SMTP id 2adb3069b0e04-5220fb76860mr9326073e87.2.1715778202449; Wed, 15 May 2024 06:03:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715778202; cv=pass; d=google.com; s=arc-20160816; b=WI1TsXtr8gCtGIlZ3JKv0vEg8ihrwqbFGp/1KXEn8Whuiy23Kv9ThSEqcCeDh36Uo2 9PBuQPWQxvwFDrnwIsUjbw9XKK0wtELumJ4hoNqGX48DwVabEStKfkFBU4H9anQQr6Jk Fy5aMmtL28B9CBLTvJX0souDMNM9pELwTdyzNp/4zYk0Pzy0h8ddisKs7pJ0lGfdTmlj xykQmrHUrSEks1uTLkS8bLomMYAF4Ba2k4FwPb8nVVizBoSBpBJq/jQopDmFsFsl3NYI fBBL6cVmCK/Wxh9L8qPYKW8le2LsV2ZwckwOy+9oIsefpFclYRuGzwwYqF0OV9JsrKWn 6dtA== 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=xi5Qzum+XxalU8ZFDSO1xkQBTCtL1JqinMk0tJJ89mM=; fh=dfpuzL54LAMf7GQmHoqmub+qZCPWKUPNTVFHnj8Vxqk=; b=auATOtu+rB26obj0AJtXYq0Pmcy2KCEB5DRrG8YZ481uCKpG+i3Y1ogIjuZbdgxy+c IqzhnfC1+6iyLUKSDoSGvHyWyKeJl5flyZhd1chMAj4DWC8DLdHS5f4dX0kWWaZFpaKA WSuBjUM2JqVPMhvF54aE3ZCEAgGmVv74Za2lDatHuXxFClnPK4S6MAhQ+K9aIGDDH6VQ Gd2H+0dqRE13MWFznS20pjRGBJnYvuQMXl9yvCw4rZd11OoLpS3/rjkq7T+sdg+OZ0c6 sGYbLP2PFVR+fCVhFge2DMcY6nBeFILGo4pKrtRaczR+xtQ5LENYNsw/CDBB3fQhx7gl koAw==; 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-179875-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179875-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-574e3a11b50si2177341a12.402.2024.05.15.06.03.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 06:03:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179875-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-179875-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179875-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 096361F2357B for ; Wed, 15 May 2024 13:03:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8EB7112C816; Wed, 15 May 2024 13:02:20 +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 80A0E129A8E; Wed, 15 May 2024 13:02:17 +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=1715778140; cv=none; b=Us+t5EJEraJJ4FytJ1Ip8oKoSh4L9WPn5ioPvVteijlxmBtHfxG4UYTL8bUDQNDSWalvHEY5xiPubfy15cAu7PY77U3mqG/nrzFXhgBNDmgZo9OlaJQglyMUw3B5Qm30qQ93EF07j/8GuZZgt0zqzk3WcrGHppTzgD0NDZFFqiM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715778140; c=relaxed/simple; bh=nflcIOvlerrwIrtNuVpU2TiKnKICjvGJg4o0sDMaUp0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eC4xwYQ6Igq9e+2uL27bBV6mDO95gIG7xfU4IAg738DQ9u2j+ZXcD8BN/QodZ9cpo5096yMjmiSfn25j3zqrKH3S82NUdlohDCjCY4usZTmYsnr03retegDsmmizkAg1uKyHrFFP2SKJ0nvISpYo/1oUIzooEtF88H0RDlehS1I= 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 4VfYK76hDZz4f3k6L; Wed, 15 May 2024 21:02:03 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 7B32A1A0181; Wed, 15 May 2024 21:02:09 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgAX6RFLskRmALLwMg--.18738S7; Wed, 15 May 2024 21:02:09 +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 3/5] cachefiles: flush ondemand_object_worker during clean object Date: Wed, 15 May 2024 20:51:34 +0800 Message-Id: <20240515125136.3714580-4-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240515125136.3714580-1-libaokun@huaweicloud.com> References: <20240515125136.3714580-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:cCh0CgAX6RFLskRmALLwMg--.18738S7 X-Coremail-Antispam: 1UD129KBjvJXoW7Ww4kur4fJF1DXFy7Zw1UKFg_yoW8uryUpF WakFy7KrW8WF4UCrWkZFs5JryrK3ykZFnrWFyYvrZ8Ar90qr4rZr12y3ZxXF15Aw1SgrZr tw4UCr9xt34qy3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPK14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWUuVWrJwAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l x2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14 v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IY x2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z2 80aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU 0xZFpf9x0JUPHUDUUUUU= 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. Fixes: 0a7e54c1959c ("cachefiles: resend an open request if the read request's object is closed") Signed-off-by: Hou Tao Signed-off-by: Baokun Li Reviewed-by: Jia Zhu --- 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