[PATCH] drm/vc4: Prevent shader BO mappings from becoming writable

Linmao Li posted 1 patch 3 days, 23 hours ago
drivers/gpu/drm/vc4/vc4_bo.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
[PATCH] drm/vc4: Prevent shader BO mappings from becoming writable
Posted by Linmao Li 3 days, 23 hours ago
vc4_gem_object_mmap() rejects a writable mapping of a validated shader
BO, but leaves VM_MAYWRITE set.  Userspace can map the BO read-only and
then turn it writable with mprotect().

Validated shader BOs must stay read-only: the validator checks the
instructions once and the GPU trusts them afterwards.  A writable
mapping lets userspace rewrite the code after validation, bypassing the
validator.

Clear VM_MAYWRITE on the read-only path so the mapping cannot be
upgraded, as i915 already does for its read-only objects.

Fixes: 463873d57014 ("drm/vc4: Add an API for creating GPU shaders in GEM BOs.")
Cc: stable@vger.kernel.org
Reported-by: sashiko-bot@kernel.org
Closes: https://lore.kernel.org/dri-devel/20260720085554.B0AF01F000E9@smtp.kernel.org/
Signed-off-by: Linmao Li <lilinmao@kylinos.cn>
---
 drivers/gpu/drm/vc4/vc4_bo.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/vc4/vc4_bo.c b/drivers/gpu/drm/vc4/vc4_bo.c
index 2161761b1f22..5e7c46dd7823 100644
--- a/drivers/gpu/drm/vc4/vc4_bo.c
+++ b/drivers/gpu/drm/vc4/vc4_bo.c
@@ -732,9 +732,13 @@ static int vc4_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct
 {
 	struct vc4_bo *bo = to_vc4_bo(obj);
 
-	if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
-		DRM_DEBUG("mmapping of shader BOs for writing not allowed.\n");
-		return -EINVAL;
+	if (bo->validated_shader) {
+		if (vma->vm_flags & VM_WRITE) {
+			DRM_DEBUG("mmapping of shader BOs for writing not allowed.\n");
+			return -EINVAL;
+		}
+
+		vm_flags_clear(vma, VM_MAYWRITE);
 	}
 
 	mutex_lock(&bo->madv_lock);
-- 
2.25.1
Re: [PATCH] drm/vc4: Prevent shader BO mappings from becoming writable
Posted by Maíra Canal 3 days, 12 hours ago
On Tue, 21 Jul 2026 09:15:58 +0800, Linmao Li wrote:
> vc4_gem_object_mmap() rejects a writable mapping of a validated shader
> BO, but leaves VM_MAYWRITE set.  Userspace can map the BO read-only and
> then turn it writable with mprotect().
> 
> Validated shader BOs must stay read-only: the validator checks the
> instructions once and the GPU trusts them afterwards.  A writable
> mapping lets userspace rewrite the code after validation, bypassing the
> validator.
> 
> [...]

Applied, thanks!

[1/1] drm/vc4: Prevent shader BO mappings from becoming writable
      commit: 0c9e6367639548307d3f578f6943ce72c9d39087

Best regards,
- Maíra