From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047919; b=MopnuY1MUmRF2IOcBV61pbbJvQdr/6PnQMTEdEoPgPES1RPQ1a3VW/Bs+0VOyl2ubrbR IxWMYOI1kSGzbcnVAZIhbB6q+z7nw/NRNTtrK9P6tKpTi6DO9woyLWsuvZt52BAf6q1kT 8TF1AEIihj3mQU8gOThH/UCTDdPhl3y97b4RlNouO/mqk2mMdxQQhI6eyagZ2o3GZq8Pd FUVfuRwf73NQ9kPvxGEgWZgY7Sf4D7S/KhABVnzTrg91VSH+RbuAYSAe0X9VJb8Q6qXUF fLAH1aderS7IbF0KWoC2NsmyS6Aky0eYPd5qMYI+87pldb98ihCIXppoYJEeyCzfPbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047919; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=gd/oqILBjQTuElbBpaDTu11XwSihCXWgqdZ39P7ZvVY=; b=TQdFJrqNQ0f+1mto2Ng55geySIOF9dES1R9WKrdYI30WI2cjFklFs01qkxiHdHZs8dih Nf8MEq140V2e9M4d3czgjz3DzD+rHwrlziYDlDuNvPDlJMW2/pAuGyi9JNYw4SXzhDCw2 phLfJPCrQQ6akl4PUNvadtGlHFSiN9cPtKyylhHwCUGjbRgem6gxezNG9ugY/B+/l8Kvf PAXuAPjw8obk9BA+AoE8ODyVlYjgGzxFIpBEcrvhPJ7umsgHquAaeBYBh0SVxMOJcwJQ1 Ryi/Moz/NiAPD0En/oo6H5YlTTF00Us927f0wYYXQ76SYzqzQ9zijMlG99fUo6LxsxA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Wk8jS5jk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E9AA606A32E; Mon, 30 Nov 2026 14:11:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Wk8jS5jk Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D2991F00925; Mon, 30 Nov 2026 14:11:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796047918; bh=gd/oqILBjQTuElbBpaDTu11XwSihCXWgqdZ39P7ZvVY=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Wk8jS5jk6VeuUyi67pinIASSRkzMKHCfgz75dgmuD9ZvPXEXXc6lKoUEirmsyRb/Y 18GtSP23AlXYJTniwEcqO4eKv/wXHH5DwC14cROx4hgp1huYTcjP4W6RJxsZhSE9qL 3l7n2bKKbPbwlXBo0zK6oIgibk9YuuqaFsNB/lhgCBq7V+aBr4bE3iW7M8810r1B5U sQYzAVXWSYqub9FgG54IQXqHbMOwKfFbmfrfndxb/4/t8Y5wYLB4JBCyqoj0bX0sNf 41uhBykKjoeCUY4yNQE5rFT6h2suaM8NKydH+srgJ1FkIK/q9u8Hv7uUZ0F9NAXNzQ Q7FvDqe8ZXn8g== Date: Mon, 30 Nov 2026 14:11:48 +0000 From: "Lorenzo Stoakes (ARM)" To: Pedro Falcato Cc: Jan Harkes , coda@cs.cmu.edu, Andrew Morton , David Rheinsberg , Hugh Dickins , Vlastimil Babka , "Liam R. Howlett" , David Hildenbrand , Jann Horn , Alex Shi , Johannes Weiner , David Rientjes , Rik van Riel , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , =?utf-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= , Arnd Bergmann , Stephen Rothwell , Muhammad Usama Anjum , codalist@coda.cs.cmu.edu, linux-mm@kvack.org, Andrei Vagin , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Chris Mason , stable@vger.kernel.org Subject: Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Message-ID: References: <20261127-mlock-madvise-mprotect-ai-fixes-v1-0-e46b2b1eb435@kernel.org> <20261127-mlock-madvise-mprotect-ai-fixes-v1-1-e46b2b1eb435@kernel.org> X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Thanks! > > > -- > Pedro -- Cheers, Lorenzo