mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw
@ 2022-03-07 17:01 Ahmad Fatoum
  2022-03-07 17:01 ` [PATCH master 2/2] clk: composite: fix possible NULL pointer dereference Ahmad Fatoum
  2022-03-08  8:19 ` [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-03-07 17:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

As clk_hw::clk is the first member and there may no be leading padding,
clk_hw_to_clk(NULL) == clk_to_clk_hw(NULL) == NULL. This would break if
we moved struct clk around, so make this more robust by using
IS_ERR_OR_NULL. No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/linux/clk.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/include/linux/clk.h b/include/linux/clk.h
index c0e998e54ae6..7fd835c35e51 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -433,12 +433,14 @@ struct clk_hw {
 
 static inline struct clk *clk_hw_to_clk(const struct clk_hw *hw)
 {
-	return IS_ERR(hw) ? ERR_CAST(hw) : (struct clk *)&hw->clk;
+	return IS_ERR_OR_NULL(hw) ? ERR_CAST(hw)
+				  : (struct clk *)&hw->clk;
 }
 
 static inline struct clk_hw *clk_to_clk_hw(const struct clk *clk)
 {
-	return IS_ERR(clk) ? ERR_CAST(clk) : (struct clk_hw *)container_of(clk, struct clk_hw, clk);
+	return IS_ERR_OR_NULL(clk) ? ERR_CAST(clk)
+				   : (struct clk_hw *)container_of(clk, struct clk_hw, clk);
 }
 
 struct clk_div_table {
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* [PATCH master 2/2] clk: composite: fix possible NULL pointer dereference
  2022-03-07 17:01 [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw Ahmad Fatoum
@ 2022-03-07 17:01 ` Ahmad Fatoum
  2022-03-08  8:19 ` [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-03-07 17:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

A composite clock is at most composed of a mux, a divider and a gate,
but it may lack one or two of these components. In that case, a NULL
pointer is passed, so we need to deal with this case.

Fixes: 8b0ca7a885ea ("clk: composite: add clk_hw registration functions")
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/clk/clk-composite.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/clk-composite.c b/drivers/clk/clk-composite.c
index 63056b769647..4ebdd399b44b 100644
--- a/drivers/clk/clk-composite.c
+++ b/drivers/clk/clk-composite.c
@@ -181,9 +181,13 @@ struct clk_hw *clk_hw_register_composite(struct device_d *dev,
 		unsigned long flags)
 {
 	struct clk *clk;
-	mux_hw->clk.ops = mux_ops;
-	rate_hw->clk.ops = rate_ops;
-	gate_hw->clk.ops = gate_ops;
+
+	if (mux_hw)
+		mux_hw->clk.ops = mux_ops;
+	if (rate_hw)
+		rate_hw->clk.ops = rate_ops;
+	if (gate_hw)
+		gate_hw->clk.ops = gate_ops;
 
 	parent_names = memdup_array(parent_names, num_parents);
 	if (!parent_names)
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw
  2022-03-07 17:01 [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw Ahmad Fatoum
  2022-03-07 17:01 ` [PATCH master 2/2] clk: composite: fix possible NULL pointer dereference Ahmad Fatoum
@ 2022-03-08  8:19 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-03-08  8:19 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Mar 07, 2022 at 06:01:05PM +0100, Ahmad Fatoum wrote:
> As clk_hw::clk is the first member and there may no be leading padding,
> clk_hw_to_clk(NULL) == clk_to_clk_hw(NULL) == NULL. This would break if
> we moved struct clk around, so make this more robust by using
> IS_ERR_OR_NULL. No functional change.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  include/linux/clk.h | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/clk.h b/include/linux/clk.h
> index c0e998e54ae6..7fd835c35e51 100644
> --- a/include/linux/clk.h
> +++ b/include/linux/clk.h
> @@ -433,12 +433,14 @@ struct clk_hw {
>  
>  static inline struct clk *clk_hw_to_clk(const struct clk_hw *hw)
>  {
> -	return IS_ERR(hw) ? ERR_CAST(hw) : (struct clk *)&hw->clk;
> +	return IS_ERR_OR_NULL(hw) ? ERR_CAST(hw)
> +				  : (struct clk *)&hw->clk;
>  }

This one doesn't apply to master, but on next. Applied it there instead.

Sascha

>  
>  static inline struct clk_hw *clk_to_clk_hw(const struct clk *clk)
>  {
> -	return IS_ERR(clk) ? ERR_CAST(clk) : (struct clk_hw *)container_of(clk, struct clk_hw, clk);
> +	return IS_ERR_OR_NULL(clk) ? ERR_CAST(clk)
> +				   : (struct clk_hw *)container_of(clk, struct clk_hw, clk);
>  }
>  
>  struct clk_div_table {
> -- 
> 2.30.2
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

end of thread, other threads:[~2022-03-08  8:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-07 17:01 [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw Ahmad Fatoum
2022-03-07 17:01 ` [PATCH master 2/2] clk: composite: fix possible NULL pointer dereference Ahmad Fatoum
2022-03-08  8:19 ` [PATCH master 1/2] clk: harden NULL handling in clk_hw_to_clk/clk_to_clk_hw Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox