Skip to contentSkip to conversation

libavb: fix avb_replace() OOM handling

PATCH Tom Rini 3 participants 3 messages May 22 – May 26 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Tom Rini

author
#2 lore raw
Quoted 45 lines · Josh Law

avb_replace() promises NULL on OOM. Once it had built the first replacement, a later allocation failure returned that partial buffer. Callers treat any result as success, so AVB could keep booting with truncated bootargs.

Free the partial result and return NULL. The existing callers can then take their OOM path.

---
 lib/libavb/avb_util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
lib/libavb/avb_util.c +3 -3
index 8719ede15a7..9e2e6ea3495 100644
@@ -272,7 +272,7 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
       num_new = num_before + replace_len + 1;
       ret = avb_malloc(num_new);
       if (ret == NULL) {
-        goto out;
+        goto fail;
       }
       avb_memcpy(ret, str, num_before);
       avb_memcpy(ret + num_before, replace, replace_len);
@@ -283,7 +283,7 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
       num_new = ret_len + num_before + replace_len + 1;
       new_str = avb_malloc(num_new);
       if (new_str == NULL) {
-        goto out;
+        goto fail;
       }
       avb_memcpy(new_str, ret, ret_len);
       avb_memcpy(new_str + ret_len, str, num_before);
@@ -308,7 +308,7 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
     size_t num_new = ret_len + num_remaining + 1;
     char* new_str = avb_malloc(num_new);
     if (new_str == NULL) {
-      goto out;
+      goto fail;
     }
     avb_memcpy(new_str, ret, ret_len);
     avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);

lib/libavb/avb_util.c char* avb_replace(const char* str, const char* search, const char* replace) { · line 329

Quoted 10 lines
lib/libavb/avb_util.c +4 -0
@@ -320,6 +320,10 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
 
 out:
   return ret;
+
+fail:
+  avb_free(ret);
+  return NULL;
 }
 
 /* We only support a limited amount of strings in avb_strdupv(). */

Thanks for the explanation and patch. This seems fine but I'll defer to Mattijs as it's his area.

Signature · 1 line
Tom
#3 lore raw

Hi Josh,

Quoted 55 lines · Tom Rini
Quoted 54 lines · Josh Law

avb_replace() promises NULL on OOM. Once it had built the first replacement, a later allocation failure returned that partial buffer. Callers treat any result as success, so AVB could keep booting with truncated bootargs.

Free the partial result and return NULL. The existing callers can then take their OOM path.

---
 lib/libavb/avb_util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
lib/libavb/avb_util.c +7 -3
index 8719ede15a7..9e2e6ea3495 100644
@@ -272,7 +272,7 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
       num_new = num_before + replace_len + 1;
       ret = avb_malloc(num_new);
       if (ret == NULL) {
-        goto out;
+        goto fail;
       }
       avb_memcpy(ret, str, num_before);
       avb_memcpy(ret + num_before, replace, replace_len);
@@ -283,7 +283,7 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
       num_new = ret_len + num_before + replace_len + 1;
       new_str = avb_malloc(num_new);
       if (new_str == NULL) {
-        goto out;
+        goto fail;
       }
       avb_memcpy(new_str, ret, ret_len);
       avb_memcpy(new_str + ret_len, str, num_before);
@@ -308,7 +308,7 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
     size_t num_new = ret_len + num_remaining + 1;
     char* new_str = avb_malloc(num_new);
     if (new_str == NULL) {
-      goto out;
+      goto fail;
     }
     avb_memcpy(new_str, ret, ret_len);
     avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
@@ -320,6 +320,10 @@ char* avb_replace(const char* str, const char* search, const char* replace) {
 
 out:
   return ret;
+
+fail:
+  avb_free(ret);
+  return NULL;
 }
 
Quoted 4 lines

lib/libavb/avb_util.c char* avb_replace(const char* str, const char* search, const char* replace) { · line 329

Quoted 1 line
lib/libavb/avb_util.c +0 -0
 /* We only support a limited amount of strings in avb_strdupv(). */

Thanks for the explanation and patch. This seems fine but I'll defer to Mattijs as it's his area.

This patch seems to posted a second time here:
https://lore.kernel.org/all/[email protected]/

Can you explain why it has been send twice, please?

Quoted 3 lines
Signature · 2 lines
-- 
Tom
#4 lore raw
Quoted 69 lines · Mattijs Korpershoek

Hi Josh,

Quoted 14 lines · Tom Rini
Quoted 13 lines · Josh Law

avb_replace() promises NULL on OOM. Once it had built the first replacement, a later allocation failure returned that partial buffer. Callers treat any result as success, so AVB could keep booting with truncated bootargs.

Free the partial result and return NULL. The existing callers can then take their OOM path.

---
 lib/libavb/avb_util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
Quoted 5 lines

lib/libavb/avb_util.c char* avb_replace(const char* str, const char*

Quoted 5 lines
lib/libavb/avb_util.c +0 -0
index 8719ede15a7..9e2e6ea3495 100644
@@ -272,7 +272,7 @@ char* avb_replace(const char* str, const char*

search, const char* replace) {

Quoted 9 lines

lib/libavb/avb_util.c char* avb_replace(const char* str, const char*

Quoted 9 lines
lib/libavb/avb_util.c +1 -1
       num_new = num_before + replace_len + 1;
       ret = avb_malloc(num_new);
       if (ret == NULL) {
-        goto out;
+        goto fail;
       }
       avb_memcpy(ret, str, num_before);
       avb_memcpy(ret + num_before, replace, replace_len);
@@ -283,7 +283,7 @@ char* avb_replace(const char* str, const char*

search, const char* replace) {

Quoted 9 lines

lib/libavb/avb_util.c char* avb_replace(const char* str, const char*

Quoted 9 lines
lib/libavb/avb_util.c +1 -1
       num_new = ret_len + num_before + replace_len + 1;
       new_str = avb_malloc(num_new);
       if (new_str == NULL) {
-        goto out;
+        goto fail;
       }
       avb_memcpy(new_str, ret, ret_len);
       avb_memcpy(new_str + ret_len, str, num_before);
@@ -308,7 +308,7 @@ char* avb_replace(const char* str, const char*

search, const char* replace) {

Quoted 8 lines

lib/libavb/avb_util.c char* avb_replace(const char* str, const char* · line 314

Quoted 8 lines
lib/libavb/avb_util.c +1 -1
     size_t num_new = ret_len + num_remaining + 1;
     char* new_str = avb_malloc(num_new);
     if (new_str == NULL) {
-      goto out;
+      goto fail;
     }
     avb_memcpy(new_str, ret, ret_len);
     avb_memcpy(new_str + ret_len, str_after_last_replace,

num_remaining);

Quoted 1 line

lib/libavb/avb_util.c char* avb_replace(const char* str, const char*

Quoted 1 line
lib/libavb/avb_util.c +0 -0
@@ -320,6 +320,10 @@ char* avb_replace(const char* str, const char*

search, const char* replace) {

Quoted 9 lines
Quoted 9 lines
lib/libavb/avb_util.c +4 -0
 out:
   return ret;
+
+fail:
+  avb_free(ret);
+  return NULL;
 }
 
Quoted 4 lines

lib/libavb/avb_util.c char* avb_replace(const char* str, const char* · line 328

Quoted 1 line
lib/libavb/avb_util.c +0 -0
 /* We only support a limited amount of strings in avb_strdupv(). */

Thanks for the explanation and patch. This seems fine but I'll defer to Mattijs as it's his area.

Quoted 4 lines

This patch seems to posted a second time here:
https://lore.kernel.org/all/[email protected]/

Can you explain why it has been send twice, please?

Heh. The like guardian of the "first post" lists, took a while, so I subscribed to the list, thought that would get it approved on-list

It happens. Honest mistake :)

Quoted 4 lines
Quoted 3 lines
Signature · 2 lines
-- 
Tom

Thanks!