drm/amd/display: avoid divide-by-zero in __is_lut_linear()
Harry Wentland
maintainerPATCH 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.
--- 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
@@ -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
Harry Wentland
maintainerPATCH 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.
--- 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
@@ -1495,6 +1495,13 @@ __set_dm_plane_degamma(struct drm_plane_state *plane_state, degamma_lut =__extract_blob_lut (dm_plane_state->degamma_lut, °amma_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
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 iteratingMAX_COLOR_LUT_ENTRIESentries regardless of the actual LUT size.A malformed
AMD_PLANE_DEGAMMA_LUTblob (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 matchMAX_COLOR_LUT_ENTRIES, mirroring the invariant the code already asserts a few lines below (and which the CRTC path enforces).The
AMD_PLANE_DEGAMMA_LUTproperty is only exposed on builds withAMD_PRIVATE_COLORdefined.Fixes 980f8710075a ("drm/amd/display: add plane degamma TF and LUT support")Signed-off-by Harry Wentland <[email protected]>--- 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
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, °amma_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
-0has_degamma_lut = degamma_lut && !__is_lut_linear (degamma_lut, degamma_size);
Quoted 21 lines · Harry Wentland
__is_lut_linear()computes the expected value of each entry withexpected = 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")Signed-off-by Harry Wentland<[email protected]>--- 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
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
-0for (i =0 ; i < size; i++) {/* All color values should equal */ if ((lut[i].red != lut[i].green) || (lut[i].green != lut[i].blue))