aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf
diff options
context:
space:
mode:
authorJakub Kicinski <jakub.kicinski@netronome.com>2018-07-10 17:43:05 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-07-11 16:13:34 -0400
commit531b014e7a2fedaeff0b19b2934d830cd4b35dc0 (patch)
tree0de8355e9d533b4f6f90c3b389f58c992e75bb14 /tools/lib/bpf
parent8d13406c02f9c38f106416e1dbe0e68059b9f59a (diff)
tools: bpf: make use of reallocarray
reallocarray() is a safer variant of realloc which checks for multiplication overflow in case of array allocation. Since it's not available in Glibc < 2.26 import kernel's overflow.h and add a static inline implementation when needed. Use feature detection to probe for existence of reallocarray. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r--tools/lib/bpf/Makefile6
-rw-r--r--tools/lib/bpf/libbpf.c10
2 files changed, 11 insertions, 5 deletions
diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile
index 5390e7725e43..7a8e4c98ef1a 100644
--- a/tools/lib/bpf/Makefile
+++ b/tools/lib/bpf/Makefile
@@ -66,7 +66,7 @@ ifndef VERBOSE
66endif 66endif
67 67
68FEATURE_USER = .libbpf 68FEATURE_USER = .libbpf
69FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf 69FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf reallocarray
70FEATURE_DISPLAY = libelf bpf 70FEATURE_DISPLAY = libelf bpf
71 71
72INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi -I$(srctree)/tools/perf 72INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi -I$(srctree)/tools/perf
@@ -120,6 +120,10 @@ ifeq ($(feature-libelf-getphdrnum), 1)
120 override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT 120 override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
121endif 121endif
122 122
123ifeq ($(feature-reallocarray), 0)
124 override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
125endif
126
123# Append required CFLAGS 127# Append required CFLAGS
124override CFLAGS += $(EXTRA_WARNINGS) 128override CFLAGS += $(EXTRA_WARNINGS)
125override CFLAGS += -Werror -Wall 129override CFLAGS += -Werror -Wall
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index f732237610e5..fd2c4433863d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -22,6 +22,7 @@
22 * License along with this program; if not, see <http://www.gnu.org/licenses> 22 * License along with this program; if not, see <http://www.gnu.org/licenses>
23 */ 23 */
24 24
25#define _GNU_SOURCE
25#include <stdlib.h> 26#include <stdlib.h>
26#include <stdio.h> 27#include <stdio.h>
27#include <stdarg.h> 28#include <stdarg.h>
@@ -41,6 +42,7 @@
41#include <sys/stat.h> 42#include <sys/stat.h>
42#include <sys/types.h> 43#include <sys/types.h>
43#include <sys/vfs.h> 44#include <sys/vfs.h>
45#include <tools/libc_compat.h>
44#include <libelf.h> 46#include <libelf.h>
45#include <gelf.h> 47#include <gelf.h>
46 48
@@ -321,7 +323,7 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
321 progs = obj->programs; 323 progs = obj->programs;
322 nr_progs = obj->nr_programs; 324 nr_progs = obj->nr_programs;
323 325
324 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1)); 326 progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
325 if (!progs) { 327 if (!progs) {
326 /* 328 /*
327 * In this case the original obj->programs 329 * In this case the original obj->programs
@@ -822,8 +824,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
822 continue; 824 continue;
823 } 825 }
824 826
825 reloc = realloc(reloc, 827 reloc = reallocarray(reloc, nr_reloc,
826 sizeof(*obj->efile.reloc) * nr_reloc); 828 sizeof(*obj->efile.reloc));
827 if (!reloc) { 829 if (!reloc) {
828 pr_warning("realloc failed\n"); 830 pr_warning("realloc failed\n");
829 err = -ENOMEM; 831 err = -ENOMEM;
@@ -1115,7 +1117,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1115 return -LIBBPF_ERRNO__RELOC; 1117 return -LIBBPF_ERRNO__RELOC;
1116 } 1118 }
1117 new_cnt = prog->insns_cnt + text->insns_cnt; 1119 new_cnt = prog->insns_cnt + text->insns_cnt;
1118 new_insn = realloc(prog->insns, new_cnt * sizeof(*insn)); 1120 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
1119 if (!new_insn) { 1121 if (!new_insn) {
1120 pr_warning("oom in prog realloc\n"); 1122 pr_warning("oom in prog realloc\n");
1121 return -ENOMEM; 1123 return -ENOMEM;