Skip to contentSkip to conversation

lib/igt_hook: Fix realloc size to avoid memory overwriting

PATCH In review Zbigniew Kempczyński 5 participants 10 messages Jun 29 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/igt_hook: Fix realloc size to avoid memory overwriting

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +1 -1
index b8f25b6c7a..a44b852da1 100644
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +1 -1
index b8f25b6c7a..a44b852da1 100644
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
#2 lore raw
In reply to Zbigniew Kempczyński
Quoted 15 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +0 -0
index b8f25b6c7a..a44b852da1 100644

lib/igt_hook.c static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct · line 303

Quoted 5 lines
lib/igt_hook.c +1 -1
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;

Oops. My bad. Thanks for finding this!

Do we need to use "+=" instead of "=" above, though?

Also, perhaps we could use roundup_power_of_two(len + 1) to reduce the number of follow-up reallocs?

--
Gustavo Sousa

Quoted 5 lines
lib/igt_hook.c +0 -0
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
2.54.0
#3 lore raw
Quoted 25 lines · Gustavo Sousa
Quoted 15 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +0 -0
index b8f25b6c7a..a44b852da1 100644

lib/igt_hook.c static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct · line 303

Quoted 5 lines
lib/igt_hook.c +1 -1
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;

Oops. My bad. Thanks for finding this!

Quoted 4 lines

Do we need to use "+=" instead of "=" above, though?

Also, perhaps we could use roundup_power_of_two(len + 1) to reduce the number of follow-up reallocs?

Yes, that was my intention, maybe I should to add a comment but that was quickfix and didn't think about it.

--
Zbigniew

Quoted 9 lines

--
Gustavo Sousa

Quoted 5 lines
lib/igt_hook.c +0 -0
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
2.54.0
#4 lore raw
In reply to Gustavo Sousa
Quoted 25 lines · Gustavo Sousa
Quoted 15 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +0 -0
index b8f25b6c7a..a44b852da1 100644

lib/igt_hook.c static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct · line 303

Quoted 5 lines
lib/igt_hook.c +1 -1
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;

Oops. My bad. Thanks for finding this!

Quoted 4 lines

Do we need to use "+=" instead of "=" above, though?

Also, perhaps we could use roundup_power_of_two(len + 1) to reduce the number of follow-up reallocs?

Sorry, I haven't explained why I used "+=" instead of "=". That was first what comes to my mind to fix the issue and try to decrease number of reallocs more than with simple '='. For smaller sizes I'm not sure if size of power of two is better than any other value. Glibc adds some overhead to malloc so I think any strategy we pick which increases buffer underneath is acceptable to me.

--
Zbigniew

Quoted 9 lines

--
Gustavo Sousa

Quoted 5 lines
lib/igt_hook.c +0 -0
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
2.54.0
#5 lore raw
Quoted 31 lines · Zbigniew Kempczyński
Quoted 25 lines · Gustavo Sousa
Quoted 15 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +0 -0
index b8f25b6c7a..a44b852da1 100644

lib/igt_hook.c static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct · line 303

Quoted 5 lines
lib/igt_hook.c +1 -1
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;

Oops. My bad. Thanks for finding this!

Quoted 4 lines

Do we need to use "+=" instead of "=" above, though?

Also, perhaps we could use roundup_power_of_two(len + 1) to reduce the number of follow-up reallocs?

Quoted 6 lines

Sorry, I haven't explained why I used "+=" instead of "=". That was first what comes to my mind to fix the issue and try to decrease number of reallocs more than with simple '='. For smaller sizes I'm not sure if size of power of two is better than any other value. Glibc adds some overhead to malloc so I think any strategy we pick which increases buffer underneath is acceptable to me.

I just though "+=" adding exactly the size needed as an "extra" looked odd and didn't initially get that this indeed had the objective of avoiding extra reallocs in the future. I wonder if future readers of the code will get the same perception or if that's just me.

--
Gustavo Sousa

Quoted 14 lines

--
Zbigniew

Quoted 9 lines

--
Gustavo Sousa

Quoted 5 lines
lib/igt_hook.c +0 -0
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
2.54.0
#6 lore raw
In reply to Zbigniew Kempczyński
Quoted 2 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

Might one to point out (if) this is the cause for the segfaults we've been seeing in CI.

BR,
Jani.

Quoted 21 lines
---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +1 -1
index b8f25b6c7a..a44b852da1 100644
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
Jani Nikula, Intel
#7 lore raw
Quoted 6 lines · Jani Nikula
Quoted 2 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

Might one to point out (if) this is the cause for the segfaults we've been seeing in CI.

Also, Fixes: ?

Quoted 25 lines

BR,
Jani.

Quoted 21 lines
---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +1 -1
index b8f25b6c7a..a44b852da1 100644
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
Jani Nikula, Intel
#8 lore raw
Quoted 9 lines · Jani Nikula
Quoted 6 lines · Jani Nikula
Quoted 2 lines · Zbigniew Kempczyński

Doubling test name size might be not enough for realloc if target name length is larger than test/subtest name size.

Might one to point out (if) this is the cause for the segfaults we've been seeing in CI.

Also, Fixes: ?

I'm sorry, I wasn't aware of this issue id, just got link to failing job so I wanted to fix it quickly. I promise I'll behave decent in the future and will check this id before sending the patch.

--
Zbigniew

Quoted 29 lines
Quoted 26 lines

BR,
Jani.

Quoted 21 lines
---
 lib/igt_hook.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/igt_hook.c +1 -1
index b8f25b6c7a..a44b852da1 100644
@@ -300,7 +300,7 @@ static void igt_hook_update_test_name_pre_call(struct igt_hook *igt_hook, struct
 	if (len + 1 > *size_ptr) {
 		size_t fullname_size;
 
-		*size_ptr *= 2;
+		*size_ptr += len + 1;
 		*name_ptr = realloc(*name_ptr, *size_ptr);
 
 		fullname_size = igt_hook_calc_test_fullname_size(igt_hook);
Signature · 1 line
Jani Nikula, Intel