Skip to content

Commit 1271a7b

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
pwm: Zero-initialize the pwm_state passed to driver's .get_state()
This is just to ensure that .usage_power is properly initialized and doesn't contain random stack data. The other members of struct pwm_state should get a value assigned in a successful call to .get_state(). So in the absence of bugs in driver implementations, this is only a safe-guard and no fix. Reported-by: Munehisa Kamata <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Uwe Kleine-König <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 8caa81e commit 1271a7b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

drivers/pwm/core.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
115115
}
116116

117117
if (pwm->chip->ops->get_state) {
118-
struct pwm_state state;
118+
/*
119+
* Zero-initialize state because most drivers are unaware of
120+
* .usage_power. The other members of state are supposed to be
121+
* set by lowlevel drivers. We still initialize the whole
122+
* structure for simplicity even though this might paper over
123+
* faulty implementations of .get_state().
124+
*/
125+
struct pwm_state state = { 0, };
119126

120127
err = pwm->chip->ops->get_state(pwm->chip, pwm, &state);
121128
trace_pwm_get(pwm, &state, err);
@@ -448,7 +455,7 @@ static void pwm_apply_state_debug(struct pwm_device *pwm,
448455
{
449456
struct pwm_state *last = &pwm->last;
450457
struct pwm_chip *chip = pwm->chip;
451-
struct pwm_state s1, s2;
458+
struct pwm_state s1 = { 0 }, s2 = { 0 };
452459
int err;
453460

454461
if (!IS_ENABLED(CONFIG_PWM_DEBUG))
@@ -530,6 +537,7 @@ static void pwm_apply_state_debug(struct pwm_device *pwm,
530537
return;
531538
}
532539

540+
*last = (struct pwm_state){ 0 };
533541
err = chip->ops->get_state(chip, pwm, last);
534542
trace_pwm_get(pwm, last, err);
535543
if (err)

0 commit comments

Comments
 (0)