Skip to contentSkip to conversation

envblk: Fix buffer overrun when attempting to shrink a variable value

PATCH In review Javier Martinez Canillas 2 participants 2 messages May 12, 2020 – May 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 envblk: Fix buffer overrun when attempting to shrink a variable value

If an existing variable is set with a value whose length is smaller than the current value, a memory corruption can happen due copying padding '#' characters outside of the environment block buffer.

This is caused by a wrong calculation of the previous free space position after moving backward the characters that followed the old variable value.

That position is calculated to fill the remaining of the buffer with the padding '#' characters. But since isn't calculated correctly, it can lead to copies outside of the buffer.

The issue can be reproduced by creating a variable with a large value and then try to set a new value that is much smaller:

$ grub2-editenv --version
grub2-editenv (GRUB) 2.04
$ grub2-editenv env create
$ grub2-editenv env set a="$(for i in {1..500}; do var="b$var"; done; echo $var)"
$ wc -c env
1024 grubenv
$ grub2-editenv env set a="$(for i in {1..50}; do var="b$var"; done; echo $var)"
malloc(): corrupted top size
Aborted (core dumped)
$ wc -c env
0 grubenv

---

 grub-core/lib/envblk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/envblk.c +1 -1
index 230e0e9d9ab..2e4e78b132d 100644
@@ -143,7 +143,7 @@ grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value)
               /* Move the following characters backward, and fill the new
                  space with harmless characters.  */
               grub_memmove (p + vl, p + len, pend - (p + len));
-              grub_memset (space + len - vl, '#', len - vl);
+              grub_memset (space - (len - vl), '#', len - vl);
             }
           else
             /* Move the following characters forward.  */
Signature · 1 line
2.25.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Javier Martinez Canillas

If an existing variable is set with a value whose length is smaller than the current value, a memory corruption can happen due copying padding '#' characters outside of the environment block buffer.

This is caused by a wrong calculation of the previous free space position after moving backward the characters that followed the old variable value.

That position is calculated to fill the remaining of the buffer with the padding '#' characters. But since isn't calculated correctly, it can lead to copies outside of the buffer.

The issue can be reproduced by creating a variable with a large value and then try to set a new value that is much smaller:

$ grub2-editenv --version
grub2-editenv (GRUB) 2.04
$ grub2-editenv env create
$ grub2-editenv env set a="$(for i in {1..500}; do var="b$var"; done; echo $var)"
$ wc -c env
1024 grubenv
$ grub2-editenv env set a="$(for i in {1..50}; do var="b$var"; done; echo $var)"
malloc(): corrupted top size
Aborted (core dumped)
Quoted 5 lines
$ wc -c env
0 grubenv

Daniel