Skip to contentSkip to conversation

gpu/drm/drm_framebuffer.c: Use Macro instead of actual number.

PATCH In review Peng Hao 3 participants 4 messages Nov 2, 2023

Subsystems (2)
lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 gpu/drm/drm_framebuffer.c: Use Macro instead of actual number.

Use Macro DRM_FORMAT_MAX_PLANES instead of 4, to improve modifiability.

---
 drivers/gpu/drm/drm_framebuffer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_framebuffer.c +1 -1
index 2dd97473ca10..bf283dae9090 100644
@@ -254,7 +254,7 @@ static int framebuffer_check(struct drm_device *dev,
 		}
 	}
 
-	for (i = info->num_planes; i < 4; i++) {
+	for (i = info->num_planes; i < DRM_FORMAT_MAX_PLANES; i++) {
 		if (r->modifier[i]) {
 			drm_dbg_kms(dev, "non-zero modifier for unused plane %d\n", i);
 			return -EINVAL;
Signature · 1 line
2.37.1
#2 lore raw
In reply to Peng Hao

Hi

Am 02.11.23 um 03:29 schrieb Peng Hao:

Quoted 12 lines

Use Macro DRM_FORMAT_MAX_PLANES instead of 4, to improve modifiability.

---
  drivers/gpu/drm/drm_framebuffer.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 2dd97473ca10..bf283dae9090 100644

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 257

Quoted 5 lines
drivers/gpu/drm/drm_framebuffer.c +1 -1
@@ -254,7 +254,7 @@ static int framebuffer_check(struct drm_device *dev,
  		}
  	}
 
-	for (i = info->num_planes; i < 4; i++) {
+	for (i = info->num_planes; i < DRM_FORMAT_MAX_PLANES; i++) {

This change makes the code more fragile. '4' is a fixed constant in the UAPI struct, while DRM_FORMAT_MAX_PLANES is an internal constant. I agree that both should reasonably have the same value. But (potentially) changing the value of DRM_FORMAT_MAX_PLANES will break these loops with a possible OOB access.

To make make this code more robust, it might be better to rewrite the tests like this

for (i = num_planes; i < ARRAY_SIZE(r->modifier); +i) {
	// the test for modifier[i]
}

if (r->flags & DRM_MODE_FB_MODIFIERS) {

 	for (i < ARRAY_SIZE(handles)) {
		// test for handles[i]
	}
 	for (i < ARRAY_SIZE(pitches)) {
		// test for pitches[i]
	}
 	for (i < ARRAY_SIZE(offsets)) {
		// test for offsets[i]
	}
}

Best regards
Thomas

Quoted 3 lines
drivers/gpu/drm/drm_framebuffer.c +0 -0
  		if (r->modifier[i]) {
  			drm_dbg_kms(dev, "non-zero modifier for unused plane %d\n", i);
  			return -EINVAL;
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author
#3 lore raw

Good idea!

Quoted 65 lines
From: "Thomas Zimmermann"<[email protected]>
Date:  Thu, Nov 2, 2023, 21:27
Subject:  Re: [PATCH] gpu/drm/drm_framebuffer.c: Use Macro instead of actual number.
To: "Peng Hao"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>

Cc: <[email protected]>, <[email protected]> Hi

Am 02.11.23 um 03:29 schrieb Peng Hao:

Quoted 12 lines

Use Macro DRM_FORMAT_MAX_PLANES instead of 4, to improve modifiability.

---
  drivers/gpu/drm/drm_framebuffer.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 2dd97473ca10..bf283dae9090 100644

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 257

Quoted 5 lines
drivers/gpu/drm/drm_framebuffer.c +1 -1
@@ -254,7 +254,7 @@ static int framebuffer_check(struct drm_device *dev,
  		}
  	}
 
-	for (i = info->num_planes; i < 4; i++) {
+	for (i = info->num_planes; i < DRM_FORMAT_MAX_PLANES; i++) {

This change makes the code more fragile. '4' is a fixed constant in the UAPI struct, while DRM_FORMAT_MAX_PLANES is an internal constant. I agree that both should reasonably have the same value. But (potentially) changing the value of DRM_FORMAT_MAX_PLANES will break these loops with a possible OOB access.

To make make this code more robust, it might be better to rewrite the tests like this

for (i = num_planes; i < ARRAY_SIZE(r->modifier); +i) {
	// the test for modifier[i]
}

if (r->flags & DRM_MODE_FB_MODIFIERS) {

  	for (i < ARRAY_SIZE(handles)) {
		// test for handles[i]
	}
  	for (i < ARRAY_SIZE(pitches)) {
		// test for pitches[i]
	}
  	for (i < ARRAY_SIZE(offsets)) {
		// test for offsets[i]
	}
}

Best regards
Thomas

Quoted 3 lines
drivers/gpu/drm/drm_framebuffer.c +0 -0
  		if (r->modifier[i]) {
  			drm_dbg_kms(dev, "non-zero modifier for unused plane %d\n", i);
  			return -EINVAL;
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)