Skip to contentSkip to conversation

drm/bridge: ti-sn65dsi86: Avoid possible buffer overflow

PATCH Superseded Su Hui 2 participants 3 messages Jun 7, 2023 – Jun 8, 2023

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Su Hui

author
#0 lore raw

PATCH v2 1/1 drm/bridge: ti-sn65dsi86: Avoid possible buffer overflow

Smatch error:buffer overflow 'ti_sn_bridge_refclk_lut' 5 <= 5.

Fixes cea86c5bb442 ("drm/bridge: ti-sn65dsi86: Implement the pwm_chip")
---
 drivers/gpu/drm/bridge/ti-sn65dsi86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/bridge/ti-sn65dsi86.c +1 -1
index 7a748785c545..bb88406495e9 100644
@@ -305,7 +305,7 @@ static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata)
 	 * The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,
 	 * regardless of its actual sourcing.
 	 */
-	pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];
+	pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i < refclk_lut_size ? i : 1];
 }
 
 static void ti_sn65dsi86_enable_comms(struct ti_sn65dsi86 *pdata)
Signature · 1 line
2.30.2
reviewer
#1 lore raw

Hi,

Quoted 14 lines · Su Hui

Smatch error:buffer overflow 'ti_sn_bridge_refclk_lut' 5 <= 5.

Fixes cea86c5bb442 ("drm/bridge: ti-sn65dsi86: Implement the pwm_chip")
---
 drivers/gpu/drm/bridge/ti-sn65dsi86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/bridge/ti-sn65dsi86.c +0 -0
index 7a748785c545..bb88406495e9 100644

drivers/gpu/drm/bridge/ti-sn65dsi86.c static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata) · line 308

Quoted 5 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +1 -1
@@ -305,7 +305,7 @@ static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata)
         * The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,
         * regardless of its actual sourcing.
         */
-       pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];
+       pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i < refclk_lut_size ? i : 1];

This looks more correct, but it really needs a comment since it's totally not obviously what you're doing here. IMO the best solution here is to update "i" right after the for loop and have a comment about the datasheet saying that "1" is the default rate so we'll fall back to that if we couldn't find a match. Moving it to right after the for loop will change the value written into the registers, but that's fine and makes it clearer what's happening.

-Doug

Quoted 25 lines · Su Hui

Smatch error:buffer overflow 'ti_sn_bridge_refclk_lut' 5 <= 5.

Fixes cea86c5bb442 ("drm/bridge: ti-sn65dsi86: Implement the pwm_chip")
---
 drivers/gpu/drm/bridge/ti-sn65dsi86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/bridge/ti-sn65dsi86.c +1 -2
index 7a748785c545..bb88406495e9 100644
@@ -305,7 +305,7 @@ static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata)
         * The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,
         * regardless of its actual sourcing.
         */
-       pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];
+       pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i < refclk_lut_size ? i : 1];
 }
 
 static void ti_sn65dsi86_enable_comms(struct ti_sn65dsi86 *pdata)
--

2.30.2

author
#2 lore raw
Quoted 20 lines · Doug Anderson

Hi,

Quoted 17 lines · Su Hui

Smatch error:buffer overflow 'ti_sn_bridge_refclk_lut' 5 <= 5.

Fixes cea86c5bb442 ("drm/bridge: ti-sn65dsi86: Implement the pwm_chip")
---
  drivers/gpu/drm/bridge/ti-sn65dsi86.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/bridge/ti-sn65dsi86.c +0 -1
index 7a748785c545..bb88406495e9 100644
@@ -305,7 +305,7 @@ static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata)
          * The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,
          * regardless of its actual sourcing.
          */
-       pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];
Quoted 8 lines

drivers/gpu/drm/bridge/ti-sn65dsi86.c static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata) · line 308

Quoted 1 line
drivers/gpu/drm/bridge/ti-sn65dsi86.c +1 -0
+       pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i < refclk_lut_size ? i : 1];

This looks more correct, but it really needs a comment since it's totally not obviously what you're doing here. IMO the best solution here is to update "i" right after the for loop and have a comment about the datasheet saying that "1" is the default rate so we'll fall back to that if we couldn't find a match. Moving it to right after the for loop will change the value written into the registers, but that's fine and makes it clearer what's happening.

Got it. Add some comment and move the code up.
I will send patch v3 soon.
Thanks for your suggestion again :) .

Su Hui

Quoted 2 lines

-Doug