All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] pwm: improve args checking in pwm_apply_state()
@ 2016-05-27 16:45 ` Brian Norris
  0 siblings, 0 replies; 17+ messages in thread
From: Brian Norris @ 2016-05-27 16:45 UTC (permalink / raw)
  To: Thierry Reding
  Cc: linux-pwm, linux-kernel, Brian Norris, Boris Brezillon,
	Doug Anderson, Brian Norris

It seems like in the process of refactoring pwm_config() to utilize the
newly-introduced pwm_apply_state() API, some args/bounds checking was
dropped.

In particular, I noted that we are now allowing invalid period
selections. e.g.:

  # echo 1 > /sys/class/pwm/pwmchip0/export
  # cat /sys/class/pwm/pwmchip0/pwm1/period
  100
  # echo 101 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
  [... driver may or may not reject the value, or trigger some logic bug ...]

It's better to see:

  # echo 1 > /sys/class/pwm/pwmchip0/export
  # cat /sys/class/pwm/pwmchip0/pwm1/period
  100
  # echo 101 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
  -bash: echo: write error: Invalid argument

This patch reintroduces some bounds checks in both pwm_config() (for its
signed parameters; we don't want to convert negative values into large
unsigned values) and in pwm_apply_state() (which fix the above described
behavior, as well as other potential API misuses).

Fixes: 5ec803edcb70 ("pwm: Add core infrastructure to allow atomic updates")
Signed-off-by: Brian Norris <briannorris@chromium.org>
---
v2:
 * changed subject, as this covers more scope now
 * add Fixes tag, as this is a v4.7-rc regression
 * add more bounds/args checks in pwm_apply_state() and pwm_config()

 drivers/pwm/core.c  | 3 ++-
 include/linux/pwm.h | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index dba3843c53b8..ed337a8c34ab 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -457,7 +457,8 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
 {
 	int err;
 
-	if (!pwm)
+	if (!pwm || !state || !state->period ||
+	    state->duty_cycle > state->period)
 		return -EINVAL;
 
 	if (!memcmp(state, &pwm->state, sizeof(*state)))
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 17018f3c066e..908b67c847cd 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -235,6 +235,9 @@ static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
 	if (!pwm)
 		return -EINVAL;
 
+	if (duty_ns < 0 || period_ns < 0)
+		return -EINVAL;
+
 	pwm_get_state(pwm, &state);
 	if (state.duty_cycle == duty_ns && state.period == period_ns)
 		return 0;
-- 
2.8.0.rc3.226.g39d4020

^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2016-06-23 16:55 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-27 16:45 [PATCH v2] pwm: improve args checking in pwm_apply_state() Brian Norris
2016-05-27 16:45 ` Brian Norris
2016-05-27 16:54 ` Boris Brezillon
2016-05-27 16:54   ` Boris Brezillon
2016-06-10 12:20 ` Thierry Reding
2016-06-21 14:42 ` Geert Uytterhoeven
2016-06-21 18:37   ` Brian Norris
2016-06-21 18:37     ` Brian Norris
2016-06-21 21:22     ` Boris Brezillon
2016-06-22  8:04     ` Boris Brezillon
2016-06-22  8:04       ` Boris Brezillon
2016-06-22 12:00       ` Thierry Reding
2016-06-22 14:32       ` Geert Uytterhoeven
2016-06-22 19:16       ` Brian Norris
2016-06-22 20:41         ` Boris Brezillon
2016-06-22 20:46           ` Brian Norris
2016-06-23 16:55             ` Thierry Reding

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.