device_tree: Fix integer overflowing in load_device_tree()
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
@@ -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
Quoted 20 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0
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 ;
David Gibson
Reviewed-byQuoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0device_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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
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
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0device_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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
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 ;
Quoted 32 lines · Markus Armbruster
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0device_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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Quoted 8 lines
Thanks!
Quoted 1 line
BTW how did you figure that out?
Downstream handling of upstream commit
da885fe1ee8led me to the function. I spotteddt_size=get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw howdt_sizegets 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 ;
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()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0device_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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Thanks!
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
BTW how did you figure that out?
Downstream handling of upstream commit
da885fe1ee8led me to the function. I spotteddt_size=get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw howdt_sizegets 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!
[...]
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()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0device_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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Thanks!
Quoted 1 line
BTW how did you figure that out?
Downstream handling of upstream commit
da885fe1ee8led me to the function. I spotteddt_size=get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw howdt_sizegets 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
[...]
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()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>--- device_tree.c | 4 ++++ 1 file changed, 4 insertions(+)device_tree.c +0
-0device_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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Thanks!
Quoted 1 line
BTW how did you figure that out?
Downstream handling of upstream commit
da885fe1ee8led me to the function. I spotteddt_size=get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw howdt_sizegets 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
[...]
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
da885fe1ee8led me to the function. I spotteddt_size=get_image_size(filename_path). Experience has taught me to check the left hand side's type. Bad. Then I saw howdt_sizegets 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 ;)
[...]
Quoted 9 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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.
Signed-off-by Markus Armbruster <[email protected]>
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
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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
Signed-off-by Markus Armbruster <[email protected]>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
Quoted 23 lines · Alistair Francis
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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
Signed-off-by Markus Armbruster <[email protected]>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
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()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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
Signed-off-by Markus Armbruster <[email protected]>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
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Markus Armbruster
If the value of
get_image_size()exceedsINT_MAX/ 2 - 10000, the computation of @dt_size overflows to a negative number, which then gets converted to a very largesize_tforg_malloc0()andload_image_size(). In the (fortunately improbable) caseg_malloc0()succeeds andload_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
Signed-off-by Markus Armbruster <[email protected]>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.