Skip to contentSkip to conversation

hw/core/loader-fit: fix freeing errp in fit_load_fdt

PATCH Superseded Vladimir Sementsov-Ogievskiy 2 participants 8 messages Nov 27, 2019 – Nov 30, 2019

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 hw/core/loader-fit: fix freeing errp in fit_load_fdt

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
 hw/core/loader-fit.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +4 -1
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
     err = fit_image_addr(itb, img_off, "load", &load_addr, errp);
     if (err == -ENOENT) {
         load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
     } else if (err) {
         error_prepend(errp, "unable to read FDT load address from FIT: ");
         ret = err;
Signature · 1 line
2.21.0
#1 lore raw
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
 hw/core/loader-fit.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
     err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
     if (err == -ENOENT) {
         load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
     } else if (err) {
         error_prepend(errp, "unable to read FDT load address from FIT: ");
         ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Not fixed:

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

We need to use a local_err here.

I'll search for the pattern, and post fix(es).

#2 lore raw
Quoted 32 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
  hw/core/loader-fit.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
      err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
      if (err == -ENOENT) {
          load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
      } else if (err) {
          error_prepend(errp, "unable to read FDT load address from FIT: ");
          ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

Quoted 8 lines

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Not fixed:

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

No, if it is error_abort or error_fatal, we will not reach this point, the execution finished before, when error was set.

Quoted 5 lines

We need to use a local_err here.

I'll search for the pattern, and post fix(es).

Signature · 2 lines
Best regards,
Vladimir
#3 lore raw
Quoted 37 lines · Vladimir Sementsov-Ogievskiy
Quoted 36 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
  hw/core/loader-fit.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
       err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
       if (err == -ENOENT) {
           load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
       } else if (err) {
           error_prepend(errp, "unable to read FDT load address from FIT: ");
           ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Quoted 7 lines
Quoted 4 lines

Not fixed:

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

No, if it is error_abort or error_fatal, we will not reach this point, the execution finished before, when error was set.

Ah, that is what you mention.. Hmm. Is it bad? At least crashing on error_abort without any recovery should not be bad..

Quoted 8 lines
Quoted 6 lines

We need to use a local_err here.

I'll search for the pattern, and post fix(es).

Signature · 2 lines
Best regards,
Vladimir
#4 lore raw
Quoted 40 lines · Vladimir Sementsov-Ogievskiy
Quoted 39 lines · Vladimir Sementsov-Ogievskiy
Quoted 38 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
  hw/core/loader-fit.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
       err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
       if (err == -ENOENT) {
           load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
       } else if (err) {
           error_prepend(errp, "unable to read FDT load address from FIT: ");
           ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Not fixed:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

No, if it is error_abort or error_fatal, we will not reach this point, the execution finished before, when error was set.

Ah, that is what you mention.. Hmm. Is it bad? At least crashing on error_abort without any recovery should not be bad..

Latent bugs aren't bad, but they could possibly become bad :)

When you pass &err to fit_load_fdt(), where @err is a local variable, the ENOENT case is not actually an error; fit_load_fdt() recovers fine and succeeds.

When you pass &error_fatal or &error_abort, it should likewise not be an error.

General rule: when you want to handle some (or all) of a function's errors yourself, you must not pass your own @errp argument. Instead, pass &err and propagate the errors you don't handle yourself with error_propagate(errp, err).

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

We need to use a local_err here.

I'll search for the pattern, and post fix(es).

Found several bugs already...

#5 lore raw
Quoted 51 lines · Markus Armbruster
Quoted 40 lines · Vladimir Sementsov-Ogievskiy
Quoted 39 lines · Vladimir Sementsov-Ogievskiy
Quoted 38 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
   hw/core/loader-fit.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
       err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
       if (err == -ENOENT) {
           load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
       } else if (err) {
           error_prepend(errp, "unable to read FDT load address from FIT: ");
           ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Not fixed:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

No, if it is error_abort or error_fatal, we will not reach this point, the execution finished before, when error was set.

Ah, that is what you mention.. Hmm. Is it bad? At least crashing on error_abort without any recovery should not be bad..

Quoted 8 lines

Latent bugs aren't bad, but they could possibly become bad :)

When you pass &err to fit_load_fdt(), where @err is a local variable, the ENOENT case is not actually an error; fit_load_fdt() recovers fine and succeeds.

When you pass &error_fatal or &error_abort, it should likewise not be an error.

Ah, yes, understand now.

Interesting, that auto-propageted errp will not catch this. It will only help with error_fatal, but not with error_abort..

So, in this case we should wrap error_abort too. And it in every function that uses error_free.

And this means, that in this case we can't make error_abort crash at point where actual error occures. That is very sad.

Quoted 12 lines

General rule: when you want to handle some (or all) of a function's errors yourself, you must not pass your own @errp argument. Instead, pass &err and propagate the errors you don't handle yourself with error_propagate(errp, err).

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

We need to use a local_err here.

I'll search for the pattern, and post fix(es).

Found several bugs already...

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw
Quoted 63 lines · Vladimir Sementsov-Ogievskiy
Quoted 51 lines · Markus Armbruster
Quoted 40 lines · Vladimir Sementsov-Ogievskiy
Quoted 39 lines · Vladimir Sementsov-Ogievskiy
Quoted 38 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
   hw/core/loader-fit.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
       err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
       if (err == -ENOENT) {
           load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
       } else if (err) {
           error_prepend(errp, "unable to read FDT load address from FIT: ");
           ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Not fixed:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

No, if it is error_abort or error_fatal, we will not reach this point, the execution finished before, when error was set.

Ah, that is what you mention.. Hmm. Is it bad? At least crashing on error_abort without any recovery should not be bad..

Quoted 8 lines

Latent bugs aren't bad, but they could possibly become bad :)

When you pass &err to fit_load_fdt(), where @err is a local variable, the ENOENT case is not actually an error; fit_load_fdt() recovers fine and succeeds.

When you pass &error_fatal or &error_abort, it should likewise not be an error.

Ah, yes, understand now.

Quoted 8 lines

Interesting, that auto-propageted errp will not catch this. It will only help with error_fatal, but not with error_abort..

So, in this case we should wrap error_abort too. And it in every function that uses error_free.

And this means, that in this case we can't make error_abort crash at point where actual error occures. That is very sad.

If your patches improve &error_abort's backtrace except for the (few) functions calling error_free(), then I'd call the situation "a bit sad" at most :)

[...]

#7 lore raw
In reply to Markus Armbruster
Quoted 65 lines · Markus Armbruster
Quoted 40 lines · Vladimir Sementsov-Ogievskiy
Quoted 39 lines · Vladimir Sementsov-Ogievskiy
Quoted 38 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

fit_load_fdt forget to check that errp is not NULL and to zero it after freeing. Fix it.

---
  hw/core/loader-fit.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader-fit.c +0 -0
index 953b16bc82..3ee9fb2f2e 100644
@@ -200,7 +200,10 @@ static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
       err = fit_image_addr(itb, img_off, "load", &load_addr, errp);

hw/core/loader-fit.c static int fit_load_fdt(const struct fit_loader *ldr, const void *itb, · line 209

Quoted 10 lines
hw/core/loader-fit.c +4 -1
       if (err == -ENOENT) {
           load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
-        error_free(*errp);
+        if (errp) {
+            error_free(*errp);
+            *errp = NULL;
+        }
       } else if (err) {
           error_prepend(errp, "unable to read FDT load address from FIT: ");
           ret = err;

This fixes latent bugs when fit_image_addr() fails with ENOENT:

* If a caller passes a null @errp, we crash dereferencing it

* Else, we pass a dangling Error * to the caller, and return success. If a caller dereferences the Error * regardless, we have a use-after-free.

Not fixed:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* If a caller passes &error_abort or &error_fatal, we die instead of taking the recovery path.

No, if it is error_abort or error_fatal, we will not reach this point, the execution finished before, when error was set.

Ah, that is what you mention.. Hmm. Is it bad? At least crashing on error_abort without any recovery should not be bad..

Latent bugs aren't bad, but they could possibly become bad :)

When you pass &err to fit_load_fdt(), where @err is a local variable, the ENOENT case is not actually an error; fit_load_fdt() recovers fine and succeeds.

When you pass &error_fatal or &error_abort, it should likewise not be an error.

General rule: when you want to handle some (or all) of a function's errors yourself, you must not pass your own @errp argument. Instead, pass &err and propagate the errors you don't handle yourself with error_propagate(errp, err).

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

We need to use a local_err here.

I'll search for the pattern, and post fix(es).

Found several bugs already...

[PATCH 00/21] Error handling fixes, may contain 4.2 material
Message-Id: <[email protected]>

I hope it doesn't clash too badly with your work.

PATCH 10 fixes the one we discussed above.