Skip to contentSkip to conversation

binfmt_flat: Fix integer overflow bug on 32 bit systems

PATCH Applied Dan Carpenter 3 participants 4 messages Dec 4, 2024 – Jan 10, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 binfmt_flat: Fix integer overflow bug on 32 bit systems

Most of these sizes and counts are capped at 256MB so the math doesn't result in an integer overflow. The "relocs" count needs to be checked as well. Otherwise on 32bit systems the calculation of "full_data" could be wrong.

	full_data = data_len + relocs * sizeof(unsigned long);
Fixes c995ee28d29d ("binfmt_flat: prevent kernel dammage from corrupted executable headers")
---
 fs/binfmt_flat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/binfmt_flat.c +1 -1
index 390808ce935d..b5b5ca1a44f7 100644
@@ -478,7 +478,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 	 * 28 bits (256 MB) is way more than reasonable in this case.
 	 * If some top bits are set we have probable binary corruption.
 	*/
-	if ((text_len | data_len | bss_len | stack_len | full_data) >> 28) {
+	if ((text_len | data_len | bss_len | stack_len | relocs | full_data) >> 28) {
 		pr_err("bad header\n");
 		ret = -ENOEXEC;
 		goto err;
Signature · 1 line
2.45.2
#1 lore raw

Ping.

regards,
dan carpenter

Quoted 29 lines · Dan Carpenter

Most of these sizes and counts are capped at 256MB so the math doesn't result in an integer overflow. The "relocs" count needs to be checked as well. Otherwise on 32bit systems the calculation of "full_data" could be wrong.

	full_data = data_len + relocs * sizeof(unsigned long);
Fixes c995ee28d29d ("binfmt_flat: prevent kernel dammage from corrupted executable headers")
---
 fs/binfmt_flat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/binfmt_flat.c +1 -1
index 390808ce935d..b5b5ca1a44f7 100644
@@ -478,7 +478,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 	 * 28 bits (256 MB) is way more than reasonable in this case.
 	 * If some top bits are set we have probable binary corruption.
 	*/
-	if ((text_len | data_len | bss_len | stack_len | full_data) >> 28) {
+	if ((text_len | data_len | bss_len | stack_len | relocs | full_data) >> 28) {
 		pr_err("bad header\n");
 		ret = -ENOEXEC;
 		goto err;
Signature · 1 line
2.45.2
Acked-by
#2 lore raw
Quoted 16 lines · Dan Carpenter

Ping.

regards,
dan carpenter

Quoted 10 lines · Dan Carpenter

Most of these sizes and counts are capped at 256MB so the math doesn't result in an integer overflow. The "relocs" count needs to be checked as well. Otherwise on 32bit systems the calculation of "full_data" could be wrong.

	full_data = data_len + relocs * sizeof(unsigned long);
Fixes c995ee28d29d ("binfmt_flat: prevent kernel dammage from corrupted executable headers")
Quoted 20 lines
Quoted 19 lines
---
 fs/binfmt_flat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/binfmt_flat.c +1 -1
index 390808ce935d..b5b5ca1a44f7 100644
@@ -478,7 +478,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 	 * 28 bits (256 MB) is way more than reasonable in this case.
 	 * If some top bits are set we have probable binary corruption.
 	*/
-	if ((text_len | data_len | bss_len | stack_len | full_data) >> 28) {
+	if ((text_len | data_len | bss_len | stack_len | relocs | full_data) >> 28) {
 		pr_err("bad header\n");
 		ret = -ENOEXEC;
 		goto err;
Signature · 1 line
2.45.2
maintainer Applied
#3 lore raw
In reply to Dan Carpenter
Quoted 8 lines · Dan Carpenter

Most of these sizes and counts are capped at 256MB so the math doesn't result in an integer overflow. The "relocs" count needs to be checked as well. Otherwise on 32bit systems the calculation of "full_data" could be wrong.

	full_data = data_len + relocs * sizeof(unsigned long);

[...]

Applied to for-next/topic/execve/core, thanks!

[1/1] binfmt_flat: Fix integer overflow bug on 32 bit systems

      https://git.kernel.org/kees/c/55cf2f4b945f

Take care,

Signature · 1 line
Kees Cook