Skip to contentSkip to conversation

drm/amd/display: avoid divide-by-zero in __is_lut_linear()

PATCH In review Harry Wentland 2 participants 4 messages Aug 4 – Aug 12

Subsystems (4)
lore
Share & tools
lore

Harry Wentland

author maintainer
#0 lore raw

PATCH 1/2 drm/amd/display: avoid divide-by-zero in __is_lut_linear()

__is_lut_linear() computes the expected value of each entry with

	expected = i * MAX_DRM_LUT_VALUE / (size - 1);

If it is ever called with a single-entry LUT, size - 1 is zero and the kernel takes a divide error (#DE). A LUT with fewer than two entries cannot describe a linear mapping anyway, so return false early instead of dividing by zero.

Fixes 086247a4b2fb ("drm/amd/display: Use 4096 lut entries")
---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +6 -0
index 7b68c6846039..26e5c89375a5 100644
@@ -471,6 +471,12 @@ bool __is_lut_linear(const struct drm_color_lut *lut, uint32_t size)
 	uint32_t expected;
 	int delta;
 
+	/* A LUT with fewer than two entries can't be interpolated and would
+	 * divide by zero below (size - 1); it can't be treated as linear.
+	 */
+	if (size < 2)
+		return false;
+
 	for (i = 0; i < size; i++) {
 		/* All color values should equal */
 		if ((lut[i].red != lut[i].green) || (lut[i].green != lut[i].blue))
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/2 drm/amd/display: validate plane degamma LUT size for private color prop

Unlike the CRTC degamma path, which is guarded by amdgpu_dm_verify_lut_sizes(), the per-plane degamma LUT size was never validated before use. __set_dm_plane_degamma() passed the user-supplied size straight into __is_lut_linear() and, for a non-linear LUT, into __set_input_tf() -> __drm_lut_to_dc_gamma(), the latter always iterating MAX_COLOR_LUT_ENTRIES entries regardless of the actual LUT size.

A malformed AMD_PLANE_DEGAMMA_LUT blob (e.g. a single entry) could thus trigger a divide-by-zero in __is_lut_linear() or an out-of-bounds read in __drm_lut_to_dc_gamma(). Reject any plane degamma LUT whose size does not match MAX_COLOR_LUT_ENTRIES, mirroring the invariant the code already asserts a few lines below (and which the CRTC path enforces).

The AMD_PLANE_DEGAMMA_LUT property is only exposed on builds with AMD_PRIVATE_COLOR defined.

Fixes 980f8710075a ("drm/amd/display: add plane degamma TF and LUT support")
---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +7 -0
index 26e5c89375a5..d55dc06167a8 100644
@@ -1495,6 +1495,13 @@ __set_dm_plane_degamma(struct drm_plane_state *plane_state,
 	degamma_lut = __extract_blob_lut(dm_plane_state->degamma_lut,
 					 &degamma_size);
 
+	if (degamma_lut && degamma_size != MAX_COLOR_LUT_ENTRIES) {
+		drm_dbg(plane_state->state->dev,
+			"Invalid Plane Degamma LUT size. Should be %u but got %u.\n",
+			MAX_COLOR_LUT_ENTRIES, degamma_size);
+		return -EINVAL;
+	}
+
 	has_degamma_lut = degamma_lut &&
 			  !__is_lut_linear(degamma_lut, degamma_size);
 
Signature · 1 line
2.55.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/amd/display: validate plane degamma LUT size for private color prop

Quoted 28 lines · Harry Wentland

Unlike the CRTC degamma path, which is guarded by amdgpu_dm_verify_lut_sizes(), the per-plane degamma LUT size was never validated before use. __set_dm_plane_degamma() passed the user-supplied size straight into __is_lut_linear() and, for a non-linear LUT, into __set_input_tf() -> __drm_lut_to_dc_gamma(), the latter always iterating MAX_COLOR_LUT_ENTRIES entries regardless of the actual LUT size.

A malformed AMD_PLANE_DEGAMMA_LUT blob (e.g. a single entry) could thus trigger a divide-by-zero in __is_lut_linear() or an out-of-bounds read in __drm_lut_to_dc_gamma(). Reject any plane degamma LUT whose size does not match MAX_COLOR_LUT_ENTRIES, mirroring the invariant the code already asserts a few lines below (and which the CRTC path enforces).

The AMD_PLANE_DEGAMMA_LUT property is only exposed on builds with AMD_PRIVATE_COLOR defined.

Fixes 980f8710075a ("drm/amd/display: add plane degamma TF and LUT support")
---
  drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +0 -0
index 26e5c89375a5..d55dc06167a8 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c __set_dm_plane_degamma(struct drm_plane_state *plane_state, · line 1504

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +7 -0
@@ -1495,6 +1495,13 @@ __set_dm_plane_degamma(struct drm_plane_state *plane_state,
  	degamma_lut = __extract_blob_lut(dm_plane_state->degamma_lut,
  					 &degamma_size);
 
+	if (degamma_lut && degamma_size != MAX_COLOR_LUT_ENTRIES) {
+		drm_dbg(plane_state->state->dev,
+			"Invalid Plane Degamma LUT size. Should be %u but got %u.\n",
+			MAX_COLOR_LUT_ENTRIES, degamma_size);
+		return -EINVAL;
+	}
+

LGTM

Thanks!

Quoted 3 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +0 -0
  	has_degamma_lut = degamma_lut &&
  			  !__is_lut_linear(degamma_lut, degamma_size);
 
Reviewed-by on v1 1/2
#3 lore raw
In reply to Harry Wentland
Quoted 21 lines · Harry Wentland

__is_lut_linear() computes the expected value of each entry with

	expected = i * MAX_DRM_LUT_VALUE / (size - 1);

If it is ever called with a single-entry LUT, size - 1 is zero and the kernel takes a divide error (#DE). A LUT with fewer than two entries cannot describe a linear mapping anyway, so return false early instead of dividing by zero.

Fixes 086247a4b2fb ("drm/amd/display: Use 4096 lut entries")
---
  drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +0 -0
index 7b68c6846039..26e5c89375a5 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c bool __is_lut_linear(const struct drm_color_lut *lut, uint32_t size) · line 479

Quoted 9 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +6 -0
@@ -471,6 +471,12 @@ bool __is_lut_linear(const struct drm_color_lut *lut, uint32_t size)
  	uint32_t expected;
  	int delta;
 
+	/* A LUT with fewer than two entries can't be interpolated and would
+	 * divide by zero below (size - 1); it can't be treated as linear.
+	 */
+	if (size < 2)
+		return false;
+

LGTM

Quoted 3 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +0 -0
  	for (i = 0; i < size; i++) {
  		/* All color values should equal */
  		if ((lut[i].red != lut[i].green) || (lut[i].green != lut[i].blue))