Linux Documentation
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Jan Harkes" <jaharkes@cs.cmu.edu>,
	coda@cs.cmu.edu, "David Rheinsberg" <david@readahead.eu>,
	"Hugh Dickins" <hughd@google.com>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Jann Horn" <jannh@google.com>, "Alex Shi" <alexs@kernel.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Pedro Falcato" <pfalcato@suse.de>,
	"David Rientjes" <rientjes@google.com>,
	"Rik van Riel" <riel@redhat.com>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Michal Hocko" <mhocko@suse.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Stephen Rothwell" <sfr@canb.auug.org.au>,
	"Muhammad Usama Anjum" <usama.anjum@arm.com>,
	codalist@coda.cs.cmu.edu, linux-mm@kvack.org,
	"Andrei Vagin" <avagin@gmail.com>,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, "Chris Mason" <mason@kernel.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
Date: Mon, 30 Nov 2026 14:20:23 +0000	[thread overview]
Message-ID: <aw2EjGyP_g-VGVGf@gremlin> (raw)
In-Reply-To: <20261127124428.f1f66c6244d3e5c2811f56de@linux-foundation.org>

On Fri, Nov 27, 2026 at 12:44:28PM -0800, Andrew Morton wrote:
> On Fri, 27 Nov 2026 19:45:40 +0000 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
>
> > Coda file systems stack differently from other stacked file systems.
> >
> > On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
> > the Coda file's f_mapping for the mapping of the container file. This
> > causes two problems.
> >
> > Firstly, the container file may not remain pinned, at which point this may
> > result in a dangling reference in coda_file->f_mapping, and so a
> > use-after-free in mmap() or lseek().
> >
> > This can happen when you open the same inode across two files, mmap()'ing
> > in one, which assigns the i_mapping, and open()'ing another, which assigns
> > that i_mapping to the file's f_mapping, if the cache manager gave the two
> > files different containers. Each file pins only its own container.
>
> Yeah, I hit this while getting
> tools/testing/selftests/mm/memory-failure.c to work on all Linux
> filesystems.
>
> > --- a/fs/coda/file.c
> > +++ b/fs/coda/file.c
> > @@ -182,7 +182,6 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
> >
> >  	cii = ITOC(coda_inode);
> >  	spin_lock(&cii->c_lock);
> > -	coda_file->f_mapping = host_file->f_mapping;
> >  	if (coda_inode->i_mapping == &coda_inode->i_data)
> >  		coda_inode->i_mapping = host_inode->i_mapping;
> >
>
> I had this change.
>
> > @@ -248,6 +247,19 @@ int coda_open(struct inode *coda_inode, struct file *coda_file)
> >
> >  	host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
> >
> > +	/*
> > +	 * On mmap coda_inode->i_mapping is updated to point at the mapping of
> > +	 * the mmap()'d file's container, and do_dentry_open() propagates that
> > +	 * to coda_file->f_mapping.
> > +	 *
> > +	 * However coda_file's container may differ, in which case the lifetime
> > +	 * of that mapping may not match that of coda_file.
> > +	 *
> > +	 * To avoid a dangling pointer, reassign the mapping to the canonical
> > +	 * one.
> > +	 */
> > +	coda_file->f_mapping = &coda_inode->i_data;
> > +
> >  	cfi->cfi_magic = CODA_MAGIC;
> >  	cfi->cfi_mapcount = 0;
> >  	cfi->cfi_container = host_file;
>
> But not this one.
>
> Is it truly needed, or is it a future-safe defensive thing?

It's absolutely needed, yes. mmap does:

  	if (coda_inode->i_mapping == &coda_inode->i_data)
  		coda_inode->i_mapping = host_inode->i_mapping;

And do_dentry_open() does:

	f->f_mapping = inode->i_mapping;

So removing the coda_file->f_mapping assignment there doesn't save you. It only
addresses things at the point of mmap not open().

So you can still end up with a dangling pointer.

The fundamental issue here is that state with 1 lifetime (container
mapping) is being tied to an inode with a separate lifetime.

And there's nothing holding a reference or expressing the dependency, meaning
you get a dangling pointer if the container manager does something insane.

(BTW I actually merged 2 AI patches into one for this fix, the original
solutions were kinda horrible, this makes it into one cohesive change).

>
> If the latter, Tested-by-me:.  If the former, Mostly-tested-by:me.
> Either way, Acked-by:me.

Thanks!

To make David's life easier (I assume he's taking?):

Acked-by: Andrew Morton <akpm@linux-foundation.org>
Tested-by: Andrew Morton <akpm@linux-foundation.org>

>
>
> It's always seemed a big wart that file->f_mapping even exists.  We
> should get that via file->f_inode->i_mapping!  But you-know-what tells
> me that "/dev/mem sets the opened file's f_mapping to the shared
> I/O-memory mapping; /proc/bus/pci does the same.  Their f_inode still
> identifies the device or proc entry" so sigh.
>

Yeah anything that is changing state like that like this is very sketchy. And we
definitely shouldn't have allowed it.

Or an mmap hook with a pointer to key kernel state...

--
Cheers, Lorenzo

  parent reply	other threads:[~2026-11-30 14:20 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
2026-11-27 20:44   ` Andrew Morton
2026-11-27 22:05     ` Pedro Falcato
2026-11-28  1:10     ` Jan Harkes
2026-11-28  6:23       ` Andrew Morton
2026-11-28 19:40         ` Jan Harkes
2026-11-30 14:09           ` Lorenzo Stoakes (ARM)
2026-11-30 14:20     ` Lorenzo Stoakes (ARM) [this message]
2026-11-27 22:01   ` Pedro Falcato
2026-11-30 14:11     ` Lorenzo Stoakes (ARM)
2026-11-27 19:45 ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Lorenzo Stoakes (ARM)
2026-11-27 22:14   ` Pedro Falcato
2026-11-30 14:02     ` Lorenzo Stoakes (ARM)
2026-11-30 12:30   ` David Hildenbrand (Arm)
2026-11-30 14:05     ` Lorenzo Stoakes (ARM)
2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
2026-11-27 22:15   ` Pedro Falcato
2026-11-30 12:28   ` David Hildenbrand (Arm)
2026-11-27 19:45 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Lorenzo Stoakes (ARM)
2026-11-27 22:24   ` Pedro Falcato
2026-11-30 12:29   ` David Hildenbrand (Arm)

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=aw2EjGyP_g-VGVGf@gremlin \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alexs@kernel.org \
    --cc=arnd@arndb.de \
    --cc=avagin@gmail.com \
    --cc=coda@cs.cmu.edu \
    --cc=codalist@coda.cs.cmu.edu \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=david@readahead.eu \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=jaharkes@cs.cmu.edu \
    --cc=jannh@google.com \
    --cc=liam@infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mirq-linux@rere.qmqm.pl \
    --cc=pfalcato@suse.de \
    --cc=rdunlap@infradead.org \
    --cc=riel@redhat.com \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=sfr@canb.auug.org.au \
    --cc=skhan@linuxfoundation.org \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=usama.anjum@arm.com \
    --cc=vbabka@kernel.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