* [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully
@ 2022-07-18 11:48 Ahmad Fatoum
2022-07-18 11:48 ` [PATCH master 2/2] common: don't fixup empty serial/machine_compatible strings Ahmad Fatoum
2022-08-08 14:21 ` [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-07-18 11:48 UTC (permalink / raw)
To: barebox; +Cc: Johannes Zink, Ahmad Fatoum
Unlike recent versions of Linux, barebox represents the dummy regulator
as a NULL pointer and regulator API is supposed to anticipate operation
on a NULL pointer. When regulator_get_voltage was ported from Linux,
this was not taken into consideration, which leads to
regulator_get_voltage(NULL) crashing with a NULL pointer dereference.
Fix this by returning -EINVAL for dummy regulators. This aligns us with
how Linux would behave in this situation.
Fixes: 6ee83ce08b24 ("regulator: add regulator_get_voltage() to API")
Reported-by: Johannes Zink <j.zink@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/regulator/core.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 1fb344656f38..472b26f3a0ac 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -574,9 +574,14 @@ EXPORT_SYMBOL_GPL(regulator_bulk_free);
int regulator_get_voltage(struct regulator *regulator)
{
- struct regulator_dev *rdev = regulator->ri->rdev;
+ struct regulator_dev *rdev;
int sel, ret;
+ if (!regulator)
+ return -EINVAL;
+
+ rdev = regulator->ri->rdev;
+
if (rdev->desc->ops->get_voltage_sel) {
sel = rdev->desc->ops->get_voltage_sel(rdev);
if (sel < 0)
--
2.30.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH master 2/2] common: don't fixup empty serial/machine_compatible strings
2022-07-18 11:48 [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully Ahmad Fatoum
@ 2022-07-18 11:48 ` Ahmad Fatoum
2022-08-08 14:21 ` [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-07-18 11:48 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
There is no point in fixing up an empty string into the kernel device
tree, yet this can happen when globalvar_add_simple_string() is called
for the variable at least once as the function replaces NULL with an
allocated empty string. globalvar_add_simple_string() was called
unconditionally for of.kernel.add_machine_compatible, which in turn
led to always fixing up an empty string as the top-most compatible.
Resolve this by having barebox_get_(of_machine_compatible|serial_number)
return NULL for the empty string as well.
Fixes: 81dd24a0946c ("of: add generic of_prepend_machine_compatible()")
Fixes: f6756e9ce6f2 ("common: add $global.serial_number with device tree fixup")
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/misc.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/common/misc.c b/common/misc.c
index 0f6de3e9e5ef..e0e32f47c59d 100644
--- a/common/misc.c
+++ b/common/misc.c
@@ -191,6 +191,8 @@ void barebox_set_serial_number(const char *__serial_number)
const char *barebox_get_serial_number(void)
{
+ if (!serial_number || *serial_number == '\0')
+ return NULL;
return serial_number;
}
@@ -204,6 +206,8 @@ void barebox_set_of_machine_compatible(const char *__compatible)
const char *barebox_get_of_machine_compatible(void)
{
+ if (!of_machine_compatible || *of_machine_compatible == '\0')
+ return NULL;
return of_machine_compatible;
}
--
2.30.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully
2022-07-18 11:48 [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully Ahmad Fatoum
2022-07-18 11:48 ` [PATCH master 2/2] common: don't fixup empty serial/machine_compatible strings Ahmad Fatoum
@ 2022-08-08 14:21 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-08-08 14:21 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: barebox, Johannes Zink
On Mon, Jul 18, 2022 at 01:48:23PM +0200, Ahmad Fatoum wrote:
> Unlike recent versions of Linux, barebox represents the dummy regulator
> as a NULL pointer and regulator API is supposed to anticipate operation
> on a NULL pointer. When regulator_get_voltage was ported from Linux,
> this was not taken into consideration, which leads to
> regulator_get_voltage(NULL) crashing with a NULL pointer dereference.
>
> Fix this by returning -EINVAL for dummy regulators. This aligns us with
> how Linux would behave in this situation.
>
> Fixes: 6ee83ce08b24 ("regulator: add regulator_get_voltage() to API")
> Reported-by: Johannes Zink <j.zink@pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> drivers/regulator/core.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
Applied, thanks
Sascha
>
> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
> index 1fb344656f38..472b26f3a0ac 100644
> --- a/drivers/regulator/core.c
> +++ b/drivers/regulator/core.c
> @@ -574,9 +574,14 @@ EXPORT_SYMBOL_GPL(regulator_bulk_free);
>
> int regulator_get_voltage(struct regulator *regulator)
> {
> - struct regulator_dev *rdev = regulator->ri->rdev;
> + struct regulator_dev *rdev;
> int sel, ret;
>
> + if (!regulator)
> + return -EINVAL;
> +
> + rdev = regulator->ri->rdev;
> +
> if (rdev->desc->ops->get_voltage_sel) {
> sel = rdev->desc->ops->get_voltage_sel(rdev);
> if (sel < 0)
> --
> 2.30.2
>
>
>
--
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 |
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-08-08 14:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-18 11:48 [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully Ahmad Fatoum
2022-07-18 11:48 ` [PATCH master 2/2] common: don't fixup empty serial/machine_compatible strings Ahmad Fatoum
2022-08-08 14:21 ` [PATCH master 1/2] regulator: handle regulator_get_voltage(NULL) gracefully Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox