Skip to contentSkip to conversation

riscv: Pass correct size to snprintf()

PATCH Applied Björn Töpel 3 participants 3 messages Nov 4, 2024 – Nov 11, 2024

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

PATCH 1/1 riscv: Pass correct size to snprintf()

From: Bj?rn T?pel <[email protected]>

The snprintf() function does not get the correct size argument passed, when the FDT ISA string is built. Instead of adjusting the size for each extension, the full size is passed for every iteration. Doing so will make __snprinf_chk() bail out on glibc.

Adjust size for each iteration.

Fixes 8aff29e1dafe ("riscv: Append ISA extensions to the device tree")
---
 riscv/fdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
riscv/fdt.c +1 -1
index 8189601f46de..85c8f95604f6 100644
@@ -157,7 +157,7 @@ static void generate_cpu_nodes(void *fdt, struct kvm *kvm)
 					   isa_info_arr[i].name);
 				break;
 			}
-			pos += snprintf(cpu_isa + pos, CPU_ISA_MAX_LEN, "_%s",
+			pos += snprintf(cpu_isa + pos, CPU_ISA_MAX_LEN - pos, "_%s",
 					isa_info_arr[i].name);
 		}
 		cpu_isa[pos] = '\0';

base-commit: 3040b298156e4e2a82b27ac8db5bd63a72b3785b

Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Bj?rn T?pel

From: Bj?rn T?pel <[email protected]>

The snprintf() function does not get the correct size argument passed, when the FDT ISA string is built. Instead of adjusting the size for each extension, the full size is passed for every iteration. Doing so will make __snprinf_chk() bail out on glibc.

Adjust size for each iteration.

Fixes 8aff29e1dafe ("riscv: Append ISA extensions to the device tree")
---
 riscv/fdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
riscv/fdt.c +0 -0
index 8189601f46de..85c8f95604f6 100644

riscv/fdt.c static void generate_cpu_nodes(void *fdt, struct kvm *kvm) · line 160

Quoted 5 lines
riscv/fdt.c +1 -1
@@ -157,7 +157,7 @@ static void generate_cpu_nodes(void *fdt, struct kvm *kvm)
 					   isa_info_arr[i].name);
 				break;
 			}
-			pos += snprintf(cpu_isa + pos, CPU_ISA_MAX_LEN, "_%s",
+			pos += snprintf(cpu_isa + pos, CPU_ISA_MAX_LEN - pos, "_%s",

Just above this we confirm strlen(isa_info_arr[i].name) + pos + 1 is less than CPU_ISA_MAX_LEN, which means we should be able to use anything for size which is greater than or equal to strlen(isa_info_arr[i].name) + 1, as snprintf won't write more anyway. But, it's understandable that __snprinf_chk doesn't know that.

riscv/fdt.c static void generate_cpu_nodes(void *fdt, struct kvm *kvm) · line 163

Quoted 3 lines
riscv/fdt.c +0 -0
 					isa_info_arr[i].name);
 		}
 		cpu_isa[pos] = '\0';

Not part of this patch, but part of the context, so I'll comment on it anyway, this '\0' assignment could be removed. It looks like it's a left over from commit 7c9aac003925 ("riscv: Generate FDT at runtime for Guest/VM") which could have been removed with commit 8aff29e1dafe ("riscv:
Append ISA extensions to the device tree")

Anyway,

Thanks,
drew

Applied
#2 lore raw
In reply to Björn Töpel
Quoted 8 lines · Bj?rn T?pel

The snprintf() function does not get the correct size argument passed, when the FDT ISA string is built. Instead of adjusting the size for each extension, the full size is passed for every iteration. Doing so will make __snprinf_chk() bail out on glibc.

Adjust size for each iteration.

[...]

Applied to kvmtool (master), thanks!

[1/1] riscv: Pass correct size to snprintf()

      https://git.kernel.org/will/kvmtool/c/574bd7b432ec

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev