Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp1157697lqh; Sun, 5 May 2024 20:23:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXTgBTMhX5fPX1BLInUqsfuzdd1ybUurrWo41gRJ3PiihCYUZ2aZr9jafTxt62UcH6vbUxaXiy3bO4vIu1BrOv4FSl2j3S/41osInvnag== X-Google-Smtp-Source: AGHT+IEgvfCl4dNXUHE5IUfuQMhcgN1tbttbnQ6SVgnOxpMFtA+aAtP3PMX4yvzM2NP+eQ8tEFVn X-Received: by 2002:a67:e407:0:b0:47f:fe4:a38c with SMTP id d7-20020a67e407000000b0047f0fe4a38cmr4612866vsf.1.1714965836805; Sun, 05 May 2024 20:23:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714965836; cv=pass; d=google.com; s=arc-20160816; b=N7Q9Xd+ICqJE0SWQkPH/6adTP+sQbi3hSTpj+Uw+/HOWrHEeYgmRVsN1COlPEuVB7M LRTkbADwCr7tgmd/5URz7aa7GM5YvCMArYvWo0J5tzPlmHkbWtEOctvQErDgG20Xb/VY Hqf5WkFTT6rr0GXiTMFlGisIx4O4BINl3+HAr/V0vTKwTkpPg+qls3mvpn9NToK/nbBy HZlGyLtdpI0Ru5BNH4DIMIOYUlvbflpdgpeb3OJb6j3npo7/BRsH2EhUEKrZfZtUo4dA fWYESC2TjEEi22apCDXGs2XRVgzHrVfFGfVm3oQFSP++luulGrl2EggaVWwBEKT4Adi2 11jQ== 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=phVbWbHaoT4c48M7D4ck8XYlIztqZLhXSLiKRha8OYc=; fh=KzDZjXUYsyQi8jiAtuIBbUJALfGcqLCridvgcb7mSyo=; b=axhyVT6XhmS1bxPYOhRcuJCNM4RbBxAhV4O2LEMharByd1xIwQ/+AdA0bsAq+rdv/L DwzfboEKGu7fUaKCkp7cQzFtg7QEffFFoGZkZacrll84Ce+Ub6t7FFzUg/6bc9X9s03N FbwQ1xTtWU/QfFZActz/P8AX5qb+tqwcr1Cvl6MGolfEbf9lDAPmZRg7eZfnbK+1LKhc pS9BubyEN7woELItVRZMaSm8gZf9PQIieNf+GnINwBB3HDGeTdVHK9Xg9LPDYi/1hkIb 2QIUTMjXDZC+/HPWJxSrnyrHYRYAe9VXLNXgFQYB3QmyhFCQySmQzN1ZHemx6Iaz6TXF tpzQ==; 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-169276-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169276-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 gy3-20020a056214242300b006a0cc2c9891si8671357qvb.11.2024.05.05.20.23.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 May 2024 20:23:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-169276-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-169276-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169276-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 763631C20E1F for ; Mon, 6 May 2024 03:23:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 992B843AC1; Mon, 6 May 2024 03:23:47 +0000 (UTC) Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) (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 725E03F9EC; Mon, 6 May 2024 03:23:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714965827; cv=none; b=ftV6qqWU9Lj3dQxhRwin90hIt9Gkywi06QU29lllI4L7HDt8bLgGO68dYZERSs5LMO9XP4Wg27d8gH+xYU2GeZjzM3eZIugB1Fh2DJjkJbznh5xq5LypUbI1eIABoOnp/chl2W6voVHWo8hj/yNlZ8q2E+e72otkT+M5JMR4Bd8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714965827; c=relaxed/simple; bh=UZMWj0o8HJjobUzCMwhoUcqFCcq5NUQ2GcZ27PWTaOQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=rR0qFOrFyVj7jFj1od0/sAt7Xjs500BTVeuCNKG5XW5fHmxo2akBvbFhNP2Q2WCQ0sND7QCrgJSxW2M3WFbqyXaheLzlVZKthFwexKKEfGYCHh0D6LoYMQUx3U55UjA0lw/834DGTLLTzsbAzO6ZsNqHVzL2OS5uowBeHBtDZG4= 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.56 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 dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4VXmvm339yz4f3jXv; Mon, 6 May 2024 11:23:32 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 946CB1A016E; Mon, 6 May 2024 11:23:40 +0800 (CST) Received: from [10.174.177.174] (unknown [10.174.177.174]) by APP1 (Coremail) with SMTP id cCh0CgBXKBE4TThmwQSJLw--.25496S3; Mon, 06 May 2024 11:23:40 +0800 (CST) Message-ID: <18cc6974-b47d-f9fd-2576-366382d0e8d0@huaweicloud.com> Date: Mon, 6 May 2024 11:23:36 +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 07/12] cachefiles: add spin_lock for cachefiles_ondemand_info Content-Language: en-US To: Jingbo Xu , netfs@lists.linux.dev Cc: dhowells@redhat.com, jlayton@kernel.org, zhujia.zj@bytedance.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, libaokun@huaweicloud.com, yangerkun References: <20240424033916.2748488-1-libaokun@huaweicloud.com> <20240424033916.2748488-8-libaokun@huaweicloud.com> <1fef9ab5-ec33-4a14-beb3-ada41a8652b3@linux.alibaba.com> From: Baokun Li In-Reply-To: <1fef9ab5-ec33-4a14-beb3-ada41a8652b3@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID:cCh0CgBXKBE4TThmwQSJLw--.25496S3 X-Coremail-Antispam: 1UD129KBjvJXoWxCr18uw17tw47Xr43Jw4ktFb_yoW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wrote: > > On 4/24/24 11:39 AM, libaokun@huaweicloud.com wrote: >> From: Baokun Li >> >> The following concurrency may cause a read request to fail to be completed >> and result in a hung: >> >> t1 | t2 >> --------------------------------------------------------- >> cachefiles_ondemand_copen >> req = xa_erase(&cache->reqs, id) >> // Anon fd is maliciously closed. >> cachefiles_ondemand_fd_release >> xa_lock(&cache->reqs) >> cachefiles_ondemand_set_object_close(object) >> xa_unlock(&cache->reqs) >> cachefiles_ondemand_set_object_open >> // No one will ever close it again. >> cachefiles_ondemand_daemon_read >> cachefiles_ondemand_select_req >> // Get a read req but its fd is already closed. >> // The daemon can't issue a cread ioctl with an closed fd, then hung. >> >> So add spin_lock for cachefiles_ondemand_info to protect ondemand_id and >> state, thus we can avoid the above problem in cachefiles_ondemand_copen() >> by using ondemand_id to determine if fd has been released. >> >> Signed-off-by: Baokun Li > This indeed looks like a reasonable scenario where the kernel side > should fix, as a non-malicious daemon could also run into this. > > How about reusing &cache->reqs spinlock rather than introducing a new > spinlock, as &cache->reqs spinlock is already held when setting object > to close state in cachefiles_ondemand_fd_release()? We've considered reusing &cache->reqs spinlock before, but their uses don't exactly overlap, and there are patches coming that will use the new spin_lock,. In addition, this reduces competition for &cache->reqs spinlock. >> --- >> fs/cachefiles/internal.h | 1 + >> fs/cachefiles/ondemand.c | 16 +++++++++++++++- >> 2 files changed, 16 insertions(+), 1 deletion(-) >> >> diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h >> index 7745b8abc3aa..45c8bed60538 100644 >> --- a/fs/cachefiles/internal.h >> +++ b/fs/cachefiles/internal.h >> @@ -55,6 +55,7 @@ struct cachefiles_ondemand_info { >> int ondemand_id; >> enum cachefiles_object_state state; >> struct cachefiles_object *object; >> + spinlock_t lock; >> }; >> >> /* >> diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c >> index 898fab68332b..b5e6a851ef04 100644 >> --- a/fs/cachefiles/ondemand.c >> +++ b/fs/cachefiles/ondemand.c >> @@ -16,13 +16,16 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, >> struct cachefiles_object *object = file->private_data; >> struct cachefiles_cache *cache = object->volume->cache; >> struct cachefiles_ondemand_info *info = object->ondemand; >> - int object_id = info->ondemand_id; >> + int object_id; >> struct cachefiles_req *req; >> XA_STATE(xas, &cache->reqs, 0); >> >> xa_lock(&cache->reqs); >> + spin_lock(&info->lock); >> + object_id = info->ondemand_id; >> info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; >> cachefiles_ondemand_set_object_close(object); >> + spin_unlock(&info->lock); >> >> /* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */ >> xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { >> @@ -127,6 +130,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) >> { >> struct cachefiles_req *req; >> struct fscache_cookie *cookie; >> + struct cachefiles_ondemand_info *info; >> char *pid, *psize; >> unsigned long id; >> long size; >> @@ -185,6 +189,14 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) >> goto out; >> } >> >> + info = req->object->ondemand; >> + spin_lock(&info->lock); >> + /* The anonymous fd was closed before copen ? */ > I would like describe more details in the comment, e.g. put the time > sequence described in the commit message here. OK, thanks for your suggestion, I will describe it in more detail in the next revision. Thanks, Baokun > >> + if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) { >> + spin_unlock(&info->lock); >> + req->error = -EBADFD; >> + goto out; >> + } >> cookie = req->object->cookie; >> cookie->object_size = size; >> if (size) >> @@ -194,6 +206,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) >> trace_cachefiles_ondemand_copen(req->object, id, size); >> >> cachefiles_ondemand_set_object_open(req->object); >> + spin_unlock(&info->lock); >> wake_up_all(&cache->daemon_pollwq); >> >> out: >> @@ -596,6 +609,7 @@ int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, >> return -ENOMEM; >> >> object->ondemand->object = object; >> + spin_lock_init(&object->ondemand->lock); >> INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker); >> return 0; >> }