Skip to contentSkip to conversation

libbpf: fix realloc usage in bpf_core_find_cands

PATCH Applied Andrii Nakryiko 3 participants 3 messages Jan 24, 2020

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

PATCH 1/1 libbpf: fix realloc usage in bpf_core_find_cands

Fix bug requesting invalid size of reallocated array when constructing CO-RE relocation candidate list. This can cause problems if there are many potential candidates and a very fine-grained memory allocator bucket sizes are used.

Fixes ddc7c3042614 ("libbpf: implement BPF CO-RE offset relocation algorithm")
---
 tools/lib/bpf/libbpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +3 -1
index ae34b681ae82..b581cb52ee5c 100644
@@ -3870,7 +3870,9 @@ static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,
 		if (strncmp(local_name, targ_name, local_essent_len) == 0) {
 			pr_debug("[%d] %s: found candidate [%d] %s\n",
 				 local_type_id, local_name, i, targ_name);
-			new_ids = realloc(cand_ids->data, cand_ids->len + 1);
+			new_ids = reallocarray(cand_ids->data,
+					       cand_ids->len + 1,
+					       sizeof(*cand_ids->data));
 			if (!new_ids) {
 				err = -ENOMEM;
 				goto err_out;
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Andrii Nakryiko

Fix bug requesting invalid size of reallocated array when constructing CO-RE relocation candidate list. This can cause problems if there are many potential candidates and a very fine-grained memory allocator bucket sizes are used.

Fixes ddc7c3042614 ("libbpf: implement BPF CO-RE offset relocation algorithm")
maintainer Applied
#2 lore raw
In reply to Andrii Nakryiko
Quoted 7 lines · Andrii Nakryiko

Fix bug requesting invalid size of reallocated array when constructing CO-RE relocation candidate list. This can cause problems if there are many potential candidates and a very fine-grained memory allocator bucket sizes are used.

Fixes ddc7c3042614 ("libbpf: implement BPF CO-RE offset relocation algorithm")

Applied, thanks!