Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp1041083lqp; Thu, 23 May 2024 07:29:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXh9X60iKZm+XLW2B6K+5srn55wgsDYw1yplIF4lUYoVal3dYaG0im9soCDHOK3LQUpnnyrMVpa1se8VyznuvJMH+uX7fdX2mPaGRlohQ== X-Google-Smtp-Source: AGHT+IEQajffqHdqL/zgcTXpMA2mOZfoSxtLI/F5/nQLZ5s9Uqt443VM467r83KLK8L6i5IA6fpM X-Received: by 2002:a17:902:f64c:b0:1f3:4bcb:5cf9 with SMTP id d9443c01a7336-1f34bcb6293mr11305055ad.17.1716474551617; Thu, 23 May 2024 07:29:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716474551; cv=pass; d=google.com; s=arc-20160816; b=FZbAJQ1eMQ2f0aPhVGK4Q1YE3lWvsAdIMwne5BhjHcbgjjAPPwtBLv5BT9hcxxrIBA H6CCLUxcfa1nI88NaT2HV8z6SdB2gqyf4iGbP8Bcal6hbX0R0VK+TqFdp6T3eI8Oeeqe 9q+N9Jj7fkeg8hjssiIp+2o+AAOZwDDTAwNlHfA/i1T/M/ECiBaH4aJA+aOOZcYMpKD3 z0+REHZSJtFI9e1Efa8kQQMZGap8Q6ksC9HiruVWmFBtlwtgwWg6xpm2kfsPxZWeFBQz QXrE66aQ8c4f6pMAPBviq1ZxfXd+Es96vpWpvIwTpcZgymCFj1NUzoA7WqowOvRalHvv +19Q== 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:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=9wPJMsA7uUhGJBy9UQb2/zbE2MRvDozVPDCHZoma3hs=; fh=VvJBipBEabZLyCpPRM9XygDwqTVFQZHqClQWlp0FMM8=; b=zPXaIuIalO4BJ+yjbO/XRF8JSN/T6ZOnKxO0yxuedKwM3DUtjSPboQs58y4j8/ZeLD EktSy2yAhaJP8YVZo/9WcWUp8LlRtuJAIhQOIsDflfWn1x92+U3Y+XpBnW2hI95OsHqi KVkCOYP1Ts3B0iIdCQF/p+1yR4a8NRPd09/49RHCPSqbwv7Wsts0PQl0JEp2qt2OWHnF 4Cqiyl1WsccGnlggqIJpSf3Q/nERZYq4LP13vIZ6FcJzZYbKqsi14FW6/NT0ftiSBUFt 6Sy3BryqziL+b+f/bnh23MqK/SvJldLH7uWH5GdLAKDkE6G0axSceP0vjWRA77s/RD2r dPig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=pqlvxgCI; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-187640-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187640-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f6d8c2254esi3029774b3a.258.2024.05.23.07.29.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 07:29:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-187640-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=pqlvxgCI; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-187640-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187640-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com 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 93D75B20BD8 for ; Thu, 23 May 2024 14:29:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2718114BFA8; Thu, 23 May 2024 14:28:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="pqlvxgCI" Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (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 B784813DBA0; Thu, 23 May 2024 14:28:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716474535; cv=none; b=qswdAMVKmGstkZkWe8821x82ZKln7AiC/rEQWlxtpoACUu1Zr6qMMXFOzUs1nVX7rEzryV0teXHMbNL7iJNcBViYUol3w3nXbd4BhC3hodDRiBmrMMpURLrSTtdCxHP5RWbEhX/oVNTPJF7WLGlucOxhZqrUTVk+S6qrwpaX1Xo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716474535; c=relaxed/simple; bh=YRzIZV+8IYLn+F4nAiVNJuRhYPZ1yBzoJnVmLoTmfFI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=EcO9x1cUlRjH0/gzvTBB9uZy8iTaHe8by9mftFgNl4jc3mBUZjLGRkenSt/pZtuk0i+Vvt7zrdQOpFfIzTWMFm45I1MFSnc6TyZoJPDUmwjTxaQJD8pM6fc3YFZ1xMcfIMV+bi5Sq+CyMSvr1aabMC5dZUHNPflTtDo+h03dle0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=pqlvxgCI; arc=none smtp.client-ip=115.124.30.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716474523; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=9wPJMsA7uUhGJBy9UQb2/zbE2MRvDozVPDCHZoma3hs=; b=pqlvxgCIOI5o9dB3XK5gyt1UAyhflc3P2qlqzJRGsSBghtHe9YD/DfvD4Mmwk+0Q4uOj6dJ0SnxpzQ6ANrV6TrWYXHwpm2E7HFkNAwMi+DToyV3pZV/YGlHodpWmJh4Up3ManIdoarILHggZYdSvQOImrZKM6lOzsPGrT3YPBZQ= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067111;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0W739OVi_1716474521; Received: from 30.39.171.189(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0W739OVi_1716474521) by smtp.aliyun-inc.com; Thu, 23 May 2024 22:28:43 +0800 Message-ID: <11f10862-9149-49c7-bac4-f0c1e0601b23@linux.alibaba.com> Date: Thu, 23 May 2024 22:28:41 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 06/12] cachefiles: add consistency check for copen/cread To: libaokun@huaweicloud.com, 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 References: <20240522114308.2402121-1-libaokun@huaweicloud.com> <20240522114308.2402121-7-libaokun@huaweicloud.com> Content-Language: en-US From: Jingbo Xu In-Reply-To: <20240522114308.2402121-7-libaokun@huaweicloud.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 5/22/24 7:43 PM, libaokun@huaweicloud.com wrote: > From: Baokun Li > > This prevents malicious processes from completing random copen/cread > requests and crashing the system. Added checks are listed below: > > * Generic, copen can only complete open requests, and cread can only > complete read requests. > * For copen, ondemand_id must not be 0, because this indicates that the > request has not been read by the daemon. > * For cread, the object corresponding to fd and req should be the same. > > Signed-off-by: Baokun Li > Acked-by: Jeff Layton > --- > fs/cachefiles/ondemand.c | 27 ++++++++++++++++++++------- > 1 file changed, 20 insertions(+), 7 deletions(-) > > diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c > index bb94ef6a6f61..898fab68332b 100644 > --- a/fs/cachefiles/ondemand.c > +++ b/fs/cachefiles/ondemand.c > @@ -82,12 +82,12 @@ static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos, > } > > static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl, > - unsigned long arg) > + unsigned long id) > { > struct cachefiles_object *object = filp->private_data; > struct cachefiles_cache *cache = object->volume->cache; > struct cachefiles_req *req; > - unsigned long id; > + XA_STATE(xas, &cache->reqs, id); > > if (ioctl != CACHEFILES_IOC_READ_COMPLETE) > return -EINVAL; > @@ -95,10 +95,15 @@ static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl, > if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) > return -EOPNOTSUPP; > > - id = arg; > - req = xa_erase(&cache->reqs, id); > - if (!req) > + xa_lock(&cache->reqs); > + req = xas_load(&xas); > + if (!req || req->msg.opcode != CACHEFILES_OP_READ || > + req->object != object) { > + xa_unlock(&cache->reqs); > return -EINVAL; > + } > + xas_store(&xas, NULL); > + xa_unlock(&cache->reqs); > > trace_cachefiles_ondemand_cread(object, id); > complete(&req->done); > @@ -126,6 +131,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) > unsigned long id; > long size; > int ret; > + XA_STATE(xas, &cache->reqs, 0); > > if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) > return -EOPNOTSUPP; > @@ -149,9 +155,16 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) > if (ret) > return ret; > > - req = xa_erase(&cache->reqs, id); > - if (!req) > + xa_lock(&cache->reqs); > + xas.xa_index = id; > + req = xas_load(&xas); > + if (!req || req->msg.opcode != CACHEFILES_OP_OPEN || > + !req->object->ondemand->ondemand_id) { For a valid opened object, I think ondemand_id shall > 0. When the copen is for the object which is in the reopening state, ondemand_id can be CACHEFILES_ONDEMAND_ID_CLOSED (actually -1)? Otherwise looks good to me. -- Thanks, Jingbo