Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping

From: Pedro Falcato

Date: Fri Nov 27 2026 - 17:05:34 EST


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@xxxxxxxxxx> 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?
>
> If the latter, Tested-by-me:. If the former, Mostly-tested-by:me.
> Either way, Acked-by:me.
>
>
> 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.

FWIW, I think it's a rather nice part of the Linux VFS design: "file" and
"inode" are separated well enough to allow for these kinds of shenanigans
without awful hacks (and, yes, this is also present for something standard
like a block device on /dev; /dev is a tmpfs mount, so the inode is tmpfs, but
the mapping and fops cleanly point to block).

But stacked filesystems, admittedly, do give me motion sickness.

--
Pedro