summaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorFlorent Revest <revest@chromium.org>2021-05-17 11:28:30 +0200
committerDaniel Borkmann <daniel@iogearbox.net>2021-05-20 23:48:38 +0200
commit0af02eb2a7d76ca85a1ecaf4b3775e2c86408fab (patch)
tree258cbac9f9e49aca8f960e3eb45f200189b2ffd2 /kernel
parent8afcc19fbf083a8459284d9a29b4b5ac1cb2396c (diff)
downloadlinux-stable-0af02eb2a7d76ca85a1ecaf4b3775e2c86408fab.tar.gz
linux-stable-0af02eb2a7d76ca85a1ecaf4b3775e2c86408fab.tar.bz2
linux-stable-0af02eb2a7d76ca85a1ecaf4b3775e2c86408fab.zip
bpf: Avoid using ARRAY_SIZE on an uninitialized pointer
The cppcheck static code analysis reported the following error: if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) { ^ ARRAY_SIZE is a macro that expands to sizeofs, so bufs is not actually dereferenced at runtime, and the code is actually safe. But to keep things tidy, this patch removes the need for a call to ARRAY_SIZE by extracting the size of the array into a macro. Cppcheck should no longer be confused and the code ends up being a bit cleaner. Fixes: e2d5b2bb769f ("bpf: Fix nested bpf_bprintf_prepare with more per-cpu buffers") Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Florent Revest <revest@chromium.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Song Liu <song@kernel.org> Link: https://lore.kernel.org/bpf/20210517092830.1026418-2-revest@chromium.org
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/helpers.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 3a5ab614cbb0..73443498d88f 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -698,8 +698,9 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
#define MAX_BPRINTF_BUF_LEN 512
/* Support executing three nested bprintf helper calls on a given CPU */
+#define MAX_BPRINTF_NEST_LEVEL 3
struct bpf_bprintf_buffers {
- char tmp_bufs[3][MAX_BPRINTF_BUF_LEN];
+ char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
};
static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
@@ -711,7 +712,7 @@ static int try_get_fmt_tmp_buf(char **tmp_buf)
preempt_disable();
nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
- if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
+ if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
this_cpu_dec(bpf_bprintf_nest_level);
preempt_enable();
return -EBUSY;