Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] mm,drm/i915,powerpc/spufs: vm_ops->access related fixes
@ 2026-11-22 17:55 David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 1/4] mm: do not call vm_ops->access with FOLL_ANON David Hildenbrand (Arm)
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 17:55 UTC (permalink / raw)
  To: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Andrew Morton, Willy Tarreau,
	Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
	David Airlie, Simona Vetter, Chris Wilson, Matthew Auld,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
	Shrikanth Hegde, Rik van Riel, Benjamin Herrenschmidt
  Cc: linux-mm, linux-kernel, intel-gfx, dri-devel, linuxppc-dev,
	David Hildenbrand (Arm), Chris Mason, stable, Felix Kuehling,
	Christian König, Felix Kuehling

While working on vm_ops->access related fixes in core code for FOLL_ANON
and VMA access permission + size handling, I stumbled over two driver
issues.

Fixing it all in this series, because it's all intertwined. Only allow
FOLL_FORCE to bypass VMA permissions, similar to what GUP does for other
VMAs.

In patch #4 we break a debugging use case for older ROCm versions, and
forced writes to mappings of read-only files (e.g., DMA-bufs). Current
ROCm no longer relies on it, but old setups (e.g., MI100 with old
userspace) might break when updating the kernel.

Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
Changes in v2:
- Drop "drm/i915/selftests: request read access for mmap access tests"
- "mm: check permissions and bounds for vm_ops->access"
 - Allow reads of write-only mappings, as we historically did
 - Combine the permission check comments into one
 - Mention breaking forced writes to read-only files
 - Extend patch description
- Pick up tags
- Link to v1: https://patch.msgid.link/20261112-access_shite-v1-0-988a5c470db2@kernel.org

---
Chris Mason (1):
      mm: do not call vm_ops->access with FOLL_ANON

David Hildenbrand (Arm) (3):
      drm/i915: consider vm_pgoff in vm_access()
      powerpc/spufs: consider vm_pgoff in spufs_mem_mmap_access()
      mm: check permissions and bounds for vm_ops->access

 arch/powerpc/platforms/cell/spufs/file.c |  3 ++-
 drivers/gpu/drm/i915/gem/i915_gem_mman.c |  4 +++-
 mm/memory.c                              | 29 +++++++++++++++++++++++++++--
 3 files changed, 32 insertions(+), 4 deletions(-)
---
base-commit: 76d96c9641d0c1c0d0e92013a30bd887f8d2dbfe
change-id: 20261112-access_shite-d8a47559572e

Best regards,
--  
David Hildenbrand (Arm) <david@kernel.org>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/4] mm: do not call vm_ops->access with FOLL_ANON
  2026-11-22 17:55 [PATCH v2 0/4] mm,drm/i915,powerpc/spufs: vm_ops->access related fixes David Hildenbrand (Arm)
@ 2026-11-22 17:55 ` David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 2/4] drm/i915: consider vm_pgoff in vm_access() David Hildenbrand (Arm)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 17:55 UTC (permalink / raw)
  To: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Andrew Morton, Willy Tarreau,
	Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
	David Airlie, Simona Vetter, Chris Wilson, Matthew Auld,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
	Shrikanth Hegde, Rik van Riel, Benjamin Herrenschmidt
  Cc: linux-mm, linux-kernel, intel-gfx, dri-devel, linuxppc-dev,
	David Hildenbrand (Arm), Chris Mason, stable

From: Chris Mason <mason@kernel.org>

Reading /proc/<pid>/cmdline or /proc/<pid>/environ can block in a device
driver, or read device memory, if the virtual addresses point at a device
mapping.

Ever since commit 7f7ccc2ccc2e ("proc: do not access cmdline nor environ
from file-backed areas") we use FOLL_ANON to only read from anonymous
memory. While GUP properly handles FOLL_ANON, __access_remote_vm()
will fall back on GUP errors to vma->vm_ops->access() without similarly
considering the flag.

Fix it by not calling vm_ops->access if FOLL_ANON was specified, to
truly limit it to anonymous memory only.

Fixes: 7f7ccc2ccc2e ("proc: do not access cmdline nor environ from file-backed areas")
Signed-off-by: Chris Mason <mason@kernel.org>
Cc: stable@vger.kernel.org
[ david: simplify description; slightly rework implementation ]
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 mm/memory.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/memory.c b/mm/memory.c
index 6a23198c1c67f..5d0d58aeff3cb 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -7448,6 +7448,9 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
 				continue;
 			}
 
+			if (gup_flags & FOLL_ANON)
+				break;
+
 			/*
 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
 			 * we can access using slightly different code.

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/4] drm/i915: consider vm_pgoff in vm_access()
  2026-11-22 17:55 [PATCH v2 0/4] mm,drm/i915,powerpc/spufs: vm_ops->access related fixes David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 1/4] mm: do not call vm_ops->access with FOLL_ANON David Hildenbrand (Arm)
@ 2026-11-22 17:55 ` David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 3/4] powerpc/spufs: consider vm_pgoff in spufs_mem_mmap_access() David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 4/4] mm: check permissions and bounds for vm_ops->access David Hildenbrand (Arm)
  3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 17:55 UTC (permalink / raw)
  To: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Andrew Morton, Willy Tarreau,
	Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
	David Airlie, Simona Vetter, Chris Wilson, Matthew Auld,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
	Shrikanth Hegde, Rik van Riel, Benjamin Herrenschmidt
  Cc: linux-mm, linux-kernel, intel-gfx, dri-devel, linuxppc-dev,
	David Hildenbrand (Arm), stable

vm_access() treats the start of every VMA as the beginning of the GEM
object. After a VMA split, vm_pgoff identifies a later part of the
object. Debugger reads and writes through ptrace or /proc/<pid>/mem then
access the wrong bytes.

Properly consider vm_pgoff just like the fault handlers already do.

Fixes: 9f909e215fea ("drm/i915: Implement vm_ops->access for gdb access into mmaps")
Cc: stable@vger.kernel.org
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/i915/gem/i915_gem_mman.c b/drivers/gpu/drm/i915/gem/i915_gem_mman.c
index 6b337684dbfe8..e3b24dc8e94f5 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_mman.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_mman.c
@@ -511,13 +511,15 @@ vm_access(struct vm_area_struct *area, unsigned long addr,
 	struct i915_mmap_offset *mmo = area->vm_private_data;
 	struct drm_i915_gem_object *obj = mmo->obj;
 	struct i915_gem_ww_ctx ww;
+	unsigned long obj_offset;
 	void *vaddr;
 	int err = 0;
 
 	if (i915_gem_object_is_readonly(obj) && write)
 		return -EACCES;
 
-	addr -= area->vm_start;
+	obj_offset = area->vm_pgoff - drm_vma_node_start(&mmo->vma_node);
+	addr += (obj_offset << PAGE_SHIFT) - area->vm_start;
 	if (range_overflows_t(u64, addr, len, obj->base.size))
 		return -EINVAL;
 

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/4] powerpc/spufs: consider vm_pgoff in spufs_mem_mmap_access()
  2026-11-22 17:55 [PATCH v2 0/4] mm,drm/i915,powerpc/spufs: vm_ops->access related fixes David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 1/4] mm: do not call vm_ops->access with FOLL_ANON David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 2/4] drm/i915: consider vm_pgoff in vm_access() David Hildenbrand (Arm)
@ 2026-11-22 17:55 ` David Hildenbrand (Arm)
  2026-11-22 17:55 ` [PATCH v2 4/4] mm: check permissions and bounds for vm_ops->access David Hildenbrand (Arm)
  3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 17:55 UTC (permalink / raw)
  To: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Andrew Morton, Willy Tarreau,
	Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
	David Airlie, Simona Vetter, Chris Wilson, Matthew Auld,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
	Shrikanth Hegde, Rik van Riel, Benjamin Herrenschmidt
  Cc: linux-mm, linux-kernel, intel-gfx, dri-devel, linuxppc-dev,
	David Hildenbrand (Arm), stable

spufs_mem_mmap_access() calculates the local store offset relative to the
start of the VMA and ignores vm_pgoff. After VMA splits, debugger reads and
writes access the wrong part of the local store. Mappings created with a
nonzero file offset have the same problem.

Properly take the vm_pgoff into account, just like spufs_mem_mmap_fault()
already does.

Fixes: a352894d0705 ("spufs: use new vm_ops->access to allow local state access from gdb")
Cc: stable@vger.kernel.org
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 arch/powerpc/platforms/cell/spufs/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/cell/spufs/file.c b/arch/powerpc/platforms/cell/spufs/file.c
index 98c47bafaf678..0a46d6f7178ab 100644
--- a/arch/powerpc/platforms/cell/spufs/file.c
+++ b/arch/powerpc/platforms/cell/spufs/file.c
@@ -248,7 +248,8 @@ static int spufs_mem_mmap_access(struct vm_area_struct *vma,
 				void *buf, int len, int write)
 {
 	struct spu_context *ctx = vma->vm_file->private_data;
-	unsigned long offset = address - vma->vm_start;
+	unsigned long offset = address - vma->vm_start +
+			       (vma->vm_pgoff << PAGE_SHIFT);
 	char *local_store;
 
 	if (write && !(vma->vm_flags & VM_WRITE))

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 4/4] mm: check permissions and bounds for vm_ops->access
  2026-11-22 17:55 [PATCH v2 0/4] mm,drm/i915,powerpc/spufs: vm_ops->access related fixes David Hildenbrand (Arm)
                   ` (2 preceding siblings ...)
  2026-11-22 17:55 ` [PATCH v2 3/4] powerpc/spufs: consider vm_pgoff in spufs_mem_mmap_access() David Hildenbrand (Arm)
@ 2026-11-22 17:55 ` David Hildenbrand (Arm)
  3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 17:55 UTC (permalink / raw)
  To: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Andrew Morton, Willy Tarreau,
	Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
	David Airlie, Simona Vetter, Chris Wilson, Matthew Auld,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
	Shrikanth Hegde, Rik van Riel, Benjamin Herrenschmidt
  Cc: linux-mm, linux-kernel, intel-gfx, dri-devel, linuxppc-dev,
	David Hildenbrand (Arm), stable, Felix Kuehling,
	Christian König, Chris Mason, Felix Kuehling

GUP rejects VM_IO and VM_PFNMAP mappings early. __access_remote_vm() then
falls back to vm_ops->access without checking VMA permissions and without
limiting the request to the VMA size.

Consequently, /proc/<pid>/mem can bypass protections even without
FOLL_FORCE, or cross into another mapping without checking its
permissions.

Check VMA permissions before calling ->access and limit each request to
the current VMA. Implement access permission checks similar to GUP,
however:

 (1) Reject writes to CoW mappings because the callbacks access backing
     memory directly and cannot handle CoW.
 (2) Allow forced writes to non-CoW mappings with VM_MAYWRITE.
 (3) Allow reads of write-only mappings, as we historically did. Most
     architectures cannot express write-only mappings in page tables
     anyway, and i915 selftests rely on it.

This intentionally breaks forced writes through legacy amdgpu PROT_NONE,
MAP_PRIVATE debugger mappings. The workaround in commit fa5239f2af98
("drm/amdgpu: workaround failed COW checks for Thunk VMAs") cleared
VM_MAYWRITE to avoid CoW, but debugger writes still succeeded by
exploiting the missing access-permission checks fixed here. Reads remain
supported. Userspace must use MAP_SHARED instead; ROCm 5.5.0, released
in May 2023, already does so.

Similarly, forced writes to MAP_SHARED mappings of files opened read-only
(e.g., read-only DMA-bufs) no longer succeed, as VM_MAYWRITE is not set.
Writing to read-only files through ptrace was never intended to work,
and would usually be a security bug.

If this turns out to break real users that cannot easily satisfy their
debugging needs differently, we can figure out how to handle that.

Link: https://github.com/ROCm/ROCT-Thunk-Interface/commit/fea44fab6e84eda52fcf6b419b35600c18f4c022
Fixes: 272ddc8b3735 ("proc: don't use FOLL_FORCE for reading cmdline and environment")
Cc: stable@vger.kernel.org
Cc: Felix Kuehling <Felix.Kuehling@amd.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Simona Vetter <simona@ffwll.ch>
Reported-by: Chris Mason <mason@kernel.org>
Acked-by: Felix Kuehling <felix.kuehling@amd.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 mm/memory.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 5d0d58aeff3cb..d43837623a063 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -7412,6 +7412,7 @@ EXPORT_SYMBOL_GPL(generic_access_phys);
 static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
 			      void *buf, int len, unsigned int gup_flags)
 {
+	const bool force = gup_flags & FOLL_FORCE;
 	void *old_buf = buf;
 	int write = gup_flags & FOLL_WRITE;
 
@@ -7451,15 +7452,36 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
 			if (gup_flags & FOLL_ANON)
 				break;
 
+			/*
+			 * In contrast to GUP, we historically allowed
+			 * FOLL_FORCE write access to non-CoW mappings and
+			 * read access to write-only mappings. However, we
+			 * must always reject writes to CoW mappings as we
+			 * cannot CoW here.
+			 */
+			if (write) {
+				if (!vma_test(vma, VMA_MAYWRITE_BIT))
+					break;
+				if (!vma_test(vma, VMA_WRITE_BIT) && !force)
+					break;
+				if (vma_is_cow_mapping(vma))
+					break;
+			} else if (!vma_test_any(vma, VMA_READ_BIT, VMA_WRITE_BIT)) {
+				if (!force || !vma_test(vma, VMA_MAYREAD_BIT))
+					break;
+			}
+
 			/*
 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
 			 * we can access using slightly different code.
 			 */
 			bytes = 0;
 #ifdef CONFIG_HAVE_IOREMAP_PROT
-			if (vma->vm_ops && vma->vm_ops->access)
+			if (vma->vm_ops && vma->vm_ops->access) {
+				bytes = min_t(unsigned long, len, vma->vm_end - addr);
 				bytes = vma->vm_ops->access(vma, addr, buf,
-							    len, write);
+							    bytes, write);
+			}
 #endif
 			if (bytes <= 0)
 				break;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-22 17:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 17:55 [PATCH v2 0/4] mm,drm/i915,powerpc/spufs: vm_ops->access related fixes David Hildenbrand (Arm)
2026-11-22 17:55 ` [PATCH v2 1/4] mm: do not call vm_ops->access with FOLL_ANON David Hildenbrand (Arm)
2026-11-22 17:55 ` [PATCH v2 2/4] drm/i915: consider vm_pgoff in vm_access() David Hildenbrand (Arm)
2026-11-22 17:55 ` [PATCH v2 3/4] powerpc/spufs: consider vm_pgoff in spufs_mem_mmap_access() David Hildenbrand (Arm)
2026-11-22 17:55 ` [PATCH v2 4/4] mm: check permissions and bounds for vm_ops->access David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox