Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2319486lqo; Mon, 20 May 2024 01:57:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVzwiiA9EQ675y2XycZJ5AntW3sW/AK3VpQht4EV77kffxBqSmbZ5zyhIENBdV0p2ZNK+5/kd4z98vdedQPsNogCfSeEqX9loa0yo4kBA== X-Google-Smtp-Source: AGHT+IEBB4yYwe1UdcVdiLzXSio7P0mkiW87gDZj+d0ivd9yanENTqer2uOjFDq8TJx95aCFqY4P X-Received: by 2002:a05:620a:14b7:b0:790:edce:3640 with SMTP id af79cd13be357-792c757788fmr2985773385a.13.1716195429772; Mon, 20 May 2024 01:57:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716195429; cv=pass; d=google.com; s=arc-20160816; b=Cie+qSQBNprHwDYrwfY8wtHiEyClaCFxT16ncYS8kJaUxQTr3Z+wFyGbX4QsETkTKL BdCjukRtNbg717/DNIwQOyxV9Z8RlffFRVlPtS/zVMQaxDu5QRGT0wX4df2YpBoR8UF1 +v4MuX9zNrLS74+uVuedKwwRiiWisGNfeSX61ZrDeWkrZgqNV8S15Y1zlUo2nT2+1Bth hupzmuWKwCHOMTVDPOdO4uUZF+KDe+VNx3hJpsspnbOyKafFMFi3gRLon5Eykb2Ul/R2 DftNEif7r2aLC/27sLzHWYSmiA9ULTILUPtaTARRIvZz6ve6+OtGnZ7mIYidYRjYyXXD iSxQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=yoP8ElH526u5r6J2PQhdY9CbEFrdm24GOQ1NgY2Qis4=; fh=KTM4KRr/LvGOw9mWzmUOwc0z5L2jyiG0h/ml8qsSDYc=; b=SXHAoitV57UuzZgh4jSVoox/vPibIXTIUYIxi4Bi+QYeN/j0O6ZyvKeUdsNkNSbutQ cG17NcId+d7WWqlSyxtPdsWtqcJA2D/MvbAGPyOE6TLjNDjO1YfuMmwR/jBrRBPzf2M1 USw34R/IGsGuDc/+H5D5RI/VF896ZaAq/Ap/DnTGb9z0Bea25Oaxft09mG7tf6rIgKr8 C+SUmBEIhqfDdN/PJJLvhpVevd6mTlmIpmaymcu/dmWUI3pN9b2mHco0H9OkpUFROfRu Anjh620VWkeqWOaczjf3fCIV41SeM8EQgtevqCMFbOls2ywppgglHGVGvNV4uPwKCEVE +Mxw==; 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-183509-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183509-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-792bf30a7eesi4267685a.280.2024.05.20.01.57.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 01:57:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-183509-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-183509-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183509-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 710A11C20DBA for ; Mon, 20 May 2024 08:57:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91BF61CAB9; Mon, 20 May 2024 08:56:57 +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 84F291B974; Mon, 20 May 2024 08:56:54 +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=1716195416; cv=none; b=FJR89wrA9NdhH8lGKStE+sGCLLgogUJ9FZoms6XvLm/0F94CPJMGA7eo+OKnw9XMagTvk45O0hyrXf4NL3gu9zfJ3Gac1UjxV8B9yB2vPE96QVlEUOdDCDeSG67t0L6p7lbi0g9Apt3LXWbXJa2P2xFHsskRXSHP2u5Vaw2URj0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716195416; c=relaxed/simple; bh=dV/gLwlucQydWQ4XAl6gXZsvgraVb977dNukWdEJyxM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=VEqGlBO6SpexaPPgv33CzEH1oRh80Wx9bWMnnTbR9Z8AF5OrPYSFAM10SgmfiFM795z/riv7r/UiVFPHgq8r477KNb33YKKKgA14V2CTOggLeXGjfmzQbE44hrQlET3/qKMCyRQek2/93jIaAPLsNagSyiIm6Qp7hYTSxypFy9E= 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 4VjWdh5fhnz4f3jMJ; Mon, 20 May 2024 16:56:40 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 2E4F71A0EF4; Mon, 20 May 2024 16:56:51 +0800 (CST) Received: from [10.174.177.174] (unknown [10.174.177.174]) by APP1 (Coremail) with SMTP id cCh0CgAX5g5PEEtmqiyyNA--.3335S3; Mon, 20 May 2024 16:56:50 +0800 (CST) Message-ID: <556ca8a0-68a6-eeb4-3aa2-a6d613c232e7@huaweicloud.com> Date: Mon, 20 May 2024 16:56:47 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Subject: Re: [PATCH v2 04/12] cachefiles: fix slab-use-after-free in cachefiles_ondemand_daemon_read() Content-Language: en-US To: Jingbo Xu , netfs@lists.linux.dev, dhowells@redhat.com, jlayton@kernel.org Cc: hsiangkao@linux.alibaba.com, zhujia.zj@bytedance.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yangerkun@huawei.com, houtao1@huawei.com, yukuai3@huawei.com, wozizhi@huawei.com, Baokun Li , libaokun@huaweicloud.com References: <20240515084601.3240503-1-libaokun@huaweicloud.com> <20240515084601.3240503-5-libaokun@huaweicloud.com> From: Baokun Li In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID:cCh0CgAX5g5PEEtmqiyyNA--.3335S3 X-Coremail-Antispam: 1UD129KBjvJXoW3WF45Gw4rAw47Jr4fWr1rJFb_yoW7ArW7pF ZIyFyxtry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wrote: > > On 5/15/24 4:45 PM, libaokun@huaweicloud.com wrote: >> From: Baokun Li >> >> We got the following issue in a fuzz test of randomly issuing the restore >> command: >> >> ================================================================== >> BUG: KASAN: slab-use-after-free in cachefiles_ondemand_daemon_read+0xb41/0xb60 >> Read of size 8 at addr ffff888122e84088 by task ondemand-04-dae/963 >> >> CPU: 13 PID: 963 Comm: ondemand-04-dae Not tainted 6.8.0-dirty #564 >> Call Trace: >> kasan_report+0x93/0xc0 >> cachefiles_ondemand_daemon_read+0xb41/0xb60 >> vfs_read+0x169/0xb50 >> ksys_read+0xf5/0x1e0 >> >> Allocated by task 116: >> kmem_cache_alloc+0x140/0x3a0 >> cachefiles_lookup_cookie+0x140/0xcd0 >> fscache_cookie_state_machine+0x43c/0x1230 >> [...] >> >> Freed by task 792: >> kmem_cache_free+0xfe/0x390 >> cachefiles_put_object+0x241/0x480 >> fscache_cookie_state_machine+0x5c8/0x1230 >> [...] >> ================================================================== >> >> Following is the process that triggers the issue: >> >> mount | daemon_thread1 | daemon_thread2 >> ------------------------------------------------------------ >> cachefiles_withdraw_cookie >> cachefiles_ondemand_clean_object(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 >> REQ_A = cachefiles_ondemand_select_req >> msg->object_id = req->object->ondemand->ondemand_id >> ------ restore ------ >> cachefiles_ondemand_restore >> xas_for_each(&xas, req, ULONG_MAX) >> xas_set_mark(&xas, CACHEFILES_REQ_NEW) >> >> cachefiles_daemon_read >> cachefiles_ondemand_daemon_read >> REQ_A = cachefiles_ondemand_select_req >> copy_to_user(_buffer, msg, n) >> xa_erase(&cache->reqs, id) >> complete(&REQ_A->done) >> ------ close(fd) ------ >> cachefiles_ondemand_fd_release >> cachefiles_put_object >> cachefiles_put_object >> kmem_cache_free(cachefiles_object_jar, object) >> REQ_A->object->ondemand->ondemand_id >> // object UAF !!! >> >> When we see the request within xa_lock, req->object must not have been >> freed yet, so grab the reference count of object before xa_unlock to >> avoid the above issue. >> >> Fixes: 0a7e54c1959c ("cachefiles: resend an open request if the read request's object is closed") >> Signed-off-by: Baokun Li >> Reviewed-by: Jia Zhu >> --- >> fs/cachefiles/ondemand.c | 2 ++ >> include/trace/events/cachefiles.h | 6 +++++- >> 2 files changed, 7 insertions(+), 1 deletion(-) >> >> diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c >> index 56d12fe4bf73..bb94ef6a6f61 100644 >> --- a/fs/cachefiles/ondemand.c >> +++ b/fs/cachefiles/ondemand.c >> @@ -336,6 +336,7 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, >> xas_clear_mark(&xas, CACHEFILES_REQ_NEW); >> cache->req_id_next = xas.xa_index + 1; >> refcount_inc(&req->ref); >> + cachefiles_grab_object(req->object, cachefiles_obj_get_read_req); >> xa_unlock(&cache->reqs); >> >> if (msg->opcode == CACHEFILES_OP_OPEN) { >> @@ -355,6 +356,7 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, >> close_fd(((struct cachefiles_open *)msg->data)->fd); >> } >> out: >> + cachefiles_put_object(req->object, cachefiles_obj_put_read_req); >> /* Remove error request and CLOSE request has no reply */ >> if (ret || msg->opcode == CACHEFILES_OP_CLOSE) { >> xas_reset(&xas); >> diff --git a/include/trace/events/cachefiles.h b/include/trace/events/cachefiles.h >> index cf4b98b9a9ed..119a823fb5a0 100644 >> --- a/include/trace/events/cachefiles.h >> +++ b/include/trace/events/cachefiles.h >> @@ -33,6 +33,8 @@ enum cachefiles_obj_ref_trace { >> cachefiles_obj_see_withdrawal, >> cachefiles_obj_get_ondemand_fd, >> cachefiles_obj_put_ondemand_fd, >> + cachefiles_obj_get_read_req, >> + cachefiles_obj_put_read_req, > How about cachefiles_obj_[get|put]_ondemand_read, so that it could be > easily identified as ondemand mode at the first glance? The ondemand_read tends to confuse whether it's ondemand_daemon_read or ondemand_data_read. I think it's better to emphasise the read request, and currently only the ondemand mode has a cachefiles req. >> }; >> >> enum fscache_why_object_killed { >> @@ -127,7 +129,9 @@ enum cachefiles_error_trace { >> EM(cachefiles_obj_see_lookup_cookie, "SEE lookup_cookie") \ >> EM(cachefiles_obj_see_lookup_failed, "SEE lookup_failed") \ >> EM(cachefiles_obj_see_withdraw_cookie, "SEE withdraw_cookie") \ >> - E_(cachefiles_obj_see_withdrawal, "SEE withdrawal") >> + EM(cachefiles_obj_see_withdrawal, "SEE withdrawal") \ >> + EM(cachefiles_obj_get_read_req, "GET read_req") \ >> + E_(cachefiles_obj_put_read_req, "PUT read_req") > Ditto. > > > Otherwise, LGTM. > > Reviewed-by: Jingbo Xu > Thank you very much for your review! -- With Best Regards, Baokun Li