Allocate stats with MAX_CPUS and check for NULL index to attempt to fix seg fault on issue #10.

This commit is contained in:
gamemann
2021-12-06 16:54:20 +00:00
parent 3ebb0a763b
commit 21b15badf6
2 changed files with 16 additions and 3 deletions

View File

@@ -493,19 +493,31 @@ int main(int argc, char *argv[])
if ((curTime - statslastupdated) > 2 && !cfg.nostats)
{
__u32 key = 0;
struct stats stats[cpus];
//memset(&stats, 0, sizeof(struct stats) * cpus);
struct stats stats[MAX_CPUS];
//memset(stats, 0, sizeof(struct stats) * MAX_CPUS);
__u64 allowed = 0;
__u64 dropped = 0;
if (bpf_map_lookup_elem(statsmap, &key, stats) != 0)
{
fprintf(stderr, "Error performing stats map lookup.\n");
fprintf(stderr, "Error performing stats map lookup. Stats map FD => %d.\n", statsmap);
continue;
}
for (int i = 0; i < cpus; i++)
{
// Although this should NEVER happen, I'm seeing very strange behavior in the following GitHub issue.
// https://github.com/gamemann/XDP-Firewall/issues/10
// Therefore, before accessing stats[i], make sure the pointer to the specific CPU ID is not NULL.
if (&stats[i] == NULL)
{
fprintf(stderr, "Stats array at CPU ID #%d is NULL! Skipping...\n", i);
continue;
}
allowed += stats[i].allowed;
dropped += stats[i].dropped;
}

View File

@@ -5,6 +5,7 @@
#define MAX_PCKT_LENGTH 65535
#define MAX_FILTERS 100
#define MAX_TRACK_IPS 100000
#define MAX_CPUS 256
#ifdef __BPF__
#define likely(x) __builtin_expect(!!(x), 1)