From 89644faae8489e9af8363efed0f2904d55d922db Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Fri, 18 Mar 2022 12:57:29 -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 (cherry picked from commit 2de3b87122c18b58b3e2b32ab2e81ac43774a7aa) --- 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 5f88972..90c38cc 100644 --- a/src/gen_bpf.c +++ b/src/gen_bpf.c @@ -1342,6 +1342,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 405f080..2e38eb6 100644 --- a/src/gen_pfc.c +++ b/src/gen_pfc.c @@ -271,6 +271,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