Skip to contentSkip to conversation

perf/x86/amd/ibs: Don't leak stack contents through PERF_SAMPLE_WEIGHT_STRUCT

PATCH New Usama Arif 2 participants 2 messages Nov 23

Subsystems (3)
lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH 1/1 perf/x86/amd/ibs: Don't leak stack contents through PERF_SAMPLE_WEIGHT_STRUCT

For a load that missed the data cache, perf_ibs_parse_ld_st_data() reports the miss latency as the sample weight and marks PERF_SAMPLE_WEIGHT_TYPE as done, so perf_prepare_sample() leaves the field alone. With PERF_SAMPLE_WEIGHT_STRUCT it writes only var1_dw and var2_w, but perf_output_sample() copies all 8 bytes of the union into the ring buffer. var3_w therefore carries whatever was left in the on-stack struct perf_sample_data in perf_ibs_handle_irq().

CONFIG_INIT_STACK_ALL_ZERO hides this by clearing the struct on entry. Without it, every such sample hands 2 bytes of kernel stack to user space. "perf mem record" asks for PERF_SAMPLE_WEIGHT_STRUCT on x86, and since v6.14 swfilt lets an unprivileged user sample their own tasks with IBS.

Write var3_w as well.

Fixes 6b2ae4952ef8 ("perf/x86/amd: Support PERF_SAMPLE_{WEIGHT|WEIGHT_STRUCT}")
---
 arch/x86/events/amd/ibs.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/events/amd/ibs.c +1 -0
index 66e67e27224f18..9aa47751b37f6c 100644
@@ -1376,6 +1376,7 @@ static void perf_ibs_parse_ld_st_data(__u64 sample_type,
 		if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
 			data->weight.var1_dw = op_data3.dc_miss_lat;
 			data->weight.var2_w = op_data.tag_to_ret_ctr;
+			data->weight.var3_w = 0;
 		} else if (sample_type & PERF_SAMPLE_WEIGHT) {
 			data->weight.full = op_data3.dc_miss_lat;
 		}
Signature · 1 line
2.53.1-Meta
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

For a load that missed the data cache, perf_ibs_parse_ld_st_data() reports the miss latency as the sample weight and marks PERF_SAMPLE_WEIGHT_TYPE as done, so perf_prepare_sample() leaves the field alone. With PERF_SAMPLE_WEIGHT_STRUCT it writes only var1_dw and var2_w, but perf_output_sample() copies all 8 bytes of the union into the ring buffer. var3_w therefore carries whatever was left in the on-stack struct perf_sample_data in perf_ibs_handle_irq().

CONFIG_INIT_STACK_ALL_ZERO hides this by clearing the struct on entry. Without it, every such sample hands 2 bytes of kernel stack to user space. "perf mem record" asks for PERF_SAMPLE_WEIGHT_STRUCT on x86, and since v6.14 swfilt lets an unprivileged user sample their own tasks with IBS.

Quoted 5 lines

Write var3_w as well.

Fixes 6b2ae4952ef8 ("perf/x86/amd: Support PERF_SAMPLE_{WEIGHT|WEIGHT_STRUCT}")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1