From: Pedro Falcato <pfalcato@suse.de>
To: Matthew Wilcox <willy@infradead.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, linux-hardening@vger.kernel.org,
Kees Cook <kees@kernel.org>, Mateusz Guzik <mjguzik@gmail.com>
Subject: Re: [RFC PATCH] fs/splice: allow for a way to block splice() with read-only files
Date: Sun, 17 May 2026 01:59:41 +0100 [thread overview]
Message-ID: <agkRpj4oU8TPgmTS@pedro-suse.lan> (raw)
In-Reply-To: <agj4mXKRVW44ZJ18@casper.infradead.org>
On Sun, May 17, 2026 at 12:07:05AM +0100, Matthew Wilcox wrote:
> On Sat, May 16, 2026 at 07:21:26PM +0100, Pedro Falcato wrote:
> > +static bool may_write_to_page(struct page *page, struct address_space **plast)
> > +{
> > + struct folio *folio = page_folio(page);
> > + struct address_space *mapping, *last = *plast;
> > + struct inode *inode;
> > + bool may = false;
> > +
> > + if (!READ_ONCE(sysctl_splice_needs_write))
> > + return true;
> > + /*
> > + * Always fine to write to anon folios.
> > + */
> > + if (folio_test_anon(folio))
> > + return true;
>
> What about KSM? It's not something we've seen attacked yet, but it'd be
> pretty nasty to be able to change a KSM page in another process.
It's my understanding that only anon pages can be KSM'd, and KSM still keeps
the FOLIO_MAPPING_ANON bit set. So folio_test_anon() should still test true
for those.
>
> I just got off a flight, so hopefully I'm semicoherent.
>
> > + mapping = READ_ONCE(folio->mapping);
> > + WARN_ON((unsigned long) mapping & FOLIO_MAPPING_FLAGS);
> > +
> > + /* If it is the same (locklessly), then LGTM, proceed. */
> > + if (mapping == last)
> > + return true;
> > + /*
> > + * Else we have to recheck with the folio lock held, for mapping
> > + * stability. TODO: killable?
>
> I wouldn't've thought that'd be necessary. The folio can't be being
> read because it's mapped, and we won't map a folio until it's uptodate.
Makes sense, I'll avoid the trouble then.
>
> > + */
> > + folio_lock(folio);
> > + mapping = folio_mapping(folio);
>
> I think you're safe to just look at folio->mapping here. You have a
> refcount on the folio so it can't be freed, and I'm not sure there's a
> way to transition from page cache folio to anon folio without taking a
> trip through the page allocator.
Yep, makes sense. I don't think there is either. The worst that can happen is
that the folio could be truncated out while we have a reference but not the lock.
I think I just used the helper for the sake of using the helper, so I'll replace
it with ->mapping.
>
> > + /* May have been truncated, etc */
> > + if (!mapping)
> > + goto out_lock;
>
> typically we call this "out_unlock".
ACK
>
> > + inode = mapping->host;
> > + may = inode_owner_or_capable(&nop_mnt_idmap, inode) ||
> > + inode_permission(&nop_mnt_idmap, inode, MAY_WRITE) == 0;
> > + if (likely(may))
> > + *plast = mapping;
> > +out_lock:
> > + folio_unlock(folio);
> > + return may;
> > +}
>
> I don't have a problem with the idea, other than it's really sad we have
> to do this.
Indeed :/
--
Pedro
next prev parent reply other threads:[~2026-05-17 0:59 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-16 18:21 [RFC PATCH] fs/splice: allow for a way to block splice() with read-only files Pedro Falcato
2026-05-16 23:07 ` Matthew Wilcox
2026-05-17 0:59 ` Pedro Falcato [this message]
2026-05-17 1:17 ` Matthew Wilcox
2026-05-17 9:01 ` Pedro Falcato
2026-05-17 22:30 ` Matthew Wilcox
2026-05-16 23:51 ` Mateusz Guzik
2026-05-17 0:52 ` Pedro Falcato
2026-05-18 11:44 ` Christian Brauner
2026-05-18 12:20 ` Christian Brauner
2026-05-18 13:02 ` Pedro Falcato
2026-05-18 18:59 ` Jann Horn
2026-05-19 6:39 ` Christoph Hellwig
2026-05-19 9:49 ` Christian Brauner
2026-05-19 10:51 ` Mateusz Guzik
2026-05-19 10:59 ` Christian Brauner
2026-05-19 11:56 ` Mateusz Guzik
2026-05-22 13:11 ` Christian Brauner
2026-05-28 12:59 ` Pedro Falcato
2026-05-19 13:28 ` James Bottomley
2026-05-19 16:28 ` Jann Horn
2026-05-23 20:41 ` Askar Safin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=agkRpj4oU8TPgmTS@pedro-suse.lan \
--to=pfalcato@suse.de \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mjguzik@gmail.com \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox