Skip to content

Commit 7f9c031

Browse files
theihoracmel
authored andcommitted
btf_encoder: Always initialize func_state to 0
BPF CI caught a segfault on aarch64 and s390x [1] after recent merges into the master branch. The segfault happened at free(func_state->annots) in btf_encoder__delete_saved_funcs(). func_state->annots arrived there uninitialized because after patch [2] in some cases func_state may be allocated with a realloc, but was not zeroed out. Fix this bug by always memset-ing a func_state to zero in btf_encoder__alloc_func_state(). [1] https://github.com/kernel-patches/bpf/actions/runs/12700574327 [2] https://lore.kernel.org/dwarves/[email protected]/ Tested-by: Alan Maguire <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Eduard Zingerman <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mykola Lysenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 24c655f commit 7f9c031

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

btf_encoder.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ static bool funcs__match(struct btf_encoder_func_state *s1,
10831083

10841084
static struct btf_encoder_func_state *btf_encoder__alloc_func_state(struct btf_encoder *encoder)
10851085
{
1086-
struct btf_encoder_func_state *tmp;
1086+
struct btf_encoder_func_state *state, *tmp;
10871087

10881088
if (encoder->func_states.cnt >= encoder->func_states.cap) {
10891089

@@ -1100,7 +1100,10 @@ static struct btf_encoder_func_state *btf_encoder__alloc_func_state(struct btf_e
11001100
encoder->func_states.array = tmp;
11011101
}
11021102

1103-
return &encoder->func_states.array[encoder->func_states.cnt++];
1103+
state = &encoder->func_states.array[encoder->func_states.cnt++];
1104+
memset(state, 0, sizeof(*state));
1105+
1106+
return state;
11041107
}
11051108

11061109
static int32_t btf_encoder__save_func(struct btf_encoder *encoder, struct function *fn, struct elf_function *func)

0 commit comments

Comments
 (0)