mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "Jan Harkes" <jaharkes@cs.cmu.edu>,
	coda@cs.cmu.edu, "Andrew Morton" <akpm@linux-foundation.org>,
	"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>
Cc: 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,
	 "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
	Chris Mason <mason@kernel.org>,
	 stable@vger.kernel.org
Subject: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
Date: Fri, 27 Nov 2026 19:45:40 +0000	[thread overview]
Message-ID: <20261127-mlock-madvise-mprotect-ai-fixes-v1-1-e46b2b1eb435@kernel.org> (raw)
In-Reply-To: <20261127-mlock-madvise-mprotect-ai-fixes-v1-0-e46b2b1eb435@kernel.org>

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@kernel.org>
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@vger.kernel.org> # 5.3.x
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 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


  reply	other threads:[~2026-11-27 19:46 UTC|newest]

Thread overview: 11+ 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 ` Lorenzo Stoakes (ARM) [this message]
2026-11-27 20:44   ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Andrew Morton
2026-11-27 22:05     ` Pedro Falcato
2026-11-27 22:01   ` Pedro Falcato
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-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-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

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=20261127-mlock-madvise-mprotect-ai-fixes-v1-1-e46b2b1eb435@kernel.org \
    --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

all inboxes | Powered by JetHome®