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

From: Lorenzo Stoakes (ARM)

Date: Fri Nov 27 2026 - 14:46:10 EST


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.

Reported-by: Chris Mason <mason@xxxxxxxxxx>
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 4bb5f5d9395b ("mm: allow drivers to prevent new writable mappings")
Fixes: 5de195060b2e ("mm: resolve faulty mmap_region() error path behaviour")
Cc: <stable@xxxxxxxxxxxxxxx> # 5.3.x
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
---
fs/coda/file.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/fs/coda/file.c b/fs/coda/file.c
index c9fba41f7cea..64f05f2fc4b3 100644
--- 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;

@@ -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;

--
2.56.1