Skip to contentSkip to conversation

drm: tests: Fix a buffer overflow in format_helper_test

PATCH In review David Gow 4 participants 7 messages Oct 19, 2022 – Oct 21, 2022

Subsystems (2)
lore
Share & tools
lore

David Gow

author
#0 lore raw

PATCH 1/1 drm: tests: Fix a buffer overflow in format_helper_test

The xrgb2101010 format conversion test (unlike for other formats) does an endianness conversion on the results. However, it always converts TEST_BUF_SIZE 32-bit integers, which results in reading from (and writing to) more memory than in present in the result buffer. Instead, use the buffer size, divided by sizeof(u32).

The issue could be reproduced with KASAN:

./tools/testing/kunit/kunit.py run --kunitconfig drivers/gpu/drm/tests \
	--kconfig_add CONFIG_KASAN=y --kconfig_add CONFIG_KASAN_VMALLOC=y \
	--kconfig_add CONFIG_KASAN_KUNIT_TEST=y \
	drm_format_helper_test.*xrgb2101010
Fixes 453114319699 ("drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_xrgb2101010()")

---

This is a fix for the issue reported here:
https://lore.kernel.org/dri-devel/CA+G9fYsuc9G+RO81E=vHMqxYStsmLURLdOB0NF26kJ1=K8pRZA@mail.gmail.com/

Note that it may conflict with the KUNIT_EXPECT_MEMEQ() series here:
https://lore.kernel.org/linux-kselftest/[email protected]/

Cheers,
-- David

---
 drivers/gpu/drm/tests/drm_format_helper_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -1
index 8d86c250c2ec..2191e57f2297 100644
@@ -438,7 +438,7 @@ static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test)
 	iosys_map_set_vaddr(&src, xrgb8888);
 
 	drm_fb_xrgb8888_to_xrgb2101010(&dst, &result->dst_pitch, &src, &fb, &params->clip);
-	buf = le32buf_to_cpu(test, buf, TEST_BUF_SIZE);
+	buf = le32buf_to_cpu(test, buf, dst_size / sizeof(u32));
 	KUNIT_EXPECT_EQ(test, memcmp(buf, result->expected, dst_size), 0);
 }
 
Signature · 1 line
2.38.0.413.g74048e4d9e-goog
Reviewed-by on v1 1/1
#1 lore raw

[cc Javier]

Hi David,

Quoted 7 lines · David Gow

The xrgb2101010 format conversion test (unlike for other formats) does an endianness conversion on the results. However, it always converts TEST_BUF_SIZE 32-bit integers, which results in reading from (and writing to) more memory than in present in the result buffer. Instead, use the buffer size, divided by sizeof(u32).

The issue could be reproduced with KASAN:

Quoted 8 lines
./tools/testing/kunit/kunit.py run --kunitconfig drivers/gpu/drm/tests \
	--kconfig_add CONFIG_KASAN=y --kconfig_add CONFIG_KASAN_VMALLOC=y \
	--kconfig_add CONFIG_KASAN_KUNIT_TEST=y \
	drm_format_helper_test.*xrgb2101010
Fixes 453114319699 ("drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_xrgb2101010()")
Quoted 7 lines

---

This is a fix for the issue reported here:
https://lore.kernel.org/dri-devel/CA+G9fYsuc9G+RO81E=vHMqxYStsmLURLdOB0NF26kJ1=K8pRZA@mail.gmail.com/

Note that it may conflict with the KUNIT_EXPECT_MEMEQ() series here:
https://lore.kernel.org/linux-kselftest/[email protected]/

I believe this patch will go to the drm-misc-fixes tree, so I can rebase the series (and address the format issues) as this patch hits mainline.

Best Regards,
- Maíra Canal

Quoted 21 lines

Cheers,
-- David

---
 drivers/gpu/drm/tests/drm_format_helper_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -1
index 8d86c250c2ec..2191e57f2297 100644
@@ -438,7 +438,7 @@ static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test)
 	iosys_map_set_vaddr(&src, xrgb8888);
 
 	drm_fb_xrgb8888_to_xrgb2101010(&dst, &result->dst_pitch, &src, &fb, &params->clip);
-	buf = le32buf_to_cpu(test, buf, TEST_BUF_SIZE);
+	buf = le32buf_to_cpu(test, buf, dst_size / sizeof(u32));
 	KUNIT_EXPECT_EQ(test, memcmp(buf, result->expected, dst_size), 0);
 }
 
#2 lore raw
Quoted 17 lines · Maíra Canal

[cc Javier]

Hi David,

Quoted 12 lines · David Gow

The xrgb2101010 format conversion test (unlike for other formats) does an endianness conversion on the results. However, it always converts TEST_BUF_SIZE 32-bit integers, which results in reading from (and writing to) more memory than in present in the result buffer. Instead, use the buffer size, divided by sizeof(u32).

The issue could be reproduced with KASAN:

./tools/testing/kunit/kunit.py run --kunitconfig drivers/gpu/drm/tests \
	--kconfig_add CONFIG_KASAN=y --kconfig_add CONFIG_KASAN_VMALLOC=y \
	--kconfig_add CONFIG_KASAN_KUNIT_TEST=y \
	drm_format_helper_test.*xrgb2101010
Quoted 6 lines
Quoted 3 lines
Fixes 453114319699 ("drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_xrgb2101010()")

The fix makes sense to me as well,

Quoted 11 lines
Quoted 7 lines

---

This is a fix for the issue reported here:
https://lore.kernel.org/dri-devel/CA+G9fYsuc9G+RO81E=vHMqxYStsmLURLdOB0NF26kJ1=K8pRZA@mail.gmail.com/

Note that it may conflict with the KUNIT_EXPECT_MEMEQ() series here:
https://lore.kernel.org/linux-kselftest/[email protected]/

I believe this patch will go to the drm-misc-fixes tree, so I can rebase the series (and address the format issues) as this patch hits mainline.

Awesome. I assume you are pushing to drm-misc-fixes then?

Thanks a lot David and Maíra for taking care of this!

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
author
#3 lore raw
In reply to Maíra Canal
Quoted 18 lines · Maíra Canal

[cc Javier]

Hi David,

Quoted 12 lines · David Gow

The xrgb2101010 format conversion test (unlike for other formats) does an endianness conversion on the results. However, it always converts TEST_BUF_SIZE 32-bit integers, which results in reading from (and writing to) more memory than in present in the result buffer. Instead, use the buffer size, divided by sizeof(u32).

The issue could be reproduced with KASAN:

./tools/testing/kunit/kunit.py run --kunitconfig drivers/gpu/drm/tests \
      --kconfig_add CONFIG_KASAN=y --kconfig_add CONFIG_KASAN_VMALLOC=y \
      --kconfig_add CONFIG_KASAN_KUNIT_TEST=y \
      drm_format_helper_test.*xrgb2101010
Quoted 6 lines
Quoted 3 lines
Fixes 453114319699 ("drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_xrgb2101010()")

Thanks a lot, Maíra!

Quoted 10 lines
Quoted 7 lines

---

This is a fix for the issue reported here:
https://lore.kernel.org/dri-devel/CA+G9fYsuc9G+RO81E=vHMqxYStsmLURLdOB0NF26kJ1=K8pRZA@mail.gmail.com/

Note that it may conflict with the KUNIT_EXPECT_MEMEQ() series here:
https://lore.kernel.org/linux-kselftest/[email protected]/

I believe this patch will go to the drm-misc-fixes tree, so I can rebase the series (and address the format issues) as this patch hits mainline.

We're probably going to want the KUNIT_EXPECT_MEMEQ() merged in the kselftest/kunit tree reasonably soon, as there's a few things we'd like to use it for that we're hoping to get into 6.2.

Given that any conflict is only likely to affect context lines, it hopefully won't be too much of a problem to resolve it when everything gets merged, though.

Cheers,
-- David

Reviewed-by on v1 1/1
#4 lore raw
In reply to David Gow
Quoted 35 lines · David Gow

The xrgb2101010 format conversion test (unlike for other formats) does an endianness conversion on the results. However, it always converts TEST_BUF_SIZE 32-bit integers, which results in reading from (and writing to) more memory than in present in the result buffer. Instead, use the buffer size, divided by sizeof(u32).

The issue could be reproduced with KASAN:

./tools/testing/kunit/kunit.py run --kunitconfig drivers/gpu/drm/tests \
	--kconfig_add CONFIG_KASAN=y --kconfig_add CONFIG_KASAN_VMALLOC=y \
	--kconfig_add CONFIG_KASAN_KUNIT_TEST=y \
	drm_format_helper_test.*xrgb2101010
Fixes 453114319699 ("drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_xrgb2101010()")

---

This is a fix for the issue reported here:
https://lore.kernel.org/dri-devel/CA+G9fYsuc9G+RO81E=vHMqxYStsmLURLdOB0NF26kJ1=K8pRZA@mail.gmail.com/

Note that it may conflict with the KUNIT_EXPECT_MEMEQ() series here:
https://lore.kernel.org/linux-kselftest/[email protected]/

Cheers,
-- David

---
 drivers/gpu/drm/tests/drm_format_helper_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +0 -0
index 8d86c250c2ec..2191e57f2297 100644

drivers/gpu/drm/tests/drm_format_helper_test.c static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test) · line 443

Quoted 7 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -1
@@ -438,7 +438,7 @@ static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test)
 	iosys_map_set_vaddr(&src, xrgb8888);
 
 	drm_fb_xrgb8888_to_xrgb2101010(&dst, &result->dst_pitch, &src, &fb, &params->clip);
-	buf = le32buf_to_cpu(test, buf, TEST_BUF_SIZE);
+	buf = le32buf_to_cpu(test, buf, dst_size / sizeof(u32));
 	KUNIT_EXPECT_EQ(test, memcmp(buf, result->expected, dst_size), 0);
 }

Thanks a lot for fixing this bug David, I just tested it and worked as expected.

Do you think that we should update the other calls to le32buf_to_cpu() to follow a similar approach?

Regardless of a possible follow up patch:

Jose

Quoted 4 lines
Signature · 2 lines
-- 
2.38.0.413.g74048e4d9e-goog
#5 lore raw

On 10/19/22 19:29, José Expósito wrote:

[...]

Quoted 10 lines
Quoted 10 lines
 drivers/gpu/drm/tests/drm_format_helper_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +0 -0
index 8d86c250c2ec..2191e57f2297 100644
@@ -438,7 +438,7 @@ static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test)
 	iosys_map_set_vaddr(&src, xrgb8888);
 
Quoted 9 lines

drivers/gpu/drm/tests/drm_format_helper_test.c static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test) · line 443

Quoted 5 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -1
 	drm_fb_xrgb8888_to_xrgb2101010(&dst, &result->dst_pitch, &src, &fb, &params->clip);
-	buf = le32buf_to_cpu(test, buf, TEST_BUF_SIZE);
+	buf = le32buf_to_cpu(test, buf, dst_size / sizeof(u32));
 	KUNIT_EXPECT_EQ(test, memcmp(buf, result->expected, dst_size), 0);
 }

Thanks a lot for fixing this bug David, I just tested it and worked as expected.

I've pushed this to drm-misc (drm-misc-fixes) now. Thanks!

Quoted 3 lines

Do you think that we should update the other calls to le32buf_to_cpu() to follow a similar approach?

Feel free to post follow-up patches if you think that other changes are needed.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
author
#6 lore raw
Quoted 25 lines · Javier Martinez Canillas

On 10/19/22 19:29, José Expósito wrote:

[...]

Quoted 10 lines
Quoted 10 lines
 drivers/gpu/drm/tests/drm_format_helper_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +0 -0
index 8d86c250c2ec..2191e57f2297 100644
@@ -438,7 +438,7 @@ static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test)
     iosys_map_set_vaddr(&src, xrgb8888);
 
Quoted 9 lines

drivers/gpu/drm/tests/drm_format_helper_test.c static void drm_test_fb_xrgb8888_to_xrgb2101010(struct kunit *test) · line 443

Quoted 5 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -1
     drm_fb_xrgb8888_to_xrgb2101010(&dst, &result->dst_pitch, &src, &fb, &params->clip);
-    buf = le32buf_to_cpu(test, buf, TEST_BUF_SIZE);
+    buf = le32buf_to_cpu(test, buf, dst_size / sizeof(u32));
     KUNIT_EXPECT_EQ(test, memcmp(buf, result->expected, dst_size), 0);
 }

Thanks a lot for fixing this bug David, I just tested it and worked as expected.

Quoted 2 lines

I've pushed this to drm-misc (drm-misc-fixes) now. Thanks!

Thanks very much!

Quoted 6 lines
Quoted 3 lines

Do you think that we should update the other calls to le32buf_to_cpu() to follow a similar approach?

Feel free to post follow-up patches if you think that other changes are needed.

I _think_ this should be the only actually necessary change. Maybe there's some cleanup of the way TEST_BUF_SIZE is used, or the sparse annotations for endianness, but I'd need to look into exactly what the various drm_format definitions are in more detail to know for sure.

Cheers,
-- David