Skip to contentSkip to conversation

drm/vc4: tests: Fix UAF in the mock helpers

PATCH Applied Maxime Ripard 3 participants 4 messages Oct 24, 2023 – Oct 26, 2023

Subsystems (3)
lore
Share & tools
lore

Maxime Ripard

author maintainer
#0 lore raw

PATCH 1/1 drm/vc4: tests: Fix UAF in the mock helpers

The VC4 mock helpers allocate the CRTC, encoders and connectors using a call to kunit_kzalloc(), but the DRM device they are attache to survives for longer than the test itself which leads to use-after-frees reported by KASAN.

Switch to drmm_kzalloc to tie the lifetime of these objects to the main DRM device.

---

Cc Naresh Kamboju <[email protected]>
Cc Dan Carpenter <[email protected]>
---
 drivers/gpu/drm/vc4/tests/vc4_mock_crtc.c   | 2 +-
 drivers/gpu/drm/vc4/tests/vc4_mock_output.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/vc4/tests/vc4_mock_crtc.c +1 -1
index 5d12d7beef0e..ade3309ae042 100644
@@ -26,7 +26,7 @@ struct vc4_dummy_crtc *vc4_mock_pv(struct kunit *test,
 	struct vc4_crtc *vc4_crtc;
 	int ret;
 
-	dummy_crtc = kunit_kzalloc(test, sizeof(*dummy_crtc), GFP_KERNEL);
+	dummy_crtc = drmm_kzalloc(drm, sizeof(*dummy_crtc), GFP_KERNEL);
 	KUNIT_ASSERT_NOT_NULL(test, dummy_crtc);
 
 	vc4_crtc = &dummy_crtc->crtc;
drivers/gpu/drm/vc4/tests/vc4_mock_output.c +1 -1
index 6e11fcc9ef45..e70d7c3076ac 100644
@@ -32,7 +32,7 @@ struct vc4_dummy_output *vc4_dummy_output(struct kunit *test,
 	struct drm_encoder *enc;
 	int ret;
 
-	dummy_output = kunit_kzalloc(test, sizeof(*dummy_output), GFP_KERNEL);
+	dummy_output = drmm_kzalloc(drm, sizeof(*dummy_output), GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy_output);
 	dummy_output->encoder.type = vc4_encoder_type;
 
Signature · 1 line
2.41.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Maxime Ripard

The VC4 mock helpers allocate the CRTC, encoders and connectors using a call to kunit_kzalloc(), but the DRM device they are attache to survives for longer than the test itself which leads to use-after-frees reported by KASAN.

Quoted 7 lines

Switch to drmm_kzalloc to tie the lifetime of these objects to the main DRM device.

Best Regards,
- Maíra Canal

Quoted 36 lines

---

Cc Naresh Kamboju <[email protected]>
Cc Dan Carpenter <[email protected]>
---
  drivers/gpu/drm/vc4/tests/vc4_mock_crtc.c   | 2 +-
  drivers/gpu/drm/vc4/tests/vc4_mock_output.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/vc4/tests/vc4_mock_crtc.c +1 -1
index 5d12d7beef0e..ade3309ae042 100644
@@ -26,7 +26,7 @@ struct vc4_dummy_crtc *vc4_mock_pv(struct kunit *test,
  	struct vc4_crtc *vc4_crtc;
  	int ret;
 
-	dummy_crtc = kunit_kzalloc(test, sizeof(*dummy_crtc), GFP_KERNEL);
+	dummy_crtc = drmm_kzalloc(drm, sizeof(*dummy_crtc), GFP_KERNEL);
  	KUNIT_ASSERT_NOT_NULL(test, dummy_crtc);
 
  	vc4_crtc = &dummy_crtc->crtc;
drivers/gpu/drm/vc4/tests/vc4_mock_output.c +1 -1
index 6e11fcc9ef45..e70d7c3076ac 100644
@@ -32,7 +32,7 @@ struct vc4_dummy_output *vc4_dummy_output(struct kunit *test,
  	struct drm_encoder *enc;
  	int ret;
 
-	dummy_output = kunit_kzalloc(test, sizeof(*dummy_output), GFP_KERNEL);
+	dummy_output = drmm_kzalloc(drm, sizeof(*dummy_output), GFP_KERNEL);
  	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy_output);
  	dummy_output->encoder.type = vc4_encoder_type;
 
Tested-by
#2 lore raw
Quoted 10 lines · Maíra Canal
Quoted 8 lines · Maxime Ripard

The VC4 mock helpers allocate the CRTC, encoders and connectors using a call to kunit_kzalloc(), but the DRM device they are attache to survives for longer than the test itself which leads to use-after-frees reported by KASAN.

Switch to drmm_kzalloc to tie the lifetime of these objects to the main DRM device.

Quoted 6 lines
Quoted 4 lines

I tested this patch ontop of next-20231025 and on next-20231011 when we first saw the issue,
both kernels booted fine on a RPI4(bcm2711-rpi-4-b)

Cheers,
Anders

Quoted 40 lines

Best Regards,
- Maíra Canal

Quoted 36 lines

---

Cc Naresh Kamboju <[email protected]>
Cc Dan Carpenter <[email protected]>
---
  drivers/gpu/drm/vc4/tests/vc4_mock_crtc.c   | 2 +-
  drivers/gpu/drm/vc4/tests/vc4_mock_output.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/vc4/tests/vc4_mock_crtc.c +1 -1
index 5d12d7beef0e..ade3309ae042 100644
@@ -26,7 +26,7 @@ struct vc4_dummy_crtc *vc4_mock_pv(struct kunit *test,
      struct vc4_crtc *vc4_crtc;
      int ret;
 
-     dummy_crtc = kunit_kzalloc(test, sizeof(*dummy_crtc), GFP_KERNEL);
+     dummy_crtc = drmm_kzalloc(drm, sizeof(*dummy_crtc), GFP_KERNEL);
      KUNIT_ASSERT_NOT_NULL(test, dummy_crtc);
 
      vc4_crtc = &dummy_crtc->crtc;
drivers/gpu/drm/vc4/tests/vc4_mock_output.c +1 -1
index 6e11fcc9ef45..e70d7c3076ac 100644
@@ -32,7 +32,7 @@ struct vc4_dummy_output *vc4_dummy_output(struct kunit *test,
      struct drm_encoder *enc;
      int ret;
 
-     dummy_output = kunit_kzalloc(test, sizeof(*dummy_output), GFP_KERNEL);
+     dummy_output = drmm_kzalloc(drm, sizeof(*dummy_output), GFP_KERNEL);
      KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy_output);
      dummy_output->encoder.type = vc4_encoder_type;
 
author maintainer Applied
#3 lore raw

Re: (subset) [PATCH] drm/vc4: tests: Fix UAF in the mock helpers

In reply to Maxime Ripard
Quoted 9 lines · Maxime Ripard

The VC4 mock helpers allocate the CRTC, encoders and connectors using a call to kunit_kzalloc(), but the DRM device they are attache to survives for longer than the test itself which leads to use-after-frees reported by KASAN.

Switch to drmm_kzalloc to tie the lifetime of these objects to the main DRM device.

[...]

Applied to drm/drm-misc (drm-misc-fixes).

Thanks!
Maxime