diff mbox series

[bpf-next] libbpf: fix realloc usage in bpf_core_find_cands

Message ID 20200124201847.212528-1-andriin@fb.com
State Accepted
Delegated to: BPF Maintainers
Headers show
Series [bpf-next] libbpf: fix realloc usage in bpf_core_find_cands | expand

Commit Message

Andrii Nakryiko Jan. 24, 2020, 8:18 p.m. UTC
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")
Reported-by: William Smith <williampsmith@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Yonghong Song Jan. 24, 2020, 9:28 p.m. UTC | #1
On 1/24/20 12:18 PM, Andrii Nakryiko wrote:
> 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")
> Reported-by: William Smith <williampsmith@fb.com>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Acked-by: Yonghong Song <yhs@fb.com>
Daniel Borkmann Jan. 24, 2020, 9:35 p.m. UTC | #2
On 1/24/20 9:18 PM, Andrii Nakryiko wrote:
> 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")
> Reported-by: William Smith <williampsmith@fb.com>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Applied, thanks!
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ae34b681ae82..b581cb52ee5c 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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;