Commit dd9cef43 authored by Breno Leitao's avatar Breno Leitao Committed by Daniel Borkmann

bpf: test_maps: fix possible out of bound access warning

When compiling test_maps selftest with GCC-8, it warns that an array
might be indexed with a negative value, which could cause a negative
out of bound access, depending on parameters of the function. This
is the GCC-8 warning:

	gcc -Wall -O2 -I../../../include/uapi -I../../../lib -I../../../lib/bpf -I../../../../include/generated -DHAVE_GENHDR -I../../../include    test_maps.c /home/breno/Devel/linux/tools/testing/selftests/bpf/libbpf.a -lcap -lelf -lrt -lpthread -o /home/breno/Devel/linux/tools/testing/selftests/bpf/test_maps
	In file included from test_maps.c:16:
	test_maps.c: In function ‘run_all_tests’:
	test_maps.c:1079:10: warning: array subscript -1 is below array bounds of ‘pid_t[<Ube20> + 1]’ [-Warray-bounds]
	   assert(waitpid(pid[i], &status, 0) == pid[i]);
		  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
	test_maps.c:1059:6: warning: array subscript -1 is below array bounds of ‘pid_t[<Ube20> + 1]’ [-Warray-bounds]
	   pid[i] = fork();
	   ~~~^~~

This patch simply guarantees that the task(s) variables are unsigned,
thus, they could never be a negative number (which they are not in
current code anyway), hence avoiding an out of bound access warning.
Signed-off-by: default avatarBreno Leitao <leitao@debian.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent a5f2d082
...@@ -45,7 +45,7 @@ static int map_flags; ...@@ -45,7 +45,7 @@ static int map_flags;
} \ } \
}) })
static void test_hashmap(int task, void *data) static void test_hashmap(unsigned int task, void *data)
{ {
long long key, next_key, first_key, value; long long key, next_key, first_key, value;
int fd; int fd;
...@@ -135,7 +135,7 @@ static void test_hashmap(int task, void *data) ...@@ -135,7 +135,7 @@ static void test_hashmap(int task, void *data)
close(fd); close(fd);
} }
static void test_hashmap_sizes(int task, void *data) static void test_hashmap_sizes(unsigned int task, void *data)
{ {
int fd, i, j; int fd, i, j;
...@@ -155,7 +155,7 @@ static void test_hashmap_sizes(int task, void *data) ...@@ -155,7 +155,7 @@ static void test_hashmap_sizes(int task, void *data)
} }
} }
static void test_hashmap_percpu(int task, void *data) static void test_hashmap_percpu(unsigned int task, void *data)
{ {
unsigned int nr_cpus = bpf_num_possible_cpus(); unsigned int nr_cpus = bpf_num_possible_cpus();
BPF_DECLARE_PERCPU(long, value); BPF_DECLARE_PERCPU(long, value);
...@@ -282,7 +282,7 @@ static int helper_fill_hashmap(int max_entries) ...@@ -282,7 +282,7 @@ static int helper_fill_hashmap(int max_entries)
return fd; return fd;
} }
static void test_hashmap_walk(int task, void *data) static void test_hashmap_walk(unsigned int task, void *data)
{ {
int fd, i, max_entries = 1000; int fd, i, max_entries = 1000;
long long key, value, next_key; long long key, value, next_key;
...@@ -353,7 +353,7 @@ static void test_hashmap_zero_seed(void) ...@@ -353,7 +353,7 @@ static void test_hashmap_zero_seed(void)
close(second); close(second);
} }
static void test_arraymap(int task, void *data) static void test_arraymap(unsigned int task, void *data)
{ {
int key, next_key, fd; int key, next_key, fd;
long long value; long long value;
...@@ -408,7 +408,7 @@ static void test_arraymap(int task, void *data) ...@@ -408,7 +408,7 @@ static void test_arraymap(int task, void *data)
close(fd); close(fd);
} }
static void test_arraymap_percpu(int task, void *data) static void test_arraymap_percpu(unsigned int task, void *data)
{ {
unsigned int nr_cpus = bpf_num_possible_cpus(); unsigned int nr_cpus = bpf_num_possible_cpus();
BPF_DECLARE_PERCPU(long, values); BPF_DECLARE_PERCPU(long, values);
...@@ -504,7 +504,7 @@ static void test_arraymap_percpu_many_keys(void) ...@@ -504,7 +504,7 @@ static void test_arraymap_percpu_many_keys(void)
close(fd); close(fd);
} }
static void test_devmap(int task, void *data) static void test_devmap(unsigned int task, void *data)
{ {
int fd; int fd;
__u32 key, value; __u32 key, value;
...@@ -519,7 +519,7 @@ static void test_devmap(int task, void *data) ...@@ -519,7 +519,7 @@ static void test_devmap(int task, void *data)
close(fd); close(fd);
} }
static void test_queuemap(int task, void *data) static void test_queuemap(unsigned int task, void *data)
{ {
const int MAP_SIZE = 32; const int MAP_SIZE = 32;
__u32 vals[MAP_SIZE + MAP_SIZE/2], val; __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
...@@ -577,7 +577,7 @@ static void test_queuemap(int task, void *data) ...@@ -577,7 +577,7 @@ static void test_queuemap(int task, void *data)
close(fd); close(fd);
} }
static void test_stackmap(int task, void *data) static void test_stackmap(unsigned int task, void *data)
{ {
const int MAP_SIZE = 32; const int MAP_SIZE = 32;
__u32 vals[MAP_SIZE + MAP_SIZE/2], val; __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
...@@ -642,7 +642,7 @@ static void test_stackmap(int task, void *data) ...@@ -642,7 +642,7 @@ static void test_stackmap(int task, void *data)
#define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o" #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
#define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o" #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
#define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o" #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o"
static void test_sockmap(int tasks, void *data) static void test_sockmap(unsigned int tasks, void *data)
{ {
struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break; struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break; int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
...@@ -1268,10 +1268,11 @@ static void test_map_large(void) ...@@ -1268,10 +1268,11 @@ static void test_map_large(void)
} }
#define run_parallel(N, FN, DATA) \ #define run_parallel(N, FN, DATA) \
printf("Fork %d tasks to '" #FN "'\n", N); \ printf("Fork %u tasks to '" #FN "'\n", N); \
__run_parallel(N, FN, DATA) __run_parallel(N, FN, DATA)
static void __run_parallel(int tasks, void (*fn)(int task, void *data), static void __run_parallel(unsigned int tasks,
void (*fn)(unsigned int task, void *data),
void *data) void *data)
{ {
pid_t pid[tasks]; pid_t pid[tasks];
...@@ -1312,7 +1313,7 @@ static void test_map_stress(void) ...@@ -1312,7 +1313,7 @@ static void test_map_stress(void)
#define DO_UPDATE 1 #define DO_UPDATE 1
#define DO_DELETE 0 #define DO_DELETE 0
static void test_update_delete(int fn, void *data) static void test_update_delete(unsigned int fn, void *data)
{ {
int do_update = ((int *)data)[1]; int do_update = ((int *)data)[1];
int fd = ((int *)data)[0]; int fd = ((int *)data)[0];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment