LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
Firewall core for 0.8.4: deny mode, subscribed blocklists in the kernel, per-port accounting, system services' traffic
Federico Pereirad19cd116c099
diff --git a/server/Makefile b/server/Makefileindex 482414d..a4028e4 100644--- a/server/Makefile+++ b/server/Makefile@@ -69,7 +69,7 @@ DOC_SRC := src/doc/doc.c src/doc/wire.c src/ui/js_doc.c src/print/ipp.c src/prinsrc/print/escl.c src/print/print.c src/ui/js_print.cSRC += $(DOC_SRC)# Per-app firewall (src/fw, docs/FIREWALL.md): rules, decision and names learned from DNS-FW_SRC := src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/ui/js_fw.c+FW_SRC := src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/fw/fwlists.c src/ui/js_fw.cSRC += $(FW_SRC)OBJ := $(SRC:src/%.c=build/obj/%.o)HDR := $(wildcard src/*.h src/gpu/*.h src/ui/*.h src/term/*.h src/wl/*.h src/wasm/*.h src/sound/*.h src/pkg/*.h src/net/*.h src/install/*.h src/remote/*.h src/users/*.h src/sec/*.h src/monitor/*.h src/media/*.h src/app/*.h src/doc/*.h src/print/*.h) $(WL_GEN) $(APP_CLIENT_HDR)@@ -131,7 +131,7 @@ endif.DEFAULT_GOAL := allBPF ?= $(shell command -v clang >/dev/null 2>&1 && echo 1 || echo 0)NP_SRC := src/netperm/netperm.c src/netperm/bpfsys.c src/netperm/npcompile.c src/netperm/npdns.c src/netperm/npdiag.c \- src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/sec/json.c+ src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/fw/fwlists.c src/sec/json.cALL += build/lord-netpermifeq ($(BPF),1)NP_GEN := build/gen/np_bpf.h@@ -313,6 +313,16 @@ build/fw-rules-test: tests/fw_rules_test.c src/fw/rules.c src/fw/fwmap.c src/sec@mkdir -p build$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/fw_rules_test.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c./$@ || { rm -f $@; exit 1; }+# subscribed blocklists (0.8.4): raw parsers, the set, the compiled file, fuzzing (docs/FIREWALL.md, "Blocklists")+test: build/fwlists-test+build/fwlists-test: tests/fwlists_test.c src/fw/fwlists.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c $(wildcard src/fw/*.h) src/sec/json.h+ @mkdir -p build+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/fwlists_test.c src/fw/fwlists.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c+ ./$@ || { rm -f $@; exit 1; }+build/fwlists-test-asan: tests/fwlists_test.c src/fw/fwlists.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c $(wildcard src/fw/*.h)+ @mkdir -p build+ $(CC) $(CPPFLAGS) -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=undefined $(WARN) -std=c11 -o $@ tests/fwlists_test.c src/fw/fwlists.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c+ ./$@# the destinations a user types in the rule sheet (tests/fw_dest_cases.txt), through the store's# parser; tests/fwrule_input_test.mjs checks the same file against sdk/fwrule.js (run_suites.sh)test: build/fwdest-testdiff --git a/server/src/fw/fwlists.c b/server/src/fw/fwlists.cnew file mode 100644index 0000000..a0979d4--- /dev/null+++ b/server/src/fw/fwlists.c@@ -0,0 +1,382 @@+/* fwlists.c — subscribed blocklists: raw parsers, the set and the compiled file (see fwlists.h). */+#include "fwlists.h"+#include "fwmap.h"+#include "rules.h"++#include <arpa/inet.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++struct FwLists {+ FwMap exact, sub, nets; /* name → tag; name → tag; (fam, prefix, network) → tag */+ unsigned char has4[33], has6[129]; /* prefix lengths present */+ size_t ndom, nnet;+};++FwLists *fwl_new(void) { return calloc(1, sizeof(FwLists)); }++void fwl_free(FwLists *l) {+ if (!l) return;+ fwmap_free(&l->exact, NULL);+ fwmap_free(&l->sub, NULL);+ fwmap_free(&l->nets, NULL);+ free(l);+}++size_t fwl_domains(const FwLists *l) { return l ? l->ndom : 0; }+size_t fwl_nets(const FwLists *l) { return l ? l->nnet : 0; }++FwlFormat fwl_format_from_name(const char *n) {+ if (!n) return 0;+ if (!strcmp(n, "hosts")) return FWL_FMT_HOSTS;+ if (!strcmp(n, "domains")) return FWL_FMT_DOMAINS;+ if (!strcmp(n, "ips")) return FWL_FMT_IPS;+ return 0;+}++/* ---- the set --------------------------------------------------------------------------------- */++static size_t net_key(int fam, const uint8_t *addr, int prefix, unsigned char *k) {+ memset(k, 0, 18);+ k[0] = (unsigned char)fam;+ k[1] = (unsigned char)prefix;+ int bytes = fam == 4 ? 4 : 16;+ for (int i = 0; i < bytes; i++) {+ int bits = prefix - i * 8;+ unsigned char m = bits >= 8 ? 0xff : bits <= 0 ? 0 : (unsigned char)(0xff << (8 - bits));+ k[2 + i] = addr[i] & m;+ }+ return (size_t)(2 + bytes);+}++static int put(FwMap *m, const void *key, size_t kl, uint16_t tag) {+ void **slot = fwmap_slot(m, key, kl, 1);+ if (!slot) return -2;+ if (*slot) return 1;+ *slot = (void *)(uintptr_t)tag;+ return 0;+}++/* A name with at least one dot, normalized; 0 ok. */+static int good_name(const char *in, size_t n, char *out) {+ char tmp[FW_NAME_MAX + 2];+ if (!n || n > FW_NAME_MAX) return -1;+ memcpy(tmp, in, n);+ tmp[n] = 0;+ if (fw_norm_domain(tmp, out)) return -1;+ return strchr(out, '.') ? 0 : -1;+}++static int parse_net(const char *s, size_t n, FwRule *r) {+ char tmp[80];+ if (!n || n >= sizeof tmp) return -1;+ memcpy(tmp, s, n);+ tmp[n] = 0;+ if (!strchr(tmp, ':') && !strchr(tmp, '/') && !strchr(tmp, '.')) return -1;+ if (fw_parse_dest(tmp, r) || r->kind != FW_DST_CIDR) return -1;+ return 0;+}++/* Networks a blocklist may never carry: the LAN (it would cut this machine off its own network)+ * and anything broader than /8 (IPv4) or /16 (IPv6). */+static int net_usable(const FwRule *r) {+ if (r->prefix < (r->fam == 4 ? 8 : 16)) return 0;+ return !fw_addr_is_lan(r->fam, r->addr);+}++int fwl_add_line(FwLists *l, const char *line, size_t len, uint16_t tag) {+ if (!l || !line || len < 3 || line[1] != ' ' || !tag) return -1;+ const char *v = line + 2;+ size_t vn = len - 2;+ if (line[0] == 'd' || line[0] == 's') {+ char name[FW_NAME_MAX];+ if (good_name(v, vn, name) || strlen(name) != vn || memcmp(name, v, vn)) return -1; /* already normalized */+ if (l->ndom >= FWL_MAX_DOMAINS) return -2;+ int r = put(line[0] == 'd' ? &l->exact : &l->sub, name, vn, tag);+ if (!r) l->ndom++;+ return r;+ }+ if (line[0] == 'n') {+ FwRule r;+ if (parse_net(v, vn, &r) || !net_usable(&r)) return -1;+ if (l->nnet >= FWL_MAX_NETS) return -2;+ unsigned char k[18];+ size_t kl = net_key(r.fam, r.addr, r.prefix, k);+ int x = put(&l->nets, k, kl, tag);+ if (!x) {+ l->nnet++;+ if (r.fam == 4) l->has4[r.prefix] = 1;+ else l->has6[r.prefix] = 1;+ }+ return x;+ }+ return -1;+}++long fwl_add_text(FwLists *l, const char *text, size_t len, uint16_t tag) {+ long added = 0;+ for (size_t i = 0; i < len;) {+ const char *nl = memchr(text + i, '\n', len - i);+ size_t e = nl ? (size_t)(nl - text) : len;+ if (e > i) {+ int r = fwl_add_line(l, text + i, e - i, tag);+ if (r == -1) return -1;+ if (r == -2) return added;+ if (r == 0) added++;+ }+ i = e + 1;+ }+ return added;+}++FwLists *fwl_load_text(const char *text, size_t len, char *err, size_t errlen) {+ size_t hl = strlen(FWL_HEADER);+ if (!text || len > FWL_FILE_MAX || memchr(text, 0, len)) { snprintf(err, errlen, "not a list file"); return NULL; }+ if (len < hl || memcmp(text, FWL_HEADER, hl) || (len > hl && text[hl] != '\n')) { snprintf(err, errlen, "no header"); return NULL; }+ FwLists *l = fwl_new();+ if (!l) { snprintf(err, errlen, "out of memory"); return NULL; }+ size_t i = hl + 1, lineno = 1;+ while (i < len) {+ const char *nl = memchr(text + i, '\n', len - i);+ size_t e = nl ? (size_t)(nl - text) : len;+ lineno++;+ int r = e > i ? fwl_add_line(l, text + i, e - i, 1) : 0;+ if (r == -1 || r == -2) {+ snprintf(err, errlen, r == -2 ? "too many entries (line %zu)" : "invalid line %zu", lineno);+ fwl_free(l);+ return NULL;+ }+ i = e + 1;+ }+ return l;+}++typedef struct { char *p; size_t n, cap; int err; } Out;++static void out_put(Out *o, const char *s, size_t n) {+ if (o->err) return;+ if (o->n + n + 2 > o->cap) {+ size_t c = o->cap ? o->cap : 4096;+ while (c < o->n + n + 2) c *= 2;+ char *p = realloc(o->p, c);+ if (!p) { o->err = 1; return; }+ o->p = p;+ o->cap = c;+ }+ memcpy(o->p + o->n, s, n);+ o->n += n;+ o->p[o->n] = 0;+}++static char *text_of(const FwLists *l, int header, size_t *len) {+ Out o = { 0 };+ if (header) out_put(&o, FWL_HEADER "\n", strlen(FWL_HEADER) + 1);+ else out_put(&o, "", 0);+ char line[FW_NAME_MAX + 8];+ FWMAP_EACH(&l->exact, e) {+ int n = snprintf(line, sizeof line, "d %.*s\n", (int)e->klen, (const char *)e->key);+ out_put(&o, line, (size_t)n);+ }+ FWMAP_EACH(&l->sub, e) {+ int n = snprintf(line, sizeof line, "s %.*s\n", (int)e->klen, (const char *)e->key);+ out_put(&o, line, (size_t)n);+ }+ FWMAP_EACH(&l->nets, e) {+ char ip[INET6_ADDRSTRLEN];+ uint8_t a[16] = { 0 };+ int fam = e->key[0], prefix = e->key[1];+ memcpy(a, e->key + 2, fam == 4 ? 4 : 16);+ if (!inet_ntop(fam == 4 ? AF_INET : AF_INET6, a, ip, sizeof ip)) continue;+ int n = prefix == (fam == 4 ? 32 : 128) ? snprintf(line, sizeof line, "n %s\n", ip) : snprintf(line, sizeof line, "n %s/%d\n", ip, prefix);+ out_put(&o, line, (size_t)n);+ }+ if (o.err) { free(o.p); return NULL; }+ *len = o.n;+ return o.p;+}++char *fwl_to_text(const FwLists *l, size_t *len) { return l ? text_of(l, 1, len) : NULL; }++uint16_t fwl_match_name(const FwLists *l, const char *in) {+ if (!l || !in || !l->ndom) return 0;+ char name[FW_NAME_MAX];+ if (strnlen(in, FW_NAME_MAX + 1) > FW_NAME_MAX || fw_norm_domain(in, name)) return 0;+ void *v = fwmap_get(&l->exact, name, strlen(name));+ if (v) return (uint16_t)(uintptr_t)v;+ for (const char *sfx = name; sfx;) {+ v = fwmap_get(&l->sub, sfx, strlen(sfx));+ if (v) return (uint16_t)(uintptr_t)v;+ sfx = strchr(sfx, '.');+ if (sfx) sfx++;+ }+ return 0;+}++uint16_t fwl_match_addr(const FwLists *l, int fam, const uint8_t *addr) {+ if (!l || !addr || !l->nnet || (fam != 4 && fam != 6)) return 0;+ uint8_t a[16] = { 0 };+ static const uint8_t mapped[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };+ if (fam == 6 && !memcmp(addr, mapped, 12)) { fam = 4; memcpy(a, addr + 12, 4); }+ else memcpy(a, addr, fam == 4 ? 4 : 16);+ const unsigned char *has = fam == 4 ? l->has4 : l->has6;+ for (int p = fam == 4 ? 32 : 128; p >= 0; p--) {+ if (!has[p]) continue;+ unsigned char k[18];+ size_t kl = net_key(fam, a, p, k);+ void *v = fwmap_get(&l->nets, k, kl);+ if (v) return (uint16_t)(uintptr_t)v;+ }+ return 0;+}++void fwl_net_each(const FwLists *l, int (*cb)(void *u, int fam, const uint8_t *addr, int prefix), void *u) {+ if (!l) return;+ FWMAP_EACH(&l->nets, e) {+ uint8_t a[16] = { 0 };+ memcpy(a, e->key + 2, e->key[0] == 4 ? 4 : 16);+ if (cb(u, e->key[0], a, e->key[1])) return;+ }+}++uint16_t fwl_block_names(const FwLists *l, const char *const *names, int n) {+ uint16_t first = 0;+ for (int i = 0; i < n; i++) {+ uint16_t t = fwl_match_name(l, names[i]);+ if (!t) return 0; /* an unlisted name shares the address */+ if (!first) first = t;+ }+ return first;+}++/* ---- raw lists ------------------------------------------------------------------------------- */++static int is_space(char c) { return c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f'; }++/* Names a hosts file maps to itself, not blocklist entries. */+static int hosts_noise(const char *name) {+ static const char *const v[] = { "localhost", "localhost.localdomain", "local", "broadcasthost", "ip6-localhost",+ "ip6-loopback", "ip6-localnet", "ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters",+ "ip6-allhosts", "0.0.0.0", NULL };+ for (int i = 0; v[i]; i++) if (!strcmp(name, v[i])) return 1;+ return 0;+}++/* The sink-hole addresses a hosts-format blocklist points names to. */+static int hosts_target(const char *s, size_t n) {+ static const char *const v[] = { "0.0.0.0", "127.0.0.1", "::", "::1", NULL };+ for (int i = 0; v[i]; i++) if (strlen(v[i]) == n && !memcmp(s, v[i], n)) return 1;+ return 0;+}++static int set_err(char *err, size_t errlen, const char *code) {+ if (err && errlen) snprintf(err, errlen, "%s", code);+ return -1;+}++/* One name from a list: adds it (d/s) or counts why not. 1 added, 0 not, -1 the set is full. */+static int raw_name(FwLists *l, const char *s, size_t n, int sub, FwlStats *st) {+ char name[FW_NAME_MAX];+ if (n > 2 && s[0] == '*' && s[1] == '.') { s += 2; n -= 2; sub = 1; }+ if (n > 3 && s[0] == '|' && s[1] == '|' && s[n - 1] == '^') { s += 2; n -= 3; sub = 1; }+ char tmp[FW_NAME_MAX + 2];+ if (!n || n > FW_NAME_MAX) { st->bad++; return 0; }+ memcpy(tmp, s, n);+ tmp[n] = 0;+ for (char *c = tmp; *c; c++) if (*c >= 'A' && *c <= 'Z') *c = (char)(*c - 'A' + 'a');+ if (hosts_noise(tmp)) { st->skipped++; return 0; }+ if (fw_norm_domain(tmp, name)) { st->bad++; return 0; }+ if (!strchr(name, '.') || hosts_noise(name)) { st->skipped++; return 0; }+ char line[FW_NAME_MAX + 4];+ int ln = snprintf(line, sizeof line, "%c %s", sub ? 's' : 'd', name);+ if (st->domains >= FWL_LIST_DOMAINS) return -1;+ int r = fwl_add_line(l, line, (size_t)ln, 1);+ if (r == -2) return -1;+ if (r == 1) { st->dups++; return 0; }+ if (r) { st->bad++; return 0; }+ st->domains++;+ return 1;+}++static int raw_net(FwLists *l, const char *s, size_t n, FwlStats *st) {+ FwRule r;+ if (parse_net(s, n, &r)) { st->bad++; return 0; }+ if (!net_usable(&r)) { st->skipped++; return 0; }+ char ip[INET6_ADDRSTRLEN], line[80];+ inet_ntop(r.fam == 4 ? AF_INET : AF_INET6, r.addr, ip, sizeof ip);+ int ln = r.prefix == (r.fam == 4 ? 32 : 128) ? snprintf(line, sizeof line, "n %s", ip) : snprintf(line, sizeof line, "n %s/%d", ip, r.prefix);+ if (st->nets >= FWL_LIST_NETS) return -1;+ int x = fwl_add_line(l, line, (size_t)ln, 1);+ if (x == -2) return -1;+ if (x == 1) { st->dups++; return 0; }+ if (x) { st->bad++; return 0; }+ st->nets++;+ return 1;+}++int fwl_parse_raw(const char *raw, size_t len, FwlFormat fmt, int sub, char **out, size_t *outlen,+ FwlStats *st, char *err, size_t errlen) {+ FwlStats z = { 0 };+ if (!st) st = &z;+ memset(st, 0, sizeof *st);+ *out = NULL;+ *outlen = 0;+ if (fmt != FWL_FMT_HOSTS && fmt != FWL_FMT_DOMAINS && fmt != FWL_FMT_IPS) return set_err(err, errlen, "format");+ if (len > FWL_RAW_MAX) return set_err(err, errlen, "big");+ if (memchr(raw, 0, len)) return set_err(err, errlen, "nul");+ FwLists *l = fwl_new();+ if (!l) return set_err(err, errlen, "oom");+ int full = 0;+ size_t i = 0;+ /* a UTF-8 byte order mark at the start */+ if (len >= 3 && (unsigned char)raw[0] == 0xef && (unsigned char)raw[1] == 0xbb && (unsigned char)raw[2] == 0xbf) i = 3;+ while (i < len && !full) {+ const char *nl = memchr(raw + i, '\n', len - i);+ size_t e = nl ? (size_t)(nl - raw) : len;+ if (e - i > FWL_LINE_MAX) { fwl_free(l); return set_err(err, errlen, "line"); }+ const char *s = raw + i;+ size_t n = e - i;+ i = e + 1;+ /* comments: whole-line (#, !, ;) and, after a space, inline (#) */+ while (n && is_space(*s)) { s++; n--; }+ if (!n || *s == '#' || *s == '!' || *s == ';' || (n >= 2 && s[0] == '/' && s[1] == '/')) continue;+ for (size_t k = 1; k < n; k++) if (s[k] == '#' && is_space(s[k - 1])) { n = k; break; }+ if (fmt == FWL_FMT_IPS) for (size_t k = 0; k < n; k++) if (s[k] == ';') { n = k; break; }+ while (n && is_space(s[n - 1])) n--;+ if (!n) continue;+ st->lines++;+ /* tokens */+ const char *tok[16];+ size_t tl[16];+ int nt = 0, over = 0;+ for (size_t k = 0; k < n;) {+ while (k < n && is_space(s[k])) k++;+ if (k >= n) break;+ size_t b = k;+ while (k < n && !is_space(s[k])) k++;+ if (nt == 16) { over = 1; break; }+ tok[nt] = s + b;+ tl[nt++] = k - b;+ }+ for (size_t k = 0; k < n; k++) if ((unsigned char)s[k] < 0x20 && !is_space(s[k])) { over = 1; break; }+ if (over || !nt) { st->bad++; continue; }+ int r = 0;+ if (fmt == FWL_FMT_HOSTS) {+ if (nt < 2 || !hosts_target(tok[0], tl[0])) { st->bad++; continue; }+ for (int t = 1; t < nt && r >= 0; t++) r = raw_name(l, tok[t], tl[t], sub, st);+ } else if (fmt == FWL_FMT_DOMAINS) {+ if (nt != 1) { st->bad++; continue; }+ r = raw_name(l, tok[0], tl[0], sub, st);+ } else {+ r = raw_net(l, tok[0], tl[0], st); /* other columns (counts, dates) are ignored */+ }+ if (r < 0) { full = 1; st->truncated = 1; }+ }+ if (st->bad > 20 && st->bad * 10 > st->lines) { fwl_free(l); return set_err(err, errlen, "format"); }+ if (!st->domains && !st->nets) { fwl_free(l); return set_err(err, errlen, "empty"); }+ char *t = text_of(l, 0, outlen);+ fwl_free(l);+ if (!t) return set_err(err, errlen, "oom");+ *out = t;+ return 0;+}diff --git a/server/src/fw/fwlists.h b/server/src/fw/fwlists.hnew file mode 100644index 0000000..24a9332--- /dev/null+++ b/server/src/fw/fwlists.h@@ -0,0 +1,91 @@+/* fwlists.h — subscribed blocklists (0.8.4, docs/FIREWALL.md, "Blocklists").+ *+ * Three pieces, all pure C with no network and no files of their own:+ *+ * 1. The raw parsers (fwl_parse_raw): what a list's server sent (a hosts file, one domain per+ * line, or one address / network per line) → normalized lines. They run in lord-fetch, the+ * sandboxed helper, on untrusted bytes. Strict: a NUL byte, a line longer than FWL_LINE_MAX,+ * or too many lines that don't parse reject the whole list; LAN networks, networks broader+ * than /8 (IPv4) or /16 (IPv6), names without a dot and the usual hosts-file names+ * (localhost…) are skipped and counted.+ *+ * 2. The set (FwLists): exact names, names with every subdomain, and networks, each with a tag+ * (which list it came from, for the Monitor's "blocked by the list …"). Name lookups walk the+ * labels right to left; address lookups try each prefix length present, one hash lookup each.+ *+ * 3. The compiled file, ~/.config/lordos/fwlists.txt: written by lord-server (js_fw.c, after+ * validating every line again) and read by lord-netperm (root) through the same fixed-path,+ * no-links reader as firewall.json, with fwl_load_text:+ *+ * lordos-fwlists 1+ * d tracker.example an exact name+ * s ads.example the name and every name below it+ * n 192.0.2.0/24 an address or a network (bits outside the prefix are 0)+ *+ * Any line that doesn't validate rejects the whole file (the previous set stays in use).+ *+ * Lists never override a rule the owner wrote for that destination (rules.h, FwQuery.listed). */+#ifndef LORD_FW_FWLISTS_H+#define LORD_FW_FWLISTS_H++#include <stddef.h>+#include <stdint.h>++#define FWL_RAW_MAX (16u << 20) /* the most a list's server may send */+#define FWL_LINE_MAX 1024 /* longer lines reject the list */+#define FWL_LIST_DOMAINS 400000 /* names in one list */+#define FWL_LIST_NETS 100000 /* networks in one list */+#define FWL_MAX_DOMAINS 600000 /* in the compiled set, every list together */+#define FWL_MAX_NETS 150000+#define FWL_FILE_MAX (32u << 20) /* the compiled file */+#define FWL_HEADER "lordos-fwlists 1"++typedef enum { FWL_FMT_HOSTS = 1, FWL_FMT_DOMAINS = 2, FWL_FMT_IPS = 3 } FwlFormat;++typedef struct {+ size_t lines; /* lines read (without comments and blank lines) */+ size_t domains, nets; /* entries written */+ size_t skipped; /* valid lines we don't use (LAN, too broad, localhost, no dot) */+ size_t bad; /* lines that didn't parse */+ size_t dups; /* entries repeated inside the list */+ int truncated; /* 1: the list had more than FWL_LIST_DOMAINS / FWL_LIST_NETS */+} FwlStats;++/* Parses a raw list in format fmt; sub = 1 makes every name cover its subdomains ("s" lines).+ * Writes the normalized lines into *out (malloc'd, NUL-terminated, *outlen bytes). 0 ok; -1 the+ * whole list is rejected (err: a short code: "nul", "line", "format", "empty", "big", "oom"). */+int fwl_parse_raw(const char *raw, size_t len, FwlFormat fmt, int sub, char **out, size_t *outlen,+ FwlStats *st, char *err, size_t errlen);+FwlFormat fwl_format_from_name(const char *name); /* "hosts" / "domains" / "ips"; 0 if unknown */++typedef struct FwLists FwLists;++FwLists *fwl_new(void);+void fwl_free(FwLists *l);+/* Adds one normalized line ("d name", "s name", "n cidr") with a tag (1..65535). 0 added, 1+ * already there (the first tag stays), -1 invalid line, -2 the set is full. */+int fwl_add_line(FwLists *l, const char *line, size_t len, uint16_t tag);+/* Adds every line of a normalized text (the lines fwl_parse_raw writes). Returns the number+ * added, or -1 if a line is invalid (the lines before it stay). */+long fwl_add_text(FwLists *l, const char *text, size_t len, uint16_t tag);+/* The compiled file: header + lines. Replaces nothing on error: a fresh set is returned, or NULL+ * with err set. */+FwLists *fwl_load_text(const char *text, size_t len, char *err, size_t errlen);+/* The compiled file's text (malloc'd), every entry once. */+char *fwl_to_text(const FwLists *l, size_t *len);++size_t fwl_domains(const FwLists *l);+size_t fwl_nets(const FwLists *l);+/* The tag of the list that covers this name (normalized or not), or 0. */+uint16_t fwl_match_name(const FwLists *l, const char *name);+/* The tag of the list whose network contains the address (fam 4: 4 bytes, 6: 16), or 0. */+uint16_t fwl_match_addr(const FwLists *l, int fam, const uint8_t *addr);+/* Walks the networks (fam, network, prefix); cb returns != 0 to stop. */+void fwl_net_each(const FwLists *l, int (*cb)(void *u, int fam, const uint8_t *addr, int prefix), void *u);++/* Should the resolver's addresses for these names be blocked? Only when at least one name is+ * listed and none is unlisted: an address a listed tracker shares with a site that isn't listed+ * (a shared CDN) stays allowed. Returns the first listed name's tag, or 0. */+uint16_t fwl_block_names(const FwLists *l, const char *const *names, int n);++#endifdiff --git a/server/src/fw/rules.c b/server/src/fw/rules.cindex 85dddb2..afaefcc 100644--- a/server/src/fw/rules.c+++ b/server/src/fw/rules.c@@ -72,6 +72,12 @@ static int ends_with(const char *s, size_t n, const char *suf) {return n >= k && !memcmp(s + n - k, suf, k);}+int fw_component_ok(const char *name) {+ static const char *const v[] = FW_COMPONENTS;+ for (int i = 0; name && v[i]; i++) if (!strcmp(name, v[i])) return 1;+ return 0;+}+int fw_app_canon(const char *in, char *out, size_t n) {if (!in || !out || !n) return -1;size_t len = strnlen(in, FW_APP_MAX);@@ -172,6 +178,17 @@ int fw_addr_is_lan(int fam, const uint8_t *a) {return 0;}+int fw_addr_is_loopback(int fam, const uint8_t *a) {+ if (!a) return 0;+ if (fam == 6 && is_mapped4(a)) return a[12] == 127;+ if (fam == 4) return a[0] == 127;+ if (fam == 6) {+ static const uint8_t zero[15];+ return !memcmp(a, zero, 15) && a[15] == 1;+ }+ return 0;+}+static int bit_at(const uint8_t *a, int i) { return a[i >> 3] >> (7 - (i & 7)) & 1; }static int host_bits_zero(const uint8_t *a, int bytes, int prefix) {@@ -602,7 +619,7 @@ void fw_app_each(const FwStore *s, int (*cb)(void *u, const char *app, int trust}void fw_set_mode(FwStore *s, FwMode m) {- if (s && (m == FW_MODE_SILENT || m == FW_MODE_ASK || m == FW_MODE_OFF)) s->mode = m;+ if (s && (m == FW_MODE_SILENT || m == FW_MODE_ASK || m == FW_MODE_OFF || m == FW_MODE_DENY)) s->mode = m;}FwMode fw_mode(const FwStore *s) { return s ? s->mode : FW_MODE_SILENT; }@@ -694,7 +711,7 @@ FwDecision fw_decide(FwStore *s, const FwQuery *q) {if (lan == 1) consider(&p, a->lan, SC_NET);else if (lan == 0) consider(&p, a->inet, SC_NET);consider(&p, a->any, SC_ANY);- if (p.best) {+ if (p.best && (p.dst >= SC_CIDR || !q->listed)) {d.verdict = p.best->r.action == FW_DENY ? FW_V_DENY : FW_V_ALLOW;d.reason = FW_BY_RULE;d.rule_id = p.best->r.id;@@ -703,6 +720,9 @@ FwDecision fw_decide(FwStore *s, const FwQuery *q) {}}+ /* 2b. a subscribed blocklist: only a rule that names the destination (above) beats it */+ if (q->listed) { d.verdict = FW_V_DENY; d.reason = FW_BY_LIST; d.record = 1; return d; }+/* 3. trusted */if (a && a->trusted) { d.verdict = FW_V_ALLOW; d.reason = FW_BY_TRUSTED; return d; }@@ -712,6 +732,10 @@ FwDecision fw_decide(FwStore *s, const FwQuery *q) {case FW_MODE_SILENT: d.verdict = FW_V_ALLOW; d.record = 1; break;case FW_MODE_ASK: d.verdict = FW_V_ASK; break;case FW_MODE_OFF: d.verdict = FW_V_ALLOW; break;+ case FW_MODE_DENY:+ d.verdict = fam && fw_addr_is_loopback(fam, addr) ? FW_V_ALLOW : FW_V_DENY;+ d.record = 1;+ break;}return d;}@@ -753,7 +777,7 @@ static void putf(Buf *b, const char *fmt, ...) {put(b, t, (size_t)n);}-static const char *const MODE_NAMES[] = { "silent", "ask", "off" };+static const char *const MODE_NAMES[] = { "silent", "ask", "off", "deny" };static int cmp_app(const void *x, const void *y) { return strcmp((*(App *const *)x)->id, (*(App *const *)y)->id); }@@ -915,7 +939,7 @@ static int load_doc(FwStore *t, const JVal *root, char *err, size_t errlen) {if ((v = json_get(root, "mode"))) {const char *m = jstr(v, 8);int k = -1;- for (int i = 0; m && i < 3; i++) if (!strcmp(m, MODE_NAMES[i])) k = i;+ for (int i = 0; m && i < 4; i++) if (!strcmp(m, MODE_NAMES[i])) k = i;if (k < 0) return jerr(err, errlen, "invalid mode");t->mode = (FwMode)k;}diff --git a/server/src/fw/rules.h b/server/src/fw/rules.hindex ec8cce7..9d4056c 100644--- a/server/src/fw/rules.h+++ b/server/src/fw/rules.h@@ -9,8 +9,12 @@* 1. the app's restrictions (FW_R_*): always deny;* 2. explicit rules: the one with the most specific destination wins; with the same specificity,* deny beats allow;+ * 2b. a subscribed blocklist (FwQuery.listed) denies, unless the winning rule names the+ * destination (a domain, an address or a network): lists beat lan/internet/any rules,+ * never a rule the owner wrote for that destination;* 3. trusted app: allow without logging or asking;- * 4. mode: silent = allow and log, ask = pending, off = allow.+ * 4. mode: silent = allow and log, ask = pending, off = allow, deny = deny and log (loopback+ * stays allowed).** Lookups: hash table of apps; per app, a hash table of exact domains, a suffix table for* wildcards (the name's labels are walked from right to left), a binary prefix trie per family@@ -32,7 +36,8 @@ typedef enum { FW_PROTO_ANY = 0, FW_PROTO_TCP = 1, FW_PROTO_UDP = 2 } FwProto;typedef enum { FW_ALLOW = 0, FW_DENY = 1 } FwAction;/* forever (saved) / until the app closes / just once (not saved) */typedef enum { FW_LIFE_FOREVER = 0, FW_LIFE_UNTIL_QUIT = 1, FW_LIFE_ONCE = 2 } FwLife;-typedef enum { FW_MODE_SILENT = 0, FW_MODE_ASK = 1, FW_MODE_OFF = 2 } FwMode;+/* deny (0.8.4, "deny silently"): what no rule allows is denied and recorded; loopback stays allowed */+typedef enum { FW_MODE_SILENT = 0, FW_MODE_ASK = 1, FW_MODE_OFF = 2, FW_MODE_DENY = 3 } FwMode;typedef enum {FW_DST_ANY = 0,@@ -89,10 +94,13 @@ typedef struct {uint16_t port; /* remote port (outbound) or local port (inbound) */const char *const *hosts; /* names of that address for this app (dnsmap_lookup) */int nhosts;+ int listed; /* 1: a blocklist the user subscribed to covers this destination+ * (fw/fwlists.h); denies unless a rule naming the destination+ * (a domain, an address or a network) decides first */} FwQuery;typedef enum { FW_V_ALLOW = 0, FW_V_DENY = 1, FW_V_ASK = 2 } FwVerdict;-typedef enum { FW_BY_RESTRICTED, FW_BY_RULE, FW_BY_TRUSTED, FW_BY_MODE } FwReason;+typedef enum { FW_BY_RESTRICTED, FW_BY_RULE, FW_BY_TRUSTED, FW_BY_MODE, FW_BY_LIST } FwReason;typedef struct {FwVerdict verdict;@@ -112,6 +120,12 @@ void fw_store_free(FwStore *s);* desktop:<id>. 0 ok, -1 if it isn't a valid id (empty, too long, odd characters). */int fw_app_canon(const char *in, char *out, size_t n);+/* (0.8.4) LordOS's own components that use the network, each in its own scope+ * lordos-net-<name>.scope so the kernel enforces their rules (docs/MONITOR.md, "Native apps as+ * their own rows"): their firewall id is "lordos:<name>". 1 if name is one of them. */+#define FW_COMPONENTS { "mail", "store", "print", "terminal", "lists", NULL }+int fw_component_ok(const char *name);+/* Destination as text ("any", "lan", "internet", "example.com", "*.example.com", "10.0.0.0/8",* "2001:db8::1") → kind/wildcard/domain/fam/addr/prefix of r. 0 ok, -1 invalid. */int fw_parse_dest(const char *s, FwRule *r);@@ -124,6 +138,8 @@ int fw_format_dest(const FwRule *r, char *out, size_t n);/* LAN: 0/8, 10/8, 100.64/10 (CGNAT), 127/8, 169.254/16, 172.16/12, 192.168/16, 224/4, 255.255.255.255;* ::, ::1, fc00::/7, fe80::/10, ff00::/8; ::ffff:a.b.c.d as its IPv4. */int fw_addr_is_lan(int fam, const uint8_t *addr);+/* 127/8 and ::1 (and ::ffff:127.x.y.z): what the deny mode never denies. */+int fw_addr_is_loopback(int fam, const uint8_t *addr);/* Adds a rule (validates and copies it). If there's already one with the same app/dir/destination/* ports/protocol, it replaces it and keeps its id. *id_out (optional) receives the id. 0 ok, -1diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.cindex 89549b1..bc51fa8 100644--- a/server/src/netperm/netperm.c+++ b/server/src/netperm/netperm.c@@ -12,6 +12,9 @@* - learns names only from the system resolver (npdns.c);* - tells each lord-server about its apps' new connections over /run/lordos/netperm.sock* (npproto.h). The only thing it accepts there is "reload my rules";+ * - (0.8.4) reads each user's subscribed blocklists (~/.config/lordos/fwlists.txt, the same+ * fixed-path reader) into np_blk4/np_blk6: their networks, and the addresses the resolver gave+ * for their domains; and counts the essential system services' bytes (np_sysc);* - cuts the connections a rule now denies: the packet filter drops their packets in the* kernel, and this process destroys their sockets over sock_diag netlink (npdiag.c, the only* socket besides AF_UNIX and the DNS AF_PACKET one), right after a reload and whenever the@@ -27,6 +30,7 @@#include "netperm/npload.h"#include "netperm/npproto.h"#include "fw/fwmap.h"+#include "fw/fwlists.h"#include "errcode.h"#include "sec/json.h"@@ -91,8 +95,19 @@ typedef struct {int dirty_counts;uint64_t user_cg; /* id of user@<uid>.service, or 0 if it isn't there (nor attached) */int ulinks[16], nulinks; /* the programs attached to user@<uid>.service */+ FwLists *lists; /* (0.8.4) the subscribed blocklists, NULL: none */+ uint32_t blkset; /* its set in np_blk4/np_blk6 (NpApp.blk) */+ FwMap blkin; /* installed entries: (fam, prefix, address) → BlkEnt */+ int nlearned; /* entries from the resolver among them */} Session;+typedef struct {+ int learned; /* 1: an address the resolver gave for a listed name */+ long long mark; /* the pass that last wanted it */+} BlkEnt;++#define BLK_LEARNED_MAX 16384 /* resolver addresses per user */+typedef struct {int fd;unsigned uid;@@ -112,6 +127,10 @@ static struct {int bpf_ok;char bpf_err[200]; /* a code line (errcode.h): the firewall page shows it translated */int m_np_cfg, m_np_apps, m_np_union, m_np_lpm4, m_np_lpm6, m_np_seen, m_np_counts, m_np_events, m_np_snd, m_np_flows, m_np_bound;+ int m_np_blk4, m_np_blk6, m_np_sysc;+ uint32_t next_blkset;+ long long blk_pass;+ struct { uint64_t cgid; int links[4], nlinks; uint64_t rx, tx; double rrx, rtx; } sys[3]; /* dns, time, network */int diag_fd; /* NETLINK_SOCK_DIAG (npdiag.c), -1 until first used */int diag_warned; /* the kernel can't destroy sockets: said once */int acct_ok; /* the traffic accounting programs loaded */@@ -161,11 +180,11 @@ static long long mono(void) {/* ~/.config/lordos/firewall.json without following links: every folder and the file must belong* to the user, the file must be regular and at most FW_FILE_MAX. The home folder comes from /etc/passwd. */-static char *read_user_rules(unsigned uid, size_t *len, char *why, size_t wn) {+static char *read_user_file(unsigned uid, const char *file, size_t max, size_t *len, char *why, size_t wn) {struct passwd *pw = getpwuid(uid);if (!pw || pw->pw_dir[0] != '/') { snprintf(why, wn, "user without a home folder"); return NULL; }int fd = open(pw->pw_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);- static const char *const parts[] = { ".config", "lordos", "firewall.json" };+ const char *const parts[] = { ".config", "lordos", file };struct stat st;if (fd < 0 || fstat(fd, &st) || st.st_uid != uid) { snprintf(why, wn, "odd home folder"); if (fd >= 0) close(fd); return NULL; }for (int i = 0; i < 3; i++) {@@ -174,7 +193,7 @@ static char *read_user_rules(unsigned uid, size_t *len, char *why, size_t wn) {close(fd);if (nfd < 0) { snprintf(why, wn, errno == ENOENT ? "no rules" : "%s: %s", parts[i], strerror(errno)); return NULL; }fd = nfd;- if (fstat(fd, &st) || st.st_uid != uid || (last && (!S_ISREG(st.st_mode) || st.st_size > (off_t)FW_FILE_MAX))) {+ if (fstat(fd, &st) || st.st_uid != uid || (last && (!S_ISREG(st.st_mode) || st.st_size > (off_t)max))) {snprintf(why, wn, "%s is not the user's or not a regular file", parts[i]);close(fd);return NULL;@@ -197,7 +216,7 @@ static char *read_user_rules(unsigned uid, size_t *len, char *why, size_t wn) {static void load_rules(Session *s) {size_t len = 0;char why[160] = "", err[200] = "";- char *text = read_user_rules(s->uid, &len, why, sizeof why);+ char *text = read_user_file(s->uid, "firewall.json", FW_FILE_MAX, &len, why, sizeof why);FwStore *n = fw_store_new();if (!n) { free(text); return; }if (text && fw_from_json(n, text, len, err, sizeof err)) {@@ -213,6 +232,26 @@ static void load_rules(Session *s) {logf_("uid %u: %zu rules, mode %d", s->uid, fw_rule_count(n), (int)fw_mode(n));}+/* (0.8.4) ~/.config/lordos/fwlists.txt, the user's subscribed blocklists compiled by lord-server+ * (fw/fwlists.h), through the same reader. A file that doesn't validate keeps the previous set. */+static void load_lists(Session *s) {+ size_t len = 0;+ char why[160] = "", err[120] = "";+ char *text = read_user_file(s->uid, "fwlists.txt", FWL_FILE_MAX, &len, why, sizeof why);+ if (!text) {+ if (strcmp(why, "no rules")) logf_("uid %u: blocklists: %s", s->uid, why);+ fwl_free(s->lists);+ s->lists = NULL;+ return;+ }+ FwLists *l = fwl_load_text(text, len, err, sizeof err);+ free(text);+ if (!l) { logf_("uid %u: invalid fwlists.txt (%s); the previous blocklists stay in use", s->uid, err); return; }+ fwl_free(s->lists);+ s->lists = fwl_domains(l) || fwl_nets(l) ? l : (fwl_free(l), NULL);+ if (s->lists) logf_("uid %u: blocklists: %zu names, %zu networks", s->uid, fwl_domains(s->lists), fwl_nets(s->lists));+}+/* ---- maps ------------------------------------------------------------------------------------ */#ifdef NP_HAVE_BPF /* the program loader's relocations (bpf_setup) */@@ -228,6 +267,9 @@ static int map_fd(const char *name) {if (!strcmp(name, "np_snd")) return G.m_np_snd;if (!strcmp(name, "np_flows")) return G.m_np_flows;if (!strcmp(name, "np_bound")) return G.m_np_bound;+ if (!strcmp(name, "np_blk4")) return G.m_np_blk4;+ if (!strcmp(name, "np_blk6")) return G.m_np_blk6;+ if (!strcmp(name, "np_sysc")) return G.m_np_sysc;return -1;}#endif@@ -301,6 +343,7 @@ static void sync_scope(Session *s, Scope *sc) {for (int i = 0; i < c.nnodes; i++) install(sc->idx, gen, &c.nodes[i]);prune(sc->idx, gen);sc->has_domains = c.has_domains;+ if (s->lists) { c.app.blk = s->blkset; c.app.can_deny = 1; } /* lists deny: the filter decides for it */sc->app_v = c.app;if (bpfs_map_update(G.m_np_apps, &sc->cgid, &c.app, BPF_ANY)) logf_("np_apps: %s", strerror(errno));/* the counters' entry exists from the start: the accounting programs only add to existing@@ -323,6 +366,100 @@ static void drop_scope(Scope *sc) {prune(sc->idx, 0);}+/* ---- blocklists (0.8.4, docs/FIREWALL.md "Blocklists") --------------------------------------+ *+ * Each user with subscribed lists gets a set in np_blk4/np_blk6 (NpApp.blk of each of their apps):+ * the lists' networks, and the addresses the system resolver gave for a listed name, as long as+ * every name the resolver gave for that address is listed (a shared CDN address stays allowed).+ * The whole set is recomputed on a reload and every 30 s (what the resolver forgot goes); a DNS+ * answer updates its own addresses at once, before the app connects. The kernel decides with+ * np_decide: a rule the owner wrote for that destination still wins. */++static size_t blk_key(int fam, const uint8_t *addr, int prefix, unsigned char *k) {+ memset(k, 0, 18);+ k[0] = (unsigned char)fam;+ k[1] = (unsigned char)prefix;+ memcpy(k + 2, addr, fam == 4 ? 4 : 16);+ return 18;+}++static int blk_map(uint32_t set, int fam, const uint8_t *addr, int prefix, int del) {+ uint8_t one = 1;+ if (fam == 4) {+ NpLpm4Key k;+ memset(&k, 0, sizeof k);+ k.prefixlen = 32 + (uint32_t)prefix;+ k.data[0] = (uint8_t)(set >> 24); k.data[1] = (uint8_t)(set >> 16); k.data[2] = (uint8_t)(set >> 8); k.data[3] = (uint8_t)set;+ memcpy(k.data + 4, addr, 4);+ return del ? bpfs_map_delete(G.m_np_blk4, &k) : bpfs_map_update(G.m_np_blk4, &k, &one, BPF_ANY);+ }+ NpLpm6Key k;+ memset(&k, 0, sizeof k);+ k.prefixlen = 32 + (uint32_t)prefix;+ k.data[0] = (uint8_t)(set >> 24); k.data[1] = (uint8_t)(set >> 16); k.data[2] = (uint8_t)(set >> 8); k.data[3] = (uint8_t)set;+ memcpy(k.data + 4, addr, 16);+ return del ? bpfs_map_delete(G.m_np_blk6, &k) : bpfs_map_update(G.m_np_blk6, &k, &one, BPF_ANY);+}++static void blk_want(Session *s, int fam, const uint8_t *addr, int prefix, int learned) {+ unsigned char k[18];+ size_t kl = blk_key(fam, addr, prefix, k);+ void **slot = fwmap_slot(&s->blkin, k, kl, 1);+ if (!slot) return;+ BlkEnt *e = *slot;+ if (e) {+ e->mark = G.blk_pass;+ if (!learned && e->learned) { e->learned = 0; s->nlearned--; }+ return;+ }+ if ((learned && s->nlearned >= BLK_LEARNED_MAX) || blk_map(s->blkset, fam, addr, prefix, 0) || !(e = malloc(sizeof *e))) {+ fwmap_take(&s->blkin, k, kl);+ return;+ }+ e->learned = learned;+ e->mark = G.blk_pass;+ s->nlearned += learned;+ *slot = e;+}++/* An address the resolver now also gives for an unlisted name: no longer blocked. */+static void blk_unwant_learned(Session *s, int fam, const uint8_t *addr) {+ unsigned char k[18];+ size_t kl = blk_key(fam, addr, fam == 4 ? 32 : 128, k);+ BlkEnt *e = fwmap_get(&s->blkin, k, kl);+ if (!e || !e->learned) return;+ blk_map(s->blkset, fam, addr, fam == 4 ? 32 : 128, 1);+ free(fwmap_take(&s->blkin, k, kl));+ s->nlearned--;+}++static int blk_net_cb(void *u, int fam, const uint8_t *addr, int prefix) { blk_want(u, fam, addr, prefix, 0); return 0; }++static int blk_dns_cb(void *u, int fam, const uint8_t *addr, const char (*names)[FW_NAME_MAX], int n) {+ Session *s = u;+ const char *p[DNSMAP_PER_IP];+ for (int i = 0; i < n && i < DNSMAP_PER_IP; i++) p[i] = names[i];+ if (fwl_block_names(s->lists, p, n < DNSMAP_PER_IP ? n : DNSMAP_PER_IP)) blk_want(s, fam, addr, fam == 4 ? 32 : 128, 1);+ return 0;+}++static void sync_blk(Session *s) {+ if (!G.bpf_ok) return;+ G.blk_pass++;+ if (s->lists) {+ if (!s->blkset) s->blkset = ++G.next_blkset;+ fwl_net_each(s->lists, blk_net_cb, s);+ if (fwl_domains(s->lists)) dnsmap_each(G.names, NAMES_APP, mono(), blk_dns_cb, s);+ }+ FWMAP_EACH(&s->blkin, e) {+ BlkEnt *b = e->val;+ if (b->mark == G.blk_pass) continue;+ blk_map(s->blkset, e->key[0], e->key + 2, e->key[1], 1);+ s->nlearned -= b->learned;+ free(fwmap_take(&s->blkin, e->key, e->klen));+ }+}+/* ---- user@<uid>.service: the systemd-run escape ------------------------------------------- *//* An app without a sandbox, running as the user, can ask its systemd manager@@ -398,6 +535,17 @@ static void sync_user_scope(Session *s) {static int scope_app(const char *name, char *app) {if (!strcmp(name, "server")) { snprintf(app, FW_APP_MAX, "lordos"); return 0; }size_t n = strlen(name);+ /* (0.8.4) LordOS's own components that use the network: lordos-net-<name>.scope → lordos:<name> */+ if (n > 17 && !strncmp(name, "lordos-net-", 11) && !strcmp(name + n - 6, ".scope")) {+ char comp[32];+ size_t cl = n - 11 - 6;+ if (cl >= sizeof comp) return -1;+ memcpy(comp, name + 11, cl);+ comp[cl] = 0;+ if (!fw_component_ok(comp)) return -1;+ snprintf(app, FW_APP_MAX, "lordos:%s", comp);+ return 0;+ }if (n < 18 || strncmp(name, "app-lordos-", 11) || strcmp(name + n - 6, ".scope")) return -1;return fw_app_canon(name, app, FW_APP_MAX);}@@ -450,6 +598,10 @@ static void scan_session(Session *s) {static void drop_session(int k) {Session *s = &G.sess[k];for (int i = 0; i < s->nsc; i++) drop_scope(&s->sc[i]);+ fwl_free(s->lists);+ s->lists = NULL;+ sync_blk(s); /* with no lists: removes every entry */+ fwmap_free(&s->blkin, free);drop_user_links(s);if (s->user_cg) { bpfs_map_delete(G.m_np_union, &s->user_cg); bpfs_map_delete(G.m_np_counts, &s->user_cg); }if (s->wd >= 0) inotify_rm_watch(G.ifd, s->wd);@@ -520,6 +672,8 @@ static void scan_all(void) {snprintf(path, sizeof path, CG_ROOT "/" CG_SERVICE "/%s", s->dir);s->wd = inotify_add_watch(G.ifd, path, IN_CREATE | IN_DELETE | IN_ONLYDIR);load_rules(s);+ load_lists(s);+ sync_blk(s);}s->seen = 1;scan_session(s);@@ -615,6 +769,8 @@ static void reload_uid(unsigned uid) {for (int i = 0; i < G.nsess; i++) {if (G.sess[i].uid != uid) continue;load_rules(&G.sess[i]);+ load_lists(&G.sess[i]);+ sync_blk(&G.sess[i]);for (int k = 0; k < G.sess[i].nsc; k++) sync_scope(&G.sess[i], &G.sess[i].sc[k]);sync_union(&G.sess[i]);}@@ -718,7 +874,26 @@ static int user_denies(const Scope *sc, const NpSock *k, int *fam, uint8_t *addr*dir = NP_D_OUT;*port = k->dport;if (!bpfs_map_lookup(G.m_np_bound, &bk, &one)) { *dir = NP_D_IN; *port = k->sport; }- np_decide(a, have ? &l : NULL, *dir, proto, *port, lan, r);+ int listed = 0;+ if (a->blk) {+ uint8_t v;+ if (*fam == 4) {+ NpLpm4Key key;+ memset(&key, 0, sizeof key);+ key.prefixlen = 64;+ key.data[0] = (uint8_t)(a->blk >> 24); key.data[1] = (uint8_t)(a->blk >> 16); key.data[2] = (uint8_t)(a->blk >> 8); key.data[3] = (uint8_t)a->blk;+ memcpy(key.data + 4, addr, 4);+ listed = !bpfs_map_lookup(G.m_np_blk4, &key, &v);+ } else {+ NpLpm6Key key;+ memset(&key, 0, sizeof key);+ key.prefixlen = 160;+ key.data[0] = (uint8_t)(a->blk >> 24); key.data[1] = (uint8_t)(a->blk >> 16); key.data[2] = (uint8_t)(a->blk >> 8); key.data[3] = (uint8_t)a->blk;+ memcpy(key.data + 4, addr, 16);+ listed = !bpfs_map_lookup(G.m_np_blk6, &key, &v);+ }+ }+ np_decide(a, have ? &l : NULL, *dir, proto, *port, lan, listed, np_is_loop(*fam, addr), r);return r->verdict == NP_V_DENY;}@@ -821,7 +996,7 @@ static void sweep(unsigned uid) {/* ---- notices and counters -------------------------------------------------------------------- */static const char *by_name(int r) {- return r == NP_BY_RESTRICTED ? "restricted" : r == NP_BY_RULE ? "rule" : r == NP_BY_TRUSTED ? "trusted" : "mode";+ return r == NP_BY_RESTRICTED ? "restricted" : r == NP_BY_RULE ? "rule" : r == NP_BY_TRUSTED ? "trusted" : r == NP_BY_LIST ? "list" : "mode";}static void on_event(void *u, const void *data, uint32_t len) {@@ -912,6 +1087,46 @@ static void on_net_event(const NpEvent *evp, int cut) {ev.flag ? " " : "", ev.flag && flag ? flag : "");}+/* ---- the essential system services (0.8.4) ----------------------------------------------------+ *+ * Name resolution, time and the network service are not apps and can't be blocked, but the Monitor+ * shows their traffic: the accounting programs alone are attached to resolved's and timesyncd's+ * cgroups (lord-netd's leaf, lordos.service/netd, is already under lordos.service), and np_sysc+ * gets an entry for each, keyed by its cgroup id (the programs only add to existing entries). */+static const struct { const char *id, *parent, *leaf; } SYS[3] = {+ { "system:dns", CG_ROOT "/system.slice", "systemd-resolved.service" },+ { "system:time", CG_ROOT "/system.slice", "systemd-timesyncd.service" },+ { "system:network", CG_ROOT "/" CG_SERVICE, "netd" },+};++static void sync_system(void) {+ if (!G.bpf_ok || G.acct_ok != 2) return;+ for (int i = 0; i < 3; i++) {+ int dfd = open(SYS[i].parent, O_RDONLY | O_DIRECTORY | O_CLOEXEC);+ uint64_t id = dfd >= 0 ? bpfs_cgroup_id(dfd, SYS[i].leaf) : 0;+ if (id == G.sys[i].cgid) { if (dfd >= 0) close(dfd); continue; }+ for (int k = 0; k < G.sys[i].nlinks; k++) close(G.sys[i].links[k]);+ if (G.sys[i].cgid) bpfs_map_delete(G.m_np_sysc, &G.sys[i].cgid);+ memset(&G.sys[i], 0, sizeof G.sys[i]);+ if (!id) { if (dfd >= 0) close(dfd); continue; }+ NpCount z = { 0, 0, 0, 0, 0 };+ bpfs_map_update(G.m_np_sysc, &id, &z, BPF_ANY);+ if (strcmp(SYS[i].leaf, "netd")) {+ int cfd = openat(dfd, SYS[i].leaf, O_RDONLY | O_DIRECTORY | O_CLOEXEC);+ for (int p = 0; cfd >= 0 && p < G.nprogs && G.sys[i].nlinks < 4; p++) {+ if (!np_attach_system(G.attach[p])) continue;+ int l = bpfs_link_create(G.progs[p], cfd, G.attach[p]);+ if (l < 0) { logf_("%s: could not attach the accounting: %s", SYS[i].leaf, strerror(errno)); break; }+ G.sys[i].links[G.sys[i].nlinks++] = l;+ }+ if (cfd >= 0) close(cfd);+ }+ close(dfd);+ G.sys[i].cgid = id;+ logf_("counting %s (%s)", SYS[i].id, SYS[i].leaf);+ }+}+/* ---- traffic per address (np_flows) ------------------------------------------------------- */typedef struct {@@ -970,9 +1185,33 @@ static void idle_flows(void) {}}-/* [["addr",rx,tx,rxRate,txRate,blocked],...] of one app, busiest first. */+/* [["addr",rx,tx,rxRate,txRate,blocked,[[port,"tcp"|"udp"|"other","out"|"in",rx,tx,rxRate,txRate],...]],...]+ * of one app, busiest address first (0.8.4: np_flows is per address and port; this folds the ports+ * of each address together and lists its 8 busiest under it). */+#define PORTS_PER_ADDR 8+typedef struct { const NpFlowKey *k; uint64_t rx, tx, blocked; double rrx, rtx; int first, n; } AddrAgg;++static int agg_cmp(const void *a, const void *b) {+ const AddrAgg *x = a, *y = b;+ double rx = x->rrx + x->rtx, ry = y->rrx + y->rtx;+ if (rx != ry) return rx < ry ? 1 : -1;+ uint64_t bx = x->rx + x->tx, by = y->rx + y->tx;+ return bx < by ? 1 : bx > by ? -1 : 0;+}++static int same_addr(const NpFlowKey *a, const NpFlowKey *b) { return a->fam == b->fam && !memcmp(a->addr, b->addr, 16); }++static int ref_addr_cmp(const void *a, const void *b) {+ const NpFlowKey *x = ((const FlowRef *)a)->k, *y = ((const FlowRef *)b)->k;+ if (x->fam != y->fam) return x->fam - y->fam;+ int c = memcmp(x->addr, y->addr, 16);+ if (c) return c;+ return flow_cmp(a, b);+}+static size_t flows_json(uint64_t cgid, char *j, size_t cap) {static FlowRef refs[NP_MAX_FLOWS];+ static AddrAgg agg[NP_MAX_FLOWS];int n = 0;FWMAP_EACH(&G.flows, e) {const NpFlowKey *k = (const NpFlowKey *)(const void *)e->key;@@ -981,15 +1220,41 @@ static size_t flows_json(uint64_t cgid, char *j, size_t cap) {refs[n].k = k;refs[n++].f = e->val;}- qsort(refs, (size_t)n, sizeof refs[0], flow_cmp);+ /* group by address (each group's ports busiest first) */+ qsort(refs, (size_t)n, sizeof refs[0], ref_addr_cmp);+ int na = 0;+ for (int i = 0; i < n; i++) {+ if (!na || !same_addr(agg[na - 1].k, refs[i].k)) {+ AddrAgg *a = &agg[na++];+ memset(a, 0, sizeof *a);+ a->k = refs[i].k;+ a->first = i;+ }+ AddrAgg *a = &agg[na - 1];+ a->rx += refs[i].f->rx;+ a->tx += refs[i].f->tx;+ a->blocked += refs[i].f->blocked;+ a->rrx += refs[i].f->rrx;+ a->rtx += refs[i].f->rtx;+ a->n++;+ }+ qsort(agg, (size_t)na, sizeof agg[0], agg_cmp);size_t w = 0;w += (size_t)snprintf(j + w, cap - w, "[");- for (int i = 0; i < n && i < FLOWS_PER_APP && w + 170 < cap; i++) {+ for (int i = 0; i < na && i < FLOWS_PER_APP && w + 170 + PORTS_PER_ADDR * 80 < cap; i++) {char addr[INET6_ADDRSTRLEN] = "";- inet_ntop(refs[i].k->fam == 4 ? AF_INET : AF_INET6, refs[i].k->addr, addr, sizeof addr);- w += (size_t)snprintf(j + w, cap - w, "%s[\"%s\",%llu,%llu,%.0f,%.0f,%llu]", i ? "," : "", addr,- (unsigned long long)refs[i].f->rx, (unsigned long long)refs[i].f->tx, refs[i].f->rrx, refs[i].f->rtx,- (unsigned long long)refs[i].f->blocked);+ inet_ntop(agg[i].k->fam == 4 ? AF_INET : AF_INET6, agg[i].k->addr, addr, sizeof addr);+ w += (size_t)snprintf(j + w, cap - w, "%s[\"%s\",%llu,%llu,%.0f,%.0f,%llu,[", i ? "," : "", addr,+ (unsigned long long)agg[i].rx, (unsigned long long)agg[i].tx, agg[i].rrx, agg[i].rtx,+ (unsigned long long)agg[i].blocked);+ for (int p = 0; p < agg[i].n && p < PORTS_PER_ADDR; p++) {+ const FlowRef *r = &refs[agg[i].first + p];+ const char *pr = r->k->proto == NP_P_TCP ? "tcp" : r->k->proto == NP_P_UDP ? "udp" : "other";+ w += (size_t)snprintf(j + w, cap - w, "%s[%u,\"%s\",\"%s\",%llu,%llu,%.0f,%.0f]", p ? "," : "", r->k->port, pr,+ r->k->dir == NP_D_IN ? "in" : "out", (unsigned long long)r->f->rx, (unsigned long long)r->f->tx,+ r->f->rrx, r->f->rtx);+ }+ w += (size_t)snprintf(j + w, cap - w, "]]");}w += (size_t)snprintf(j + w, cap - w, "]");return w;@@ -1025,6 +1290,20 @@ static void push_counts(void) {sc->rtx = rtx;}}+ int sys_moved = 0;+ for (int i = 0; i < 3; i++) {+ if (!G.sys[i].cgid) continue;+ NpCount c = { 0, 0, 0, 0, 0 };+ bpfs_map_lookup(G.m_np_sysc, &G.sys[i].cgid, &c);+ double rrx = dt > 0 && c.rx >= G.sys[i].rx ? (double)(c.rx - G.sys[i].rx) / dt : 0;+ double rtx = dt > 0 && c.tx >= G.sys[i].tx ? (double)(c.tx - G.sys[i].tx) / dt : 0;+ if (c.rx != G.sys[i].rx || c.tx != G.sys[i].tx || (rrx == 0) != (G.sys[i].rrx == 0) || (rtx == 0) != (G.sys[i].rtx == 0)) sys_moved = 1;+ G.sys[i].rx = c.rx;+ G.sys[i].tx = c.tx;+ G.sys[i].rrx = rrx;+ G.sys[i].rtx = rtx;+ }+ if (sys_moved) for (int i = 0; i < G.nsess; i++) G.sess[i].dirty_counts = 1;/* the per-address table only while bytes move (one batch call per second) */if (G.bpf_ok && G.acct_ok == 2) {if (bytes_moved) read_flows(dt);@@ -1037,8 +1316,8 @@ static void push_counts(void) {for (int c = 0; c < G.ncl && !has_client; c++) has_client = G.cl[c].uid == s->uid;if (!has_client) continue; /* stays dirty until someone connects */s->dirty_counts = 0;- size_t per = FW_APP_MAX * 2 + 200 + (size_t)FLOWS_PER_APP * 170;- size_t cap = 64 + (size_t)s->nsc * per, w = 0;+ size_t per = FW_APP_MAX * 2 + 200 + (size_t)FLOWS_PER_APP * (170 + PORTS_PER_ADDR * 80);+ size_t cap = 64 + (size_t)s->nsc * per + 3 * 160, w = 0;char *j = malloc(cap);if (!j) continue;j[w++] = '{';@@ -1052,6 +1331,12 @@ static void push_counts(void) {w += flows_json(sc->cgid, j + w, cap - w - 40);w += (size_t)snprintf(j + w, cap - w, ",%llu]", (unsigned long long)sc->blocked);}+ /* the essential system services: the same shape, never a connection count or a rule */+ for (int k = 0; k < 3; k++) {+ if (!G.sys[k].cgid) continue;+ w += (size_t)snprintf(j + w, cap - w, "%s\"%s\":[0,0,%llu,%llu,%.0f,%.0f,[],0]", w > 1 ? "," : "", SYS[k].id,+ (unsigned long long)G.sys[k].rx, (unsigned long long)G.sys[k].tx, G.sys[k].rrx, G.sys[k].rtx);+ }j[w++] = '}';send_uid(s->uid, NP_MSG_COUNTS, j, w, 0);free(j);@@ -1074,6 +1359,12 @@ static void on_dns(void *u, const uint8_t *pkt, size_t len) {if (!n) continue;for (int i = 0; i < G.nsess; i++) {Session *s = &G.sess[i];+ if (s->lists && fwl_domains(s->lists)) { /* blocklists: before the app connects */+ const char *p[DNSMAP_PER_IP];+ for (int k = 0; k < n; k++) p[k] = names[k];+ if (fwl_block_names(s->lists, p, n)) blk_want(s, r->addr[a].fam, r->addr[a].addr, r->addr[a].fam == 4 ? 32 : 128, 1);+ else blk_unwant_learned(s, r->addr[a].fam, r->addr[a].addr);+ }for (int k = 0; k < s->nsc; k++) {Scope *sc = &s->sc[k];if (!sc->has_domains) continue;@@ -1117,7 +1408,7 @@ static int bpf_setup(void) {#define NP_MKMAP(n, t, k, v, m, f) G.m_##n = bpfs_map_create(t, k, v, m, f, #n);NP_MAPS(NP_MKMAP)#undef NP_MKMAP- if (G.m_np_bound < 0 || G.m_np_flows < 0 || G.m_np_snd < 0 || G.m_np_cfg < 0 || G.m_np_apps < 0 || G.m_np_union < 0 || G.m_np_lpm4 < 0 || G.m_np_lpm6 < 0 || G.m_np_seen < 0 || G.m_np_counts < 0 || G.m_np_events < 0) {+ if (G.m_np_blk4 < 0 || G.m_np_blk6 < 0 || G.m_np_sysc < 0 || G.m_np_bound < 0 || G.m_np_flows < 0 || G.m_np_snd < 0 || G.m_np_cfg < 0 || G.m_np_apps < 0 || G.m_np_union < 0 || G.m_np_lpm4 < 0 || G.m_np_lpm6 < 0 || G.m_np_seen < 0 || G.m_np_counts < 0 || G.m_np_events < 0) {lerr_set(G.bpf_err, sizeof G.bpf_err, "fw.bpfMaps", "errno", lerr_errno(errno), NULL);return -1;}@@ -1296,10 +1587,12 @@ int main(int argc, char **argv) {if (read(G.tfd, &x, sizeof x) < 0) {}G.tick++;attach_service();+ sync_system();if (G.dns) { npdns_tick(G.dns); watch_dns(); }if (G.tick % 5 == 0) scan_all(); /* in case an inotify notice was lost */if (G.tick % 30 == 0) {dnsmap_expire(G.names, mono());+ for (int s = 0; s < G.nsess; s++) if (G.sess[s].lists || G.sess[s].blkin.n) sync_blk(&G.sess[s]);for (int s = 0; s < G.nsess; s++)for (int k = 0; k < G.sess[s].nsc; k++)if (G.sess[s].sc[k].has_domains) sync_scope(&G.sess[s], &G.sess[s].sc[k]);diff --git a/server/src/netperm/np.bpf.c b/server/src/netperm/np.bpf.cindex d97dddc..1be756c 100644--- a/server/src/netperm/np.bpf.c+++ b/server/src/netperm/np.bpf.c@@ -17,7 +17,8 @@* cgroup/lsm_socket_create "no Bluetooth": socket(AF_BLUETOOTH) (a per-cgroup LSM program,* BPF_LSM_CGROUP: the inet sock_create hook never sees that family)* cgroup/skb_ingress, egress traffic accounting for the Monitor: bytes per app (np_counts) and- * per (app, remote address) (np_flows); and the cut of connections+ * per (app, remote address, port) (np_flows), and (0.8.4) of the+ * essential system services (np_sysc); and the cut of connections* opened before a rule changed: a packet whose connection the app's* current rules deny is dropped (counted as "blocked") and its socket* is reported once, so lord-netperm destroys it (SOCK_DESTROY).@@ -59,6 +60,7 @@ static long (*bpf_skb_load_bytes)(const void *skb, unsigned int offset, void *tostatic unsigned long long (*bpf_skb_ancestor_cgroup_id)(void *skb, int level) = (void *)83;static unsigned long long (*bpf_get_socket_cookie)(void *ctx) = (void *)46;static struct bpf_sock *(*bpf_sk_fullsock)(struct bpf_sock *sk) = (void *)95;+static unsigned long long (*bpf_skb_cgroup_id)(void *skb) = (void *)79;/* the maps: only their names (lord-netperm defines them, netperm.c) */char np_cfg[4] SEC("maps");@@ -72,6 +74,9 @@ char np_events[4] SEC("maps");char np_snd[4] SEC("maps");char np_flows[4] SEC("maps");char np_bound[4] SEC("maps");+char np_blk4[4] SEC("maps");+char np_blk6[4] SEC("maps");+char np_sysc[4] SEC("maps");#define AF_INET 2#define AF_INET6 10@@ -143,6 +148,29 @@ NP_INLINE NpList *lookup_list(NpApp *app, int fam, const np_u8 *addr, int *lan)return l;}+/* (0.8.4) Is the address in the app's blocklist set (the user's subscribed lists: their networks+ * and the addresses the resolver gave for their domains)? */+NP_INLINE int lookup_blk(NpApp *app, int fam, const np_u8 *addr) {+ if (!app->blk) return 0;+ if (fam == 4) {+ NpLpm4Key k;+ k.prefixlen = 64;+ k.data[0] = (np_u8)(app->blk >> 24); k.data[1] = (np_u8)(app->blk >> 16);+ k.data[2] = (np_u8)(app->blk >> 8); k.data[3] = (np_u8)app->blk;+ k.data[4] = addr[0]; k.data[5] = addr[1]; k.data[6] = addr[2]; k.data[7] = addr[3];+ return bpf_map_lookup_elem(&np_blk4, &k) != 0;+ }+ if (fam == 6) {+ NpLpm6Key k;+ k.prefixlen = 160;+ k.data[0] = (np_u8)(app->blk >> 24); k.data[1] = (np_u8)(app->blk >> 16);+ k.data[2] = (np_u8)(app->blk >> 8); k.data[3] = (np_u8)app->blk;+ for (int i = 0; i < 16; i++) k.data[4 + i] = addr[i];+ return bpf_map_lookup_elem(&np_blk6, &k) != 0;+ }+ return 0;+}+/* fam 0: unknown address. addr: 16 bytes (IPv4 in the first 4). always_count: connect and* bind always count; sendto only the first time within NP_REPEAT_NS. */NP_INLINE int handle(int fam, np_u8 *addr, int port, int proto, int dir, int always_count) {@@ -156,7 +184,7 @@ NP_INLINE int handle(int fam, np_u8 *addr, int port, int proto, int dir, int alwint lan = -1;NpList *l = lookup_list(app, fam, addr, &lan);NpRes r;- np_decide(app, l, dir, proto, port, lan, &r);+ np_decide(app, l, dir, proto, port, lan, lookup_blk(app, fam, addr), np_is_loop(fam, addr), &r);int deny = r.verdict == NP_V_DENY;NpSeenKey sk;@@ -422,81 +450,96 @@ NP_INLINE void report_cut(struct __sk_buff *skb, np_u64 cgid, const NpFlowKey *kbpf_ringbuf_output(&np_events, &ev, sizeof ev, 0);}+/* (0.8.4) The essential system services (name resolution, time, the network service), which are+ * not apps: lord-netperm attaches these programs to their own cgroups too and keys np_sysc by the+ * socket's cgroup. They are only counted, never filtered: their traffic can't be blocked. */+NP_INLINE int count_system(struct __sk_buff *skb, int in) {+ np_u64 id = bpf_skb_cgroup_id(skb);+ if (!id) return 1;+ NpCount *c = bpf_map_lookup_elem(&np_sysc, &id);+ if (!c) return 1;+ np_u64 len = skb->len;+ if (in) __sync_fetch_and_add(&c->rx, len);+ else __sync_fetch_and_add(&c->tx, len);+ return 1;+}+/* Only for apps lord-netperm knows (it creates their np_counts entry when the scope appears), so* nothing else (the login screen, the system) is counted, stored or filtered. Loopback isn't* counted or filtered. The skb starts at the IP header in both directions. Returns 1 (let it* through) or 0 (drop). An allowed packet adds its bytes to its app (the socket's cgroup, the- * scope at cfg->level) and to the (app, remote address) pair; a dropped one to "blocked". */+ * scope at cfg->level) and to the (app, remote address, port) flow (0.8.4: the port too: the+ * remote one, or the local one of a port the app bound); a dropped one to "blocked". */NP_INLINE int filter(struct __sk_buff *skb, int in) {if (skb->ifindex == LOOPBACK_IFINDEX) return 1;np_u32 zero = 0;NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);if (!cfg) return 1;np_u64 cgid = bpf_skb_ancestor_cgroup_id(skb, (int)cfg->level);- if (!cgid) return 1;- NpCount *c = bpf_map_lookup_elem(&np_counts, &cgid);- if (!c) return 1;+ NpCount *c = cgid ? bpf_map_lookup_elem(&np_counts, &cgid) : 0;+ if (!c) return count_system(skb, in);/* the IP header in one read (an IP packet is at least 20 bytes; IPv6 needs 40) */np_u8 h[40];if (bpf_skb_load_bytes(skb, 0, h, 20)) return 1;NpFlowKey k;__builtin_memset(&k, 0, sizeof k);k.cgid = cgid;- int v6 = h[0] >> 4 == 6;+ np_u8 la[16];+ __builtin_memset(la, 0, sizeof la);+ int v6 = h[0] >> 4 == 6, l4 = -1, proto;if (v6) {if (bpf_skb_load_bytes(skb, 0, h, 40)) return 1;/* the remote address: the source when it comes in, the destination when it goes out */__builtin_memcpy(k.addr, in ? h + 8 : h + 24, 16);+ __builtin_memcpy(la, in ? h + 24 : h + 8, 16);k.fam = 6;+ proto = proto_of(h[6]); /* next header (an extension header: no ports) */+ l4 = 40;} else if (h[0] >> 4 == 4) {__builtin_memcpy(k.addr, in ? h + 12 : h + 16, 4);+ __builtin_memcpy(la, in ? h + 16 : h + 12, 4);k.fam = 4;+ proto = proto_of(h[9]);+ if (!(((np_u32)h[6] << 8 | h[7]) & 0x1fff)) l4 = (h[0] & 0xf) * 4; /* not a later fragment */} else return 1;np_u64 len = skb->len;- int deny = 0, proto = NP_P_OTHER, dir = NP_D_OUT, port = 0, lport = 0, rport = 0;- np_u8 la[16];+ int lport = 0, rport = 0, rst = 0, dir = NP_D_OUT;+ if (l4 >= 0 && proto != NP_P_OTHER) {+ np_u8 p[14]; /* ports, and the TCP flags at 13 */+ if (!bpf_skb_load_bytes(skb, (np_u32)l4, p, proto == NP_P_TCP ? 14 : 4)) {+ int sport = p[0] << 8 | p[1], dport = p[2] << 8 | p[3];+ lport = in ? dport : sport;+ rport = in ? sport : dport;+ rst = proto == NP_P_TCP && (p[13] & TCP_FLAG_RST);+ }+ }+ int port = rport;+ if (lport) {+ NpBoundKey bk;+ __builtin_memset(&bk, 0, sizeof bk);+ bk.cgid = cgid;+ bk.port = (np_u16)lport;+ bk.proto = (np_u8)proto;+ if (bpf_map_lookup_elem(&np_bound, &bk)) { dir = NP_D_IN; port = lport; } /* a connection to the app's server */+ }+ int deny = 0;NpRes r;+ __builtin_memset(&r, 0, sizeof r);NpApp *app = cfg->on ? bpf_map_lookup_elem(&np_apps, &cgid) : 0;if (app && app->can_deny) {- __builtin_memset(la, 0, sizeof la);- __builtin_memset(&r, 0, sizeof r);- int l4 = -1;- if (v6) {- __builtin_memcpy(la, in ? h + 24 : h + 8, 16);- proto = proto_of(h[6]); /* next header (an extension header: no ports) */- l4 = 40;- } else {- __builtin_memcpy(la, in ? h + 16 : h + 12, 4);- proto = proto_of(h[9]);- if (!(((np_u32)h[6] << 8 | h[7]) & 0x1fff)) l4 = (h[0] & 0xf) * 4; /* not a later fragment */- }- int rst = 0;- if (l4 >= 0 && proto != NP_P_OTHER) {- np_u8 p[14]; /* ports, and the TCP flags at 13 */- if (!bpf_skb_load_bytes(skb, (np_u32)l4, p, proto == NP_P_TCP ? 14 : 4)) {- int sport = p[0] << 8 | p[1], dport = p[2] << 8 | p[3];- lport = in ? dport : sport;- rport = in ? sport : dport;- rst = proto == NP_P_TCP && (p[13] & TCP_FLAG_RST);- }- }- port = rport;- if (lport) {- NpBoundKey bk;- __builtin_memset(&bk, 0, sizeof bk);- bk.cgid = cgid;- bk.port = (np_u16)lport;- bk.proto = (np_u8)proto;- if (bpf_map_lookup_elem(&np_bound, &bk)) { dir = NP_D_IN; port = lport; } /* a connection to the app's server */- }int lan = -1;NpList *l = lookup_list(app, k.fam, k.addr, &lan);- np_decide(app, l, dir, proto, port, lan, &r);+ np_decide(app, l, dir, proto, port, lan, lookup_blk(app, k.fam, k.addr), np_is_loop(k.fam, k.addr), &r);/* a reset always goes through: it is what tells the other end the connection is over */deny = r.verdict == NP_V_DENY && !rst;}+ /* the flow's port and protocol go into the key only after the decision: as stack values known+ * to the verifier they would multiply the states it walks through np_decide */+ k.proto = (np_u8)proto;+ k.dir = (np_u8)dir;+ k.port = (np_u16)port;NpFlow *f = bpf_map_lookup_elem(&np_flows, &k);if (!f) {NpFlow z = { 0, 0, 0 };diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.hindex ea5d94c..8d624fd 100644--- a/server/src/netperm/np_shared.h+++ b/server/src/netperm/np_shared.h@@ -20,6 +20,10 @@* np_events RINGBUF NpEvent towards lord-netperm* np_snd HASH ALSA minor (u32) → u32 every ALSA node in /sys/class/sound: NP_SND_CAPTURE for the* capture PCMs (/dev/snd/pcmC*D*c), NP_SND_OTHER for the rest+ * np_blk4 LPM (list set, IPv4/n) → u8 (0.8.4) a user's subscribed blocklists: their networks and+ * np_blk6 LPM (list set, IPv6/n) → u8 the addresses the resolver gave for their domains (NpApp.blk)+ * np_sysc HASH cgroup id → NpCount (0.8.4) the essential system services' bytes (name+ * resolution, time, the network service), counted only** Besides the network, the same per-app policy carries the device restrictions (docs/FIREWALL.md,* "Linux apps"): "no camera", "no USB" and "no screen capture" (/dev/fb*) (cgroup/dev, by device@@ -51,7 +55,9 @@ typedef uint64_t np_u64;#define NP_MAX_LPM 65536 /* entries of each trie */#define NP_MAX_SEEN 16384#define NP_MAX_BOUND 4096 /* (app, local port) of the apps' servers */-#define NP_MAX_FLOWS 4096 /* (app, remote address) byte counters; the least recently used go first */+#define NP_MAX_FLOWS 8192 /* (app, remote address, port) byte counters; the least recently used go first */+#define NP_MAX_BLK 131072 /* entries of each blocklist trie (every user's sets together) */+#define NP_MAX_SYS 16 /* system service cgroups counted (np_sysc) */#define NP_RINGBUF (256u << 10)#define NP_REPEAT_NS (5ull * 1000000000ull) /* a repeated connection is reported again after 5 s */@@ -84,8 +90,8 @@ enum { NP_P_ANY = 0, NP_P_TCP = 1, NP_P_UDP = 2, NP_P_OTHER = 3 }; /* like FwPenum { NP_D_OUT = 0, NP_D_IN = 1, NP_D_DEV = 2, NP_D_BT = 3, NP_D_CUT = 4 }; /* events: DEV = a device open, BT = a Bluetooth socket; CUT: np_seen key of a cut socket */enum { NP_V_ALLOW = 0, NP_V_DENY = 1 };enum { NP_K_RULE = 0, NP_K_ANY = 1, NP_K_LAN = 2, NP_K_INET = 3 }; /* NpEnt.kind */-enum { NP_M_SILENT = 0, NP_M_OFF = 2 }; /* "ask" is treated as silent */-enum { NP_BY_RESTRICTED = 0, NP_BY_RULE = 1, NP_BY_TRUSTED = 2, NP_BY_MODE = 3 };+enum { NP_M_SILENT = 0, NP_M_OFF = 2, NP_M_DENY = 3 }; /* "ask" is treated as silent */+enum { NP_BY_RESTRICTED = 0, NP_BY_RULE = 1, NP_BY_TRUSTED = 2, NP_BY_MODE = 3, NP_BY_LIST = 4 };typedef struct {np_u16 port_lo, port_hi; /* 0..65535 = any */@@ -109,8 +115,11 @@ typedef struct {np_u8 mode; /* NP_M_* */np_u8 has4, has6; /* there are entries in np_lpm4 / np_lpm6 */np_u32 ngen;- np_u32 can_deny; /* 1: a restriction or a deny entry exists (gen or tries): the packet- * filter (cgroup_skb) only decides for these apps */+ np_u32 can_deny; /* 1: a restriction, a deny entry (gen or tries), the deny mode or a+ * blocklist set exists: the packet filter (cgroup_skb) only decides+ * for these apps */+ np_u32 blk; /* the user's blocklist set in np_blk4/np_blk6 (0: none) */+ np_u32 pad0;NpEnt gen[NP_GEN_MAX]; /* lan / internet / any rules, in order */} NpApp;@@ -143,7 +152,12 @@ typedef struct {np_u64 cgid;np_u8 addr[16]; /* the remote address; IPv4 (also IPv4-mapped IPv6) in the first 4 */np_u8 fam; /* 4 or 6 */- np_u8 pad[7];+ np_u8 proto; /* (0.8.4) NP_P_TCP / NP_P_UDP / NP_P_OTHER */+ np_u8 dir; /* (0.8.4) NP_D_OUT: port is the remote one; NP_D_IN: a port the app+ * bound (np_bound), its local one */+ np_u8 pad;+ np_u16 port; /* (0.8.4) host order; 0 when there is none (ICMP, a later fragment) */+ np_u8 pad2[2];} NpFlowKey;typedef struct { np_u64 rx, tx, blocked; } NpFlow;@@ -215,6 +229,15 @@ NP_INLINE int np_is_lan6(const np_u8 *a) {return 0;}+/* Loopback, same as fw_addr_is_loopback (IPv4-mapped addresses already come as IPv4). */+NP_INLINE int np_is_loop(int fam, const np_u8 *a) {+ if (fam == 4) return a[0] == 127;+ if (fam != 6) return 0;+ np_u32 any = 0; /* without early exits: fewer paths for the verifier */+ for (int i = 0; i < 15; i++) any |= a[i];+ return (any | (np_u32)(a[15] ^ 1)) == 0;+}+NP_INLINE int np_ent_match(const NpEnt *e, int dir, int proto, int port) {if (e->dir != dir) return 0;if (e->proto != NP_P_ANY && e->proto != proto) return 0;@@ -223,9 +246,12 @@ NP_INLINE int np_ent_match(const NpEnt *e, int dir, int proto, int port) {}/* The decision, in the same order as fw_decide: restrictions, rules (first the trie's list,- * which already has domains and networks ordered, then lan/internet/any), trusted and mode.- * lan: 1 LAN, 0 internet, -1 unknown address (bind to an address that isn't local). */-NP_INLINE void np_decide(const NpApp *a, const NpList *l, int dir, int proto, int port, int lan, NpRes *r) {+ * which already has domains and networks ordered), a subscribed blocklist, then lan/internet/any,+ * trusted and mode.+ * lan: 1 LAN, 0 internet, -1 unknown address (bind to an address that isn't local).+ * listed: 1 if the address is in the app's blocklist set (np_blk4/np_blk6).+ * loop: 1 for a loopback address (127/8, ::1): the deny mode lets it through. */+NP_INLINE void np_decide(const NpApp *a, const NpList *l, int dir, int proto, int port, int lan, int listed, int loop, NpRes *r) {r->verdict = NP_V_DENY; r->reason = NP_BY_RESTRICTED; r->record = 1; r->rule_id = 0; r->flag = 0;np_u32 f = a->flags;if (f & NP_R_NO_NETWORK) { r->flag = NP_R_NO_NETWORK; return; }@@ -244,6 +270,7 @@ NP_INLINE void np_decide(const NpApp *a, const NpList *l, int dir, int proto, in}}}+ if (listed) { r->reason = NP_BY_LIST; r->record = 1; return; }np_u32 ng = a->ngen;for (int i = 0; i < NP_GEN_MAX; i++) {if ((np_u32)i >= ng) break;@@ -258,7 +285,8 @@ NP_INLINE void np_decide(const NpApp *a, const NpList *l, int dir, int proto, inr->verdict = NP_V_ALLOW;if (a->trusted) { r->reason = NP_BY_TRUSTED; return; }r->reason = NP_BY_MODE;- r->record = a->mode == NP_M_SILENT;+ r->record = a->mode == NP_M_SILENT || a->mode == NP_M_DENY;+ if (a->mode == NP_M_DENY && !loop) r->verdict = NP_V_DENY;}/* Does a list of entries deny anything? (NpApp.can_deny, computed by userspace.) */diff --git a/server/src/netperm/npcompile.c b/server/src/netperm/npcompile.cindex f4b5336..46e86ac 100644--- a/server/src/netperm/npcompile.c+++ b/server/src/netperm/npcompile.c@@ -198,7 +198,7 @@ int np_compile_app(FwStore *s, const char *app, uint32_t idx, DnsMap *names, conuint32_t keep = NP_R_NET_MASK | (strcmp(app, "lordos") ? NP_R_DEV_MASK : 0);a->flags = fw_restricted(s, app) & keep;a->trusted = (np_u8)(fw_is_trusted(s, app) != 0);- a->mode = fw_mode(s) == FW_MODE_OFF ? NP_M_OFF : NP_M_SILENT;+ a->mode = fw_mode(s) == FW_MODE_OFF ? NP_M_OFF : fw_mode(s) == FW_MODE_DENY ? NP_M_DENY : NP_M_SILENT;int rc = -1;const FwRule **v = malloc(((size_t)R.n + 1) * sizeof *v);@@ -240,7 +240,7 @@ int np_compile_app(FwStore *s, const char *app, uint32_t idx, DnsMap *names, condnsmap_each(names, names_app, now, host_cb, &h);if (h.err) goto done;}- a->can_deny = (a->flags & NP_R_NET_MASK) || dom_deny || np_ents_deny(a->gen, a->ngen);+ a->can_deny = (a->flags & NP_R_NET_MASK) || dom_deny || a->mode == NP_M_DENY || np_ents_deny(a->gen, a->ngen);for (int i = 0; i < out->nnodes; i++) {if (out->nodes[i].fam == 4) a->has4 = 1;else a->has6 = 1;diff --git a/server/src/netperm/npcompile.h b/server/src/netperm/npcompile.hindex a7c5074..bfe4e0f 100644--- a/server/src/netperm/npcompile.h+++ b/server/src/netperm/npcompile.h@@ -11,7 +11,8 @@* as fw_decide.** "Just once" rules aren't compiled (there's nobody to delete them in the kernel; without ask- * mode they don't appear). "Ask" mode is compiled as silent. */+ * mode they don't appear). "Ask" mode is compiled as silent, "deny" (0.8.4) as NP_M_DENY.+ * The blocklist set (NpApp.blk) is lord-netperm's: it sets it after compiling (fw/fwlists.h). */#ifndef LORD_NP_COMPILE_H#define LORD_NP_COMPILE_Hdiff --git a/server/src/netperm/npload.h b/server/src/netperm/npload.hindex 19fd189..0ef4abb 100644--- a/server/src/netperm/npload.h+++ b/server/src/netperm/npload.h@@ -24,7 +24,10 @@X(np_events, BPF_MAP_TYPE_RINGBUF, 0, 0, NP_RINGBUF, 0) \X(np_snd, BPF_MAP_TYPE_HASH, 4, 4, NP_MAX_SND, 0) \X(np_flows, BPF_MAP_TYPE_LRU_HASH, sizeof(NpFlowKey), sizeof(NpFlow), NP_MAX_FLOWS, 0) \- X(np_bound, BPF_MAP_TYPE_LRU_HASH, sizeof(NpBoundKey), 1, NP_MAX_BOUND, 0)+ X(np_bound, BPF_MAP_TYPE_LRU_HASH, sizeof(NpBoundKey), 1, NP_MAX_BOUND, 0) \+ X(np_blk4, BPF_MAP_TYPE_LPM_TRIE, sizeof(NpLpm4Key), 1, NP_MAX_BLK, BPF_F_NO_PREALLOC) \+ X(np_blk6, BPF_MAP_TYPE_LPM_TRIE, sizeof(NpLpm6Key), 1, NP_MAX_BLK, BPF_F_NO_PREALLOC) \+ X(np_sysc, BPF_MAP_TYPE_HASH, 8, sizeof(NpCount), NP_MAX_SYS, 0)/* Each program: its section, kind and attach point. btf: the kernel function an LSM program* hooks (its id comes from /sys/kernel/btf/vmlinux). opt: 1 = the device, Bluetooth and traffic@@ -56,6 +59,8 @@ static const NpProgSpec np_prog_specs[] = {#define NP_NSPECS (int)(sizeof np_prog_specs / sizeof np_prog_specs[0])static inline int np_attach_user(uint32_t attach) { return attach != BPF_CGROUP_INET_INGRESS && attach != BPF_CGROUP_INET_EGRESS; }+/* (0.8.4) Only the accounting programs go to the essential system services' cgroups. */+static inline int np_attach_system(uint32_t attach) { return !np_attach_user(attach); }/* The license the kernel sees: LSM programs must declare a GPL-compatible one (see bpfsys.h). */static inline const char *np_prog_license(const NpProgSpec *p) { return p->type == BPF_PROG_TYPE_LSM ? "Dual MIT/GPL" : "LordOS"; }diff --git a/server/src/remote/netsup.c b/server/src/remote/netsup.cindex e97d45f..56bb431 100644--- a/server/src/remote/netsup.c+++ b/server/src/remote/netsup.c@@ -11,6 +11,7 @@* - Leaves /run/lordos/netd.enabled: with that mark, the sessions' helpers never bring the* network up themselves (while lord-netd restarts, the action answers with an error). */#include "sessiond.h"+#include "../sec/cgroup.h"#include <errno.h>#include <fcntl.h>@@ -95,6 +96,7 @@ void sd_netd_start(void) {signal(SIGTERM, SIG_DFL);signal(SIGHUP, SIG_DFL);setsid(); /* does not die with lord-sessiond (KillMode=process) */+ cgroup_service_leaf("netd"); /* its own leaf: Monitor → Network counts its traffic (0.8.4) */char *const argv[] = { path, NULL };char *const env[] = { "PATH=/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", NULL };execve(path, argv, env);diff --git a/server/src/sec/cgroup.c b/server/src/sec/cgroup.cindex cad2176..ef75be4 100644--- a/server/src/sec/cgroup.c+++ b/server/src/sec/cgroup.c@@ -226,6 +226,20 @@ int cgroup_service_init(void) {return 0;}+int cgroup_service_leaf(const char *name) {+ if (!name || strcmp(name, "netd")) return -1; /* the only one (docs/MONITOR.md, "Essential system traffic") */+ char svc[PATH_MAX];+ if (geteuid() != 0 || service_root(svc, sizeof svc)) return -1;+ int fd = open(svc, O_PATH | O_DIRECTORY | O_CLOEXEC);+ if (fd < 0) return -1;+ if (mkdirat(fd, name, 0755) && errno != EEXIST) { close(fd); return -1; }+ int lfd = openat(fd, name, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ int r = lfd >= 0 ? write_at(lfd, "cgroup.procs", "0") : -1;+ if (lfd >= 0) close(lfd);+ close(fd);+ return r;+}+int cgroup_delegate_session(unsigned uid, unsigned gid) {char svc[PATH_MAX], sess[PATH_MAX];(void)gid;diff --git a/server/src/sec/cgroup.h b/server/src/sec/cgroup.hindex a275a93..cac6a81 100644--- a/server/src/sec/cgroup.h+++ b/server/src/sec/cgroup.h@@ -43,6 +43,11 @@ int cgroup_delegate_session(unsigned uid, unsigned gid);* sessions' subtrees (cgroup v2's "no internal processes" rule). 0 if it did. */int cgroup_service_init(void);+/* (0.8.4) Called by lord-sessiond in lord-netd's child, before its execve: moves the process into+ * the root-owned lordos.service/<name> leaf (only "netd"), so lord-netperm can count the network+ * service's own traffic (DHCP, the VPN daemons) apart from lord-sessiond's. Best effort: 0 placed. */+int cgroup_service_leaf(const char *name);+/* ---- Root-owned app scopes (lord-sessiond) ----* With lord-sessiond, a session's subtree stays root-owned: the scopes are created by root, with a* root-owned cgroup.procs, so an app cannot move itself to another cgroup (it would escape itsdiff --git a/server/src/ui/js_fw.c b/server/src/ui/js_fw.cindex c2d2a7d..c77178e 100644--- a/server/src/ui/js_fw.c+++ b/server/src/ui/js_fw.c@@ -305,7 +305,7 @@ static int each_app(void *u, const char *app, int trusted, uint32_t flags) {return 0;}-static const char *mode_name(FwMode m) { return m == FW_MODE_ASK ? "ask" : m == FW_MODE_OFF ? "off" : "silent"; }+static const char *mode_name(FwMode m) { return m == FW_MODE_ASK ? "ask" : m == FW_MODE_OFF ? "off" : m == FW_MODE_DENY ? "deny" : "silent"; }static JSValue f_state(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {(void)t; (void)argc; (void)argv;@@ -411,7 +411,8 @@ static JSValue f_set_mode(JSContext *c, JSValueConst t, int argc, JSValueConst *const char *s = JS_ToCString(c, argv[0]);snprintf(m, sizeof m, "%s", s ? s : "");if (s) JS_FreeCString(c, s);- FwMode mode = !strcmp(m, "silent") ? FW_MODE_SILENT : !strcmp(m, "ask") ? FW_MODE_ASK : !strcmp(m, "off") ? FW_MODE_OFF : (FwMode)-1;+ FwMode mode = !strcmp(m, "silent") ? FW_MODE_SILENT : !strcmp(m, "ask") ? FW_MODE_ASK : !strcmp(m, "off") ? FW_MODE_OFF :+ !strcmp(m, "deny") ? FW_MODE_DENY : (FwMode)-1;if ((int)mode < 0) return js_throw_code(c, "fw.invalidMode", NULL);fw_set_mode(store, mode);return JS_NewBool(c, save() == 0);diff --git a/server/tests/fw_rules_test.c b/server/tests/fw_rules_test.cindex 20cc073..0b5769c 100644--- a/server/tests/fw_rules_test.c+++ b/server/tests/fw_rules_test.c@@ -166,6 +166,76 @@ static void test_mode_and_trusted(void) {fw_store_free(s);}+/* 0.8.4: the deny mode ("deny silently") and subscribed blocklists (FwQuery.listed) */+static FwDecision decl(FwStore *s, const char *app, const char *ip, const char *host, int listed) {+ FwQuery q = qry(app, FW_OUT, ip, 443, host);+ q.listed = listed;+ return fw_decide(s, &q);+}++static void test_deny_mode_and_lists(void) {+ FwStore *s = fw_store_new();+ fw_set_mode(s, FW_MODE_DENY);+ CHECK(fw_mode(s) == FW_MODE_DENY);+ FwDecision d = dec(s, "desktop:a", "8.8.8.8", 443, NULL);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_MODE && d.record == 1);+ d = dec(s, "desktop:a", "192.168.1.1", 443, NULL);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_MODE);+ d = dec(s, "desktop:a", "127.0.0.53", 53, NULL); /* the resolver's stub: loopback stays */+ CHECK(d.verdict == FW_V_ALLOW && d.reason == FW_BY_MODE);+ d = dec(s, "desktop:a", "::1", 631, NULL);+ CHECK(d.verdict == FW_V_ALLOW);+ d = dec(s, "desktop:a", "::ffff:127.0.0.1", 631, NULL);+ CHECK(d.verdict == FW_V_ALLOW);+ add(s, "desktop:a", FW_OUT, "example.org", FW_ALLOW); /* a rule still allows */+ d = dec(s, "desktop:a", "203.0.113.5", 443, "example.org");+ CHECK(d.verdict == FW_V_ALLOW && d.reason == FW_BY_RULE);+ CHECK(!fw_set_trusted(s, "desktop:t", 1)); /* trusted apps keep the network */+ d = dec(s, "desktop:t", "8.8.8.8", 443, NULL);+ CHECK(d.verdict == FW_V_ALLOW && d.reason == FW_BY_TRUSTED);+ /* the mode survives the file */+ size_t n;+ char *j = fw_to_json(s, &n);+ CHECK(j && strstr(j, "\"mode\":\"deny\""));+ FwStore *t = fw_store_new();+ char err[128];+ CHECK(j && !fw_from_json(t, j, n, err, sizeof err) && fw_mode(t) == FW_MODE_DENY);+ free(j);+ fw_store_free(t);+ fw_store_free(s);++ /* lists: deny before lan/internet/any rules, never before a rule naming the destination */+ s = fw_store_new();+ const char *A = "desktop:b";+ d = decl(s, A, "203.0.113.9", "tracker.example", 1);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_LIST && d.record == 1);+ d = decl(s, A, "203.0.113.9", "tracker.example", 0);+ CHECK(d.verdict == FW_V_ALLOW && d.reason == FW_BY_MODE);+ add(s, A, FW_OUT, "any", FW_ALLOW);+ add(s, A, FW_OUT, "internet", FW_ALLOW);+ d = decl(s, A, "203.0.113.9", "tracker.example", 1);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_LIST); /* broad rules don't override a list */+ uint32_t mine = add(s, A, FW_OUT, "tracker.example", FW_ALLOW);+ d = decl(s, A, "203.0.113.9", "tracker.example", 1);+ CHECK(d.verdict == FW_V_ALLOW && d.reason == FW_BY_RULE && d.rule_id == mine); /* the owner's allow wins */+ uint32_t net = add(s, A, FW_OUT, "198.51.100.0/24", FW_ALLOW);+ d = decl(s, A, "198.51.100.7", NULL, 1);+ CHECK(d.verdict == FW_V_ALLOW && d.rule_id == net);+ add(s, A, FW_OUT, "*.ads.example", FW_DENY);+ d = decl(s, A, "203.0.113.10", "x.ads.example", 1);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_RULE); /* the owner's own deny still says so */+ CHECK(!fw_set_trusted(s, A, 1));+ d = decl(s, "desktop:c", "203.0.113.9", NULL, 1);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_LIST);+ CHECK(!fw_set_trusted(s, "desktop:c", 1)); /* lists apply to trusted apps too */+ d = decl(s, "desktop:c", "203.0.113.9", NULL, 1);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_LIST);+ CHECK(!fw_set_restricted(s, "desktop:d", FW_R_NO_INTERNET)); /* a restriction comes first */+ d = decl(s, "desktop:d", "203.0.113.9", NULL, 1);+ CHECK(d.verdict == FW_V_DENY && d.reason == FW_BY_RESTRICTED);+ fw_store_free(s);+}+static void test_precedence(void) {FwStore *s = fw_store_new();fw_set_mode(s, FW_MODE_ASK);@@ -646,6 +716,7 @@ int main(void) {test_dest();test_lan();test_mode_and_trusted();+ test_deny_mode_and_lists();test_precedence();test_restricted();test_lifetimes();diff --git a/server/tests/fwlists_test.c b/server/tests/fwlists_test.cnew file mode 100644index 0000000..1172796--- /dev/null+++ b/server/tests/fwlists_test.c@@ -0,0 +1,226 @@+/* fwlists_test.c — subscribed blocklists (src/fw/fwlists.c, docs/FIREWALL.md "Blocklists"):+ * the raw parsers (hosts, domains, addresses) and their strictness, the set's lookups, the+ * compiled file's reader, the shared-address rule, mutation fuzzing and a measurement. */+#define _POSIX_C_SOURCE 200809L+#include "fw/fwlists.h"+#include "fw/rules.h"++#include <arpa/inet.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>++static int fails, checks;+#define CHECK(c) do { checks++; if (!(c)) { fails++; fprintf(stderr, "%s:%d: failed: %s\n", __FILE__, __LINE__, #c); } } while (0)++static uint64_t rng = 0x243f6a8885a308d3ull;+static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); }++static int has_line(const char *text, const char *line) {+ size_t n = strlen(line);+ for (const char *p = text; (p = strstr(p, line)); p += n)+ if ((p == text || p[-1] == '\n') && (p[n] == '\n' || !p[n])) return 1;+ return 0;+}++static int parse(const char *raw, FwlFormat f, int sub, char **out, FwlStats *st, char *err) {+ size_t ol;+ return fwl_parse_raw(raw, strlen(raw), f, sub, out, &ol, st, err, 32);+}++static uint8_t *ip(const char *s, int *fam) {+ static uint8_t a[16];+ memset(a, 0, 16);+ *fam = strchr(s, ':') ? 6 : 4;+ inet_pton(*fam == 4 ? AF_INET : AF_INET6, s, a);+ return a;+}++static void test_hosts(void) {+ const char *raw =+ "# a hosts file\n"+ "127.0.0.1 localhost\n"+ "127.0.0.1 localhost.localdomain\n"+ "::1 ip6-localhost ip6-loopback\n"+ "0.0.0.0 0.0.0.0\n"+ "\n"+ "0.0.0.0 Tracker.Example # inline comment\n"+ "0.0.0.0 ads.example.net metrics.example.net\r\n"+ "127.0.0.1\tpixel.example.org\n"+ "0.0.0.0 tracker.example\n" /* repeated */+ "0.0.0.0 intranet\n" /* no dot: skipped */+ "192.168.1.10 printer.example.com\n"; /* not a sink-hole: a bad line */+ char *out = NULL, err[32] = "";+ FwlStats st;+ CHECK(!parse(raw, FWL_FMT_HOSTS, 0, &out, &st, err));+ CHECK(out && has_line(out, "d tracker.example") && has_line(out, "d ads.example.net") && has_line(out, "d metrics.example.net") &&+ has_line(out, "d pixel.example.org"));+ CHECK(out && !strstr(out, "localhost") && !strstr(out, "printer") && !strstr(out, "intranet"));+ CHECK(st.domains == 4 && st.dups == 1 && st.bad == 1 && st.skipped >= 4);+ free(out);+ /* with sub = 1 every name covers its subdomains */+ CHECK(!parse("0.0.0.0 ads.example.net\n", FWL_FMT_HOSTS, 1, &out, &st, err) && has_line(out, "s ads.example.net"));+ free(out);+}++static void test_domains(void) {+ char *out = NULL, err[32] = "";+ FwlStats st;+ CHECK(!parse("! comment\nexample.com\n*.cdn-ads.example\n||adserver.example^\nWWW.Example.ORG.\n", FWL_FMT_DOMAINS, 0, &out, &st, err));+ CHECK(has_line(out, "d example.com") && has_line(out, "s cdn-ads.example") && has_line(out, "s adserver.example") && has_line(out, "d www.example.org"));+ free(out);+ /* two names on a line, an address or odd bytes are bad lines; enough of them reject the list */+ char big[8000] = "";+ for (int i = 0; i < 30; i++) strcat(big, "this is not a domain list\n");+ for (int i = 0; i < 10; i++) strcat(big, "ok.example\n");+ CHECK(parse(big, FWL_FMT_DOMAINS, 0, &out, &st, err) == -1 && !strcmp(err, "format"));+ CHECK(parse("1.2.3.4\n", FWL_FMT_DOMAINS, 0, &out, &st, err) == -1 && !strcmp(err, "empty"));+ CHECK(parse("# nothing\n\n", FWL_FMT_DOMAINS, 0, &out, &st, err) == -1 && !strcmp(err, "empty"));+ /* a NUL byte or a very long line reject the whole list */+ char nul[] = "a.example\n\0b.example\n";+ size_t ol;+ CHECK(fwl_parse_raw(nul, sizeof nul - 1, FWL_FMT_DOMAINS, 0, &out, &ol, &st, err, sizeof err) == -1 && !strcmp(err, "nul"));+ char *lng = malloc(FWL_LINE_MAX + 40);+ memset(lng, 'a', FWL_LINE_MAX + 30);+ memcpy(lng, "x.example\n", 10);+ lng[FWL_LINE_MAX + 30] = 0;+ CHECK(parse(lng, FWL_FMT_DOMAINS, 0, &out, &st, err) == -1 && !strcmp(err, "line"));+ free(lng);+ CHECK(fwl_parse_raw("a.example\n", FWL_RAW_MAX + 1, FWL_FMT_DOMAINS, 0, &out, &ol, &st, err, sizeof err) == -1 && !strcmp(err, "big"));+ CHECK(fwl_parse_raw("a.example\n", 10, (FwlFormat)9, 0, &out, &ol, &st, err, sizeof err) == -1);+ CHECK(fwl_format_from_name("hosts") == FWL_FMT_HOSTS && fwl_format_from_name("ips") == FWL_FMT_IPS && !fwl_format_from_name("csv"));+}++static void test_ips(void) {+ char *out = NULL, err[32] = "";+ FwlStats st;+ const char *raw = "# feed\n203.0.113.7\n198.51.100.0/24 ; a comment\n203.0.113.9\t7\n"+ "10.0.0.0/8\n192.168.1.1\n127.0.0.1\n0.0.0.0/0\n2.0.0.0/7\n" /* LAN and too broad: skipped */+ "2001:db8::/32\n2001:db8:5::1\n::/0\nfe80::1\n"+ "198.51.100.1/24\n" /* host bits set: bad */+ "1.2.3.4-1.2.3.9\n"; /* a range: bad */+ CHECK(!parse(raw, FWL_FMT_IPS, 0, &out, &st, err));+ CHECK(has_line(out, "n 203.0.113.7") && has_line(out, "n 198.51.100.0/24") && has_line(out, "n 203.0.113.9") &&+ has_line(out, "n 2001:db8::/32") && has_line(out, "n 2001:db8:5::1"));+ CHECK(!strstr(out, "10.0.0.0") && !strstr(out, "192.168") && !strstr(out, "127.") && !strstr(out, "0.0.0.0/0") && !strstr(out, "fe80"));+ CHECK(st.nets == 5 && st.skipped == 7 && st.bad == 2);+ free(out);+}++static void test_set(void) {+ FwLists *l = fwl_new();+ CHECK(fwl_add_line(l, "d tracker.example", 17, 1) == 0);+ CHECK(fwl_add_line(l, "d tracker.example", 17, 2) == 1); /* the first tag stays */+ CHECK(fwl_add_line(l, "s ads.example", 13, 2) == 0);+ CHECK(fwl_add_line(l, "n 203.0.113.0/24", 16, 3) == 0);+ CHECK(fwl_add_line(l, "n 2001:db8::/32", 15, 4) == 0);+ CHECK(fwl_add_line(l, "d Upper.example", 15, 1) == -1); /* not normalized */+ CHECK(fwl_add_line(l, "n 10.0.0.0/8", 12, 1) == -1); /* LAN */+ CHECK(fwl_add_line(l, "x y", 3, 1) == -1 && fwl_add_line(l, "d a.b", 5, 0) == -1);+ CHECK(fwl_domains(l) == 2 && fwl_nets(l) == 2);+ CHECK(fwl_match_name(l, "tracker.example") == 1 && fwl_match_name(l, "TRACKER.example.") == 1);+ CHECK(fwl_match_name(l, "www.tracker.example") == 0); /* exact only */+ CHECK(fwl_match_name(l, "ads.example") == 2 && fwl_match_name(l, "a.b.ads.example") == 2);+ CHECK(fwl_match_name(l, "notads.example") == 0 && fwl_match_name(l, "example") == 0);+ int fam;+ uint8_t *a = ip("203.0.113.99", &fam);+ CHECK(fwl_match_addr(l, fam, a) == 3);+ a = ip("203.0.114.1", &fam);+ CHECK(fwl_match_addr(l, fam, a) == 0);+ a = ip("::ffff:203.0.113.5", &fam);+ CHECK(fwl_match_addr(l, fam, a) == 3); /* an IPv4-mapped address is IPv4 */+ a = ip("2001:db8:ffff::1", &fam);+ CHECK(fwl_match_addr(l, fam, a) == 4);+ /* the shared-address rule */+ const char *both[] = { "x.ads.example", "tracker.example" }, *mixed[] = { "tracker.example", "site.example" };+ CHECK(fwl_block_names(l, both, 2) == 2);+ CHECK(fwl_block_names(l, mixed, 2) == 0);+ CHECK(fwl_block_names(l, NULL, 0) == 0);+ /* the compiled file: round trip, and any bad line rejects it */+ size_t n;+ char *t = fwl_to_text(l, &n);+ char err[64];+ FwLists *b = t ? fwl_load_text(t, n, err, sizeof err) : NULL;+ CHECK(b && fwl_domains(b) == 2 && fwl_nets(b) == 2 && fwl_match_name(b, "q.ads.example") && fwl_match_addr(b, 4, ip("203.0.113.1", &fam)));+ fwl_free(b);+ const char *bad[] = { "", "lordos-fwlists 2\nd a.example\n", "lordos-fwlists 1\nd a.example\nn 10.0.0.0/8\n", "lordos-fwlists 1\nd ../etc\n",+ "lordos-fwlists 1\nq a.example\n", "lordos-fwlists 1x\n", NULL };+ for (int i = 0; bad[i]; i++) CHECK(!fwl_load_text(bad[i], strlen(bad[i]), err, sizeof err));+ char withnul[] = "lordos-fwlists 1\nd a.example\0\n";+ CHECK(!fwl_load_text(withnul, sizeof withnul - 1, err, sizeof err));+ b = fwl_load_text("lordos-fwlists 1", 16, err, sizeof err);+ CHECK(b && !fwl_domains(b));+ fwl_free(b);+ free(t);+ fwl_free(l);+}++/* random bytes and mutations of a real-looking list: never a crash, and whatever is accepted+ * loads back as a compiled file */+static void test_fuzz(void) {+ const char *seed = "0.0.0.0 a.example\n0.0.0.0 b.example c.example\n# x\n203.0.113.1\n*.d.example\n";+ int accepted = 0;+ for (int i = 0; i < 20000; i++) {+ char buf[256];+ size_t n = strlen(seed);+ memcpy(buf, seed, n);+ int muts = 1 + (int)(rnd() % 8);+ for (int m = 0; m < muts; m++) buf[rnd() % n] = (char)(rnd() & 0xff);+ if (i % 4 == 0) for (size_t k = 0; k < n; k++) buf[k] = (char)(rnd() & 0xff);+ char *out = NULL, err[32];+ size_t ol;+ FwlStats st;+ FwlFormat f = (FwlFormat)(1 + rnd() % 3);+ if (!fwl_parse_raw(buf, n, f, (int)(rnd() & 1), &out, &ol, &st, err, sizeof err)) {+ accepted++;+ FwLists *l = fwl_new();+ CHECK(fwl_add_text(l, out, ol, 1) >= 1);+ fwl_free(l);+ free(out);+ }+ }+ printf("fwlists-test: 20000 mutated lists, %d accepted\n", accepted);+}++static double secs(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec + ts.tv_nsec / 1e9; }++static void bench(void) {+ size_t cap = 200000 * 40, n = 0;+ char *raw = malloc(cap);+ for (int i = 0; i < 200000; i++) n += (size_t)snprintf(raw + n, cap - n, "0.0.0.0 host%d.tracker%d.example\n", i, i % 977);+ double t0 = secs();+ char *out, err[32];+ size_t ol;+ FwlStats st;+ int rc = fwl_parse_raw(raw, n, FWL_FMT_HOSTS, 0, &out, &ol, &st, err, sizeof err);+ double t1 = secs();+ CHECK(!rc && st.domains == 200000);+ FwLists *l = fwl_new();+ CHECK(!rc && fwl_add_text(l, out, ol, 1) == 200000);+ double t2 = secs();+ int hits = 0;+ char name[64];+ for (int i = 0; i < 1000000; i++) {+ snprintf(name, sizeof name, "host%d.tracker%d.example", i % 400000, (i % 400000) % 977);+ hits += fwl_match_name(l, name) != 0;+ }+ double t3 = secs();+ CHECK(hits == 600000);+ printf("fwlists-test: 200000-name hosts list parsed in %.0f ms, loaded in %.0f ms, 1e6 lookups in %.0f ms\n",+ (t1 - t0) * 1e3, (t2 - t1) * 1e3, (t3 - t2) * 1e3);+ fwl_free(l);+ free(out);+ free(raw);+}++int main(void) {+ test_hosts();+ test_domains();+ test_ips();+ test_set();+ test_fuzz();+ bench();+ printf("fwlists-test: %d/%d passed\n", checks - fails, checks);+ return fails ? 1 : 0;+}diff --git a/server/tests/npcompile_test.c b/server/tests/npcompile_test.cindex d5cd951..0160c51 100644--- a/server/tests/npcompile_test.c+++ b/server/tests/npcompile_test.c@@ -42,6 +42,9 @@ static const NpList *lpm(const NpCompiled *c, int fam, const uint8_t *addr) {return best ? &best->list : NULL;}+/* 1: the destination is in a subscribed blocklist (np_blk4/np_blk6 hit; FwQuery.listed) */+static int g_listed;+/* What the BPF program does with a connection (np.bpf.c), with the app already compiled. */static NpRes kernel(const NpCompiled *c, int dir, int proto, int fam, const uint8_t *in, int port) {uint8_t addr[16] = { 0 };@@ -53,7 +56,7 @@ static NpRes kernel(const NpCompiled *c, int dir, int proto, int fam, const uintif (fam == 4 && c->app.has4) l = lpm(c, 4, addr);if (fam == 6 && c->app.has6) l = lpm(c, 6, addr);NpRes r;- np_decide(&c->app, l, dir, proto, port, lan, &r);+ np_decide(&c->app, l, dir, proto, port, lan, g_listed, np_is_loop(fam, addr), &r);return r;}@@ -93,11 +96,12 @@ static int same(FwStore *s, DnsMap *names, const NpCompiled *c, const char *app,q.app = app; q.dir = dir; q.proto = proto; q.fam = fam; q.port = port;if (fam) memcpy(q.addr, addr, 16);q.hosts = hp; q.nhosts = nh;+ q.listed = g_listed;*ref = fw_decide(s, &q);*got = kernel(c, dir == FW_IN ? NP_D_IN : NP_D_OUT, proto == FW_PROTO_TCP ? NP_P_TCP : NP_P_UDP, fam, addr, port);int rv = ref->verdict == FW_V_DENY ? NP_V_DENY : NP_V_ALLOW; /* "ask" compiles as silent */int rr = ref->reason == FW_BY_RESTRICTED ? NP_BY_RESTRICTED : ref->reason == FW_BY_RULE ? NP_BY_RULE :- ref->reason == FW_BY_TRUSTED ? NP_BY_TRUSTED : NP_BY_MODE;+ ref->reason == FW_BY_TRUSTED ? NP_BY_TRUSTED : ref->reason == FW_BY_LIST ? NP_BY_LIST : NP_BY_MODE;if (got->verdict != rv || got->reason != rr) return 0;if (rr == NP_BY_RULE && got->rule_id != ref->rule_id) return 0;if (rr == NP_BY_RESTRICTED && got->flag != ref->flag) return 0;@@ -345,7 +349,8 @@ static void random_round(int *diffs, int *queries) {FwStore *s = fw_store_new();DnsMap *names = dnsmap_new(0);long long now = 5000;- fw_set_mode(s, pick(3) == 0 ? FW_MODE_OFF : pick(4) == 0 ? FW_MODE_ASK : FW_MODE_SILENT);+ int m = pick(8);+ fw_set_mode(s, m < 2 ? FW_MODE_OFF : m == 2 ? FW_MODE_ASK : m == 3 ? FW_MODE_DENY : FW_MODE_SILENT);for (int i = 0; i < NAPPS; i++) {int nr = pick(9);for (int k = 0; k < nr; k++) random_rule(s, APPS[i]);@@ -370,6 +375,7 @@ static void random_round(int *diffs, int *queries) {FwDecision ref;NpRes got;(*queries)++;+ g_listed = pick(5) == 0;if (!same(s, names, &c, APPS[i], dir, pick(2) ? FW_PROTO_TCP : FW_PROTO_UDP, fam, a, (uint16_t)port, now, &ref, &got)) {if (*diffs < 5) fprintf(stderr, "diferencia: app %s ip %s port %d dir %d: ref %d/%d/%u, kernel %d/%d/%u\n", APPS[i],fam ? IPS[0] : "?", port, dir, ref.verdict, ref.reason, ref.rule_id, got.verdict, got.reason, got.rule_id);@@ -378,6 +384,7 @@ static void random_round(int *diffs, int *queries) {}np_compiled_free(&c);}+ g_listed = 0;dnsmap_free(names);fw_store_free(s);}