Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
From: Lorenzo Stoakes (ARM)
Date: Mon Nov 30 2026 - 09:12:02 EST
On Fri, Nov 27, 2026 at 10:01:32PM +0000, Pedro Falcato wrote:
> On Fri, Nov 27, 2026 at 07:45:40PM +0000, Lorenzo Stoakes (ARM) 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.
> >
> > Consider (A, B = separate open files of the same inode):
> >
> > A, with container C1 B, with container C2
> > ==================== ====================
> > mmap()
> > coda_file_mmap()
> > i_mapping = C1's mapping
> > open()
> > do_dentry_open()
> > f_mapping = i_mapping
> > coda_open()
> > cfi_container = C2
> > munmap()
> > close()
> > coda_release()
> > i_mapping = &i_data
> > fput(C1), the last one
> > mmap()
> > mmap_region()
> > mapping_map_writable(f_mapping)
> >
> > Secondly, mmap_region() takes a temporary i_mmap_writable count on
> > file->f_mapping ahead of the mmap hook, and drops it on file->f_mapping
> > afterwards. As the hook changes f_mapping in between, the count is dropped
> > on the container's mapping rather than on the mapping it was taken on:
> >
> > i_mmap_writable of Coda container
> > mmap_region()
> > mapping_map_writable(file->f_mapping) 1 0
> > __mmap_region()
> > coda_file_mmap()
> > coda_file->f_mapping = host_file->f_mapping
> > vma->vm_file = get_file(host_file)
> > vma_link_file()
> > mapping_allow_writable() 1 1
> > mapping_unmap_writable(file->f_mapping) 1 0
> > munmap()
> > mapping_unmap_writable() 1 -1
> >
> > The container's count is negative after munmap(), so the next shared
> > mapping of that Coda file, if it is open for writing, fails with -EPERM.
> >
> > Fix both issues by no longer updating coda_file->f_mapping in
> > coda_file_mmap(), and by having coda_open() always assign the canonical
> > mapping of the inode, as do_dentry_open() may have propagated an i_mapping
> > which coda_file_mmap() redirected.
>
> This might be the most confusing thing I've read this year?
I completely rewrote it, you should have seen what it originally said :)
Unfortunately it is _just_ confusing this.
>
> Anyway, in the sense that filesystems definitely shouldn't be poking f_mapping
> after open:
>
> Acked-by: Pedro Falcato <pfalcato@xxxxxxx>
Thanks!
>
>
> --
> Pedro
--
Cheers, Lorenzo