Skip to contentSkip to conversation

media: atomisp: Fix buffer overrun in gmin_get_var_int()

PATCH In review Hans de Goede 2 participants 5 messages May 27, 2023 – May 28, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: atomisp: Fix buffer overrun in gmin_get_var_int()

Not all functions used in gmin_get_var_int() update len to the actual length of the returned string. So len may still have its initial value of the length of val[] when "val[len] = 0;" is run to ensure 0 termination.

If this happens we end up writing one beyond the bounds of val[], fix this.

Note this is a quick fix for this since the entirety of atomisp_gmin_platform.c will be removed once all atomisp sensor drivers have been moved over to runtime-pm + v4l2-async device registration.

---
 drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c +2 -2
index 93bfb3fadcf7..139ad7ad1dcf 100644
@@ -1429,8 +1429,8 @@ static int gmin_get_config_var(struct device *maindev,
 
 int gmin_get_var_int(struct device *dev, bool is_gmin, const char *var, int def)
 {
-	char val[CFG_VAR_NAME_MAX];
-	size_t len = sizeof(val);
+	char val[CFG_VAR_NAME_MAX + 1];
+	size_t len = CFG_VAR_NAME_MAX;
 	long result;
 	int ret;
 
Signature · 1 line
2.40.1
#1 lore raw
Quoted 11 lines · Hans de Goede

Not all functions used in gmin_get_var_int() update len to the actual length of the returned string. So len may still have its initial value of the length of val[] when "val[len] = 0;" is run to ensure 0 termination.

If this happens we end up writing one beyond the bounds of val[], fix this.

Note this is a quick fix for this since the entirety of atomisp_gmin_platform.c will be removed once all atomisp sensor drivers have been moved over to runtime-pm + v4l2-async device registration.

...

Quoted 1 line

Is this a new official tag? (Just to my surprise)

...

drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c static int gmin_get_config_var(struct device *maindev, · line 1433

Quoted 4 lines
drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c +2 -2
-       char val[CFG_VAR_NAME_MAX];
-       size_t len = sizeof(val);
+       char val[CFG_VAR_NAME_MAX + 1];
+       size_t len = CFG_VAR_NAME_MAX;

Why not sizeof() - 1? At least it will be a single point when change can be made and not breaking again in a similar way the code.

Quoted 2 lines
        long result;
        int ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi,

Quoted 13 lines · Andy Shevchenko
Quoted 11 lines · Hans de Goede

Not all functions used in gmin_get_var_int() update len to the actual length of the returned string. So len may still have its initial value of the length of val[] when "val[len] = 0;" is run to ensure 0 termination.

If this happens we end up writing one beyond the bounds of val[], fix this.

Note this is a quick fix for this since the entirety of atomisp_gmin_platform.c will be removed once all atomisp sensor drivers have been moved over to runtime-pm + v4l2-async device registration.

Quoted 5 lines

...

Quoted 1 line

Is this a new official tag? (Just to my surprise)

Yes, I was surprised too, checkpatch.pl now wants a Closes: tag after a Reported-by: one, rather then a Link: tag.

Quoted 10 lines

...

drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c static int gmin_get_config_var(struct device *maindev, · line 1433

Quoted 4 lines
drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c +2 -2
-       char val[CFG_VAR_NAME_MAX];
-       size_t len = sizeof(val);
+       char val[CFG_VAR_NAME_MAX + 1];
+       size_t len = CFG_VAR_NAME_MAX;

Why not sizeof() - 1? At least it will be a single point when change can be made and not breaking again in a similar way the code.

I wanted to make the buffer one byte bigger to make room for the terminating 0, not 1 bute smaller.

Regards,

Hams

#3 lore raw
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko

On Sat, May 27, 2023 at 6:51 PM Hans de Goede <[email protected]> wrote:

...

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Is this a new official tag? (Just to my surprise)

Yes, I was surprised too, checkpatch.pl now wants a Closes: tag after a Reported-by: one, rather then a Link: tag.

Interesting...

...

Quoted 10 lines
Quoted 7 lines

drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c static int gmin_get_config_var(struct device *maindev, · line 1433

Quoted 4 lines
drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c +2 -2
-       char val[CFG_VAR_NAME_MAX];
-       size_t len = sizeof(val);
+       char val[CFG_VAR_NAME_MAX + 1];
+       size_t len = CFG_VAR_NAME_MAX;

Why not sizeof() - 1? At least it will be a single point when change can be made and not breaking again in a similar way the code.

I wanted to make the buffer one byte bigger to make room for the terminating 0, not 1 bute smaller.

I understand, and I'm commenting only on the len assignment. Sorry for not being clear.

Hence you will have

  buf[SIZE + 1];
  sizeof(buf) - 1;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Hi,

Quoted 28 lines · Andy Shevchenko
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko

On Sat, May 27, 2023 at 6:51 PM Hans de Goede <[email protected]> wrote:

...

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Is this a new official tag? (Just to my surprise)

Yes, I was surprised too, checkpatch.pl now wants a Closes: tag after a Reported-by: one, rather then a Link: tag.

Interesting...

...

Quoted 10 lines
Quoted 7 lines

drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c static int gmin_get_config_var(struct device *maindev, · line 1433

Quoted 4 lines
drivers/staging/media/atomisp/pci/atomisp_gmin_platform.c +2 -2
-       char val[CFG_VAR_NAME_MAX];
-       size_t len = sizeof(val);
+       char val[CFG_VAR_NAME_MAX + 1];
+       size_t len = CFG_VAR_NAME_MAX;

Why not sizeof() - 1? At least it will be a single point when change can be made and not breaking again in a similar way the code.

I wanted to make the buffer one byte bigger to make room for the terminating 0, not 1 bute smaller.

Quoted 7 lines

I understand, and I'm commenting only on the len assignment. Sorry for not being clear.

Hence you will have

  buf[SIZE + 1];
  sizeof(buf) - 1;

That is just ugly IMHO, why take the sizeof something which we know is SIZE + 1 and then substract 1 instead of just writing SIZE ?

Note that for any future SIZE define changes both methods are equally future proof in that they both automatically adjust to the define changing.

Regards,

Hans