Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

build(linux): hardened, portable build; fix GLIBC_2.38 requirement (#146)

anof-cyber6ee21fd91116
diff --git a/Linux/build.sh b/Linux/build.sh
index 96f2672..e779a98 100755
--- a/Linux/build.sh
+++ b/Linux/build.sh
@@ -1,32 +1,42 @@
#!/bin/bash
# Builds libproxybridge.so, the CLI and (if GTK3 is present) the GUI.
-# Everything is compiled in a temporary directory; only the final binaries are copied to output/,
-# so the source tree never collects .o files, generated headers or libraries.
+# Everything is compiled in a temporary directory; only the final binaries are copied to output/, after an audit of their
+# portability (glibc floor) and hardening.
#
-# ./build.sh -> Linux/output/
+# ./build.sh -> Linux/output/
# OUTPUT_DIR=/some/dir ./build.sh
+# WERROR=1 ./build.sh warnings are errors (CI) LTO=0 ./build.sh no link-time optimisation
+# AUDIT=0 ./build.sh skip the binary audit STATIC_LIBBPF=0 ./build.sh use the system libbpf
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_DIR="${OUTPUT_DIR:-$SCRIPT_DIR/output}"
BUILD_DIR="$(mktemp -d "${TMPDIR:-/tmp}/proxybridge-build.XXXXXX")"
trap 'rm -rf "$BUILD_DIR"' EXIT
+JOBS="$(nproc 2>/dev/null || echo 2)"
+export WERROR LTO OPT
need() { # command, install hint
command -v "$1" >/dev/null 2>&1 || { echo "ERROR: '$1' not found. $2"; exit 1; }
}
need gcc "Install: sudo apt install build-essential | sudo dnf install gcc"
+need curl "Needed to fetch the pinned libbpf source. Install: sudo apt install curl"
need make "Install: sudo apt install make | sudo dnf install make"
need clang "Needed to compile the eBPF program. Install: sudo apt install clang | sudo dnf install clang"
BPFTOOL="${BPFTOOL:-bpftool}" # set BPFTOOL=/path/to/bpftool if it is not on PATH (e.g. /usr/lib/linux-tools/*/bpftool on Ubuntu)
export BPFTOOL
need "$BPFTOOL" "Needed to generate the eBPF skeleton. Install: sudo apt install linux-tools-generic (Ubuntu) | sudo apt install bpftool (Debian) | sudo dnf install bpftool"
-if [ ! -f /usr/include/bpf/libbpf.h ] && ! pkg-config --exists libbpf 2>/dev/null; then
+if [ "${STATIC_LIBBPF:-1}" != 1 ] && [ ! -f /usr/include/bpf/libbpf.h ] && ! pkg-config --exists libbpf 2>/dev/null; then
echo "ERROR: libbpf development files not found. Install: sudo apt install libbpf-dev | sudo dnf install libbpf-devel"
exit 1
fi
+for h in libelf.h zlib.h; do
+ [ -f "/usr/include/$h" ] || find /usr/include -name "$h" -print -quit 2>/dev/null | grep -q . || {
+ echo "ERROR: $h not found (needed to build the static libbpf). Install: sudo apt install libelf-dev zlib1g-dev | sudo dnf install elfutils-libelf-devel zlib-devel"
+ exit 1
+ }
+done
-# Fresh output directory
if [ -e "$OUTPUT_DIR" ]; then
echo "Removing existing output directory..."
rm -rf "$OUTPUT_DIR" 2>/dev/null || {
@@ -38,36 +48,35 @@ fi
mkdir -p "$OUTPUT_DIR"
echo ""
-# Library
echo "=== Building Library ==="
-make -C "$SCRIPT_DIR/src" BUILD_DIR="$BUILD_DIR/lib"
+LIBBPF_ARGS=()
+if [ "${STATIC_LIBBPF:-1}" = 1 ]; then
+ if make -s -C "$SCRIPT_DIR/src" libbpf-static; then
+ LIBBPF_ARGS=(LIBBPF_DIR="$(make -s -C "$SCRIPT_DIR/src" libbpf-path)")
+ echo "libbpf: linking the pinned static build"
+ else
+ echo "WARNING: could not build the pinned libbpf (needs network, curl, libelf-dev and zlib1g-dev);"
+ echo " falling back to the system libbpf. The result will not run on systems without libbpf >= 1.0."
+ export ALLOW_DYNAMIC_LIBBPF=1
+ fi
+else
+ export ALLOW_DYNAMIC_LIBBPF=1
+fi
+make -j"$JOBS" -C "$SCRIPT_DIR/src" BUILD_DIR="$BUILD_DIR/lib" "${LIBBPF_ARGS[@]}"
[ -f "$BUILD_DIR/lib/libproxybridge.so" ] || { echo "Library build failed!"; exit 1; }
echo "Library build successful"
echo ""
-# CLI
echo "=== Building CLI ==="
-make -C "$SCRIPT_DIR/cli" BUILD_DIR="$BUILD_DIR/cli" LIBDIR="$BUILD_DIR/lib"
+make -j"$JOBS" -C "$SCRIPT_DIR/cli" BUILD_DIR="$BUILD_DIR/cli" LIBDIR="$BUILD_DIR/lib"
[ -f "$BUILD_DIR/cli/ProxyBridge" ] || { echo "CLI build failed!"; exit 1; }
echo "CLI build successful"
echo ""
-# GUI (optional)
echo "=== Building GUI ==="
GUI_BUILT=0
if pkg-config --exists gtk+-3.0; then
- mkdir -p "$BUILD_DIR/gui"
- GUI_CFLAGS=(-Wall -Wno-unused-parameter -O3 "-I$SCRIPT_DIR/src" -D_GNU_SOURCE -fstack-protector-strong
- -D_FORTIFY_SOURCE=2 -fPIE -Wformat -Wformat-security -Werror=format-security -fno-strict-overflow
- -fno-delete-null-pointer-checks -fwrapv $(pkg-config --cflags gtk+-3.0))
- GUI_OBJS=()
- for src in "$SCRIPT_DIR"/gui/*.c; do
- obj="$BUILD_DIR/gui/$(basename "${src%.c}").o"
- gcc "${GUI_CFLAGS[@]}" -c "$src" -o "$obj"
- GUI_OBJS+=("$obj")
- done
- gcc -o "$BUILD_DIR/gui/ProxyBridgeGUI" "${GUI_OBJS[@]}" -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -s \
- "-L$BUILD_DIR/lib" -Wl,-rpath,'$ORIGIN' -lproxybridge -lpthread $(pkg-config --libs gtk+-3.0) -export-dynamic
+ make -j"$JOBS" -C "$SCRIPT_DIR/gui" BUILD_DIR="$BUILD_DIR/gui" LIBDIR="$BUILD_DIR/lib"
GUI_BUILT=1
echo "GUI build successful"
else
@@ -77,13 +86,18 @@ else
fi
echo ""
-# Only the final files go to output/
echo "Copying binaries to $OUTPUT_DIR ..."
cp "$BUILD_DIR/lib/libproxybridge.so" "$OUTPUT_DIR/"
cp "$BUILD_DIR/cli/ProxyBridge" "$OUTPUT_DIR/"
[ "$GUI_BUILT" = 1 ] && cp "$BUILD_DIR/gui/ProxyBridgeGUI" "$OUTPUT_DIR/"
echo ""
+if [ "${AUDIT:-1}" = 1 ]; then
+ echo "=== Auditing binaries ==="
+ "$SCRIPT_DIR/tests/check_binaries.sh" "$OUTPUT_DIR" || { echo "Audit failed: the binaries are not portable/hardened enough to ship."; exit 1; }
+ echo ""
+fi
+
echo "==================================="
echo "Build Complete!"
echo "==================================="
diff --git a/Linux/cli/Makefile b/Linux/cli/Makefile
index 0d9464e..ed4e85d 100644
--- a/Linux/cli/Makefile
+++ b/Linux/cli/Makefile
@@ -1,25 +1,15 @@
-CC = gcc
+include ../common.mk
# BUILD_DIR / LIBDIR: where generated files go and where libproxybridge.so was built (build.sh sets both).
BUILD_DIR ?= .
LIBDIR ?= ../src
-CFLAGS = -Wall -Wextra -O3 -I../src \
- -fstack-protector-strong \
- -D_FORTIFY_SOURCE=2 \
- -fPIE \
- -Wformat -Wformat-security \
- -Werror=format-security \
- -fno-strict-overflow \
- -fno-delete-null-pointer-checks \
- -fwrapv
-LDFLAGS = -L$(LIBDIR) -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -s \
- -Wl,-rpath,/usr/local/lib
+CFLAGS = $(CFLAGS_COMMON) -fPIE -I../src
+LDFLAGS = -pie $(HARDEN_LDFLAGS) -L$(LIBDIR) -Wl,-rpath,/usr/local/lib $(STRIP_FLAG)
LIBS = -lproxybridge -lpthread
TARGET = $(BUILD_DIR)/ProxyBridge
-SOURCES = main.c
-OBJECTS = $(addprefix $(BUILD_DIR)/,$(SOURCES:.c=.o))
+OBJECTS = $(BUILD_DIR)/main.o
all: $(TARGET)
@@ -27,9 +17,7 @@ $(BUILD_DIR):
mkdir -p $@
$(TARGET): $(OBJECTS)
- $(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
- @echo "Stripping debug symbols..."
- strip --strip-all $@
+ $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
diff --git a/Linux/common.mk b/Linux/common.mk
new file mode 100644
index 0000000..546df91
--- /dev/null
+++ b/Linux/common.mk
@@ -0,0 +1,23 @@
+# Compiler and linker flags shared by the library, the CLI and the GUI.
+# OPT=-O2 optimisation level LTO=0 disable link-time optimisation
+# WERROR=1 warnings are errors (CI) EXTRA_CFLAGS / EXTRA_LDFLAGS e.g. sanitizers
+CC ?= gcc
+OPT ?= -O2
+LTO ?= 1
+
+# Keeps a flag only if the compiler accepts it (other GCC versions and architectures differ).
+cc-option = $(shell $(CC) $(1) -Werror -x c -c /dev/null -o /dev/null 2>/dev/null && echo $(1))
+
+HARDEN_CFLAGS = -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstack-protector-strong \
+ $(call cc-option,-fstack-clash-protection) $(call cc-option,-fcf-protection=full) \
+ $(call cc-option,-mbranch-protection=standard) $(call cc-option,-ftrivial-auto-var-init=zero) \
+ $(call cc-option,-fno-plt) -fno-strict-overflow -fno-delete-null-pointer-checks -fwrapv \
+ -Wformat -Wformat-security -Werror=format-security
+
+CFLAGS_COMMON = $(OPT) -Wall -Wextra $(if $(WERROR),-Werror) $(HARDEN_CFLAGS) \
+ -ffunction-sections -fdata-sections $(if $(filter 1,$(LTO)),-flto=auto) $(EXTRA_CFLAGS)
+
+HARDEN_LDFLAGS = -Wl,-z,relro,-z,now -Wl,-z,noexecstack -Wl,--as-needed -Wl,--gc-sections \
+ $(if $(filter 1,$(LTO)),-flto=auto) $(EXTRA_LDFLAGS)
+
+STRIP_FLAG ?= -s
diff --git a/Linux/gui/Makefile b/Linux/gui/Makefile
new file mode 100644
index 0000000..b7ca1a3
--- /dev/null
+++ b/Linux/gui/Makefile
@@ -0,0 +1,33 @@
+include ../common.mk
+
+BUILD_DIR ?= .
+LIBDIR ?= ../src
+
+# Compile-time API limits keep the GUI runnable on Ubuntu 22.04 (GTK 3.24.33, GLib 2.72).
+GTK_CFLAGS = $(shell pkg-config --cflags gtk+-3.0) -DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_3_22 -DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_3_22 \
+ -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_72 -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_72
+GTK_LIBS = $(shell pkg-config --libs gtk+-3.0)
+
+CFLAGS = $(CFLAGS_COMMON) -Wno-unused-parameter -fPIE -D_GNU_SOURCE -I../src $(GTK_CFLAGS)
+LDFLAGS = -pie $(HARDEN_LDFLAGS) -L$(LIBDIR) -Wl,-rpath,'$$ORIGIN' -export-dynamic $(STRIP_FLAG)
+LIBS = -lproxybridge -lpthread $(GTK_LIBS)
+
+SOURCES = $(filter-out tests/%,$(wildcard *.c))
+OBJECTS = $(addprefix $(BUILD_DIR)/,$(SOURCES:.c=.o))
+TARGET = $(BUILD_DIR)/ProxyBridgeGUI
+
+all: $(TARGET)
+
+$(BUILD_DIR):
+ mkdir -p $@
+
+$(TARGET): $(OBJECTS)
+ $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
+
+$(BUILD_DIR)/%.o: %.c gui.h | $(BUILD_DIR)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+clean:
+ rm -f $(OBJECTS) $(TARGET)
+
+.PHONY: all clean
diff --git a/Linux/src/Makefile b/Linux/src/Makefile
index 7c9d5e8..ab84111 100644
--- a/Linux/src/Makefile
+++ b/Linux/src/Makefile
@@ -1,26 +1,31 @@
-CC ?= gcc
+include ../common.mk
+
CLANG ?= clang
BPFTOOL ?= bpftool
MULTIARCH := $(shell $(CC) -print-multiarch)
-# BUILD_DIR keeps every generated file (objects, BPF object, skeleton, library) out of the source tree.
-# build.sh points it at a temporary directory; a plain `make` builds in place for quick development.
+# BUILD_DIR keeps every generated file out of the source tree; build.sh points it at a temporary directory.
BUILD_DIR ?= .
-# EXTRA_CFLAGS / EXTRA_LDFLAGS: e.g. -fsanitize=address,undefined (use STRIP_FLAG= to keep symbols for the report).
-STRIP_FLAG ?= -s
-
-CFLAGS = -Wall -Wextra -O2 -fPIC -D_GNU_SOURCE -I$(BUILD_DIR) \
- -fstack-protector-strong \
- -D_FORTIFY_SOURCE=2 \
- -fPIE \
- -Wformat -Wformat-security \
- -Werror=format-security \
- -fno-strict-overflow \
- -fno-delete-null-pointer-checks \
- -fwrapv -fvisibility=hidden -ffunction-sections -fdata-sections $(EXTRA_CFLAGS)
-LDFLAGS = -shared -Wl,-z,relro,-z,now -Wl,-z,noexecstack -Wl,--gc-sections $(STRIP_FLAG) $(EXTRA_LDFLAGS)
-LIBS = -lpthread -lbpf
+# libbpf is linked statically from a pinned release built as position-independent code, so the library does not depend on the
+# distribution's libbpf (Ubuntu 22.04 ships 0.5, and distribution libbpf.a is not PIC). With LIBBPF_DIR unset the system
+# libbpf is used instead.
+LIBBPF_VERSION ?= 1.6.3
+LIBBPF_SHA256 ?= 989ed3c1a3db8ff0f7c08dd43953c6b9d0c3ac252653a48d566aaedf98bc80ca
+LIBBPF_RECIPE ?= 2
+LIBBPF_CACHE ?= $(or $(XDG_CACHE_HOME),$(HOME)/.cache)/proxybridge/libbpf-$(LIBBPF_VERSION)-r$(LIBBPF_RECIPE)
+LIBBPF_DIR ?=
+
+ifneq ($(LIBBPF_DIR),)
+LIBBPF_INCLUDE = -I$(LIBBPF_DIR)/include
+LIBBPF_LIBS = $(LIBBPF_DIR)/lib/libbpf.a -lelf -lz
+else
+LIBBPF_LIBS = -lbpf
+endif
+
+CFLAGS = $(CFLAGS_COMMON) -fPIC -fvisibility=hidden -D_GNU_SOURCE -I$(BUILD_DIR) $(LIBBPF_INCLUDE)
+LDFLAGS = -shared $(HARDEN_LDFLAGS) -Wl,-z,defs -Wl,--version-script=$(CURDIR)/exports.map $(STRIP_FLAG)
+LIBS = -lpthread $(LIBBPF_LIBS)
SOURCES = ProxyBridge.c pb_rules.c pb_dns.c pb_proc.c pb_bpf.c pb_relay.c
OBJECTS = $(addprefix $(BUILD_DIR)/,$(SOURCES:.c=.o))
@@ -34,24 +39,39 @@ $(BUILD_DIR):
mkdir -p $@
$(BPF_OBJ): bpf/proxybridge.bpf.c bpf/pb_bpf_shared.h | $(BUILD_DIR)
- $(CLANG) -O2 -g -target bpf -D__BPF__ -I/usr/include/$(MULTIARCH) -c $< -o $@
+ $(CLANG) -O2 -g -target bpf -D__BPF__ $(LIBBPF_INCLUDE) -I/usr/include/$(MULTIARCH) -c $< -o $@
$(SKEL): $(BPF_OBJ)
$(BPFTOOL) gen skeleton $< > [email protected] && mv [email protected] $@
$(BUILD_DIR)/pb_bpf.o: $(SKEL)
-$(TARGET): $(OBJECTS)
- $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+$(TARGET): $(OBJECTS) exports.map
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBS)
$(BUILD_DIR)/%.o: %.c pb_internal.h ProxyBridge.h | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
-# Unit tests (no root needed); binaries go to BUILD_DIR as well.
test-rules: tests/rules_test.c tests/dns_test.c pb_rules.c pb_rules.h pb_dns.c pb_dns.h | $(BUILD_DIR)
$(CC) -Wall -Wextra -g -fsanitize=address,undefined -D_GNU_SOURCE tests/rules_test.c pb_rules.c -o $(BUILD_DIR)/rules_test && $(BUILD_DIR)/rules_test
$(CC) -Wall -Wextra -g -fsanitize=address,undefined -D_GNU_SOURCE tests/dns_test.c pb_dns.c -o $(BUILD_DIR)/dns_test && $(BUILD_DIR)/dns_test
+# Downloads, verifies and builds libbpf (cached); prints nothing. `make -s libbpf-path` prints the directory to use as LIBBPF_DIR.
+libbpf-static: $(LIBBPF_CACHE)/lib/libbpf.a
+
+$(LIBBPF_CACHE)/lib/libbpf.a:
+ @set -e; work=$$(mktemp -d); cd "$$work"; \
+ curl -fsSL -o libbpf.tar.gz https://github.com/libbpf/libbpf/archive/refs/tags/v$(LIBBPF_VERSION).tar.gz; \
+ echo "$(LIBBPF_SHA256) libbpf.tar.gz" | sha256sum -c --quiet -; \
+ tar xzf libbpf.tar.gz; \
+ $(MAKE) -s -C libbpf-$(LIBBPF_VERSION)/src BUILD_STATIC_ONLY=y OBJDIR="$$work/obj" CFLAGS="-O2 -fPIC -include $(CURDIR)/glibc_compat.h" \
+ DESTDIR="$$work/root" PREFIX=/ LIBDIR=/lib install >/dev/null; \
+ mkdir -p "$(LIBBPF_CACHE)"; cp -r "$$work/root/include" "$$work/root/lib" "$(LIBBPF_CACHE)/"; \
+ cd /; rm -rf "$$work"
+
+libbpf-path:
+ @echo $(LIBBPF_CACHE)
+
clean:
rm -f $(OBJECTS) $(TARGET) $(BPF_OBJ) $(SKEL) $(BUILD_DIR)/rules_test $(BUILD_DIR)/dns_test
@@ -59,4 +79,4 @@ install: $(TARGET)
install -m 0755 $(TARGET) /usr/local/lib/
ldconfig
-.PHONY: all clean install test-rules
+.PHONY: all clean install test-rules libbpf-static libbpf-path
diff --git a/Linux/src/exports.map b/Linux/src/exports.map
new file mode 100644
index 0000000..0084447
--- /dev/null
+++ b/Linux/src/exports.map
@@ -0,0 +1,6 @@
+{
+ global:
+ ProxyBridge_*;
+ local:
+ *;
+};
diff --git a/Linux/src/glibc_compat.h b/Linux/src/glibc_compat.h
new file mode 100644
index 0000000..873c1a8
--- /dev/null
+++ b/Linux/src/glibc_compat.h
@@ -0,0 +1,23 @@
+// glibc 2.38 headers redirect strto*/scanf to C23 variants (GLIBC_2.38 symbols), which stops the binary from loading on older
+// systems (GitHub issue #146). This header binds the names back to the classic symbol versions; it is force-included when
+// building third-party C code (libbpf) that uses them. Our own sources avoid these functions.
+#ifndef PB_GLIBC_COMPAT_H
+#define PB_GLIBC_COMPAT_H
+
+#include <features.h>
+
+#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 38))
+# if defined(__x86_64__)
+# define PB_BASE_GLIBC "GLIBC_2.2.5"
+# elif defined(__aarch64__)
+# define PB_BASE_GLIBC "GLIBC_2.17"
+# endif
+# ifdef PB_BASE_GLIBC
+# define PB_SYMVER(name) __asm__(".symver __isoc23_" #name "," #name "@" PB_BASE_GLIBC)
+PB_SYMVER(strtol); PB_SYMVER(strtoul); PB_SYMVER(strtoll); PB_SYMVER(strtoull);
+PB_SYMVER(scanf); PB_SYMVER(fscanf); PB_SYMVER(sscanf);
+PB_SYMVER(vscanf); PB_SYMVER(vfscanf); PB_SYMVER(vsscanf);
+# endif
+#endif
+
+#endif
diff --git a/Linux/src/pb_proc.c b/Linux/src/pb_proc.c
index 93c8fc3..61b756e 100644
--- a/Linux/src/pb_proc.c
+++ b/Linux/src/pb_proc.c
@@ -10,6 +10,40 @@
#define MAX_INODES 8
+static bool parse_number(const char *s, unsigned base, unsigned long long *out, const char **end)
+{
+ unsigned long long v = 0;
+ const char *p = s;
+ for (;; p++) {
+ unsigned d = (unsigned)(*p - '0');
+ if (*p >= 'a' && *p <= 'f') d = (unsigned)(*p - 'a') + 10;
+ else if (*p >= 'A' && *p <= 'F') d = (unsigned)(*p - 'A') + 10;
+ if (d >= base) break;
+ v = v * base + d;
+ }
+ if (p == s) return false;
+ *out = v;
+ if (end) *end = p;
+ return true;
+}
+
+// Fields of a /proc/net/tcp row: sl, local, remote, state, queues, timer, retrans, uid, timeout, inode.
+static bool parse_tcp_row(char *line, uint32_t *addr, uint32_t *port, unsigned *state, unsigned long long *inode)
+{
+ char *fields[10], *save = NULL;
+ size_t n = 0;
+ for (char *t = strtok_r(line, " \t\n", &save); t && n < 10; t = strtok_r(NULL, " \t\n", &save)) fields[n++] = t;
+ if (n < 10) return false;
+ unsigned long long a, p, st;
+ const char *colon;
+ if (!parse_number(fields[1], 16, &a, &colon) || *colon != ':' || !parse_number(colon + 1, 16, &p, NULL)) return false;
+ if (!parse_number(fields[3], 16, &st, NULL) || !parse_number(fields[9], 10, inode, NULL)) return false;
+ *addr = (uint32_t)a;
+ *port = (uint32_t)p;
+ *state = (unsigned)st;
+ return true;
+}
+
static size_t listen_inodes(uint32_t ip_be, uint16_t port, uint64_t *inodes)
{
FILE *f = fopen("/proc/net/tcp", "r");
@@ -18,10 +52,10 @@ static size_t listen_inodes(uint32_t ip_be, uint16_t port, uint64_t *inodes)
size_t n = 0;
if (!fgets(line, sizeof line, f)) { fclose(f); return 0; }
while (n < MAX_INODES && fgets(line, sizeof line, f)) {
- unsigned addr, lport, state;
+ uint32_t addr, lport;
+ unsigned state;
unsigned long long inode;
- // sl local rem st tx:rx tr:when retrnsmt uid timeout inode
- if (sscanf(line, " %*d: %x:%x %*x:%*x %x %*x:%*x %*x:%*x %*x %*d %*d %llu", &addr, &lport, &state, &inode) != 4) continue;
+ if (!parse_tcp_row(line, &addr, &lport, &state, &inode)) continue;
if (state == 0x0A && lport == port && inode && (addr == ip_be || addr == 0)) inodes[n++] = inode;
}
fclose(f);
@@ -58,7 +92,11 @@ size_t pb_proc_listeners(uint32_t ip_be, uint16_t port, uint32_t *tgids, size_t
while (n < max && (de = readdir(d))) {
if (de->d_name[0] < '1' || de->d_name[0] > '9') continue;
for (size_t i = 0; i < ni; i++)
- if (pid_has_socket(de->d_name, inodes[i])) { tgids[n++] = (uint32_t)strtoul(de->d_name, NULL, 10); break; }
+ if (pid_has_socket(de->d_name, inodes[i])) {
+ unsigned long long pid;
+ if (parse_number(de->d_name, 10, &pid, NULL)) tgids[n++] = (uint32_t)pid;
+ break;
+ }
}
closedir(d);
return n;
diff --git a/Linux/tests/check_binaries.sh b/Linux/tests/check_binaries.sh
new file mode 100755
index 0000000..b4dbdb1
--- /dev/null
+++ b/Linux/tests/check_binaries.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+# Audits the built binaries: portability (glibc floor, no libbpf dependency) and hardening.
+# tests/check_binaries.sh [dir] MAX_GLIBC=2.34 (default) is the highest glibc symbol version allowed
+set -u
+DIR="${1:-$(dirname "$(dirname "${BASH_SOURCE[0]}")")/output}"
+MAX_GLIBC="${MAX_GLIBC:-2.34}"
+fails=0
+ok() { printf ' ok %s\n' "$1"; }
+bad() { printf ' FAIL %s\n' "$1"; fails=$((fails + 1)); }
+check() { if eval "$2"; then ok "$1"; else bad "$1"; fi; }
+
+audit() { # file kind(lib|exe) allowed-NEEDED-regex
+ local f="$DIR/$1" kind="$2" needed_ok="$3"
+ [ -f "$f" ] || { [ "$1" = ProxyBridgeGUI ] && { echo "== $1 (not built)"; return; }; bad "$1 is missing"; return; }
+ echo "== $1"
+ local hdr prog dyn syms need
+ hdr=$(readelf -h "$f"); prog=$(readelf -lW "$f"); dyn=$(readelf -d "$f"); syms=$(nm -D "$f" 2>/dev/null)
+ if [ "$kind" = exe ]; then check "position independent executable" 'grep -q "Position-Independent" <<<"$hdr"'
+ else check "shared object" 'grep -q "DYN" <<<"$hdr"'; fi
+ check "full RELRO (GNU_RELRO + BIND_NOW)" 'grep -q GNU_RELRO <<<"$prog" && grep -q -E "BIND_NOW|FLAGS.*NOW" <<<"$dyn"'
+ check "non-executable stack" 'grep GNU_STACK <<<"$prog" | grep -vq "RWE"'
+ check "no writable+executable segments" '! grep -E "LOAD" <<<"$prog" | grep -q "RWE"'
+ check "no text relocations" '! grep -q TEXTREL <<<"$dyn"'
+ check "stack protector in use" 'grep -q __stack_chk_fail <<<"$syms"'
+ check "fortified libc calls in use" 'grep -q "_chk" <<<"$syms"'
+ if [ "$(uname -m)" = x86_64 ]; then check "CET (IBT + SHSTK) marked" 'readelf -n "$f" | grep -q IBT && readelf -n "$f" | grep -q SHSTK'; fi
+ need=$(grep NEEDED <<<"$dyn" | sed 's/.*\[\(.*\)\]/\1/' | tr '\n' ' ')
+ check "dependencies are only portable libraries ($need)" '! tr " " "\n" <<<"$need" | grep -v "^$" | grep -q -v -E "$needed_ok"'
+ local top
+ top=$(objdump -T "$f" 2>/dev/null | grep -o 'GLIBC_[0-9][0-9.]*' | sed 's/GLIBC_//' | sort -uV | tail -1)
+ check "needs glibc ${top:-none} (limit $MAX_GLIBC, see issue #146)" '[ -z "$top" ] || [ "$(printf "%s\n%s\n" "$top" "$MAX_GLIBC" | sort -V | tail -1)" = "$MAX_GLIBC" ]'
+ if [ -n "$top" ] && [ "$(printf '%s\n%s\n' "$top" "$MAX_GLIBC" | sort -V | tail -1)" != "$MAX_GLIBC" ]; then
+ objdump -T "$f" | grep -E "GLIBC_(2\.(3[5-9]|[4-9][0-9])|[3-9])" | awk '{print " "$NF" "$(NF-1)}' | head -5
+ fi
+}
+
+LIBBPF_DEP=''; [ "${ALLOW_DYNAMIC_LIBBPF:-0}" = 1 ] && LIBBPF_DEP='|libbpf\.so\.1'
+audit libproxybridge.so lib "^(libc\.so\.6|libelf\.so\.1|libz\.so\.1|libm\.so\.6|ld-linux.*$LIBBPF_DEP)\$"
+exports=$(nm -D --defined-only "$DIR/libproxybridge.so" 2>/dev/null | awk '$2=="T"{print $3}' | grep -v '^ProxyBridge_' | tr '\n' ' ')
+check "library exports only the ProxyBridge_* API${exports:+ (also: $exports)}" '[ -z "$exports" ]'
+audit ProxyBridge exe '^(libc\.so\.6|libproxybridge\.so|libpthread\.so\.0|libm\.so\.6|ld-linux.*)$'
+audit ProxyBridgeGUI exe '^(libc\.so\.6|libproxybridge\.so|libpthread\.so\.0|libm\.so\.6|libgtk-3\.so\.0|libgdk-3\.so\.0|libglib-2\.0\.so\.0|libgobject-2\.0\.so\.0|libgio-2\.0\.so\.0|libcairo\.so\.2|libgdk_pixbuf-2\.0\.so\.0|libpango-1\.0\.so\.0|libpangocairo-1\.0\.so\.0|libatk-1\.0\.so\.0|ld-linux.*)$'
+
+echo
+if [ "$fails" = 0 ]; then echo "binary audit: all checks passed"; else echo "binary audit: $fails check(s) failed"; fi
+[ "$fails" = 0 ]