From patchwork Wed Nov 6 12:35:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13864801 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0340AD44D54 for ; Wed, 6 Nov 2024 12:40:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92F466B0083; Wed, 6 Nov 2024 07:40:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B5A36B0085; Wed, 6 Nov 2024 07:40:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72F9E6B0098; Wed, 6 Nov 2024 07:40:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4AFF96B0083 for ; Wed, 6 Nov 2024 07:40:24 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 029B640FC6 for ; Wed, 6 Nov 2024 12:40:23 +0000 (UTC) X-FDA: 82755626898.27.8C4312E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 8694880008 for ; Wed, 6 Nov 2024 12:39:17 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RV1k2EKE; spf=pass (imf30.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730896685; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Jlfv2ApsSBgn//KdBsSlx8BkotK5+tCpg2808yZLllw=; b=29vVXJ2bVz2dBVwF8fwqRBr5uhknyXrJy8z72fQ3WCB6hO4LVon9UngekN+YVCrHT1w/fN LJyeyoG1a8r9Vl4tUrF7jA58SYDA3blsOcMWA0wK/9T5iguHcQAWPt/QDWDJZKbvWEKk7c ppE2U6obbVgLD5iabMydZmflDC1V43M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730896685; a=rsa-sha256; cv=none; b=2sRootW38r1KPgkVL8vPjA3u+yCTAJff077Z9sPnZzqE6rZp3cTiftXjJIa/dHiKzTkI8R GacDp9l7JxgQc1qwCu3muZh9owm1N+/V/Ji/nbx0qaqdmaTZX1hzW4lfnMFA2NBWLQ17u6 LW6kzy0u9pSoSlnHufyXjSX2W67OnfI= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RV1k2EKE; spf=pass (imf30.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730896821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jlfv2ApsSBgn//KdBsSlx8BkotK5+tCpg2808yZLllw=; b=RV1k2EKEPCA2BAAkDMbgWN3/vt9TUdGLaE/yjOQQiGJKUHKaVl0zcrNNVchQ1E/WG0hlPF t5hGM36jHUyQKuxdYA49ZjVrH41cKbtheqHVqGwtLTRy0SpjAgHqFhz14nhchggeFv6y+T I/7biYrvW3SItfxiIrBxEgTyTh5zoN8= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-330-gA0AskNvPJqluj550Slfjg-1; Wed, 06 Nov 2024 07:40:18 -0500 X-MC-Unique: gA0AskNvPJqluj550Slfjg-1 X-Mimecast-MFC-AGG-ID: gA0AskNvPJqluj550Slfjg Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1A730195608C; Wed, 6 Nov 2024 12:40:15 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.231]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3A3071955F40; Wed, 6 Nov 2024 12:40:09 +0000 (UTC) From: David Howells To: Christian Brauner , Steve French , Matthew Wilcox Cc: David Howells , Jeff Layton , Gao Xiang , Dominique Martinet , Marc Dionne , Paulo Alcantara , Shyam Prasad N , Tom Talpey , Eric Van Hensbergen , Ilya Dryomov , netfs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+af5c06208fa71bf31b16@syzkaller.appspotmail.com, Chang Yu Subject: [PATCH v3 33/33] netfs: Report on NULL folioq in netfs_writeback_unlock_folios() Date: Wed, 6 Nov 2024 12:35:57 +0000 Message-ID: <20241106123559.724888-34-dhowells@redhat.com> In-Reply-To: <20241106123559.724888-1-dhowells@redhat.com> References: <20241106123559.724888-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8694880008 X-Stat-Signature: zi5zfjnrp86gy73nn9qbywtanuotkwe7 X-HE-Tag: 1730896757-884303 X-HE-Meta: U2FsdGVkX18QjrGbEHt3zQ4993oyRG7vFXUglzdsJpc0n+q+PauLY4f4toL19tEVT/GJGhtXZpSMivYDQ6kMPHR5SRkuRVCib5YkZYOemVVBP/jQXmruj3CFDcIYqOp1E4VExuFPJIOxxbYXBfuKmsko4Pd9czCROvmSMBtacEvty8f6CZZDbgKkeXXBZTu4uK3H44fWfq52XVZ8I0IP3bybLGk2ZB81BNxOwXrJ/B4g6rn9RnEDyWl0YIhz1SYpUZOAQWwsDhy99URlrUgMau7hQRxeNItamyHx5sdKLnh/RmdHuFdDiOtQ2+cf74bjvZNosLZlNlymt8B7YMpzvkuM1OyhcKa7/Dx/wbDTt2MIFro++6dbYqnPOyM4Xw7kJ2xzOihv3gA7Cg/aotgB6Rsmxp1Rd+fF+3Ib7EtK9yri80CxzquhhyU511HT/8YDCG/bE9k9Hp5ljSvyAIGWsbxARthIJgsS5QOptki5ojd1RXdpVMWAahY3L1A0Df8X/rpb4wW1XR0OflNJ3CwGR0axgByKgbEWMLt3ZeI0h5/CqceA0MqyDL1rOmAf9wKdi+RAVXO0JPrGrW9LEjN9Zoixwbt9holDIkOJRPgr2paD9Nweo7WSjBfF9UgD8Wot8td9veESojhmLKcdqTov/jS9Bf4MEnaPiBlz1707M8p0gtb5xcZ8vffBPvdOPuPn448YOtSBeMqwB7D/NNfWNOKj6wPEgoxvRqpdvLCCCGISYCAyWExTPaEe/Tu6OJ0nyVRCqe5QW04YfHPbLkEON/wG3XdcpXvz7Ll+Ze2rAbietyhdL7kbKWfuU9fmgL3SWTtkeAYruyzYe5ias8F09uN8ao70s5YUCSlLVS5zVlFA4QtzL8k4wAOLM8SZ5/Jadrz5RjxZ0LxAmaHKRCT+8nH3/ImKgMws4YAhhsjc7+NuMEhEXIpideknRcYuwn6E8Ww7SeKy30Oxn89rtmf WYMsq7oz 0Xh64YMye5dn6NHopQpEiT5ql7ZmXur91siWIoIfzzFislaWVGNVSVEgI7Ez7ComAZQIg5Y1+coXaOAkJlbK38hTkMrGphYy7Gv861hjmYrBcupGl+aOECmqiubEgsB9PI3wkoh05yJCpVo6MkoVH7aoVAOZ9/3XIXTw4ckSHEMOOmUFUIN7LF0M9Ya2C6/nHjatENBlBXG3r8KI8KW+ZuuFPEsXjcmAAL1wqrj3nqUcmFgZOwamy2gciMBePz9tJgKhn6kTkS80XX3ilNhKV1taxMqS6PRMuvcsLuaZ12lxOLT8uMCwLbcbVok7pSMAiwWPDMHJ9FjuMgVZ5HDipcYRIlAmEqBSNjOtIPPkM6v3UmPPJLhU8bao8Lt3P6kxyftiU52Ivs2mSTc/cwbcKPC3biKJ5VI2RyWgmyeW/bwFKPE77kCP9ugxiqHU6enjUm4dQX/n3Qi0ZkS+IWnKyhjCRycW4/J3GRccVkln3OKSSGkQNbt/qGU6g7hw3Q7xgTkFy+oaNvPrsXKRoBgjnMmxec24bmWyJ2cqohw2a1PbCWnejrdJTBNvWnXU7/68CSS0k+F0gC7UOBJmfXcke8JNbPWX8Jh0T2K+bCfT3wOeiT6T7MPD9JmG0ne8BAgj3yM9d X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: It seems that it's possible to get to netfs_writeback_unlock_folios() with an empty rolling buffer during buffered writes. This should not be possible as the rolling buffer is initialised as the write request is set up and thereafter maintains at least one folio_queue struct therein until it gets destroyed. This allows lockless addition and removal of folio_queue structs in the buffer because, unlike with a ring buffer, the producer and consumer each only need to look at and alter one pointer into the buffer. Now, the rolling buffer is only used for buffered I/O operations as netfs_collect_write_results() should only call netfs_writeback_unlock_folios() if the request is of origin type NETFS_WRITEBACK, NETFS_WRITETHROUGH or NETFS_PGPRIV2_COPY_TO_CACHE. So it would seem that one of the following occurred: (1) I/O started before the request was fully initialised, (2) the origin got switched mid-flow or (3) the request has already been freed and this is a UAF error. I think the last is the most likely. Make netfs_writeback_unlock_folios() report information about the request and subrequests if folioq is seen to be NULL to try and help debug this, throw a warning and return. Note that this does not try to fix the problem. Reported-by: syzbot+af5c06208fa71bf31b16@syzkaller.appspotmail.com Link: https://syzkaller.appspot.com/bug?extid=af5c06208fa71bf31b16 Signed-off-by: David Howells cc: Chang Yu Link: https://lore.kernel.org/r/ZxshMEW4U7MTgQYa@gmail.com/ cc: Jeff Layton cc: netfs@lists.linux.dev cc: linux-fsdevel@vger.kernel.org --- fs/netfs/write_collect.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/fs/netfs/write_collect.c b/fs/netfs/write_collect.c index 3d8b87c8e6a6..4a1499167770 100644 --- a/fs/netfs/write_collect.c +++ b/fs/netfs/write_collect.c @@ -21,6 +21,34 @@ #define NEED_RETRY 0x10 /* A front op requests retrying */ #define SAW_FAILURE 0x20 /* One stream or hit a permanent failure */ +static void netfs_dump_request(const struct netfs_io_request *rreq) +{ + pr_err("Request R=%08x r=%d fl=%lx or=%x e=%ld\n", + rreq->debug_id, refcount_read(&rreq->ref), rreq->flags, + rreq->origin, rreq->error); + pr_err(" st=%llx tsl=%zx/%llx/%llx\n", + rreq->start, rreq->transferred, rreq->submitted, rreq->len); + pr_err(" cci=%llx/%llx/%llx\n", + rreq->cleaned_to, rreq->collected_to, atomic64_read(&rreq->issued_to)); + pr_err(" iw=%pSR\n", rreq->netfs_ops->issue_write); + for (int i = 0; i < NR_IO_STREAMS; i++) { + const struct netfs_io_subrequest *sreq; + const struct netfs_io_stream *s = &rreq->io_streams[i]; + + pr_err(" str[%x] s=%x e=%d acnf=%u,%u,%u,%u\n", + s->stream_nr, s->source, s->error, + s->avail, s->active, s->need_retry, s->failed); + pr_err(" str[%x] ct=%llx t=%zx\n", + s->stream_nr, s->collected_to, s->transferred); + list_for_each_entry(sreq, &s->subrequests, rreq_link) { + pr_err(" sreq[%x:%x] sc=%u s=%llx t=%zx/%zx r=%d f=%lx\n", + sreq->stream_nr, sreq->debug_index, sreq->source, + sreq->start, sreq->transferred, sreq->len, + refcount_read(&sreq->ref), sreq->flags); + } + } +} + /* * Successful completion of write of a folio to the server and/or cache. Note * that we are not allowed to lock the folio here on pain of deadlocking with @@ -87,6 +115,12 @@ static void netfs_writeback_unlock_folios(struct netfs_io_request *wreq, unsigned long long collected_to = wreq->collected_to; unsigned int slot = wreq->buffer.first_tail_slot; + if (WARN_ON_ONCE(!folioq)) { + pr_err("[!] Writeback unlock found empty rolling buffer!\n"); + netfs_dump_request(wreq); + return; + } + if (wreq->origin == NETFS_PGPRIV2_COPY_TO_CACHE) { if (netfs_pgpriv2_unlock_copied_folios(wreq)) *notes |= MADE_PROGRESS;