Skip to contentSkip to conversation

iio: Fix the allocation size for cros_ec_command

PATCH Superseded Yiyuan Guo 3 participants 7 messages Jun 29, 2023 – Jun 30, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yiyuan Guo

author
#0 lore raw

PATCH 1/1 iio: Fix the allocation size for cros_ec_command

The struct cros_ec_command contains several integer fields and a trailing array. An allocation size neglecting the integer fields can lead to buffer overrun.

---
 drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -2
index 943e9e14d1e9..e4c01f1072bd 100644
@@ -253,8 +253,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	platform_set_drvdata(pdev, indio_dev);
 
 	state->ec = ec->ec_dev;
-	state->msg = devm_kzalloc(&pdev->dev,
-				max((u16)sizeof(struct ec_params_motion_sense),
+	state->msg = devm_kzalloc(&pdev->dev, sizeof(*state->msg) +
+			max((u16)sizeof(struct ec_params_motion_sense),
 				state->ec->max_response), GFP_KERNEL);
 	if (!state->msg)
 		return -ENOMEM;
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v2 1/1 iio: cros_ec: Fix the allocation size for cros_ec_command

In reply to Yiyuan Guo

The struct cros_ec_command contains several integer fields and a trailing array. An allocation size neglecting the integer fields can lead to buffer overrun.

---
 drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -2
index 943e9e14d1e9..e4c01f1072bd 100644
@@ -253,8 +253,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	platform_set_drvdata(pdev, indio_dev);
 
 	state->ec = ec->ec_dev;
-	state->msg = devm_kzalloc(&pdev->dev,
-				max((u16)sizeof(struct ec_params_motion_sense),
+	state->msg = devm_kzalloc(&pdev->dev, sizeof(*state->msg) +
+			max((u16)sizeof(struct ec_params_motion_sense),
 				state->ec->max_response), GFP_KERNEL);
 	if (!state->msg)
 		return -ENOMEM;
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2] iio: cros_ec: Fix the allocation size for cros_ec_command

Quoted 5 lines · Yiyuan Guo

The struct cros_ec_command contains several integer fields and a trailing array. An allocation size neglecting the integer fields can lead to buffer overrun.

You could attach my R-b tag as v2 has no major changes from v1.

Quoted 1 line

---

Always a good practice to put changelog here. Search "changelog" in [1].

[1]: https://www.kernel.org/doc/html/latest/process/submitting-patches.html

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 258

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -2
@@ -253,8 +253,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	platform_set_drvdata(pdev, indio_dev);
 
 	state->ec = ec->ec_dev;
-	state->msg = devm_kzalloc(&pdev->dev,
-				max((u16)sizeof(struct ec_params_motion_sense),
+	state->msg = devm_kzalloc(&pdev->dev, sizeof(*state->msg) +
+			max((u16)sizeof(struct ec_params_motion_sense),
 				state->ec->max_response), GFP_KERNEL);

While looking at the patch again, I found a nit. Please align the code by adding an extra tab before "max".

author
#4 lore raw

PATCH v3 1/1 iio: cros_ec: Fix the allocation size for cros_ec_command

The struct cros_ec_command contains several integer fields and a trailing array. An allocation size neglecting the integer fields can lead to buffer overrun.

---
v2->v3:
* Added R-b tag from Tzung-Bi Shih
* Aligned the code by adding an extra tab before "max"
* Added a patch changelog
v1->v2: Prefixed the commit title with "iio: cros_ec:"

 drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -1
index 943e9e14d1e9..b72d39fc2434 100644
@@ -253,7 +253,7 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	platform_set_drvdata(pdev, indio_dev);
 
 	state->ec = ec->ec_dev;
-	state->msg = devm_kzalloc(&pdev->dev,
+	state->msg = devm_kzalloc(&pdev->dev, sizeof(*state->msg) +
 				max((u16)sizeof(struct ec_params_motion_sense),
 				state->ec->max_response), GFP_KERNEL);
 	if (!state->msg)
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v3] iio: cros_ec: Fix the allocation size for cros_ec_command

Quoted 7 lines · Yiyuan Guo

The struct cros_ec_command contains several integer fields and a trailing array. An allocation size neglecting the integer fields can lead to buffer overrun.

Please _never_ send a patch as reply to a previous one, much less with a Re: subject.

Guenter

Quoted 26 lines

---
v2->v3:
* Added R-b tag from Tzung-Bi Shih
* Aligned the code by adding an extra tab before "max"
* Added a patch changelog
v1->v2: Prefixed the commit title with "iio: cros_ec:"

 drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -2
index 943e9e14d1e9..b72d39fc2434 100644
@@ -253,7 +253,7 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
        platform_set_drvdata(pdev, indio_dev);
 
        state->ec = ec->ec_dev;
-       state->msg = devm_kzalloc(&pdev->dev,
+       state->msg = devm_kzalloc(&pdev->dev, sizeof(*state->msg) +
                                max((u16)sizeof(struct ec_params_motion_sense),
                                state->ec->max_response), GFP_KERNEL);
        if (!state->msg)
--

2.25.1

author
#6 lore raw

Re: [PATCH v3] iio: cros_ec: Fix the allocation size for cros_ec_command

Got it. I have resent the patch in a separate thread.

Thanks,
Yiyuan

Quoted 41 lines · Guenter Roeck
Quoted 7 lines · Yiyuan Guo

The struct cros_ec_command contains several integer fields and a trailing array. An allocation size neglecting the integer fields can lead to buffer overrun.

Please _never_ send a patch as reply to a previous one, much less with a Re: subject.

Guenter

Quoted 26 lines

---
v2->v3:
* Added R-b tag from Tzung-Bi Shih
* Aligned the code by adding an extra tab before "max"
* Added a patch changelog
v1->v2: Prefixed the commit title with "iio: cros_ec:"

 drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -2
index 943e9e14d1e9..b72d39fc2434 100644
@@ -253,7 +253,7 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
        platform_set_drvdata(pdev, indio_dev);
 
        state->ec = ec->ec_dev;
-       state->msg = devm_kzalloc(&pdev->dev,
+       state->msg = devm_kzalloc(&pdev->dev, sizeof(*state->msg) +
                                max((u16)sizeof(struct ec_params_motion_sense),
                                state->ec->max_response), GFP_KERNEL);
        if (!state->msg)
--

2.25.1