Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ jobs:
shell: bash

- name: Locate bpftool
# On Ubuntu "bpftool" is only a virtual package; the real binary ships in linux-tools-* and is not on PATH
# for the runner's cloud kernel, so find it explicitly.
run: |
shopt -s nullglob
candidates=(/usr/lib/linux-tools/*/bpftool)
[ ${#candidates[@]} -gt 0 ] || { echo "bpftool binary not found"; ls /usr/lib/linux-tools 2>&1 || true; exit 1; }
echo "BPFTOOL=${candidates[0]}" >> "$GITHUB_ENV"
"${candidates[0]}" version | head -1
# the runner image's kernel tools can lack the bpftool binary; the script looks everywhere, installs the package
# for the running kernel if needed, and prints what is on the machine when it fails (Linux/ci/find-bpftool.sh)
run: bash Linux/ci/find-bpftool.sh
shell: bash

- name: Verify dependencies
Expand Down Expand Up @@ -186,11 +181,11 @@ jobs:
continue-on-error: true
timeout-minutes: 15
run: |
sudo apt-get install -y dnsutils libgtk-3-bin >/dev/null
sudo apt-get install -y dnsutils libgtk-3-bin btrfs-progs >/dev/null
uname -r
ls /sys/kernel/btf/vmlinux && mount | grep -q "type cgroup2" || { echo "kernel lacks BTF or cgroup2: skipping"; exit 0; }
fail=0
for t in e2e e2e_rules e2e_domains e2e_security e2e_profile e2e_parity e2e_cli e2e_faults e2e_udp e2e_rules_diff e2e_logging e2e_exempt e2e_lifecycle api_live e2e_daemon e2e_service; do
for t in e2e e2e_rules e2e_domains e2e_security e2e_profile e2e_parity e2e_cli e2e_faults e2e_udp e2e_rules_diff e2e_logging e2e_exempt e2e_lifecycle api_live e2e_btrfs e2e_daemon e2e_service; do
echo "::group::$t"
sudo env PB_DIR="$PWD/Linux/output" BPFTOOL="$BPFTOOL" bash "Linux/tests/$t.sh" || fail=1
echo "::endgroup::"
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,9 @@ jobs:
shell: bash

- name: Locate bpftool
# on Ubuntu "bpftool" is only a virtual package, the real binary ships in linux-tools-*
run: |
shopt -s nullglob
candidates=(/usr/lib/linux-tools/*/bpftool)
[ ${#candidates[@]} -gt 0 ] || { echo "bpftool binary not found"; ls /usr/lib/linux-tools 2>&1 || true; exit 1; }
echo "BPFTOOL=${candidates[0]}" >> "$GITHUB_ENV"
"${candidates[0]}" version | head -1
# the runner image's kernel tools can lack the bpftool binary; the script looks everywhere, installs the package
# for the running kernel if needed, and prints what is on the machine when it fails (Linux/ci/find-bpftool.sh)
run: bash Linux/ci/find-bpftool.sh
shell: bash

# build.sh compiles the library (src), the CLI and the GTK GUI, which is every C translation unit under
Expand Down
11 changes: 3 additions & 8 deletions .github/workflows/release-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ jobs:
shell: bash

- name: Locate bpftool
# On Ubuntu "bpftool" is only a virtual package; the real binary ships in linux-tools-* and is not on PATH
# for the runner's cloud kernel, so find it explicitly.
run: |
shopt -s nullglob
candidates=(/usr/lib/linux-tools/*/bpftool)
[ ${#candidates[@]} -gt 0 ] || { echo "bpftool binary not found"; ls /usr/lib/linux-tools 2>&1 || true; exit 1; }
echo "BPFTOOL=${candidates[0]}" >> "$GITHUB_ENV"
"${candidates[0]}" version | head -1
# the runner image's kernel tools can lack the bpftool binary; the script looks everywhere, installs the package
# for the running kernel if needed, and prints what is on the machine when it fails (Linux/ci/find-bpftool.sh)
run: bash Linux/ci/find-bpftool.sh
shell: bash

- name: Build project
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/sonarqube.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ jobs:
shell: bash

- name: Locate bpftool
run: |
shopt -s nullglob
candidates=(/usr/lib/linux-tools/*/bpftool)
[ ${#candidates[@]} -gt 0 ] || { echo "bpftool binary not found"; ls /usr/lib/linux-tools 2>&1 || true; exit 1; }
echo "BPFTOOL=${candidates[0]}" >> "$GITHUB_ENV"
# the runner image's kernel tools can lack the bpftool binary; the script looks everywhere, installs the package
# for the running kernel if needed, and prints what is on the machine when it fails (Linux/ci/find-bpftool.sh)
run: bash Linux/ci/find-bpftool.sh
shell: bash

- name: Install Build Wrapper
Expand Down
1 change: 1 addition & 0 deletions Linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ sudo ./e2e_rules_diff.sh # random rule sets vs an indep
sudo ./e2e_logging.sh # connection log: de-duplication, log-every-connection, one line per decision (root)
./e2e_checker.sh # the proxy check against working and broken proxies (no root)
sudo ./gui_ui.sh # drives the real windows and dialogs: proxies, rules, profiles, file choosers, menus, engine - once with its own engine and once through a service (root, needs broadwayd)
sudo ./e2e_btrfs.sh # programs that live on Btrfs (Fedora's default): its per-subvolume stat() device must not defeat rules, DIRECT and the UDP sender check (root, needs mkfs.btrfs and a loop device; skips otherwise)
sudo ./e2e_daemon.sh # the service: protocol and random garbage, access control by group, profile store, engine control, live connections, batch, Start at Boot (root)
sudo ./e2e_service.sh # the service under real systemd with the installer's exact unit: sandbox, eBPF, crash and restart, stop (root, systemd)
sudo ./gui_service.sh # the shipped GUI run by an unprivileged user in front of a real service (root, needs broadwayd and setpriv)
Expand Down
45 changes: 45 additions & 0 deletions Linux/ci/find-bpftool.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
# Finds a working bpftool for the CI runners and exports it as BPFTOOL (through $GITHUB_ENV). The build only needs bpftool to generate
# the eBPF skeleton from the running kernel's BTF, so any version that runs will do.
#
# On Ubuntu "bpftool" is not a package of its own: /usr/sbin/bpftool is a wrapper that runs the copy for the running kernel, and that copy
# lives in a folder named after the kernel (/usr/lib/linux-tools/<release>/ or, for HWE and cloud kernels, /usr/lib/linux-*-tools-<release>/).
# A runner image can ship a kernel whose folder has no copy yet, so look in every place, check that the candidate runs, install the
# package for this kernel if none does, and say what is on the machine if that fails too.
# Linux/ci/find-bpftool.sh (BPFTOOL_ROOT=/some/root searches under that root: used by the test)
set -u
ROOT="${BPFTOOL_ROOT:-}"

find_working() {
shopt -s nullglob
local c
for c in "$ROOT"/usr/lib/linux-tools/"$(uname -r)"/bpftool "$ROOT"/usr/lib/linux-tools/*/bpftool "$ROOT"/usr/lib/linux-*tools-*/bpftool \
"$ROOT"/usr/lib/linux-tools-*/bpftool "$ROOT"/usr/local/sbin/bpftool "$ROOT"/usr/sbin/bpftool "$ROOT"/usr/bin/bpftool; do
if [ -x "$c" ] && [ ! -d "$c" ] && "$c" version >/dev/null 2>&1; then echo "$c"; return 0; fi
done
return 1
}

found="$(find_working)"
if [ -z "$found" ] && [ -z "$ROOT" ] && command -v apt-get >/dev/null 2>&1; then
echo "no working bpftool yet; installing the tools for $(uname -r)"
sudo apt-get update -qq >/dev/null 2>&1 || true
for pkg in "linux-tools-$(uname -r)" linux-tools-generic bpftool; do
sudo apt-get install -y "$pkg" >/dev/null 2>&1 && echo " installed $pkg" || echo " could not install $pkg"
found="$(find_working)" && [ -n "$found" ] && break
done
fi

if [ -z "$found" ]; then
echo "ERROR: no working bpftool found. What is on this machine:"
echo "kernel: $(uname -r)"
ls -la "$ROOT"/usr/lib/linux-tools 2>&1 | head -20
ls -d "$ROOT"/usr/lib/linux*tools* 2>&1 | head
find "$ROOT"/usr "$ROOT"/lib -name 'bpftool*' 2>/dev/null | head -20
dpkg -l 2>/dev/null | grep -E 'linux-.*tools|bpftool' | awk '{print $1, $2, $3}' | head -20
exit 1
fi
echo "bpftool: $found"
"$found" version 2>&1 | head -1
[ -n "${GITHUB_ENV:-}" ] && echo "BPFTOOL=$found" >> "$GITHUB_ENV"
exit 0
7 changes: 5 additions & 2 deletions Linux/gui/tests/boot_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ int main(void)
g_free(why); why = NULL;
CHECK(!boot_path_trusted("relative/ProxyBridge", &why) && why); g_free(why); why = NULL;
CHECK(!boot_path_trusted("/nonexistent/ProxyBridge", &why) && why); g_free(why); why = NULL;
if (access("/usr/bin/true", X_OK) == 0) CHECK(boot_path_trusted("/usr/bin/true", &why) && why == NULL);
// a real program of the system: /usr/bin/true may itself be a symlink (newer distributions), which is exactly what is refused
char *real = realpath("/usr/bin/true", NULL);
if (real && access(real, X_OK) == 0) { bool trusted = boot_path_trusted(real, &why); CHECK(trusted && why == NULL); g_free(why); why = NULL; }
char *lnk = g_build_filename(dir, "lnk", NULL);
CHECK(symlink("/usr/bin/true", lnk) == 0 && !boot_path_trusted(lnk, &why)); g_free(why); why = NULL; g_free(lnk);
CHECK(symlink(real ? real : "/usr/bin/true", lnk) == 0 && !boot_path_trusted(lnk, &why)); g_free(why); why = NULL; g_free(lnk);
free(real);

t = boot_unit_text("/usr/local/bin/ProxyBridge", &err);
CHECK(t && strstr(t, "ExecStart=\"/usr/local/bin/ProxyBridge\" --daemon\n") && strstr(t, "WantedBy=multi-user.target") &&
Expand Down
4 changes: 2 additions & 2 deletions Linux/gui/tests/ui_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,11 @@ static void updates_scenario(void) {
feed_case("feed_noversion.json", "{\"linux\": {\"download\": \"https://example.test/d\"}}", GTK_RESPONSE_OK, "Could not parse", false);
feed_case("feed_escape.json", "{\"linux\": {\"version\": \"9.9.9\\u001b[31m\"}}", GTK_RESPONSE_OK, "Could not parse", false); // remote text never reaches a dialog raw
feed_case("feed_same.json", "{\"linux\": {\"version\": \"" PROXYBRIDGE_VERSION "\"}}", GTK_RESPONSE_OK, "latest version", false);
feed_case("feed_beta_suffix.json", "{\"linux\": {\"version\": \"4.0.0\"}}", GTK_RESPONSE_OK, "latest version", false);
feed_case("feed_suffix.json", "{\"linux\": {\"version\": \"" PROXYBRIDGE_VERSION "-Beta\"}}", GTK_RESPONSE_OK, "latest version", false); // a suffix is ignored
feed_case("feed_older.json", "{\"linux\": {\"version\": \"3.2.0\"}}", GTK_RESPONSE_OK, "latest version", false);
feed_case("feed_newer.json", "{\"linux\": {\"version\": \"9.9.9\", \"download\": \"https://example.test/d\", \"release_notes\": \"https://example.test/n\"}}",
GTK_RESPONSE_CANCEL, "Version 9.9.9 is available", false);
feed_case("feed_patch.json", "{\"linux\": {\"version\": \"4.0.1\", \"download\": \"https://example.test/d\"}}", GTK_RESPONSE_CANCEL, "Version 4.0.1 is available", false);
feed_case("feed_minor.json", "{\"linux\": {\"version\": \"4.99.0\", \"download\": \"https://example.test/d\"}}", GTK_RESPONSE_CANCEL, "Version 4.99.0 is available", false);
// a feed must never make the program open anything but an https page
feed_case("feed_unsafe.json", "{\"linux\": {\"version\": \"9.9.9\", \"download\": \"http://insecure.test/x\", \"release_notes\": \"javascript:alert(1)\"}}",
GTK_RESPONSE_ACCEPT, "Version 9.9.9 is available", true);
Expand Down
10 changes: 3 additions & 7 deletions Linux/src/ProxyBridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,9 @@ int pb_core_udp_sender_ok(const struct pb_orig *o, uint16_t client_port)
snprintf(dir, sizeof dir, "/proc/%u", o->pid);
if (stat(dir, &st) != 0) return 1; /* the flow's owner has exited */
if (o->exe_ino) {
char link[64];
struct stat ex;
snprintf(link, sizeof link, "/proc/%u/exe", o->pid);
if (stat(link, &ex) == 0) {
uint32_t dev = (major(ex.st_dev) << 20) | minor(ex.st_dev); /* same encoding as the BPF program's exe2class key */
if (dev != o->exe_dev || ex.st_ino != o->exe_ino) return 0; /* the pid now runs another program */
}
uint32_t dev;
uint64_t ino;
if (pb_proc_exe_identity(o->pid, &dev, &ino) == 0 && (dev != o->exe_dev || ino != o->exe_ino)) return 0; /* the pid now runs another program */
}
for (size_t i = 0; i < n; i++)
if (uids[i] != 0 && uids[i] != (uint32_t)st.st_uid) return 0;
Expand Down
2 changes: 1 addition & 1 deletion Linux/src/ProxyBridge.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PROXYBRIDGE_H
#define PROXYBRIDGE_H

#define PROXYBRIDGE_VERSION "4.0.0"
#define PROXYBRIDGE_VERSION "4.0.1"

#include <stddef.h>
#include <stdint.h>
Expand Down
11 changes: 6 additions & 5 deletions Linux/src/pb_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sys/sysmacros.h>
#include <unistd.h>
#include "pb_bpf.h"
#include "pb_proc.h"
#include "proxybridge.skel.h"

#ifndef CGROUP2_SUPER_MAGIC
Expand Down Expand Up @@ -254,8 +255,6 @@ void pb_bpf_set_flags(bool log, bool dns_via_proxy)
pthread_mutex_unlock(&G.lock);
}

static uint32_t kernel_dev(dev_t d) { return (major(d) << 20) | minor(d); }

static struct exe_ent *exe_find(uint32_t dev, uint64_t ino)
{
for (size_t i = 0; i < G.nexes; i++)
Expand Down Expand Up @@ -291,14 +290,16 @@ static void scan_proc(void)
while ((de = readdir(d))) {
if (de->d_name[0] < '1' || de->d_name[0] > '9') continue;
char link[300], path[4096];
struct stat st;
uint32_t pid = 0, dev;
uint64_t ino;
for (const char *q = de->d_name; *q >= '0' && *q <= '9' && pid < 100000000u; q++) pid = pid * 10 + (uint32_t)(*q - '0');
snprintf(link, sizeof link, "/proc/%.200s/exe", de->d_name);
if (stat(link, &st) != 0) continue;
if (pb_proc_exe_identity(pid, &dev, &ino) != 0) continue; /* the kernel's own notion of the file system's device, not stat()'s */
ssize_t n = readlink(link, path, sizeof path - 1);
if (n <= 0) continue;
path[n] = 0;
strip_deleted(path);
exe_add(kernel_dev(st.st_dev), st.st_ino, path);
exe_add(dev, ino, path);
}
closedir(d);
}
Expand Down
94 changes: 94 additions & 0 deletions Linux/src/pb_proc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <arpa/inet.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <time.h>
#include <dirent.h>
#include <ifaddrs.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -156,3 +164,89 @@ bool pb_proc_is_local_ip(uint32_t ip_be)
freeifaddrs(list);
return local;
}

// ---- executable identity -------------------------------------------------------------------------------------------------------
#ifndef STATX_MNT_ID
#define STATX_MNT_ID 0x00001000U
#endif

bool pb_mountinfo_dev(const char *text, unsigned mnt_id, uint32_t *dev)
{
for (const char *line = text; line && *line;) {
unsigned long long id, parent, maj, min;
const char *p = line;
if (parse_number(p, 10, &id, &p) && *p == ' ' && parse_number(p + 1, 10, &parent, &p) && *p == ' ' &&
parse_number(p + 1, 10, &maj, &p) && *p == ':' && parse_number(p + 1, 10, &min, &p) && id == mnt_id && maj < 4096 && min < (1u << 20)) {
*dev = (uint32_t)((maj << 20) | min);
return true;
}
const char *nl = strchr(line, '\n');
line = nl ? nl + 1 : NULL;
}
return false;
}

static char *read_all(const char *path)
{
FILE *f = fopen(path, "r");
if (!f) return NULL;
size_t cap = 1 << 16, n = 0;
char *buf = malloc(cap + 1);
while (buf) {
size_t r = fread(buf + n, 1, cap - n, f);
n += r;
if (n < cap) break;
if (cap >= (16u << 20)) break;
char *bigger = realloc(buf, cap * 2 + 1);
if (!bigger) { free(buf); buf = NULL; break; }
buf = bigger;
cap *= 2;
}
fclose(f);
if (buf) buf[n] = 0;
return buf;
}

// The mountinfo of this process, kept for a moment: a scan of /proc asks for every process, and most share the same mounts. A mount that
// is not in it (another mount namespace, or a mount made since) is looked up in that process's own mountinfo.
static pthread_mutex_t mnt_mu = PTHREAD_MUTEX_INITIALIZER;
static char *mnt_text;
static time_t mnt_loaded;

static bool mount_dev(unsigned mnt_id, uint32_t pid, uint32_t *dev)
{
pthread_mutex_lock(&mnt_mu);
time_t now = time(NULL);
bool found = mnt_text && pb_mountinfo_dev(mnt_text, mnt_id, dev);
if (!found && (!mnt_text || now != mnt_loaded)) { /* a new mount: look again, at most once a second */
char *fresh = read_all("/proc/self/mountinfo");
if (fresh) {
free(mnt_text);
mnt_text = fresh;
mnt_loaded = now;
found = pb_mountinfo_dev(mnt_text, mnt_id, dev);
}
}
pthread_mutex_unlock(&mnt_mu);
if (found) return true;
char path[64];
snprintf(path, sizeof path, "/proc/%u/mountinfo", pid);
char *theirs = read_all(path);
found = theirs && pb_mountinfo_dev(theirs, mnt_id, dev);
free(theirs);
return found;
}

int pb_proc_exe_identity(uint32_t pid, uint32_t *dev, uint64_t *ino)
{
char link[64];
struct statx sx;
snprintf(link, sizeof link, "/proc/%u/exe", pid);
if (statx(AT_FDCWD, link, 0, STATX_INO | STATX_MNT_ID, &sx) != 0) return -1;
*ino = sx.stx_ino;
uint32_t d = (sx.stx_dev_major << 20) | sx.stx_dev_minor; /* what stat() says: right on most file systems */
uint32_t sb;
if ((sx.stx_mask & STATX_MNT_ID) && mount_dev(sx.stx_mnt_id, pid, &sb)) d = sb;
*dev = d;
return 0;
}
10 changes: 10 additions & 0 deletions Linux/src/pb_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ size_t pb_proc_udp_port_uids(uint16_t port, uint32_t *uids, size_t max);
// True for 127.0.0.0/8 and for any IPv4 address configured on a local interface.
bool pb_proc_is_local_ip(uint32_t ip_be);

// Identity of the executable of process `pid`, in the form the BPF program records it: *dev is the device number of the file system
// (the superblock's, major << 20 | minor) and *ino the inode. stat() cannot be used for this: on Btrfs it reports a different, per-subvolume
// device number than the superblock's, so the same file would never match the kernel's record. Returns 0, or -1 if the process or its
// executable is gone or unreadable.
int pb_proc_exe_identity(uint32_t pid, uint32_t *dev, uint64_t *ino);

// The device (major << 20 | minor) of the mount with id `mnt_id` in a mountinfo text (the numbers /proc/PID/mountinfo prints for the
// file system's superblock). Pure; exposed for the tests. True if the mount is listed.
bool pb_mountinfo_dev(const char *text, unsigned mnt_id, uint32_t *dev);

#endif
Loading
Loading