From mboxrd@z Thu Jan 1 00:00:00 1970 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 85F79C0219B for ; Tue, 11 Feb 2025 19:24:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB6CF6B007B; Tue, 11 Feb 2025 14:23:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D66846B0082; Tue, 11 Feb 2025 14:23:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C07C56B0085; Tue, 11 Feb 2025 14:23:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9C7D06B007B for ; Tue, 11 Feb 2025 14:23:59 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0546C1A096B for ; Tue, 11 Feb 2025 19:23:59 +0000 (UTC) X-FDA: 83108638998.24.39FCDFE Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by imf20.hostedemail.com (Postfix) with ESMTP id 224621C000C for ; Tue, 11 Feb 2025 19:23:56 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lQVZBcPj; spf=pass (imf20.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.160.175 as permitted sender) smtp.mailfrom=joannelkoong@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739301837; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AyquudzGFfxKdiRxksRXadMuPylK1GlogDa9cblcCyI=; b=fOjtMwDFeBJCF3ZnH921MAdW7z1vbFJN+jhCORfXORBvixHTIeWW0qXQeii560B3DpMf8u Hn4Zf596oqNDJoR5tmdVucfHsD0O4FY4j/Gdoda9l+nSaf9+IGpx4DXeZXISnomDgHUSRr pMC2TTvxenx1SM3UcuYp+Efu2043pOY= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lQVZBcPj; spf=pass (imf20.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.160.175 as permitted sender) smtp.mailfrom=joannelkoong@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739301837; a=rsa-sha256; cv=none; b=mHjf6U+kNoK+NcxpvCLlLNFuRGQwsFdhHWEWkEUVCpUeJ7opuRJCa//6uJtIDnwWSmg0RL 9BrmaoA79aYtpNpFTGaDjfBTxX4YHbqUJz3ChVzl15/69yrugASet6MmfEkwYh2yZAVSno oTa+LN+U6kEjz39kPirFZBxMaNyjHJo= Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-4718d9dbcd3so36294031cf.1 for ; Tue, 11 Feb 2025 11:23:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739301836; x=1739906636; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=AyquudzGFfxKdiRxksRXadMuPylK1GlogDa9cblcCyI=; b=lQVZBcPjVmbNKDc3t9GgQfVEjpmkVF99AC+zzXg90ZFgURv0c7nmS/UU2vNA2bnzMD iDCpUQHmWs7gSDnocqI7qpeUchb9MRva8AqNAKQS75CrILlz9ASc10EwyIEiSDhIx2QD dWDrBw/VjLiqXPySXRW3hzsdfL6sHO5FXsWkHTTiNLJeAwerHKoCb+/ARsdUiMJ8xwQw VsG2ZDn4OwpdfCVcuTsZ1iwmu1oEztFUpNBDTlcDE6BSbMzKpDf9j+ne7202TuarJTY9 j303gQx/2AhwTH/9z8X1/0qILcREgPnMp2M7XMS/LiTbaFzm/g6Cqub9m0q1LHfQehKb WSLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739301836; x=1739906636; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AyquudzGFfxKdiRxksRXadMuPylK1GlogDa9cblcCyI=; b=Sm/OsWChV9fzC1yvd5QTCKl2FNBYVWT+PUke2xLyVWHn+TrhhO1di2mXV5+8YH9/Se l8+g47PP/XXkgcZi06GTOnc173SX5so7ZZUrdQKFAaPKNUYYCTCSS0xfPlpz6b6El9F1 kKML2x++cD9D47NK9DorGHpWE0un3Alm/ihr7arzyE4Tg/8gGoV3Rf7eJS+ywM6gJ/1v FF6jrRvVpd6i9SRwsTF9sdATqggoML+0EgmRU21TCm5KlZo0PHQKtyNI2XFJGbidUPyB 96X6GI1EdubW7WLY79zjSDavb/CP/ca0lqVIzU0B4vLSIIvLGLPpKwhoSZMxhEkggRMI ztpQ== X-Forwarded-Encrypted: i=1; AJvYcCVlCSGwiaX3TFKrYFxLv778jJqAL/YCE4ITHzQvtcd57J2LUvR5y56+dNMKSCg+/ejyYdd5EWYUGA==@kvack.org X-Gm-Message-State: AOJu0YysxkZDiOD0kLdOz5ZMVSw0BOf1EK+S7rThTCp3gBgoEdW9zuU6 Bv7vTJCyZuGVtn0UifbnNwPHZnEnVZogHLV8NNKcAy0sg1cvCmtPuKMr6jxTXSd0AHfmukg906U 3nx3poNHtMcB/R5Se1NVv4MfFom8= X-Gm-Gg: ASbGncsXE/KeN5O3vYa2YULhnInYfNFNA8dJq9VnUphBv88QjaddA72Yt5DmCZVlf8L dGU/uz3jInpbi4SadgpO041ywyJwnlI1/93hMcM3qY31QI3/2E3MKRWXx9jVqS/bBhmfhv2t4sQ == X-Google-Smtp-Source: AGHT+IHsO/hy//zVuTr38WbJwX4RVSGuIWDkkbwLY9Ry/XOTJk49t0soPTp74YtJBvE7sBPSsUvSPjOv/yPpUkJ8ia4= X-Received: by 2002:a05:622a:4b:b0:471:8dab:d4f2 with SMTP id d75a77b69052e-471afe0ee3amr5145021cf.3.1739301836091; Tue, 11 Feb 2025 11:23:56 -0800 (PST) MIME-Version: 1.0 References: <9cd88643-daa8-4379-be0a-bd31de277658@suse.cz> <20250207172917.GA2072771@perftesting> <8f7333f2-1ba9-4df4-bc54-44fd768b3d5b@suse.cz> <81298bd1-e630-4940-ae5b-7882576b6bf4@suse.cz> <20250210191235.GA2256827@perftesting> <8a99f6bf3f0b5cb909f11539fb3b0ef0d65b3a73.camel@kernel.org> In-Reply-To: From: Joanne Koong Date: Tue, 11 Feb 2025 11:23:45 -0800 X-Gm-Features: AWEUYZl1GfPTZXzSetqKFD7DNB6FXqKr6c58XQWiS0Jq_hJKJpwnR8_iG_rZU74 Message-ID: Subject: Re: [REGRESSION][BISECTED] Crash with Bad page state for FUSE/Flatpak related applications since v6.13 To: Jeff Layton Cc: Matthew Wilcox , Josef Bacik , Vlastimil Babka , Miklos Szeredi , Christian Heusel , Miklos Szeredi , regressions@lists.linux.dev, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm , =?UTF-8?Q?Mantas_Mikul=C4=97nas?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 224621C000C X-Stat-Signature: 55cqzx1jjeinywt1ush7aahgxsaddkeq X-HE-Tag: 1739301836-823334 X-HE-Meta: U2FsdGVkX19JiM8Jw/61MVoNXczjcQwPQCKrKiTCygOcknRx1gKnbiCMT9Gtqbt5i7/WwZVmpEB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MMvexcsyPAm0EjQhjMZKypFcTzUHW8uPElku0nxhBY+LWEEalLrDLHSbzD21NodWaLnauah1yPr1GOuGr0DUyqZ6K6AWh+Wk7o/cm/9VM9m6HgePDLzaROzl6IJetNJfRz6e/XDDx8xF4qnbSet0W7uk2ObApF1+Pg4AJCp3cznJCeUJBeMa/VDTyzWWT2kZklj9ZZ2Yhe3zPzHG47IATQLqoObTMGawfE67S5SMRt3oZPUbpARNVRLTLO1Ufuv1unN/4kJzTvAJoXZZjdCyWEgDut02ST2f7NmesfX25 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: On Tue, Feb 11, 2025 at 6:01=E2=80=AFAM Jeff Layton wr= ote: > > On Mon, 2025-02-10 at 17:38 -0500, Jeff Layton wrote: > > On Mon, 2025-02-10 at 20:36 +0000, Matthew Wilcox wrote: > > > On Mon, Feb 10, 2025 at 02:12:35PM -0500, Josef Bacik wrote: > > > > From: Josef Bacik > > > > Date: Mon, 10 Feb 2025 14:06:40 -0500 > > > > Subject: [PATCH] fuse: drop extra put of folio when using pipe spli= ce > > > > > > > > In 3eab9d7bc2f4 ("fuse: convert readahead to use folios"), I conver= ted > > > > us to using the new folio readahead code, which drops the reference= on > > > > the folio once it is locked, using an inferred reference on the fol= io. > > > > Previously we held a reference on the folio for the entire duration= of > > > > the readpages call. > > > > > > > > This is fine, however I failed to catch the case for splice pipe > > > > responses where we will remove the old folio and splice in the new > > > > folio. Here we assumed that there is a reference held on the folio= for > > > > ap->folios, which is no longer the case. > > > > > > > > To fix this, simply drop the extra put to keep us consistent with t= he > > > > non-splice variation. This will fix the UAF bug that was reported. > > > > > > > > Link: https://lore.kernel.org/linux-fsdevel/2f681f48-00f5-4e09-8431= -2b3dbfaa881e@heusel.eu/ > > > > Fixes: 3eab9d7bc2f4 ("fuse: convert readahead to use folios") > > > > Signed-off-by: Josef Bacik > > > > --- > > > > fs/fuse/dev.c | 2 -- > > > > 1 file changed, 2 deletions(-) > > > > > > > > diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c > > > > index 5b5f789b37eb..5bd6e2e184c0 100644 > > > > --- a/fs/fuse/dev.c > > > > +++ b/fs/fuse/dev.c > > > > @@ -918,8 +918,6 @@ static int fuse_try_move_page(struct fuse_copy_= state *cs, struct page **pagep) > > > > } > > > > > > > > folio_unlock(oldfolio); > > > > - /* Drop ref for ap->pages[] array */ > > > > - folio_put(oldfolio); > > > > cs->len =3D 0; > > > > > > But aren't we now leaking a reference to newfolio? ie shouldn't > > > we also: > > > > > > - folio_get(newfolio); > > > > > > a few lines earlier? > > > > > > > > > I think that ref was leaking without Josef's patch, but your proposed > > fix seems correct to me. There is: > > > > - 1 reference stolen from the pipe_buffer > > - 1 reference taken for the pagecache in replace_page_cache_folio() > > - the folio_get(newfolio) just after that > > > > The pagecache ref doesn't count here, and we only need the reference > > that was stolen from the pipe_buffer to replace the one in pagep. > > Actually, no. I'm wrong here. A little after the folio_get(newfolio) > call, we do: > > /* > * Release while we have extra ref on stolen page. Otherwise > * anon_pipe_buf_release() might think the page can be reused. > */ > pipe_buf_release(cs->pipe, buf); > > ...so that accounts for the extra reference. I think the newfolio > refcounting is correct as-is. I think we do need to remove the folio_get(newfolio); here or we are leaking the reference. new_folio =3D page_folio(buf->page) # ref is 1 replace_page_cache_folio() # ref is 2 folio_get() # ref is 3 pipe_buf_release() # ref is 2 One ref belongs to the page cache and will get dropped by that, but the other ref is unaccounted for (since the original patch removed "folio_put()" from fuse_readpages_end()). I still think acquiring an explicit reference on the folio before we add it to ap->folio and then dropping it when we're completely done with it in fuse_readpages_end() is the best solution, as that imo makes the refcounting / lifetimes the most explicit / clear. For example, in try_move_pages(), if we get rid of that "folio_get()" call, the page cache is the holder of the remaining reference on it, and we rely on the earlier "folio_clear_uptodate(newfolio);" line in try_move_pages() to guarantee that the newfolio isn't freed out from under us if memory gets tight and it's evicted from the page cache. imo, a patch like this makes the refcounting the most clear: >From 923fa98b97cf6dfba3bb486833179c349d566d64 Mon Sep 17 00:00:00 2001 From: Joanne Koong Date: Tue, 11 Feb 2025 10:59:40 -0800 Subject: [PATCH] fuse: acquire explicit folio refcount for readahead In 3eab9d7bc2f4 ("fuse: convert readahead to use folios"), the logic was converted to using the new folio readahead code, which drops the reference on the folio once it is locked, using an inferred reference on the folio. Previously we held a reference on the folio for the entire duration of the readpages call. This is fine, however for the case for splice pipe responses where we will remove the old folio and splice in the new folio (see fuse_try_move_page()), we assume that there is a reference held on the folio for ap->folios, which is no longer the case. To fix this and make the refcounting explicit, acquire a refcount on the folio before we add it to ap->folios[] and drop it when we are done with the folio in fuse_readpages_end(). This will fix the UAF bug that was reported. Link: https://lore.kernel.org/linux-fsdevel/2f681f48-00f5-4e09-8431-2b3dbfa= a881e@heusel.eu/ Fixes: 3eab9d7bc2f4 ("fuse: convert readahead to use folios") Signed-off-by: Joanne Koong --- fs/fuse/file.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 7d92a5479998..6fa535c73d93 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -955,8 +955,10 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args, fuse_invalidate_atime(inode); } - for (i =3D 0; i < ap->num_folios; i++) + for (i =3D 0; i < ap->num_folios; i++) { folio_end_read(ap->folios[i], !err); + folio_put(ap->folios[i]); + } if (ia->ff) fuse_file_put(ia->ff, false); @@ -1049,6 +1051,12 @@ static void fuse_readahead(struct readahead_control = *rac) while (ap->num_folios < cur_pages) { folio =3D readahead_folio(rac); + /* + * Acquire an explicit reference on the folio in ca= se + * it's replaced in the page cache in the splice ca= se + * (see fuse_try_move_page()). + */ + folio_get(folio); ap->folios[ap->num_folios] =3D folio; ap->descs[ap->num_folios].length =3D folio_size(fol= io); ap->num_folios++; -- 2.43.5 > -- > Jeff Layton