Skip to contentSkip to conversation

tools/bootconfig: Fix integer overflow in size check

PATCH Superseded Masami Hiramatsu (Google) 2 participants 4 messages Sep 10

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

PATCH 1/1 tools/bootconfig: Fix integer overflow in size check

From: Masami Hiramatsu (Google) <[email protected]>

Sashiko reported that on 32-bit systems, if the size in the bootconfig footer is corrupted such that adding BOOTCONFIG_FOOTER_SIZE wraps around (for instance, if size is 0xFFFFFFFF), the size check in load_xbc_from_initrd() can be bypassed:

    if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
        pr_err("bootconfig size is too big\n");
        return -E2BIG;
    }

This would lead to load_xbc_fd() being called with a negative size, resulting in an undersized allocation, heap overflow on read, and out-of-bounds write on the terminating null byte.

Avoid the integer overflow by checking whether size is greater than stat.st_size - BOOTCONFIG_FOOTER_SIZE. Since stat.st_size is already verified to be at least BOOTCONFIG_FOOTER_SIZE earlier in the function, this subtraction will never underflow.

Fixes 950313ebf79c ("tools: bootconfig: Add bootconfig command")
Assisted-by Antigravity:gemini-3.8-flash
---
 tools/bootconfig/main.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bootconfig/main.c +1 -1
index 7dc9fff9b637..b380ad6777fa 100644
@@ -218,7 +218,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 	csum = le32toh(csum);
 
 	/* Wrong size error  */
-	if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
+	if (size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) {
 		pr_err("bootconfig size is too big\n");
 		return -E2BIG;
 	}
author maintainer
#1 lore raw
Quoted 21 lines · On Thu, 10 Sep 2026 09:30:46 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Sashiko reported that on 32-bit systems, if the size in the bootconfig footer is corrupted such that adding BOOTCONFIG_FOOTER_SIZE wraps around (for instance, if size is 0xFFFFFFFF), the size check in load_xbc_from_initrd() can be bypassed:

    if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
        pr_err("bootconfig size is too big\n");
        return -E2BIG;
    }

This would lead to load_xbc_fd() being called with a negative size, resulting in an undersized allocation, heap overflow on read, and out-of-bounds write on the terminating null byte.

Avoid the integer overflow by checking whether size is greater than stat.st_size - BOOTCONFIG_FOOTER_SIZE. Since stat.st_size is already verified to be at least BOOTCONFIG_FOOTER_SIZE earlier in the function, this subtraction will never underflow.

Quoted 4 lines
Fixes 950313ebf79c ("tools: bootconfig: Add bootconfig command")
Assisted-by Antigravity:gemini-3.8-flash

OOps, I forgot to add:

Thanks,

Quoted 19 lines
---
 tools/bootconfig/main.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bootconfig/main.c +1 -1
index 7dc9fff9b637..b380ad6777fa 100644
@@ -218,7 +218,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 	csum = le32toh(csum);
 
 	/* Wrong size error  */
-	if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
+	if (size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) {
 		pr_err("bootconfig size is too big\n");
 		return -E2BIG;
 	}
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#2 lore raw
In reply to Masami Hiramatsu (Google)
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Incomplete Fix / Integer Truncation Leading to Heap Overflow --

commit 2d027c4ee8625f17122dea0afb65c1e4b21ae531
Author: Masami Hiramatsu (Google) <[email protected]>

tools/bootconfig: Fix integer overflow in size check

This commit modifies the size check in load_xbc_from_initrd() to prevent an integer overflow on 32-bit systems. It changes the condition to subtract BOOTCONFIG_FOOTER_SIZE from stat.st_size instead of adding it to the bootconfig size.

tools/bootconfig/main.c static int load_xbc_from_initrd(int fd, char **buf) · line 221

Quoted 10 lines
tools/bootconfig/main.c +1 -1
index 7dc9fff9b6370..b380ad6777faa 100644
@@ -218,7 +218,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 	csum = le32toh(csum);
 
 	/* Wrong size error  */
-	if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
+	if (size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) {

[Severity: High]
This is a pre-existing issue, but the fix doesn't seem to completely resolve the integer truncation and heap overflow vulnerabilities.

Can this size check still be bypassed for large files? Since stat.st_size is a 64-bit off_t, if the file size is larger than 4.29 GB, stat.st_size - BOOTCONFIG_FOOTER_SIZE can exceed UINT32_MAX. A corrupted uint32_t size of 0xFFFFFFFF would bypass this check because it is always smaller than the right side.

If bypassed, does passing 0xFFFFFFFF to load_xbc_fd() cause an integer truncation and heap overflow?

tools/bootconfig/main.c:load_xbc_fd() {
    ...
    static int load_xbc_fd(int fd, char **buf, int size)
    ...
        *buf = malloc(size + 1);
        if (!*buf)
            return -ENOMEM;
    
        ret = read(fd, *buf, size);
    ...
        (*buf)[size] = '\0';
    ...
}

Because size is an int, 0xFFFFFFFF is truncated to -1. This results in malloc(0) which returns a valid heap chunk. The read() call then receives -1 which gets cast to SIZE_MAX, overflowing the heap chunk. Finally, a null byte is written to a negative array index (-1), causing an out-of-bounds write.

Does this same vulnerability also affect load_xbc_file() when processing standalone files >= 2GB?

tools/bootconfig/main.c:load_xbc_file() {
    ...
    ret = fstat(fd, &stat);
    ...
    ret = load_xbc_fd(fd, buf, stat.st_size);
    ...
}

This implicitly truncates the 64-bit off_t stat.st_size into the 32-bit int size parameter of load_xbc_fd().

Quoted 3 lines
tools/bootconfig/main.c +0 -0
 		pr_err("bootconfig size is too big\n");
 		return -E2BIG;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178900024679.183840.10345859264677533824.stgit@devnote2?part=1
author maintainer
#3 lore raw
Quoted 56 lines · On Thu, 10 Sep 2026 00:40:55 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Incomplete Fix / Integer Truncation Leading to Heap Overflow --

commit 2d027c4ee8625f17122dea0afb65c1e4b21ae531
Author: Masami Hiramatsu (Google) <[email protected]>

tools/bootconfig: Fix integer overflow in size check

This commit modifies the size check in load_xbc_from_initrd() to prevent an integer overflow on 32-bit systems. It changes the condition to subtract BOOTCONFIG_FOOTER_SIZE from stat.st_size instead of adding it to the bootconfig size.

tools/bootconfig/main.c static int load_xbc_from_initrd(int fd, char **buf) · line 221

Quoted 10 lines
tools/bootconfig/main.c +1 -1
index 7dc9fff9b6370..b380ad6777faa 100644
@@ -218,7 +218,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 	csum = le32toh(csum);
 
 	/* Wrong size error  */
-	if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
+	if (size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) {

[Severity: High]
This is a pre-existing issue, but the fix doesn't seem to completely resolve the integer truncation and heap overflow vulnerabilities.

Can this size check still be bypassed for large files? Since stat.st_size is a 64-bit off_t, if the file size is larger than 4.29 GB, stat.st_size - BOOTCONFIG_FOOTER_SIZE can exceed UINT32_MAX. A corrupted uint32_t size of 0xFFFFFFFF would bypass this check because it is always smaller than the right side.

If bypassed, does passing 0xFFFFFFFF to load_xbc_fd() cause an integer truncation and heap overflow?

tools/bootconfig/main.c:load_xbc_fd() {
    ...
    static int load_xbc_fd(int fd, char **buf, int size)
    ...
        *buf = malloc(size + 1);
        if (!*buf)
            return -ENOMEM;
    
        ret = read(fd, *buf, size);
    ...
        (*buf)[size] = '\0';
    ...
}
Quoted 4 lines

Because size is an int, 0xFFFFFFFF is truncated to -1. This results in malloc(0) which returns a valid heap chunk. The read() call then receives -1 which gets cast to SIZE_MAX, overflowing the heap chunk. Finally, a null byte is written to a negative array index (-1), causing an out-of-bounds write.

This part should be fixed in v2. But below is another problem.

Quoted 12 lines

Does this same vulnerability also affect load_xbc_file() when processing standalone files >= 2GB?

tools/bootconfig/main.c:load_xbc_file() {
    ...
    ret = fstat(fd, &stat);
    ...
    ret = load_xbc_fd(fd, buf, stat.st_size);
    ...
}
Quoted 6 lines

This implicitly truncates the 64-bit off_t stat.st_size into the 32-bit int size parameter of load_xbc_fd().

Quoted 3 lines
tools/bootconfig/main.c +0 -0
 		pr_err("bootconfig size is too big\n");
 		return -E2BIG;
 	}

OK, let me update the patch.

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>