From 2de3b87122c18b58b3e2b32ab2e81ac43774a7aa Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Wed, 16 Mar 2022 11:19:14 -0600 Subject: bpf: pfc: Add handling for 0 syscalls in the binary tree Handle the unlikely case where a user has chosen the binary tree optimization but has zero syscalls in their filter. Fixes: https://github.com/seccomp/libseccomp/issues/370 Fixes: a3732b32b8e67 ("bpf:pfc: Add optimization option to use a binary tree") Signed-off-by: Tom Hromatka Acked-by: Paul Moore --- src/gen_bpf.c | 3 +++ src/gen_pfc.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/gen_bpf.c b/src/gen_bpf.c index c878f44..7131761 100644 --- a/src/gen_bpf.c +++ b/src/gen_bpf.c @@ -1348,6 +1348,9 @@ static int _get_bintree_levels(unsigned int syscall_cnt) { unsigned int i = 2, max_level = SYSCALLS_PER_NODE * 2; + if (syscall_cnt == 0) + return 0; + while (max_level < syscall_cnt) { max_level <<= 1; i++; diff --git a/src/gen_pfc.c b/src/gen_pfc.c index c7fb536..4916055 100644 --- a/src/gen_pfc.c +++ b/src/gen_pfc.c @@ -275,6 +275,9 @@ static int _get_bintree_levels(unsigned int syscall_cnt, /* Only use a binary tree if requested */ return 0; + if (syscall_cnt == 0) + return 0; + do { max_level = SYSCALLS_PER_NODE << i; i++; -- cgit v1.2.1