drm/shmem-helper: Fix BUG_ON() on mmap(PROT_WRITE, MAP_PRIVATE)
Subsystems (3)
PATCH 1/1 drm/shmem-helper: Fix BUG_ON() on mmap(PROT_WRITE, MAP_PRIVATE)
From: "Wachowski, Karol" <[email protected]>
Lack of check for copy-on-write (COW) mapping in drm_gem_shmem_mmap allows users to call mmap with PROT_WRITE and MAP_PRIVATE flag causing a kernel panic due to BUG_ON in vmf_insert_pfn_prot:BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Return -EINVAL early if COW mapping is detected.
This bug affects all drm drivers using default shmem helpers. It can be reproduced by this simple example:
void *ptr = mmap(0, size, PROT_WRITE, MAP_PRIVATE, fd, mmap_offset); ptr[0] = 0;
--- drivers/gpu/drm/drm_gem_shmem_helper.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/gpu/drm/drm_gem_shmem_helper.c +3 -0
@@ -611,6 +611,9 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_structreturn ret; } +if (is_cow_mapping (vma->vm_flags)) +return -EINVAL ; +dma_resv_lock (shmem->base.resv,NULL ); ret =drm_gem_shmem_get_pages (shmem);dma_resv_unlock (shmem->base.resv);
Signature · 1 line
2.45.1
Quoted 14 lines · Jacek Lawrynowicz
From: "Wachowski, Karol" <[email protected]>
Lack of check for copy-on-write (COW) mapping in
drm_gem_shmem_mmapallows users to call mmap withPROT_WRITEandMAP_PRIVATEflag causing a kernel panic due toBUG_ONinvmf_insert_pfn_prot:BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));Return
-EINVALearly if COW mapping is detected.This bug affects all drm drivers using default shmem helpers. It can be reproduced by this simple example:
void *ptr =mmap(0, size, PROT_WRITE, MAP_PRIVATE, fd, mmap_offset); ptr[0] = 0;
Quoted 13 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")Cc Noralf Trønnes <[email protected]>Cc Eric Anholt <[email protected]>Cc Rob Herring <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Cc <[email protected]> # v5.2+Signed-off-by Wachowski, Karol <[email protected]>Signed-off-by Jacek Lawrynowicz <[email protected]>
Excellent catch!
I reviewed the other helpers, and ttm/vram helpers already block this with the check in ttm_bo_mmap_obj.
But the dma helpers does not, because the remap_pfn_range that underlies the various dma_mmap* function (at least on most platforms) allows some limited use of cow. But it makes no sense at all to all that only for gpu buffer objects backed by specific allocators.
Would you be up for the 2nd patch that also adds this check to drm_gem_dma_mmap, so that we have a consistent uapi?
I'll go ahead and apply this one to drm-misc-fixes meanwhile.
Thanks, Sima
Quoted 21 lines
--- drivers/gpu/drm/drm_gem_shmem_helper.c | 3 +++ 1 file changed, 3 insertions(+)drivers/gpu/drm/drm_gem_shmem_helper.c +3
-0@@ -611,6 +611,9 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_structreturn ret; } +if (is_cow_mapping (vma->vm_flags)) +return -EINVAL ; +dma_resv_lock (shmem->base.resv,NULL ); ret =drm_gem_shmem_get_pages (shmem);dma_resv_unlock (shmem->base.resv);Signature · 1 line
2.45.1
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Quoted 42 lines · Daniel Vetter
Quoted 14 lines · Jacek Lawrynowicz
From: "Wachowski, Karol" <[email protected]>
Lack of check for copy-on-write (COW) mapping in
drm_gem_shmem_mmapallows users to call mmap withPROT_WRITEandMAP_PRIVATEflag causing a kernel panic due toBUG_ONinvmf_insert_pfn_prot:BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));Return
-EINVALearly if COW mapping is detected.This bug affects all drm drivers using default shmem helpers. It can be reproduced by this simple example:
void *ptr =mmap(0, size, PROT_WRITE, MAP_PRIVATE, fd, mmap_offset); ptr[0] = 0;Quoted 13 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")Cc Noralf Trønnes <[email protected]>Cc Eric Anholt <[email protected]>Cc Rob Herring <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Cc <[email protected]> # v5.2+Signed-off-by Wachowski, Karol <[email protected]>Signed-off-by Jacek Lawrynowicz <[email protected]>Excellent catch!
Reviewed-by Daniel Vetter <[email protected]>I reviewed the other helpers, and ttm/vram helpers already block this with the check in
ttm_bo_mmap_obj.But the dma helpers does not, because the
remap_pfn_rangethat underlies the various dma_mmap* function (at least on most platforms) allows some limited use of cow. But it makes no sense at all to all that only for gpu buffer objects backed by specific allocators.
Quoted 4 lines
Would you be up for the 2nd patch that also adds this check to
drm_gem_dma_mmap, so that we have a consistent uapi?I'll go ahead and apply this one to drm-misc-fixes meanwhile.
Forgot to add: A testcase in igt would also be really lovely.
https://dri.freedesktop.org/docs/drm/gpu/drm-uapi.html#validating-changes-with-igt
-Sima
Quoted 29 lines
Thanks, Sima
Quoted 19 lines
--- drivers/gpu/drm/drm_gem_shmem_helper.c | 3 +++ 1 file changed, 3 insertions(+)drivers/gpu/drm/drm_gem_shmem_helper.c +3
-1@@ -611,6 +611,9 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_structreturn ret; } +if (is_cow_mapping (vma->vm_flags)) +return -EINVAL ; +dma_resv_lock (shmem->base.resv,NULL ); ret =drm_gem_shmem_get_pages (shmem);dma_resv_unlock (shmem->base.resv); --Quoted 2 lines
2.45.1
--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Hi,
Quoted 46 lines · Daniel Vetter
Quoted 45 lines · Daniel Vetter
Quoted 14 lines · Jacek Lawrynowicz
From: "Wachowski, Karol" <[email protected]>
Lack of check for copy-on-write (COW) mapping in
drm_gem_shmem_mmapallows users to call mmap withPROT_WRITEandMAP_PRIVATEflag causing a kernel panic due toBUG_ONinvmf_insert_pfn_prot:BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));Return
-EINVALearly if COW mapping is detected.This bug affects all drm drivers using default shmem helpers. It can be reproduced by this simple example:
void *ptr =mmap(0, size, PROT_WRITE, MAP_PRIVATE, fd, mmap_offset); ptr[0] = 0;Quoted 13 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")Cc Noralf Trønnes <[email protected]>Cc Eric Anholt <[email protected]>Cc Rob Herring <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Cc <[email protected]> # v5.2+Signed-off-by Wachowski, Karol <[email protected]>Signed-off-by Jacek Lawrynowicz <[email protected]>Excellent catch!
Reviewed-by Daniel Vetter <[email protected]>I reviewed the other helpers, and ttm/vram helpers already block this with the check in
ttm_bo_mmap_obj.But the dma helpers does not, because the
remap_pfn_rangethat underlies the various dma_mmap* function (at least on most platforms) allows some limited use of cow. But it makes no sense at all to all that only for gpu buffer objects backed by specific allocators.Would you be up for the 2nd patch that also adds this check to
drm_gem_dma_mmap, so that we have a consistent uapi?
Quoted 6 lines
Quoted 1 line
I'll go ahead and apply this one to drm-misc-fixes meanwhile.
Forgot to add: A testcase in igt would also be really lovely.
https://dri.freedesktop.org/docs/drm/gpu/drm-uapi.html#validating-changes-with-igt
-Sima
OK, we will take a look at the test case.
We have no easy way to test dma helpers, so it would be best if someone using them could make a fix.
Regards,
Jacek