Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp1143895lqh; Sun, 5 May 2024 19:31:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXspaYPk0iLieu9/UKBLghKsH8vEYeQvHy4Rz8OA1Lj/Oa8SQ5EeaHeZHLaGYVJ11ktsiegTOrSipl/PxjbIvzyCo0H1Phl7PaMQcXXKQ== X-Google-Smtp-Source: AGHT+IFQ2A435nrorHXfc8zSQYEYjVuRpM1rir3cFl6nBKXl+t9BTMXfQRN1SoYW1Neh+Qy9kZjn X-Received: by 2002:a17:907:72c1:b0:a59:be8a:bd6f with SMTP id du1-20020a17090772c100b00a59be8abd6fmr2027550ejc.61.1714962687544; Sun, 05 May 2024 19:31:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714962687; cv=pass; d=google.com; s=arc-20160816; b=DTc/NsgWbZhq6el/o1nElhqFQYDwveJs7ykYHZO8J3+ONYXdQ3uVziOggmXW3DfQVG 3IjZZavweGr1OrOZkANkCy6K90pA9025QqEwqKiy2dILcHqSrvYEEsEAmEosGyw9XOnI DyIZP0Lp4JvdbtfMtAi4fvka5FQB+azbZBF5utFUo1Br5Dkoc2xxwDNzLiEp+8LtGmU8 zJzpVeklInLFS7bJiEB9WWyXERbRpZxx1z0Ccm2zkub1nyobMDGd3dFM07GMKzxc0AV6 LL1BjHsm6/Sxh5KqPYJLsAGOWMKvgfQ2aiPoRAsaLKqOAE8KBqm6KsRZaywEo7WsD6mi l2NA== 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=ob0YO4yjvcUQvEjXT5JdBhT5d+S7+Y7wcj+uRXLaRgQ=; fh=iMK7N1JiksCi/0omnYSDfuai+RJNcbpz1q87dv7YUA4=; b=xh3PjIfqg105gyI3+UpFIXGZ+GYYw/7hSnW0qprzGVdjXxKolzpApQIBjPVqfP4b19 PiSIkEWLKPG4uViuWkallGxyBV7Y7iJ2QaKU9jBacYZu9DgLV7bB6CWFs7xr+bT6VLj0 Nxy1tTUu35ymjHQgVLzmCKBZjcccgL4e82f4QEDRy1kRTzs5JhNzZEbzf4q/fZbCafn7 MJZupsaJMTe0zI2iOC3MJceo2p6Pb0uRgrdJi9K2GAni27QCV/JHX1IfwRGwo8B34WiS xaXlVdoFfhfxH+SROMJ3aKxzKSxYGpFyRr4TVEhG5ISwAZv/lSZyIKFiX2pw8LorQvq2 edlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=kVN0+SDY; 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-169259-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169259-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 am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id qu20-20020a170907111400b00a59c1442379si1373831ejb.286.2024.05.05.19.31.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 May 2024 19:31:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-169259-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; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=kVN0+SDY; 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-169259-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169259-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 493EB1F21F1E for ; Mon, 6 May 2024 02:31:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB44B405CC; Mon, 6 May 2024 02:31:19 +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="kVN0+SDY" Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) (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 F2580181; Mon, 6 May 2024 02:31:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.110 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714962678; cv=none; b=IbCg7YmD4vh3Xf2hqM3HNsF6QlYPdU7G5IdO4A4idxKBpfQ3e/FVr6RXgH2HLUjkVg1pwFSB+5oSeA2xhKymQg0skx2kAVjg3ElVrJzhtUhG7mnh1NWiQV+31DeVrPD48dJQjQnWmdDjPhAxcd7IzjvQaDbDVaI2fXA2rz+bAoM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714962678; c=relaxed/simple; bh=2pFWK0MLTTo2t+NIWJC3RactBtGq7GIQL3dy2XWRsr8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=izPxN8uoj0tiNT02vX3H7KlTxhGD0iD7niSdGDn4zjZDoRalRjP0yo3ZDgiwrbf5tVVNMf2IxTiG9B1u1ZrkYWgyw+7RoAlIPQcokxlnqAR0pX6CyRH2gazL/YZUwBIavF9C/EWToOztbnCuyTDnNHMlfC2/wIFC6mTTLzrQzJA= 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=kVN0+SDY; arc=none smtp.client-ip=115.124.30.110 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=1714962667; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=ob0YO4yjvcUQvEjXT5JdBhT5d+S7+Y7wcj+uRXLaRgQ=; b=kVN0+SDYsDehP9TM9vSpzb6LdDo62Q7wKQJKieBS1tk04ViH6xf/HYdt8kwe8H1NuP1oFxRIk39xkcLIADiuI2FMvscB2GliayG39x3aedaX51tcxWKJkEOusT43GKSb+2xdLerbrsqhSzB23SyUfX9EMuLtR4sTUT0mdazM3ck= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R681e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067109;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0W5pq6Lp_1714962665; Received: from 30.221.146.217(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0W5pq6Lp_1714962665) by smtp.aliyun-inc.com; Mon, 06 May 2024 10:31:06 +0800 Message-ID: <75566e68-bb5f-4458-8140-a59f263cc98a@linux.alibaba.com> Date: Mon, 6 May 2024 10:31:04 +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 06/12] cachefiles: add consistency check for copen/cread To: libaokun@huaweicloud.com, 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, Baokun Li References: <20240424033916.2748488-1-libaokun@huaweicloud.com> <20240424033916.2748488-7-libaokun@huaweicloud.com> Content-Language: en-US From: Jingbo Xu In-Reply-To: <20240424033916.2748488-7-libaokun@huaweicloud.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi Baokun, Thanks for improving on this! On 4/24/24 11:39 AM, 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 > --- > 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) { > + xa_unlock(&cache->reqs); > return -EINVAL; > + } > + xas_store(&xas, NULL); > + xa_unlock(&cache->reqs); > > /* fail OPEN request if copen format is invalid */ > ret = kstrtol(psize, 0, &size); The code looks good to me, but I still have some questions. First, what's the worst consequence if the daemon misbehaves like completing random copen/cread requests? I mean, does that affect other processes on the system besides the direct users of the ondemand mode, e.g. will the misbehavior cause system crash? Besides, it seems that the above security improvement is only "best effort". It can not completely prevent a malicious misbehaved daemon from completing random copen/cread requests, right? -- Thanks, Jingbo