Skip to contentSkip to conversation

gpio-sim: fix memory corruption when adding named lines and unnamed hogs

PATCH Superseded Kent Gibson 2 participants 4 messages Jun 6, 2023 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio-sim: fix memory corruption when adding named lines and unnamed hogs

When constructing the sim, gpio-sim constructs an array of named lines, sized based on the largest offset of any named line, and then initializes that array with the names of all lines, including unnamed hogs with higher offsets. In doing so it writes NULLs beyond the extent of the array.

Add a check that only named lines are used to initialize the array.

Fixes cb8c474e79be ("gpio: sim: new testing module")

---

After writing the comment above, and looking at the code again, it may be clearer to instead check that the offset is within the bounds of the array. Or do both. Consider that my review.

Cheers,
Kent.

 drivers/gpio/gpio-sim.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpio/gpio-sim.c +4 -2
index e5dfd636c63c..923e8ff53128 100644
@@ -721,8 +721,10 @@ static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
 	if (!line_names)
 		return ERR_PTR(-ENOMEM);
 
-	list_for_each_entry(line, &bank->line_list, siblings)
-		line_names[line->offset] = line->name;
+	list_for_each_entry(line, &bank->line_list, siblings) {
+		if (line->name)
+			line_names[line->offset] = line->name;
+	}
 
 	return line_names;
 }
Signature · 1 line
2.40.1
#1 lore raw
Quoted 8 lines · Kent Gibson

When constructing the sim, gpio-sim constructs an array of named lines, sized based on the largest offset of any named line, and then initializes that array with the names of all lines, including unnamed hogs with higher offsets. In doing so it writes NULLs beyond the extent of the array.

Add a check that only named lines are used to initialize the array.

Quoted 8 lines
Fixes cb8c474e79be ("gpio: sim: new testing module")

---

After writing the comment above, and looking at the code again, it may be clearer to instead check that the offset is within the bounds of the array. Or do both. Consider that my review.

Like:

if (line->offset <= max_offset)

    line_names[line->offset] = line->name;

? If so, then I agree it makes the purpose of the check clearer.

Bart

Quoted 26 lines

Cheers,
Kent.

 drivers/gpio/gpio-sim.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpio/gpio-sim.c +4 -3
index e5dfd636c63c..923e8ff53128 100644
@@ -721,8 +721,10 @@ static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
        if (!line_names)
                return ERR_PTR(-ENOMEM);
 
-       list_for_each_entry(line, &bank->line_list, siblings)
-               line_names[line->offset] = line->name;
+       list_for_each_entry(line, &bank->line_list, siblings) {
+               if (line->name)
+                       line_names[line->offset] = line->name;
+       }
 
        return line_names;
 }
--

2.40.1

#2 lore raw
Quoted 18 lines · Bartosz Golaszewski
Quoted 8 lines · Kent Gibson

When constructing the sim, gpio-sim constructs an array of named lines, sized based on the largest offset of any named line, and then initializes that array with the names of all lines, including unnamed hogs with higher offsets. In doing so it writes NULLs beyond the extent of the array.

Add a check that only named lines are used to initialize the array.

Quoted 8 lines
Fixes cb8c474e79be ("gpio: sim: new testing module")

---

After writing the comment above, and looking at the code again, it may be clearer to instead check that the offset is within the bounds of the array. Or do both. Consider that my review.

Quoted 7 lines

Like:

if (line->offset <= max_offset)

    line_names[line->offset] = line->name;

? If so, then I agree it makes the purpose of the check clearer.

Using line_names_size might be even clearer.

So, either that or

if (line->name && (line->offset <= max_offset))

    line_names[line->offset] = line->name;

to also not repeat the zeroing that the kcalloc() did.

Too many options. Let me know which you prefer.

Cheers,
Kent.

#3 lore raw
Quoted 28 lines · Kent Gibson
Quoted 18 lines · Bartosz Golaszewski
Quoted 8 lines · Kent Gibson

When constructing the sim, gpio-sim constructs an array of named lines, sized based on the largest offset of any named line, and then initializes that array with the names of all lines, including unnamed hogs with higher offsets. In doing so it writes NULLs beyond the extent of the array.

Add a check that only named lines are used to initialize the array.

Quoted 8 lines
Fixes cb8c474e79be ("gpio: sim: new testing module")

---

After writing the comment above, and looking at the code again, it may be clearer to instead check that the offset is within the bounds of the array. Or do both. Consider that my review.

Quoted 7 lines

Like:

if (line->offset <= max_offset)

    line_names[line->offset] = line->name;

? If so, then I agree it makes the purpose of the check clearer.

Quoted 7 lines

Using line_names_size might be even clearer.

So, either that or

if (line->name && (line->offset <= max_offset))

    line_names[line->offset] = line->name;

Yeah, let's go with this one.

Bart

Quoted 9 lines

to also not repeat the zeroing that the kcalloc() did.

Too many options. Let me know which you prefer.

Cheers,
Kent.