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:01:46 EST
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?
Anyway, in the sense that filesystems definitely shouldn't be poking f_mapping
after open:
Acked-by: Pedro Falcato <pfalcato@xxxxxxx>
--
Pedro