# OpenSnitch - 2026 # # On Debian based distros we need the following 2 directories. # Otherwise, just use the kernel headers from the kernel sources. # KERNEL_VER ?= $(shell find /lib/modules/* -maxdepth 1 \( -type d -o -type l \) \( -name "build" -o -name "source" \) | sort | tail -1 | cut -d/ -f4) ifeq ($(KERNEL_VER),) $(error KERNEL_VER is missing.) endif KERNEL_DIR ?= $(shell find /lib/modules/$(KERNEL_VER) -maxdepth 1 \( -type d -o -type l \) \( -name "build" -o -name "source" \) | sort | tail -1) ifeq ($(KERNEL_DIR),) $(error KERNEL_DIR is missing.) endif KERNEL_HEADERS ?= /usr/src/linux-headers-$(KERNEL_VER)/ # use KERNEL_ARCH, as ARCH is being changed KERNEL_ARCH ?= $(shell uname -m) KERNEL_6_19_CHECK = $(shell expr "$(KERNEL_VER)" \>= "6.19") CC = clang BPF_TARGET ?= bpf LLC ?= llc OPT ?= opt READELF ?= readelf PREFIX ?= /usr/local LIBDIR ?= $(PREFIX)/lib ARCH ?= $(shell uname -m) # as in /usr/src/linux-headers-*/arch/ # TODO: extract correctly the archs, and add more if needed. ifeq ($(ARCH),x86_64) ARCH := x86 else ifeq ($(ARCH),i686) ARCH := x86 else ifeq ($(ARCH),armv7l) ARCH := arm else ifeq ($(ARCH),armv8l) ARCH := arm else ifeq ($(ARCH),aarch64) ARCH := arm64 else ifeq ($(ARCH),loongarch64) ARCH := loongarch else ifeq ($(ARCH),riscv64) ARCH := riscv else ifeq ($(ARCH),s390x) ARCH := s390 endif # https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++#tldr-what-compiler-options-should-i-use EXTRA_FLAGS = -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 ifeq ($(ARCH),arm) # on previous archs, it fails with "SMP not supported on pre-ARMv6" EXTRA_FLAGS += -D__LINUX_ARM_ARCH__=7 endif # These objects execute in the BPF VM, not as host-ISA code: do not add # -fcf-protection / -mbranch-protection. Keep the native frontend for the # kernel headers, then explicitly retarget the optimized IR below. ifeq ($(ARCH),x86) # Kbuild normally supplies this marker when parsing x86 ftrace headers. # BPF programs do not need host function-entry instrumentation (-mfentry). EXTRA_FLAGS += -DCC_USING_FENTRY endif # https://lore.kernel.org/bpf/20251208130748.68371-1-qmo@kernel.org/ ifeq ($(KERNEL_6_19_CHECK),1) EXTRA_FLAGS += -Wno-microsoft-anon-tag -fms-extensions endif $(info ebpf_prog build env:) $(info ARCH = $(ARCH)) $(info KERNEL_VER = $(KERNEL_VER)) $(info KERNEL_DIR = $(KERNEL_DIR)) $(info KERNEL_HEADERS = $(KERNEL_HEADERS)) $(info KERNEL_ARCH = $(KERNEL_ARCH)) $(info EXTRA_FLAGS = $(EXTRA_FLAGS)) SRC := $(wildcard *.c) BIN := $(SRC:.c=.o) CFLAGS = -I. \ -I$(KERNEL_HEADERS)/arch/$(ARCH)/include/generated/ \ -I$(KERNEL_HEADERS)/include \ -include $(KERNEL_DIR)/include/linux/kconfig.h \ -I$(KERNEL_DIR)/include \ -I$(KERNEL_DIR)/include/uapi \ -I$(KERNEL_DIR)/include/generated/uapi \ -I$(KERNEL_DIR)/arch/$(ARCH)/include \ -I$(KERNEL_DIR)/arch/$(ARCH)/include/generated \ -I$(KERNEL_DIR)/arch/$(ARCH)/include/uapi \ -I$(KERNEL_DIR)/arch/$(ARCH)/include/generated/uapi \ -I$(KERNEL_DIR)/tools/testing/selftests/bpf/ \ -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ -D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \ $(EXTRA_FLAGS) \ -Wunused \ -Wno-unused-value \ -Wno-gnu-variable-sized-type-not-at-end \ -Wno-address-of-packed-member \ -Wno-tautological-compare \ -Wno-unknown-warning-option \ -fno-stack-protector \ -g -O2 -emit-llvm .PHONY: all check install clean .DELETE_ON_ERROR: all: $(BIN) %.bc: %.c Makefile $(wildcard *.h bpf_headers/*.h) $(CC) $(CFLAGS) -c $< -o $@ # Full kernel headers need the native frontend (word size, named address # spaces, etc.). LLVM 23 rejects feeding its native DataLayout to BPF llc. # Ask the selected compiler for BPF's layout instead of hard-coding one. %.bpf.bc: %.bc @set -eu; \ ir=$$($(CC) -target $(BPF_TARGET) -S -emit-llvm -x c /dev/null -o -); \ layout=$$(printf '%s\n' "$$ir" | sed -n 's/^target datalayout = "\([^"]*\)"/\1/p'); \ test -n "$$layout" || { echo 'Cannot determine BPF DataLayout' >&2; exit 1; }; \ $(OPT) --mtriple=$(BPF_TARGET) --data-layout="$$layout" -passes=verify $< -o $@ %.o: %.bpf.bc $(LLC) -march=$(BPF_TARGET) -mcpu=generic -filetype=obj -o $@ $< # Unprivileged structural check only; verifier acceptance/attach needs a live test. check: $(BIN) @set -eu; \ for obj in $(BIN); do \ header=$$(LC_ALL=C $(READELF) -h "$$obj"); \ printf '%s\n' "$$header" | grep -Eq 'Machine:.*(Linux BPF|EM_BPF)' || \ { echo "Not a BPF object: $$obj" >&2; exit 1; }; \ case "$$obj" in \ opensnitch.o) section='kprobe/tcp_v4_connect' ;; \ opensnitch-dns.o) section='uprobe/getaddrinfo' ;; \ opensnitch-procs.o) section='tracepoint/syscalls/sys_enter_execve' ;; \ *) echo "No section check defined for $$obj" >&2; exit 1 ;; \ esac; \ sections=$$(LC_ALL=C $(READELF) -SW "$$obj"); \ printf '%s\n' "$$sections" | grep -Fq "$$section" || \ { echo "Missing $$section in $$obj" >&2; exit 1; }; \ echo "BPF object/section OK: $$obj"; \ done # Separate from daemon/UI installation; copies only checked objects, never # changes service state. DESTDIR supports unprivileged package staging. install: check install -d "$(DESTDIR)$(LIBDIR)/opensnitchd/ebpf" @set -eu; for obj in $(BIN); do \ install -m644 "$$obj" "$(DESTDIR)$(LIBDIR)/opensnitchd/ebpf/$$obj"; \ done clean: rm -f $(BIN) $(SRC:.c=.bc) $(SRC:.c=.bpf.bc) .SUFFIXES: