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=1796048435; b=raP4GNPKcCgGxIai4vsAdzJs34axzcK/fK/l9Q9TeOVOvc5W110U/SGPjDO36fVZ5Xvv YWIA3zhbRkQzvPpn9nQyu/cAuaF76xT1Pw3iHw6sjTtMJEsb/EQfXrWWe798onq2NkChl Ky0iS3pLy81r5+mjlKjBQWcpgR0rxIzCX8guKj1x5gSo5ndlNjbFfgDkEgNX8ZerLDgCo hu8dS22y2Giz7xnbFkVjiseIsGJm0F98GsLHcdXJj+MdLjtg8B0Clm19xUppQNfOpFHRj fsaywNU5EgRKk5N2GWTJoCK1klxzbZv9PbIn/yKxdBieJ2mNQpZP9yFGh8VGb3vz9wQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796048435; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=9TarlZ99nQ5Hy5pYw8hwe/JGpK7QURPk7bm55qLC3eY=; b=4lLZBpv7yyy4Qj9ygDvIwVQ1aw9OqhRt+VHBA+vkJMi3jttjY90o5W7+B2mWXoAsqB4Y Ra06x1YwJvALIEHnNREljXbr0gvq3sxjJ2HSM7bILzn0RSnEZ3vEHEF8cNN5IgG5HAN6u 49UDKuvju3UZaaUAXy5hYNUgEsszUYaavp/4J0pB6FWTshntPeguOHR3lyy7IsiTOxU5b T9zTx86IyKMa+V/9oYZDqBdTR/XJlojICOFsIYJYCGpGI9Odl8HdAR3PdnzQzX6/VQHla 7Nr35tgLVWcSY3MpI3aeX/kQXJ+5tcNLMhu9zjkCq+K5m0MvweEguag52NNQXG5fSjw== 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=UvczYcgA; 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 E4997606A320; Mon, 30 Nov 2026 14:20:34 +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=UvczYcgA Received: by smtp.kernel.org (Postfix) with ESMTPSA id C88821F0092A; Mon, 30 Nov 2026 14:20:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796048434; bh=9TarlZ99nQ5Hy5pYw8hwe/JGpK7QURPk7bm55qLC3eY=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=UvczYcgAwuMhr2jCVfAUgdaXaeCl3I9QWl2ovKokNd4YWrsJO9HeXpeMPY+ZepJNm zj8ZsdHQx2uZm8/KrqXAlgVPlNG+4oGhyWZ8Gz32Mv0CULnl9P/p3olh7AOChuNIyl 0Tr4Z2Glf3SGzysmT3GrN+K9AUSrUN8bvdOngkFxk5OvO7OeZ9fuYwN6xqy2OupkFE fdVaA0nYzUztoxSkzqTZmWGJ5LigOrUYVyaVCElTf2HCW4sYzbTda1+C0P35fhdLau 8rjPyCB14f4l8fBM6p73aKGR8HzwzgaJ4+pRArRIfswenH5uKFuIhntnOjq4WJ1DMW qTlUdnhds3APQ== Date: Mon, 30 Nov 2026 14:20:23 +0000 From: "Lorenzo Stoakes (ARM)" To: Andrew Morton Cc: Jan Harkes , coda@cs.cmu.edu, David Rheinsberg , Hugh Dickins , Vlastimil Babka , "Liam R. Howlett" , David Hildenbrand , Jann Horn , Alex Shi , Johannes Weiner , Pedro Falcato , 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> <20261127124428.f1f66c6244d3e5c2811f56de@linux-foundation.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: <20261127124428.f1f66c6244d3e5c2811f56de@linux-foundation.org> On Fri, Nov 27, 2026 at 12:44:28PM -0800, Andrew Morton wrote: > On Fri, 27 Nov 2026 19:45:40 +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. > > Yeah, I hit this while getting > tools/testing/selftests/mm/memory-failure.c to work on all Linux > filesystems. > > > --- 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; > > > > I had this change. > > > @@ -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; > > But not this one. > > Is it truly needed, or is it a future-safe defensive thing? It's absolutely needed, yes. mmap does: if (coda_inode->i_mapping == &coda_inode->i_data) coda_inode->i_mapping = host_inode->i_mapping; And do_dentry_open() does: f->f_mapping = inode->i_mapping; So removing the coda_file->f_mapping assignment there doesn't save you. It only addresses things at the point of mmap not open(). So you can still end up with a dangling pointer. The fundamental issue here is that state with 1 lifetime (container mapping) is being tied to an inode with a separate lifetime. And there's nothing holding a reference or expressing the dependency, meaning you get a dangling pointer if the container manager does something insane. (BTW I actually merged 2 AI patches into one for this fix, the original solutions were kinda horrible, this makes it into one cohesive change). > > If the latter, Tested-by-me:. If the former, Mostly-tested-by:me. > Either way, Acked-by:me. Thanks! To make David's life easier (I assume he's taking?): Acked-by: Andrew Morton Tested-by: Andrew Morton > > > It's always seemed a big wart that file->f_mapping even exists. We > should get that via file->f_inode->i_mapping! But you-know-what tells > me that "/dev/mem sets the opened file's f_mapping to the shared > I/O-memory mapping; /proc/bus/pci does the same. Their f_inode still > identifies the device or proc entry" so sigh. > Yeah anything that is changing state like that like this is very sketchy. And we definitely shouldn't have allowed it. Or an mmap hook with a pointer to key kernel state... -- Cheers, Lorenzo