Skip to contentSkip to conversation

device_tree: Fix integer overflowing in load_device_tree()

PATCH In review Markus Armbruster 5 participants 14 messages Apr 9, 2019 – Apr 11, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device_tree: Fix integer overflowing in load_device_tree()

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +4 -0
index 296278e12a..f8b46b3c73 100644
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {
+        error_report("Device tree file '%s' is too large", filename_path);
+        goto fail;
+    }
 
     /* Expand to 2x size to give enough room for manipulation.  */
     dt_size += 10000;
Signature · 1 line
2.17.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

BTW how did you figure that out?

Quoted 7 lines
device_tree.c +3 -0
+        error_report("Device tree file '%s' is too large", filename_path);
+        goto fail;
+    }
 
     /* Expand to 2x size to give enough room for manipulation.  */
     dt_size += 10000;
Reviewed-by
#2 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

Quoted 1 line

As Philippe says, the test condition is kinda ugly and I hope we can refine it in future. But since it fixes a real problem for now,

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

Quoted 1 line

Thanks!

Quoted 1 line

BTW how did you figure that out?

Downstream handling of upstream commit da885fe1ee8 led me to the function. I spotted dt_size = get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw how dt_size gets increased. Worse.

Quoted 7 lines
Quoted 7 lines
device_tree.c +3 -0
+        error_report("Device tree file '%s' is too large", filename_path);
+        goto fail;
+    }
 
     /* Expand to 2x size to give enough room for manipulation.  */
     dt_size += 10000;
#4 lore raw
Quoted 32 lines · Markus Armbruster
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

Quoted 1 line
Quoted 8 lines

Thanks!

Quoted 1 line

BTW how did you figure that out?

Downstream handling of upstream commit da885fe1ee8 led me to the function. I spotted dt_size = get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw how dt_size gets increased. Worse.

So you genuinely neglected to mention Kurtis Miller then :)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
device_tree.c +3 -0
+        error_report("Device tree file '%s' is too large", filename_path);
+        goto fail;
+    }
 
     /* Expand to 2x size to give enough room for manipulation.  */
     dt_size += 10000;
#5 lore raw
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 34 lines · Markus Armbruster
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

Quoted 1 line

Thanks!

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

BTW how did you figure that out?

Downstream handling of upstream commit da885fe1ee8 led me to the function. I spotted dt_size = get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw how dt_size gets increased. Worse.

So you genuinely neglected to mention Kurtis Miller then :)

Explanation, not excuse: the only occurence of the name in my downstream reading was a two-liner BZ comment, which I totally missed in my haste to give the fix a chance to make 4.0. I certainly didn't mean to deprive him of credit!

[...]

#6 lore raw
Quoted 45 lines · Markus Armbruster
Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 40 lines · Markus Armbruster
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

Quoted 1 line

Thanks!

Quoted 1 line

BTW how did you figure that out?

Downstream handling of upstream commit da885fe1ee8 led me to the function. I spotted dt_size = get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw how dt_size gets increased. Worse.

Quoted 6 lines
Quoted 1 line

So you genuinely neglected to mention Kurtis Miller then :)

Explanation, not excuse: the only occurence of the name in my downstream reading was a two-liner BZ comment, which I totally missed in my haste to give the fix a chance to make 4.0. I certainly didn't mean to deprive him of credit!

No worries. I have sent the pull request and it includes the reported by.

Alistair

Quoted 3 lines

[...]

#7 lore raw

PATCH 1/1 device_tree: Fix integer overflowing in load_device_tree()

Quoted 44 lines · Alistair Francis
Quoted 43 lines · Markus Armbruster
Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 40 lines · Markus Armbruster
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

---
 device_tree.c | 4 ++++
 1 file changed, 4 insertions(+)
device_tree.c +0 -0
index 296278e12a..f8b46b3c73 100644

device_tree.c void *load_device_tree(const char *filename_path, int *sizep) · line 87

Quoted 4 lines
device_tree.c +1 -0
@@ -84,6 +84,10 @@ void *load_device_tree(const char *filename_path, int *sizep)
                      filename_path);
         goto fail;
     }
+    if (dt_size > INT_MAX / 2 - 10000) {

We should avoid magic number duplication.
That said, this patch looks safe.

Quoted 1 line

Thanks!

Quoted 1 line

BTW how did you figure that out?

Downstream handling of upstream commit da885fe1ee8 led me to the function. I spotted dt_size = get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw how dt_size gets increased. Worse.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

So you genuinely neglected to mention Kurtis Miller then :)

Explanation, not excuse: the only occurence of the name in my downstream reading was a two-liner BZ comment, which I totally missed in my haste to give the fix a chance to make 4.0. I certainly didn't mean to deprive him of credit!

My English teacher explained me neglected sounds like a reprimand/reproach (as in negligence), sorry I didn't mean to be rude here, I wanted to say something like "Peter remarked you did gave credits to the first one who reported this issue, but since you figured this bug via your own way, the omission was not intentional then."

Quoted 7 lines

No worries. I have sent the pull request and it includes the reported by.

Alistair

Quoted 3 lines

[...]

#8 lore raw
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Alistair Francis
Quoted 3 lines · Markus Armbruster
Quoted 2 lines · Philippe Mathieu-Daudé
Quoted 1 line · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

[...]

Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

BTW how did you figure that out?

Downstream handling of upstream commit da885fe1ee8 led me to the function. I spotted dt_size = get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw how dt_size gets increased. Worse.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

So you genuinely neglected to mention Kurtis Miller then :)

Explanation, not excuse: the only occurence of the name in my downstream reading was a two-liner BZ comment, which I totally missed in my haste to give the fix a chance to make 4.0. I certainly didn't mean to deprive him of credit!

Quoted 5 lines

My English teacher explained me neglected sounds like a reprimand/reproach (as in negligence), sorry I didn't mean to be rude here, I wanted to say something like "Peter remarked you did gave credits to the first one who reported this issue, but since you figured this bug via your own way, the omission was not intentional then."

Don't worry, my social interactions teacher taught me to assume good intent ;)

[...]

#9 lore raw
In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Quoted 3 lines

Fix by rejecting images whose size would overflow.

I think this patch is missing some attributions for the security researchers who found the issue initially. PJP's patch for this from a couple of weeks back has a reported-by credit:
https://patchew.org/QEMU/[email protected]/

thanks
-- PMM

#10 lore raw
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

Quoted 7 lines
Quoted 1 line

I think this patch is missing some attributions for the security researchers who found the issue initially. PJP's patch for this from a couple of weeks back has a reported-by credit:
https://patchew.org/QEMU/[email protected]/

It seems like from that discussion that this patch is the correct approach.

I can add the attributions and send a PR for 4.0. I'll send it by EOD unless anyone has any objections.

Alistair

Quoted 4 lines

thanks
-- PMM

#11 lore raw
Quoted 23 lines · Alistair Francis
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

Quoted 7 lines
Quoted 1 line

I think this patch is missing some attributions for the security researchers who found the issue initially. PJP's patch for this from a couple of weeks back has a reported-by credit:
https://patchew.org/QEMU/[email protected]/

Quoted 4 lines

It seems like from that discussion that this patch is the correct approach.

I can add the attributions and send a PR for 4.0. I'll send it by EOD unless anyone has any objections.

Thanks. I think given it's 21:30 here I'm going to postpone tagging rc3 til tomorrow (mid-afternoon UK time). I'm still hoping we can avoid an rc4...

thanks
-- PMM

#12 lore raw
Quoted 16 lines · Peter Maydell
Quoted 15 lines · Alistair Francis
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

I think this patch is missing some attributions for the security researchers who found the issue initially. PJP's patch for this from a couple of weeks back has a reported-by credit:
https://patchew.org/QEMU/[email protected]/

Uh, I missed that thread. Thanks for doing my homework for me!

Quoted 8 lines
Quoted 4 lines

It seems like from that discussion that this patch is the correct approach.

I can add the attributions and send a PR for 4.0. I'll send it by EOD unless anyone has any objections.

Thanks. I think given it's 21:30 here I'm going to postpone tagging rc3 til tomorrow (mid-afternoon UK time). I'm still hoping we can avoid an rc4...

Want me to look for a few more integer overflows today? ;-P

#13 lore raw
In reply to Peter Maydell
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster

If the value of get_image_size() exceeds INT_MAX / 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very large size_t for g_malloc0() and load_image_size(). In the (fortunately improbable) case g_malloc0() succeeds and load_image_size() survives, we'd assign the negative number to *sizep. What that would do to the callers I can't say, but it's unlikely to be good.

Fix by rejecting images whose size would overflow.

Quoted 7 lines
Quoted 1 line

I think this patch is missing some attributions for the security researchers who found the issue initially. PJP's patch for this from a couple of weeks back has a reported-by credit:
https://patchew.org/QEMU/[email protected]/

Oh I missed that thread while on PTO. This answers my "how did you figure that out?" question :)

Thanks,

Phil.