Skip to contentSkip to conversation

clk: check ops pointer on clock register

PATCH Applied Jerome Brunet 2 participants 2 messages Dec 19, 2017 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 clk: check ops pointer on clock register

Nothing really prevents a provider from (trying to) register a clock without providing the clock ops structure.

We do check the individual fields before using them, but not the structure pointer itself. This may have the usual nasty consequences when the pointer is dereferenced, most likely when checking one the field during the initialization.

This is fixed by returning an error on clock register if the ops pointer is NULL.

---
Changes since v1:
* Add WARN_ON so the error cannot be silently ignored
* Remove fixes tag

 drivers/clk/clk.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/clk/clk.c +7 -0
index 8a1860a36c77..211f97e8dc65 100644
@@ -2683,7 +2683,13 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
 		ret = -ENOMEM;
 		goto fail_name;
 	}
+
+	if (WARN_ON(!hw->init->ops)) {
+		ret = -EINVAL;
+		goto fail_ops;
+	}
 	core->ops = hw->init->ops;
+
 	if (dev && pm_runtime_enabled(dev))
 		core->dev = dev;
 	if (dev && dev->driver)
@@ -2745,6 +2751,7 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
 		kfree_const(core->parent_names[i]);
 	kfree(core->parent_names);
 fail_parent_names:
+fail_ops:
 	kfree_const(core->name);
 fail_name:
 	kfree(core);
Signature · 1 line
2.14.3
#1 lore raw

Quoting Jerome Brunet (2017-12-19 00:33:29)

Quoted 10 lines

Nothing really prevents a provider from (trying to) register a clock without providing the clock ops structure.
=

We do check the individual fields before using them, but not the structure pointer itself. This may have the usual nasty consequences when the pointer is dereferenced, most likely when checking one the field during the initialization.
=

Quoted 5 lines

This is fixed by returning an error on clock register if the ops pointer is NULL.
=

Applied to clk-check-ops-ptr

Regards,
Mike

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struc=

Quoted 15 lines

---
Changes since v1:
* Add WARN_ON so the error cannot be silently ignored
* Remove fixes tag
=

 drivers/clk/clk.c | 7 +++++++
 1 file changed, 7 insertions(+)

=

drivers/clk/clk.c +0 -0
index 8a1860a36c77..211f97e8dc65 100644
@@ -2683,7 +2683,13 @@ struct clk *clk_register(struct device *dev, struc=

t clk_hw *hw)

drivers/clk/clk.c struct clk *clk_register(struct device *dev, struct=

Quoted 14 lines
drivers/clk/clk.c +6 -0
                ret =3D -ENOMEM;
                goto fail_name;
        }
+
+       if (WARN_ON(!hw->init->ops)) {
+               ret =3D -EINVAL;
+               goto fail_ops;
+       }
        core->ops =3D hw->init->ops;
+
        if (dev && pm_runtime_enabled(dev))
                core->dev =3D dev;
        if (dev && dev->driver)
@@ -2745,6 +2751,7 @@ struct clk *clk_register(struct device *dev, struct=

clk_hw *hw)

Quoted 11 lines
drivers/clk/clk.c +1 -1
                kfree_const(core->parent_names[i]);
        kfree(core->parent_names);
 fail_parent_names:
+fail_ops:
        kfree_const(core->name);
 fail_name:
        kfree(core);
-- =

2.14.3
=20