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: cut the connections a rule now denies (packet filter drop + SOCK_DESTROY)
Federico Pereira98c3bff41b4a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5c29fe0..9f79beb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -86,10 +86,28 @@ of the matching [GitHub release](https://github.com/lordbasex/LordOS/releases),- Apps with device or "no files" restrictions get no X display.- Clipboard offers to Wayland apps are bound to the selection they were made for and to the appwith the keyboard.+- **The per-app firewall applies to open connections, not only to new ones.** A rule change used to+ leave established TCP connections and connected UDP sockets (QUIC) outside the policy until they+ closed. `lord-netperm`'s packet filter (the cgroup programs that already counted bytes per app)+ now takes the same decision as at connect time for every packet of an app whose rules deny+ something, and `lord-netperm` destroys those sockets over sock_diag netlink (its unit now also+ allows `AF_NETLINK`; it still creates no internet sockets). "No incoming connections" now also+ cuts connections already accepted. The Raspberry Pi kernel build enables+ `CONFIG_INET_DIAG_DESTROY`; on a kernel without it the packets are still dropped.### Fixed- Java (AWT/Swing) windows were blank or shifted (non-reparenting window manager).+- **Blocking an app now cuts the connections it already has open.** Turning an app's rule off in+ Monitor → Network (or adding a deny rule for a destination or domain, or a restriction such as+ "no internet") counted its new connections as denied, but what was already open kept flowing:+ a video kept playing over its established TCP and QUIC connections. Now the matching traffic stops+ within milliseconds: the kernel drops the packets of those connections and LordOS closes them, so+ the app sees the connection end instead of a stall. Only what the new rule denies is cut: other+ destinations, the local network under "no internet", and the clients of a server the app runs+ keep working. Switching the rule back to allow works again at once. The Monitor counts every cut+ connection as denied and shows the dropped bytes as "Blocked traffic" in the app's details. See+ [docs/FIREWALL.md](docs/FIREWALL.md), "Existing connections are cut".## [0.8.2-beta] - 2026-10-19diff --git a/apps/monitor/Network.jsx b/apps/monitor/Network.jsxindex 03f3d10..d6bbea3 100644--- a/apps/monitor/Network.jsx+++ b/apps/monitor/Network.jsx@@ -112,16 +112,18 @@ function useGroups(conns) {// Agrega a cada app su regla y sus contadores del firewall (lord.fw), y las apps que no tienen// conexiones abiertas pero sí intentos (una app denegada no llega a tener sockets).-// lord-netperm's byte counters for one app ([allowed, denied, rx, tx, rxRate, txRate, flows]): the-// kernel counts every packet of the app's sockets (TCP and UDP, so QUIC too, and the connections-// that already closed), which replaces the open-socket estimate. flows: [[addr, rx, tx, rxRate,-// txRate], ...], its busiest addresses.+// lord-netperm's byte counters for one app ([allowed, denied, rx, tx, rxRate, txRate, flows,+// blocked]): the kernel counts every packet of the app's sockets (TCP and UDP, so QUIC too, and the+// connections that already closed), which replaces the open-socket estimate. flows: [[addr, rx, tx,+// rxRate, txRate, blocked], ...], its busiest addresses. blocked: bytes of open connections the+// kernel dropped because a rule now denies them.function applyBytes(g, v) {g.recv = v[2] || 0;g.sent = v[3] || 0;g.rateRx = v[4] || 0;g.rateTx = v[5] || 0;g.bytesFrom = 'kernel';+ g.blocked = v[7] || 0;for (const f of Array.isArray(v[6]) ? v[6] : []) {if (!Array.isArray(f) || typeof f[0] !== 'string') continue;const known = g.hosts.get(f[0]), geo = geoSeen.get(f[0]);@@ -130,6 +132,7 @@ function applyBytes(g, v) {h.sent = f[2] || 0;h.rateRx = f[3] || 0;h.rateTx = f[4] || 0;+ h.blocked = f[5] || 0;g.hosts.set(f[0], h);}}@@ -620,6 +623,7 @@ function Detail({ g, tr, user, names, host, onHost, onRule, hist, range, end })<KV k={tr('monitor.net.rule')} v={tr(g.rule === 'deny' ? 'firewall.rule.deny' : g.rule === 'allow' ? 'firewall.rule.allow' : 'firewall.rule.none')}color={g.rule === 'deny' ? '#ff453a' : '#30d158'} />{g.allowed || g.denied ? <KV k={tr('firewall.counts')} v={tr('firewall.countsValue', { allowed: g.allowed || 0, denied: g.denied || 0 })} /> : null}+ {g.blocked ? <KV k={tr('firewall.blocked')} v={formatBytes(g.blocked)} color="#ff453a" /> : null}{can ? (<View onClick={() => openAppRules(g.key)} style={{ flexDirection: 'row', justifyContent: 'flex-end' }}><Text style={{ fontSize: 11, color: t.accent }}>{tr('fwrules.appRulesLink', { count: firewall.rules(g.key).length })}</Text>diff --git a/assets/locales/en/firewall.json b/assets/locales/en/firewall.jsonindex 9ad5624..b1b3f62 100644--- a/assets/locales/en/firewall.json+++ b/assets/locales/en/firewall.json@@ -38,6 +38,7 @@"rule.none": "No rule (per the mode)","counts": "Connections","countsValue": "{allowed} allowed · {denied} denied",+ "blocked": "Blocked traffic","r.no-clipboard": "No reading the clipboard","desktopAlways": "The desktop itself always has access to files, removable drives, the clipboard and Bluetooth; restrict individual apps instead.","whenNextLaunch": "{name} (at next launch)",diff --git a/assets/locales/es/firewall.json b/assets/locales/es/firewall.jsonindex dd090ed..f480a94 100644--- a/assets/locales/es/firewall.json+++ b/assets/locales/es/firewall.json@@ -38,6 +38,7 @@"rule.none": "Sin regla (según el modo)","counts": "Conexiones","countsValue": "{allowed} permitidas · {denied} denegadas",+ "blocked": "Tráfico bloqueado","r.no-clipboard": "Sin leer el portapapeles","desktopAlways": "El escritorio en sí siempre tiene acceso a los archivos, las unidades extraíbles, el portapapeles y Bluetooth; restringe apps individuales.","whenNextLaunch": "{name} (al próximo inicio)",diff --git a/assets/locales/pt/firewall.json b/assets/locales/pt/firewall.jsonindex a3cbd9d..0bd3430 100644--- a/assets/locales/pt/firewall.json+++ b/assets/locales/pt/firewall.json@@ -38,6 +38,7 @@"rule.none": "Sem regra (conforme o modo)","counts": "Conexões","countsValue": "{allowed} permitidas · {denied} negadas",+ "blocked": "Tráfego bloqueado","r.no-clipboard": "Sem ler a área de transferência","desktopAlways": "A própria área de trabalho sempre tem acesso aos arquivos, unidades removíveis, área de transferência e Bluetooth; restrinja apps individuais.","whenNextLaunch": "{name} (na próxima abertura)",diff --git a/docs/FIREWALL.md b/docs/FIREWALL.mdindex 2a23179..90be780 100644--- a/docs/FIREWALL.md+++ b/docs/FIREWALL.md@@ -577,7 +577,8 @@ tests/netperm_bench.c, netperm_measure.sh measurements- **No arguments, no execution.** It doesn't launch programs. With arguments, it doesn't start.- **No network of its own.** It creates no AF_INET or AF_INET6 sockets. The unit enforces this with- `RestrictAddressFamilies=AF_UNIX AF_PACKET` and leaves it only `CAP_BPF CAP_NET_ADMIN CAP_PERFMON+ `RestrictAddressFamilies=AF_UNIX AF_PACKET AF_NETLINK` (netlink only for sock_diag, to destroy+ the sockets of a connection a rule now denies) and leaves it only `CAP_BPF CAP_NET_ADMIN CAP_PERFMONCAP_DAC_READ_SEARCH CAP_NET_RAW`. It has `ProtectSystem=strict`, `ProtectHome=read-only` and therest listed in the unit (`VM-SETUP.md`, step 20).- **The programs are inside the binary.** `clang -target bpf` compiles `np.bpf.c` and `build/bpf2c`@@ -615,7 +616,7 @@ tests/netperm_bench.c, netperm_measure.sh measurements| `cgroup/sock_create` | "no network": the app can't even create an AF_INET/AF_INET6 socket, so it can't ping either || `cgroup/dev` | "no camera", "no USB", "no screen capture" (`/dev/fb*`) and "no microphone" (ALSA capture nodes): device `open()`/`mknod()` (see [Linux apps](#linux-apps)) || `cgroup/lsm_socket_create` | "no Bluetooth": `socket(AF_BLUETOOTH)`, a per-cgroup LSM program (`BPF_LSM_CGROUP`) |-| `cgroup/skb_ingress`, `skb_egress` | nothing: **traffic accounting** for the Monitor (bytes per app and per remote address, see below). They always let the packet through |+| `cgroup/skb_ingress`, `skb_egress` | **traffic accounting** for the Monitor (bytes per app and per remote address, see below) and, since 0.8.3, the packets of a connection the app's current rules deny: they are dropped, so a connection opened before the rule changed stops at once (see [Existing connections are cut](#existing-connections-are-cut)) |Each program does the following:@@ -722,6 +723,100 @@ Every second `lord-netperm` reads `np_counts` (one lookup per app) and, only ifwhole `np_flows` table in one `BPF_MAP_LOOKUP_BATCH` call, works out the rates and sends `COUNTS`(below). With nothing moving it reads no table and sends nothing.+### Existing connections are cut++Up to 0.8.2 a verdict was taken only when a connection started (`connect`, the first `sendto`,+`bind`). Turning an app's rule off in Monitor → Network counted "Denied" for its new connections,+but what was already open kept flowing: a video kept playing over its established TCP and QUIC+(connected UDP) sockets. Since 0.8.3, when a rule changes to deny (the whole app, a network or+domain rule, or a restriction such as "no internet"), the matching traffic stops within+milliseconds, in two layers:++1. **Drop in the kernel.** The `cgroup/skb_ingress` and `skb_egress` programs, which already counted+ every packet, also decide for apps whose rules can deny something (`NpApp.can_deny`: a+ network restriction, a deny entry, or a deny domain rule whose addresses may come later). The+ decision is the very `np_decide` of `connect()`, on the packet's remote address and port, with+ the same maps: the app's generic list, its trie (networks and the addresses the resolver gave for+ its domain rules) and its restrictions. TCP and UDP, IPv4 and IPv6 (an IPv4-mapped socket sends+ IPv4 packets, so it is decided as IPv4). Loopback is never filtered, and "no internet" keeps the+ LAN (the same `np_is_lan4/6`). A denied packet is dropped (`return 0`) and its bytes go to+ **blocked**, not to received/sent. A TCP reset always goes through (it's what tells the other end+ the connection is over).+ - **Incoming connections:** a packet on a local port the app bound (`np_bound`, filled by the+ `bind` programs and, after a restart of the service, from the listening sockets of a+ sock_diag dump) is decided as `in` on that port, like `bind`. So denying an app's outgoing+ internet doesn't cut the clients of a server it runs, and "no incoming connections" now also+ cuts the connections already accepted.+2. **Destroy the socket.** So the app sees the connection end instead of a stall, `lord-netperm`+ destroys it over sock_diag netlink (`SOCK_DESTROY`, `npdiag.c`): the kernel aborts it (TCP sends+ a reset, the app gets `ECONNABORTED`). Two triggers:+ - **right after every reload:** a dump of the machine's TCP sockets and connected UDP sockets+ with their cgroup (`INET_DIAG_CGROUP_ID`); each one in one of that user's app scopes that the+ same decision denies is destroyed. This also cuts idle connections (a push channel, an SSH+ session) that send nothing;+ - **when the filter drops a packet of a socket** it reports it once (an `NpEvent` with `cut`,+ the 4-tuple and the socket cookie, deduplicated in `np_seen` by cookie) and `lord-netperm`+ destroys exactly that socket (the cookie must match). That covers a domain whose address the+ resolver gave after the connection opened, and sockets outside the dump's reach.++ Listening sockets and unconnected UDP sockets are never destroyed (they may also serve+ allowed peers): their denied packets are only dropped. QUIC is a connected UDP socket, so it+ is destroyed too; its reconnect attempts are refused at `connect`.+3. **Back to allow:** nothing stays behind. The filter reads the maps on every packet, so the first+ packet after the reload passes, and new connections work at once.++**What the Monitor shows:** every cut socket counts as one more *denied* connection (the kernel+counts it on the first drop; a sweep's destroyed sockets are added by `lord-netperm`) and appears+in the connection log as denied; the bytes dropped are sent as *blocked* (`COUNTS`, index 7, and+per address at index 5) and the app's detail shows "Blocked traffic". The counters go out every+second, so the row shows the block within a second.++**Kernels:** `SOCK_DESTROY` needs `CONFIG_INET_DIAG_DESTROY` (TCP) and `CONFIG_INET_UDP_DIAG` (UDP).+Debian 13's kernel (6.12) has both. The Raspberry Pi kernel of the 0.8.2 image doesn't:+`tools/rpi/build-kernel.sh` enables them from 0.8.3 on; with an older kernel the drop still+applies and the journal says once that the app will see a stall (TCP retransmits until it gives+up) instead of a reset. All 11 programs pass the verifier on 6.12 (VM 118) and on the Pi's+6.18.57 (`npverify-test`, built and run in a temporary folder, removed afterwards).++**Tests** (`tests/netperm_cut.sh`, VM 118, real network):++| Case | Result |+|---|---|+| a TCP download (`curl`, 800 KB/s) and an idle TCP session to a LAN SSH port; the app denied | the download ends 0.13–0.45 s after the rule is written (`curl: (56) Recv failure: Software caused connection abort`), the idle session gets `ECONNABORTED` 0.03 s after it |+| an HTTP/3 download (`curl --http3-only`, one QUIC connection) | ends 0.07–0.08 s after the rule (`Software caused connection abort`) |+| switched back to allow | a new TCP connection works at once (HTTP 200 in 0.2 s), and a new HTTP/3 request too |+| a domain rule denying one of two running downloads | that one ends in 0.3–0.8 s, the other keeps downloading |+| "no internet" with an internet download and an idle LAN session | the download ends, the LAN session stays open |+| a server in the app scope, a LAN client sending (another machine), the app's outgoing traffic denied | the server receives every byte (427,000 of 427,000) |+| the same server with "no incoming connections" | the accepted connection is cut (`ECONNABORTED` in the server, a reset at the client) |+| Chromium playing a video (3 TCP connections, one to port 5228, and 2 QUIC sockets), the switch turned off in Monitor → Network | all 5 sockets destroyed within a second (`ss` shows none), the video stops, the row shows the denied count and Rule "Deny"; turned back on, Chromium reconnects |++`netperm_integration.sh` (19/19) and `make -C server test` (0 warnings) pass as before.+++++On a kernel with `SOCK_DESTROY` the sockets usually go before more packets arrive, so "Blocked+traffic" stays small (often 0); it grows on a kernel without it, or for a socket the sweep+couldn't see.++**Cost** (`iperf3` through a veth pair into a private network namespace, the client in an app+scope, as in 0.8.1; per-packet time of the program from `kernel.bpf_stats_enabled`, turned off+again afterwards):++| | TCP, ~24 Gbit/s | UDP 1 Gbit/s, 1200 B, sending | UDP 1 Gbit/s, receiving | per packet (UDP send) |+|---|---|---|---|---|+| 0.8.2 (accounting only), a deny rule for another network | 24.2 Gbit/s, 98.5 % sys | 54.2 / 54.7 % | 25.9 / 26.2 % | 200–206 ns |+| 0.8.3, no rules for the app | 24.4 / 24.2 Gbit/s, 98.6 / 98.4 % | 55.5 / 52.8 % | 26.1 / 26.0 % | 231–233 ns |+| 0.8.3, a deny rule for another network (the full decision per packet: trie, bound port, `np_decide`) | 23.9 / 24.2 Gbit/s, 99.5 / 98.7 % | 56.9 / 55.1 % | 26.2 / 26.1 % | 346–369 ns |++- An app without a deny rule pays about **30 ns more per packet** (the `np_apps` lookup); one with a+ deny rule about **150 ns**. At 104,000 packets per second that is 0.3 % and 1.6 % of one core;+ a video over QUIC is a few thousand packets per second. TCP throughput is unchanged within+ run-to-run noise.+- The CPU columns are the client's system CPU (% of one core over 15 s, `iperf3 -J`), two runs each.+- `lord-netperm` itself only works on a reload (one sock_diag dump) and on a cut socket.+### From rules to maps (`npcompile.c`)There's no time in the kernel for the full precedence of `fw_decide`, so it is precomputed:@@ -1003,10 +1098,16 @@ programs:default rules.- **What isn't checked:**- ICMP: denied only with "no network", through `sock_create`;- - TCP `listen` without `bind` (ephemeral port);- - incoming connections that arrive at an already open port.-- That would need `cgroup_skb/ingress`, which runs per packet.+ - TCP `listen` without `bind` (ephemeral port): its connections are decided as outgoing ones.++ Incoming connections that arrive at an already open port are now decided per packet by the+ filter (0.8.3, [Existing connections are cut](#existing-connections-are-cut)).+- **Cutting under `user@`:** the packet filter is attached only to `lordos.service`, so a process+ that escaped to `user@<uid>.service` keeps the connections it already had when a restriction is+ added; its new ones are refused by the union as before.+- **Cuts after a restart of `lord-netperm`:** the ports an app bound before the restart come back+ from the listening TCP sockets only; a UDP server bound before it is decided as outgoing until it+ binds again.- **Per-port rules with direction `in`** are applied at `bind`, with an unknown address.- **The "ask" mode** is applied as silent.- **Per-destination rules** in the Monitor: done ("Allow / Block this domain", seediff --git a/docs/MONITOR.md b/docs/MONITOR.mdindex a57beb3..069a53a 100644--- a/docs/MONITOR.md+++ b/docs/MONITOR.md@@ -107,6 +107,14 @@ server, and this app's rule is that no address leaves the machine.**"This computer"** is placed by the time zone (`/etc/localtime` → coordinates from`/usr/share/zoneinfo/zone1970.tab`), without the network.+### Blocking cuts what is open (0.8.3)++Switching an app's rule off (or a destination's switch, or adding a restriction) now also ends the+connections it already had: the kernel drops their packets and `lord-netperm` closes the sockets+(`FIREWALL.md`, "Existing connections are cut"). Each cut connection adds one to the app's+*denied* count, and the bytes dropped meanwhile show in its details as **Blocked traffic**+(`COUNTS` index 7). Within a second of the switch the row shows it.+### Per-destination switches (0.8.2)An open app row lists the destinations it reached and gives each one an allow/block switchdiff --git a/docs/VM-SETUP.md b/docs/VM-SETUP.mdindex 5a3113a..eb958fb 100644--- a/docs/VM-SETUP.md+++ b/docs/VM-SETUP.md@@ -751,7 +751,7 @@ See `FIREWALL.md`, step 2. On VM 122 (172.17.0.56), which already had `lord-sess```The unit is the same one that goes into the ISO (the text was handed to the main session). It has- `RestrictAddressFamilies=AF_UNIX AF_PACKET`, capabilities `CAP_BPF CAP_NET_ADMIN CAP_PERFMON+ `RestrictAddressFamilies=AF_UNIX AF_PACKET` (`AF_NETLINK` added in 0.8.3, for sock_diag), capabilities `CAP_BPF CAP_NET_ADMIN CAP_PERFMONCAP_DAC_READ_SEARCH CAP_NET_RAW`, `ProtectSystem=strict`, `ProtectHome=read-only`,`ProtectControlGroups=yes` and `RuntimeDirectory=lordos` with `RuntimeDirectoryPreserve=yes`.- **Restarting `lordos.service`:** with `KillMode=process`, a `systemctl restart` leaves sessiondiff --git a/docs/firewall-cut-detail.png b/docs/firewall-cut-detail.pngnew file mode 100644index 0000000..c712e36Binary files /dev/null and b/docs/firewall-cut-detail.png differdiff --git a/docs/firewall-cut-monitor.png b/docs/firewall-cut-monitor.pngnew file mode 100644index 0000000..02620d9Binary files /dev/null and b/docs/firewall-cut-monitor.png differdiff --git a/sdk/monitor.js b/sdk/monitor.jsindex c831b73..32fd372 100644--- a/sdk/monitor.js+++ b/sdk/monitor.js@@ -143,7 +143,8 @@ export function useMonitor({ procs = false, conns = false, geo = true, everyMs =// ---- per-app firewall (lord.fw, docs/FIREWALL.md) ----// lord-netperm (root) applies the rules in the kernel and counts each app's new connections;// lord-server passes them to JS. firewallState: { status, counts: { app: [allowed, denied, rx, tx,-// rxRate, txRate, [[addr, rx, tx, rxRate, txRate], ...]] } (the bytes when status.bytes),+// rxRate, txRate, [[addr, rx, tx, rxRate, txRate, blocked], ...], blocked] } (the bytes when+// status.bytes; blocked: bytes of open connections dropped because a rule now denies them),// events: [the last 200 new connections], rev }. rev goes up when a rule changes.const fwApi = () => (typeof lord !== 'undefined' && lord.fw) || null;export const firewallState = createStore({ status: null, counts: {}, events: [], rev: 0 });diff --git a/server/Makefile b/server/Makefileindex 3efe777..be2d721 100644--- a/server/Makefile+++ b/server/Makefile@@ -130,7 +130,7 @@ endif# builds and the service warns that it has no BPF. No libbpf or bcc..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 \+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.cALL += build/lord-netpermifeq ($(BPF),1)diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.cindex e2caa87..89549b1 100644--- a/server/src/netperm/netperm.c+++ b/server/src/netperm/netperm.c@@ -11,13 +11,18 @@* validated path) and compiles them into maps (npcompile.c);* - 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".+ * (npproto.h). The only thing it accepts there is "reload my rules";+ * - 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+ * filter reports a socket.** If the kernel has no cgroup BPF (or CAP_BPF is missing), it says so in the log and carries on* doing nothing: the desktop doesn't notice. With the service stopped, the programs detach on* their own (bpf_link): everything is allowed. */#include "netperm/bpfsys.h"#include "netperm/npcompile.h"+#include "netperm/npdiag.h"#include "netperm/npdns.h"#include "netperm/npload.h"#include "netperm/npproto.h"@@ -30,6 +35,7 @@#include <errno.h>#include <fcntl.h>#include <linux/bpf.h>+#include <netinet/in.h>#include <pwd.h>#include <signal.h>#include <stdarg.h>@@ -69,6 +75,8 @@ typedef struct {int has_domains, seen;NpApp app_v; /* the last thing written to np_apps */uint64_t allowed, denied; /* kernel counters */+ uint64_t blocked; /* bytes the packet filter dropped (np_counts) */+ uint64_t cuts; /* sockets destroyed by a sweep (counted as denied) */uint64_t rx, tx; /* bytes (np_counts) */double rrx, rtx; /* bytes per second over the last tick */} Scope;@@ -103,7 +111,9 @@ typedef struct {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;+ 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 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 */FwMap flows; /* NpFlowKey → FlowSt: the previous reading of np_flows */long long counts_ns; /* when push_counts last read the counters */@@ -217,6 +227,7 @@ static int map_fd(const char *name) {if (!strcmp(name, "np_events")) return G.m_np_events;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;return -1;}#endif@@ -294,7 +305,7 @@ static void sync_scope(Session *s, Scope *sc) {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* entries, so they never store anything for a cgroup that isn't an app */- NpCount zero = { 0, 0, 0, 0 };+ NpCount zero = { 0, 0, 0, 0, 0 };bpfs_map_update(G.m_np_counts, &sc->cgid, &zero, BPF_NOEXIST);np_compiled_free(&c);}@@ -598,6 +609,8 @@ static void accept_client(void) {}}+static void sweep(unsigned uid);+static void reload_uid(unsigned uid) {for (int i = 0; i < G.nsess; i++) {if (G.sess[i].uid != uid) continue;@@ -605,6 +618,7 @@ static void reload_uid(unsigned uid) {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]);}+ sweep(uid); /* the open connections the new rules deny */}static void client_read(int k) {@@ -625,6 +639,185 @@ static void client_read(int k) {}}+/* ---- cutting the connections a rule now denies (docs/FIREWALL.md, "Existing connections are cut") ----+ *+ * The packet filter (np.bpf.c, cgroup_skb) already drops every packet of a connection the app's+ * current rules deny, the moment the maps change. This side destroys the sockets too, so the app+ * sees the connection end (ECONNABORTED, and TCP sends a reset) instead of a stall: right after a+ * reload (a sweep of the machine's TCP and connected UDP sockets, by cgroup) and whenever the+ * filter reports a socket it dropped a packet of (one that wasn't there in the sweep, or a domain+ * whose address was learned later). Only sockets in an app scope of that user, only those the+ * same np_decide denies. */++static int diag_fd(void) {+ if (G.diag_fd < 0 && (G.diag_fd = npdiag_open()) < 0 && !G.diag_warned) {+ logf_("sock_diag: %s: open connections are cut only by dropping their packets", strerror(errno));+ G.diag_warned = 1;+ }+ return G.diag_fd;+}++static int cut_sock(const NpSock *k) {+ int fd = diag_fd();+ if (fd < 0) return -ENOTSOCK;+ int rc = npdiag_destroy(fd, k);+ if (rc == -EAGAIN || rc == -EINTR || rc == -EIO) { /* a lost answer: start over with a new socket */+ close(G.diag_fd);+ G.diag_fd = -1;+ }+ if ((rc == -EOPNOTSUPP || rc == -EPERM) && !G.diag_warned) {+ logf_("the kernel can't destroy %s sockets (%s; CONFIG_INET_DIAG_DESTROY): their packets are dropped, the app sees a stall",+ k->proto == IPPROTO_TCP ? "TCP" : "UDP", strerror(-rc));+ G.diag_warned = 1;+ }+ return rc;+}++/* The remote address of a socket as the rules see it: an IPv4-mapped IPv6 one is IPv4. */+static int remote_of(const NpSock *k, uint8_t *a) {+ memset(a, 0, 16);+ if (k->fam == AF_INET) { memcpy(a, k->dst, 4); return 4; }+ static const uint8_t mapped[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };+ if (!memcmp(k->dst, mapped, 12)) { memcpy(a, k->dst + 12, 4); return 4; }+ memcpy(a, k->dst, 16);+ return 6;+}++/* The same decision as the packet filter, from userspace: the app's NpApp, its trie list for the+ * address and whether the local port is one it bound (an incoming connection). 1 if denied. */+static int user_denies(const Scope *sc, const NpSock *k, int *fam, uint8_t *addr, int *dir, int *port, NpRes *r) {+ const NpApp *a = &sc->app_v;+ if (!a->can_deny) return 0;+ *fam = remote_of(k, addr);+ int proto = k->proto == IPPROTO_TCP ? NP_P_TCP : NP_P_UDP, lan;+ NpList l;+ int have = 0;+ if (*fam == 4) {+ lan = np_is_lan4(addr);+ NpLpm4Key key;+ memset(&key, 0, sizeof key);+ key.prefixlen = 64;+ key.data[0] = (uint8_t)(a->idx >> 24); key.data[1] = (uint8_t)(a->idx >> 16); key.data[2] = (uint8_t)(a->idx >> 8); key.data[3] = (uint8_t)a->idx;+ memcpy(key.data + 4, addr, 4);+ have = a->has4 && !bpfs_map_lookup(G.m_np_lpm4, &key, &l);+ } else {+ lan = np_is_lan6(addr);+ NpLpm6Key key;+ memset(&key, 0, sizeof key);+ key.prefixlen = 160;+ key.data[0] = (uint8_t)(a->idx >> 24); key.data[1] = (uint8_t)(a->idx >> 16); key.data[2] = (uint8_t)(a->idx >> 8); key.data[3] = (uint8_t)a->idx;+ memcpy(key.data + 4, addr, 16);+ have = a->has6 && !bpfs_map_lookup(G.m_np_lpm6, &key, &l);+ }+ NpBoundKey bk;+ memset(&bk, 0, sizeof bk);+ bk.cgid = sc->cgid;+ bk.port = k->sport;+ bk.proto = (np_u8)proto;+ uint8_t one;+ *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);+ return r->verdict == NP_V_DENY;+}++typedef struct {+ NpSock k;+ NpEvent ev;+} CutReq;++typedef struct {+ unsigned uid; /* only this user's apps; (unsigned)-1: everyone's */+ CutReq *v;+ int n, cap;+} Sweep;++static int sweep_cb(void *u, const NpSock *k) {+ Sweep *w = u;+ Session *s;+ Scope *sc = k->cgid ? scope_by_cgid(k->cgid, &s) : NULL;+ if (!sc || (w->uid != (unsigned)-1 && s->uid != w->uid)) return 0;+ int proto = k->proto == IPPROTO_TCP ? NP_P_TCP : NP_P_UDP;+ if (k->proto == IPPROTO_TCP && k->state == 10) { /* TCP_LISTEN: the app's server (also after a restart of this service) */+ NpBoundKey bk;+ memset(&bk, 0, sizeof bk);+ bk.cgid = sc->cgid;+ bk.port = k->sport;+ bk.proto = (np_u8)proto;+ uint8_t one = 1;+ bpfs_map_update(G.m_np_bound, &bk, &one, BPF_ANY);+ return 0;+ }+ if (!k->dport) return 0; /* not connected */+ CutReq q;+ memset(&q, 0, sizeof q);+ int fam, dir, port;+ NpRes r;+ if (!user_denies(sc, k, &fam, q.ev.addr, &dir, &port, &r)) return 0;+ if (w->n == w->cap) {+ int nc = w->cap ? w->cap * 2 : 32;+ CutReq *nv = realloc(w->v, (size_t)nc * sizeof *nv);+ if (!nv) return 1;+ w->v = nv;+ w->cap = nc;+ }+ q.k = *k;+ q.ev.cgid = sc->cgid;+ q.ev.port = (np_u16)port;+ q.ev.fam = (np_u8)fam; q.ev.proto = (np_u8)proto; q.ev.dir = (np_u8)dir; q.ev.verdict = NP_V_DENY;+ q.ev.reason = (np_u8)r.reason; q.ev.record = (np_u8)r.record; q.ev.rule_id = r.rule_id; q.ev.flag = r.flag;+ w->v[w->n++] = q;+ return 0;+}++static void on_net_event(const NpEvent *ev, int cut);++/* Destroys the open sockets of uid's apps (or everyone's) that their rules now deny, and learns+ * the ports their servers listen on. */+static void sweep(unsigned uid) {+ if (!G.bpf_ok) return;+ int fd = diag_fd();+ if (fd < 0) return;+ Sweep w = { uid, NULL, 0, 0 };+ /* TCP: every state but TIME_WAIT (6), CLOSE (7) and the request sockets (3, 12); UDP: connected (1) */+ uint32_t tcp_states = ~((1u << 3) | (1u << 6) | (1u << 7) | (1u << 12)), udp_states = 1u << 1;+ static const int fams[2] = { AF_INET, AF_INET6 };+ for (int f = 0; f < 2; f++) {+ int rc = npdiag_each(fd, fams[f], IPPROTO_TCP, tcp_states, sweep_cb, &w);+ if (rc >= 0) rc = npdiag_each(fd, fams[f], IPPROTO_UDP, udp_states, sweep_cb, &w);+ if (rc < 0 && rc != -ENOENT) {+ logf_("sock_diag dump: %s", strerror(-rc));+ close(G.diag_fd);+ G.diag_fd = -1;+ break;+ }+ }+ struct timespec ts;+ clock_gettime(CLOCK_MONOTONIC, &ts); /* the clock of bpf_ktime_get_ns */+ uint64_t now = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;+ for (int i = 0; i < w.n; i++) {+ if (cut_sock(&w.v[i].k)) continue; /* gone already, or the kernel can't: the drop holds */+ /* the filter may have dropped one of its packets in the meantime and counted it already+ * (np_seen by cookie, as report_cut keys it); otherwise mark it so it isn't counted twice */+ NpSeenKey sk;+ memset(&sk, 0, sizeof sk);+ sk.cgid = w.v[i].ev.cgid;+ uint64_t cookie = (uint64_t)w.v[i].k.cookie[0] | (uint64_t)w.v[i].k.cookie[1] << 32;+ for (int b = 0; b < 8; b++) sk.addr[b] = (uint8_t)(cookie >> (b * 8));+ sk.dir = NP_D_CUT;+ sk.verdict = 1;+ uint64_t last;+ if (!bpfs_map_lookup(G.m_np_seen, &sk, &last)) continue;+ bpfs_map_update(G.m_np_seen, &sk, &now, BPF_ANY);+ Session *s;+ Scope *sc = scope_by_cgid(w.v[i].ev.cgid, &s);+ if (sc) { sc->cuts++; s->dirty_counts = 1; }+ on_net_event(&w.v[i].ev, 1);+ }+ free(w.v);+}+/* ---- notices and counters -------------------------------------------------------------------- */static const char *by_name(int r) {@@ -650,6 +843,34 @@ static void on_event(void *u, const void *data, uint32_t len) {logf_("uid %u %s: denied a Bluetooth socket (%s)", us->uid, sc ? sc->app : "user@ (union of restricted apps)", fw_flag_name(ev.flag));return;}+ if (ev.cut) {+ /* the packet filter dropped a packet of an open socket the rules now deny: destroy it */+ int cut = 0;+ if (ev.destroy && sc && (ev.fam == 4 || ev.fam == 6) && (ev.proto == NP_P_TCP || ev.proto == NP_P_UDP)) {+ NpSock k;+ memset(&k, 0, sizeof k);+ k.fam = ev.fam == 4 ? AF_INET : AF_INET6;+ k.proto = ev.proto == NP_P_TCP ? IPPROTO_TCP : IPPROTO_UDP;+ memcpy(k.src, ev.laddr, 16);+ memcpy(k.dst, ev.addr, 16);+ k.sport = ev.lport;+ k.dport = ev.rport;+ k.cookie[0] = (uint32_t)ev.cookie;+ k.cookie[1] = (uint32_t)(ev.cookie >> 32);+ cut = cut_sock(&k) == 0;+ }+ if (ev.cut != 1) return; /* asked again: already counted and reported */+ on_net_event(&ev, cut);+ return;+ }+ on_net_event(&ev, 0);+}++/* A connection notice (new, denied or cut) towards the user's lord-server and the journal. */+static void on_net_event(const NpEvent *evp, int cut) {+ NpEvent ev = *evp;+ Session *s;+ Scope *sc = scope_by_cgid(ev.cgid, &s);char addr[INET6_ADDRSTRLEN] = "";if (ev.fam == 4) inet_ntop(AF_INET, ev.addr, addr, sizeof addr);else if (ev.fam == 6) inet_ntop(AF_INET6, ev.addr, addr, sizeof addr);@@ -686,7 +907,7 @@ static void on_event(void *u, const void *data, uint32_t len) {ev.rule_id, flag ? flag : "", ev.record, hosts);if (n > 0 && (size_t)n < sizeof j) send_uid(s->uid, NP_MSG_EVENT, j, (size_t)n, 1);if (ev.verdict == NP_V_DENY)- logf_("uid %u %s: denied %s %s %s:%u (%s%s%s)", s->uid, sc->app, ev.dir == NP_D_IN ? "in" : "out",+ logf_("uid %u %s: %s %s %s %s:%u (%s%s%s)", s->uid, sc->app, cut ? "cut open" : ev.cut ? "dropping open" : "denied", ev.dir == NP_D_IN ? "in" : "out",ev.proto == NP_P_TCP ? "tcp" : ev.proto == NP_P_UDP ? "udp" : "other", addr[0] ? addr : "?", ev.port, by_name(ev.reason),ev.flag ? " " : "", ev.flag && flag ? flag : "");}@@ -694,7 +915,7 @@ static void on_event(void *u, const void *data, uint32_t len) {/* ---- traffic per address (np_flows) ------------------------------------------------------- */typedef struct {- uint64_t rx, tx; /* the last reading */+ uint64_t rx, tx, blocked; /* the last reading */double rrx, rtx; /* bytes per second over the last tick */long long active; /* tick of the last change */long long tick; /* tick of the last reading that saw it */@@ -728,9 +949,10 @@ static void read_flows(double dt) {uint64_t dtx = vals[i].tx >= f->tx ? vals[i].tx - f->tx : vals[i].tx;f->rrx = dt > 0 ? (double)drx / dt : 0;f->rtx = dt > 0 ? (double)dtx / dt : 0;- if (drx || dtx || !f->active) f->active = G.tick;+ if (drx || dtx || vals[i].blocked != f->blocked || !f->active) f->active = G.tick;f->rx = vals[i].rx;f->tx = vals[i].tx;+ f->blocked = vals[i].blocked;f->tick = G.tick;}/* the table mirrors the kernel's: what the LRU evicted (or a closed scope left) goes */@@ -748,7 +970,7 @@ static void idle_flows(void) {}}-/* [["addr",rx,tx,rxRate,txRate],...] of one app, busiest first. */+/* [["addr",rx,tx,rxRate,txRate,blocked],...] of one app, busiest first. */static size_t flows_json(uint64_t cgid, char *j, size_t cap) {static FlowRef refs[NP_MAX_FLOWS];int n = 0;@@ -762,17 +984,18 @@ static size_t flows_json(uint64_t cgid, char *j, size_t cap) {qsort(refs, (size_t)n, sizeof refs[0], flow_cmp);size_t w = 0;w += (size_t)snprintf(j + w, cap - w, "[");- for (int i = 0; i < n && i < FLOWS_PER_APP && w + 120 < cap; i++) {+ for (int i = 0; i < n && i < FLOWS_PER_APP && w + 170 < 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]", i ? "," : "", addr,- (unsigned long long)refs[i].f->rx, (unsigned long long)refs[i].f->tx, refs[i].f->rrx, refs[i].f->rtx);+ 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);}w += (size_t)snprintf(j + w, cap - w, "]");return w;}-/* Every second: {"app":[allowed,denied,rx,tx,rxRate,txRate,[[addr,rx,tx,rxRate,txRate],...]],...}+/* Every second: {"app":[allowed,denied,rx,tx,rxRate,txRate,[[addr,rx,tx,rxRate,txRate,blocked],...],blocked],...}* to each user with something new (the rates are bytes per second over the last second). With* no traffic and no new connections nothing is sent. */static void push_counts(void) {@@ -786,14 +1009,16 @@ static void push_counts(void) {Session *s = &G.sess[i];for (int k = 0; k < s->nsc; k++) {Scope *sc = &s->sc[k];- NpCount c = { 0, 0, 0, 0 };+ NpCount c = { 0, 0, 0, 0, 0 };if (G.bpf_ok) bpfs_map_lookup(G.m_np_counts, &sc->cgid, &c);- if (c.allowed != sc->allowed || c.denied != sc->denied) s->dirty_counts = 1;+ c.denied += sc->cuts; /* the sockets a sweep destroyed are denied connections too */+ if (c.allowed != sc->allowed || c.denied != sc->denied || c.blocked != sc->blocked) { s->dirty_counts = 1; bytes_moved = 1; }double rrx = dt > 0 && c.rx >= sc->rx ? (double)(c.rx - sc->rx) / dt : 0;double rtx = dt > 0 && c.tx >= sc->tx ? (double)(c.tx - sc->tx) / dt : 0;if (c.rx != sc->rx || c.tx != sc->tx || (rrx == 0) != (sc->rrx == 0) || (rtx == 0) != (sc->rtx == 0)) { s->dirty_counts = 1; bytes_moved = 1; }sc->allowed = c.allowed;sc->denied = c.denied;+ sc->blocked = c.blocked;sc->rx = c.rx;sc->tx = c.tx;sc->rrx = rrx;@@ -812,7 +1037,7 @@ 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 + 160 + (size_t)FLOWS_PER_APP * 120;+ 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;char *j = malloc(cap);if (!j) continue;@@ -824,8 +1049,8 @@ static void push_counts(void) {w += (size_t)snprintf(j + w, cap - w, "%s%s:[%llu,%llu,%llu,%llu,%.0f,%.0f,", w > 1 ? "," : "", app,(unsigned long long)sc->allowed, (unsigned long long)sc->denied,(unsigned long long)sc->rx, (unsigned long long)sc->tx, sc->rrx, sc->rtx);- w += flows_json(sc->cgid, j + w, cap - w - 2);- j[w++] = ']';+ w += flows_json(sc->cgid, j + w, cap - w - 40);+ w += (size_t)snprintf(j + w, cap - w, ",%llu]", (unsigned long long)sc->blocked);}j[w++] = '}';send_uid(s->uid, NP_MSG_COUNTS, j, w, 0);@@ -892,7 +1117,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_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_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;}@@ -1024,6 +1249,7 @@ int main(int argc, char **argv) {sigaction(SIGINT, &sa, NULL);G.dns_fd = -1;G.cgfd = -1;+ G.diag_fd = -1;G.empty = fw_store_new();G.names = dnsmap_new(16384);G.ep = epoll_create1(EPOLL_CLOEXEC);@@ -1050,6 +1276,7 @@ int main(int argc, char **argv) {attach_service();if (G.root_wd < 0) G.root_wd = inotify_add_watch(G.ifd, CG_ROOT "/" CG_SERVICE, IN_CREATE | IN_DELETE | IN_ONLYDIR);scan_all();+ sweep((unsigned)-1); /* what was opened while this service wasn't running, and the servers' ports */watch_dns();while (!quit) {diff --git a/server/src/netperm/np.bpf.c b/server/src/netperm/np.bpf.cindex b84a231..d97dddc 100644--- a/server/src/netperm/np.bpf.c+++ b/server/src/netperm/np.bpf.c@@ -17,7 +17,10 @@* 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). They never drop a packet.+ * per (app, remote address) (np_flows); 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).** The same programs are also attached to the user@<uid>.service of each LordOS session, to* close the `systemd-run --user` escape (the process ends up there, outside lordos.service). A@@ -33,10 +36,13 @@* sendto. A UDP connect() counts only the first time within 5 s, like a sendto: getaddrinfo does one* per address to sort them (RFC 6724) and they aren't real connections.*- * The one exception is traffic accounting (skb_ingress / skb_egress), which runs per packet- * (per GRO/GSO segment): only for the sessions' apps, two map lookups and two atomic adds, and it- * always lets the packet through. It's what counts UDP (QUIC) and the bytes of connections that- * already closed, which the sockets' own counters (tcp_info) can't. */+ * The one exception is the packet filter (skb_ingress / skb_egress), which runs per packet (per+ * GRO/GSO segment): only for the sessions' apps, two map lookups and two atomic adds. It's what+ * counts UDP (QUIC) and the bytes of connections that already closed, which the sockets' own+ * counters (tcp_info) can't. Only for an app whose rules can deny something (NpApp.can_deny) does+ * it also decide, with the same np_decide as connect(): that is what cuts a TCP connection or a+ * connected UDP socket (QUIC) that was opened before the rule changed (docs/FIREWALL.md, "Existing+ * connections are cut"). Loopback is never filtered. */#define NP_BPF 1#include <linux/bpf.h>#include "netperm/np_shared.h"@@ -51,6 +57,8 @@ static unsigned long long (*bpf_get_current_ancestor_cgroup_id)(int level) = (vostatic long (*bpf_ringbuf_output)(void *ringbuf, void *data, unsigned long long size, unsigned long long flags) = (void *)130;static long (*bpf_skb_load_bytes)(const void *skb, unsigned int offset, void *to, unsigned int len) = (void *)26;static 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;/* the maps: only their names (lord-netperm defines them, netperm.c) */char np_cfg[4] SEC("maps");@@ -63,6 +71,7 @@ char np_counts[4] SEC("maps");char np_events[4] SEC("maps");char np_snd[4] SEC("maps");char np_flows[4] SEC("maps");+char np_bound[4] SEC("maps");#define AF_INET 2#define AF_INET6 10@@ -97,7 +106,7 @@ NP_INLINE NpApp *find_app(NpCfg *cfg, np_u64 *cgid) {NP_INLINE void count(np_u64 cgid, int deny) {NpCount *c = bpf_map_lookup_elem(&np_counts, &cgid);if (!c) {- NpCount z = { 0, 0, 0, 0 };+ NpCount z = { 0, 0, 0, 0, 0 };bpf_map_update_elem(&np_counts, &cgid, &z, BPF_NOEXIST);c = bpf_map_lookup_elem(&np_counts, &cgid);if (!c) return;@@ -106,20 +115,12 @@ NP_INLINE void count(np_u64 cgid, int deny) {else __sync_fetch_and_add(&c->allowed, 1);}-/* 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) {- np_u32 zero = 0;- NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);- if (!cfg || !cfg->on) return 1;- np_u64 cgid = 0;- NpApp *app = find_app(cfg, &cgid);- if (!app) return 1; /* neither an app scope nor a restricted user@ */-- int lan = -1;+/* The app's trie list for an address (its networks and resolved domains), or 0; *lan: 1 LAN, 0+ * internet, -1 unknown (fam 0). */+NP_INLINE NpList *lookup_list(NpApp *app, int fam, const np_u8 *addr, int *lan) {NpList *l = 0;if (fam == 4) {- lan = np_is_lan4(addr);+ *lan = np_is_lan4(addr);if (app->has4) {NpLpm4Key k;k.prefixlen = 64;@@ -129,7 +130,7 @@ NP_INLINE int handle(int fam, np_u8 *addr, int port, int proto, int dir, int alwl = bpf_map_lookup_elem(&np_lpm4, &k);}} else if (fam == 6) {- lan = np_is_lan6(addr);+ *lan = np_is_lan6(addr);if (app->has6) {NpLpm6Key k;k.prefixlen = 160;@@ -139,6 +140,21 @@ NP_INLINE int handle(int fam, np_u8 *addr, int port, int proto, int dir, int alwl = bpf_map_lookup_elem(&np_lpm6, &k);}}+ return l;+}++/* 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) {+ np_u32 zero = 0;+ NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);+ if (!cfg || !cfg->on) return 1;+ np_u64 cgid = 0;+ NpApp *app = find_app(cfg, &cgid);+ if (!app) return 1; /* neither an app scope nor a restricted user@ */++ int lan = -1;+ NpList *l = lookup_list(app, fam, addr, &lan);NpRes r;np_decide(app, l, dir, proto, port, lan, &r);int deny = r.verdict == NP_V_DENY;@@ -221,6 +237,24 @@ SEC("cgroup/sendmsg6") int np_sendmsg6(struct bpf_sock_addr *ctx) {/* bind: the remote address isn't known. Only if it's loopback is the connection surely local* (like a fw_decide query with that address); otherwise "unknown" = internet. Port 0* (ephemeral, used by clients) isn't checked. */+/* A port an app bound (allowed): the packet filter treats the packets on it as an incoming+ * connection (decided as "in" on the local port, like bind), not as one the app opened. Only for+ * the apps' scopes (an np_counts entry). */+NP_INLINE void remember_bound(int port, int proto) {+ np_u32 zero = 0;+ NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);+ if (!cfg) return;+ np_u64 id = bpf_get_current_ancestor_cgroup_id((int)cfg->level);+ if (!id || !bpf_map_lookup_elem(&np_counts, &id)) return;+ NpBoundKey k;+ __builtin_memset(&k, 0, sizeof k);+ k.cgid = id;+ k.port = (np_u16)port;+ k.proto = (np_u8)proto;+ np_u8 one = 1;+ bpf_map_update_elem(&np_bound, &k, &one, BPF_ANY);+}+SEC("cgroup/bind4") int np_bind4(struct bpf_sock_addr *ctx) {int port = port_of(ctx);if (!port) return 1;@@ -228,7 +262,10 @@ SEC("cgroup/bind4") int np_bind4(struct bpf_sock_addr *ctx) {addr4(ctx, a);int fam = a[0] == 127 ? 4 : 0;if (!fam) a[0] = a[1] = a[2] = a[3] = 0;- return handle(fam, a, port, proto_of(ctx->protocol), NP_D_IN, 1);+ int proto = proto_of(ctx->protocol);+ int ok = handle(fam, a, port, proto, NP_D_IN, 1);+ if (ok) remember_bound(port, proto);+ return ok;}SEC("cgroup/bind6") int np_bind6(struct bpf_sock_addr *ctx) {@@ -241,7 +278,10 @@ SEC("cgroup/bind6") int np_bind6(struct bpf_sock_addr *ctx) {for (int i = 0; i < 16; i++) a[i] = 0;fam = 0;}- return handle(fam, a, port, proto_of(ctx->protocol), NP_D_IN, 1);+ int proto = proto_of(ctx->protocol);+ int ok = handle(fam, a, port, proto, NP_D_IN, 1);+ if (ok) remember_bound(port, proto);+ return ok;}/* "no network": the app can't create internet sockets (nor ICMP, which doesn't go through connect). */@@ -338,48 +378,145 @@ SEC("cgroup/lsm_socket_create") int np_bt_socket(unsigned long long *ctx) {return 0;}-/* ---- traffic accounting (the Monitor's bytes per app and per address) ---- */+/* ---- the packet filter: traffic accounting and the cut of open connections ---- */#define LOOPBACK_IFINDEX 1 /* "lo" is always ifindex 1 (in every network namespace) */+#define TCP_FLAG_RST 0x04++/* A dropped packet of a socket the app's rules now deny: reported once per socket (np_seen keyed+ * by its cookie), so lord-netperm destroys it (TCP gets a reset instead of a stall, a connected UDP+ * socket an error) and the Monitor counts one more denied connection. If the socket is still+ * there 5 s later (a kernel without SOCK_DESTROY), it's asked again, without counting. */+NP_INLINE void report_cut(struct __sk_buff *skb, np_u64 cgid, const NpFlowKey *k, const np_u8 *la, int lport, int rport,+ int proto, int dir, int port, const NpRes *r) {+ np_u64 cookie = bpf_get_socket_cookie(skb);+ NpSeenKey sk;+ __builtin_memset(&sk, 0, sizeof sk);+ sk.cgid = cgid;+ for (int i = 0; i < 8; i++) sk.addr[i] = (np_u8)(cookie >> (i * 8));+ sk.dir = NP_D_CUT; sk.verdict = 1;+ np_u64 now = bpf_ktime_get_ns();+ np_u64 *last = bpf_map_lookup_elem(&np_seen, &sk);+ if (last && now - *last <= NP_REPEAT_NS) return;+ int first = !last;+ bpf_map_update_elem(&np_seen, &sk, &now, BPF_ANY);+ if (first) count(cgid, 1);+ NpEvent ev;+ __builtin_memset(&ev, 0, sizeof ev);+ ev.cgid = cgid;+ ev.ts = now;+ for (int i = 0; i < 16; i++) { ev.addr[i] = k->addr[i]; ev.laddr[i] = la[i]; }+ ev.port = (np_u16)port;+ ev.lport = (np_u16)lport; ev.rport = (np_u16)rport;+ ev.fam = k->fam; ev.proto = (np_u8)proto; ev.dir = (np_u8)dir; ev.verdict = NP_V_DENY;+ ev.reason = (np_u8)r->reason; ev.record = (np_u8)r->record;+ ev.rule_id = r->rule_id; ev.flag = r->flag;+ ev.cut = first ? 1 : 2;+ ev.cookie = cookie;+ /* which sockets may be destroyed: a TCP connection, not a listener (a SYN from a denied peer+ * mustn't take the server away from the allowed ones), and a connected UDP socket (QUIC), not+ * an unconnected one that also talks to allowed addresses */+ struct bpf_sock *s = skb->sk;+ if (s) s = bpf_sk_fullsock(s);+ if (s) ev.destroy = proto == NP_P_TCP ? s->state != 10 /* TCP_LISTEN */ : proto == NP_P_UDP && s->dst_port != 0;+ bpf_ringbuf_output(&np_events, &ev, sizeof ev, 0);+}-/* Adds the packet's bytes to its app (the socket's cgroup, the scope at cfg->level) and to the- * (app, remote address) pair. 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 or stored.- * Loopback isn't counted. The skb starts at the IP header in both directions. */-NP_INLINE void account(struct __sk_buff *skb, int in) {- if (skb->ifindex == LOOPBACK_IFINDEX) return;+/* 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". */+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;+ if (!cfg) return 1;np_u64 cgid = bpf_skb_ancestor_cgroup_id(skb, (int)cfg->level);- if (!cgid) return;+ if (!cgid) return 1;NpCount *c = bpf_map_lookup_elem(&np_counts, &cgid);- if (!c) return;+ if (!c) return 1;+ /* 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;- np_u8 v = 0;- if (bpf_skb_load_bytes(skb, 0, &v, 1)) return;- if (v >> 4 == 4) {- if (bpf_skb_load_bytes(skb, in ? 12 : 16, k.addr, 4)) return; /* source when it comes in, destination when it goes out */- k.fam = 4;- } else if (v >> 4 == 6) {- if (bpf_skb_load_bytes(skb, in ? 8 : 24, k.addr, 16)) return;+ int v6 = h[0] >> 4 == 6;+ 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);k.fam = 6;- } else return;+ } else if (h[0] >> 4 == 4) {+ __builtin_memcpy(k.addr, in ? h + 12 : h + 16, 4);+ k.fam = 4;+ } else return 1;np_u64 len = skb->len;- if (in) __sync_fetch_and_add(&c->rx, len);- else __sync_fetch_and_add(&c->tx, len);++ int deny = 0, proto = NP_P_OTHER, dir = NP_D_OUT, port = 0, lport = 0, rport = 0;+ np_u8 la[16];+ NpRes 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);+ /* a reset always goes through: it is what tells the other end the connection is over */+ deny = r.verdict == NP_V_DENY && !rst;+ }+NpFlow *f = bpf_map_lookup_elem(&np_flows, &k);if (!f) {- NpFlow z = { 0, 0 };+ NpFlow z = { 0, 0, 0 };bpf_map_update_elem(&np_flows, &k, &z, BPF_NOEXIST);f = bpf_map_lookup_elem(&np_flows, &k);- if (!f) return;}- if (in) __sync_fetch_and_add(&f->rx, len);- else __sync_fetch_and_add(&f->tx, len);+ if (deny) {+ __sync_fetch_and_add(&c->blocked, len);+ if (f) __sync_fetch_and_add(&f->blocked, len);+ report_cut(skb, cgid, &k, la, lport, rport, proto, dir, port, &r);+ return 0;+ }+ if (in) __sync_fetch_and_add(&c->rx, len);+ else __sync_fetch_and_add(&c->tx, len);+ if (f) {+ if (in) __sync_fetch_and_add(&f->rx, len);+ else __sync_fetch_and_add(&f->tx, len);+ }+ return 1;}-SEC("cgroup/skb_ingress") int np_skb_in(struct __sk_buff *skb) { account(skb, 1); return 1; }-SEC("cgroup/skb_egress") int np_skb_out(struct __sk_buff *skb) { account(skb, 0); return 1; }+SEC("cgroup/skb_ingress") int np_skb_in(struct __sk_buff *skb) { return filter(skb, 1); }+SEC("cgroup/skb_egress") int np_skb_out(struct __sk_buff *skb) { return filter(skb, 0); }diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.hindex 50a6c92..ea5d94c 100644--- a/server/src/netperm/np_shared.h+++ b/server/src/netperm/np_shared.h@@ -14,7 +14,9 @@* np_lpm6 LPM (app idx, IPv6/n) → NpList* np_seen LRU NpSeenKey → u64 ns to report only new connections* np_counts HASH cgroup id → NpCount allowed / denied, bytes received / sent- * np_flows LRU NpFlowKey → NpFlow bytes received / sent per (app, remote address)+ * np_flows LRU NpFlowKey → NpFlow bytes received / sent / blocked per (app, remote address)+ * np_bound LRU NpBoundKey → u8 local ports an app bound (bind with port != 0): a packet+ * on one of them belongs to an incoming connection* 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@@ -48,6 +50,7 @@ typedef uint64_t np_u64;#define NP_MAX_APPS 8192 /* cgroups with a policy */#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_RINGBUF (256u << 10)#define NP_REPEAT_NS (5ull * 1000000000ull) /* a repeated connection is reported again after 5 s */@@ -78,7 +81,7 @@ typedef uint64_t np_u64;#define NP_DEV_CHAR 2 /* BPF_DEVCG_DEV_CHAR */enum { NP_P_ANY = 0, NP_P_TCP = 1, NP_P_UDP = 2, NP_P_OTHER = 3 }; /* like FwProto (+ other) */-enum { NP_D_OUT = 0, NP_D_IN = 1, NP_D_DEV = 2, NP_D_BT = 3 }; /* events: DEV = a device open, BT = a Bluetooth socket */+enum { 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 */@@ -106,6 +109,8 @@ 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 */NpEnt gen[NP_GEN_MAX]; /* lan / internet / any rules, in order */} NpApp;@@ -127,10 +132,12 @@ typedef struct {np_u8 pad[2];} NpSeenKey;-/* allowed / denied: connections (connect, bind, the first sendto in 5 s). rx / tx: IP bytes- * received and sent by the app's sockets (cgroup/skb_ingress and skb_egress, every protocol:- * TCP, UDP and so QUIC), loopback excluded so they compare with the interfaces' counters. */-typedef struct { np_u64 allowed, denied, rx, tx; } NpCount;+/* allowed / denied: connections (connect, bind, the first sendto in 5 s, and each open socket+ * cut because a rule now denies it). rx / tx: IP bytes received and sent by the app's sockets+ * (cgroup/skb_ingress and skb_egress, every protocol: TCP, UDP and so QUIC), loopback excluded so+ * they compare with the interfaces' counters. blocked: IP bytes of packets the same programs+ * dropped (in and out) because the app's current rules deny their connection; not in rx / tx. */+typedef struct { np_u64 allowed, denied, rx, tx, blocked; } NpCount;typedef struct {np_u64 cgid;@@ -139,7 +146,14 @@ typedef struct {np_u8 pad[7];} NpFlowKey;-typedef struct { np_u64 rx, tx; } NpFlow;+typedef struct { np_u64 rx, tx, blocked; } NpFlow;++typedef struct {+ np_u64 cgid; /* the app's scope */+ np_u16 port; /* local port, host order */+ np_u8 proto; /* NP_P_TCP / NP_P_UDP */+ np_u8 pad[5];+} NpBoundKey;typedef struct {np_u64 cgid;@@ -149,6 +163,15 @@ typedef struct {np_u8 fam, proto, dir, verdict, reason, record;np_u32 rule_id;np_u32 flag;+ /* only for a cut (an open socket whose packets the filter now drops): */+ np_u8 laddr[16]; /* the local address (same family as addr) */+ np_u16 lport, rport; /* the local and remote ports (port is the one the rule looked at) */+ np_u8 cut; /* 0: a connection attempt; 1: first drop of this socket; 2: repeated */+ np_u8 destroy; /* 1: a TCP connection (not a listener) or a connected UDP socket:+ * lord-netperm destroys it. 0: an unconnected UDP socket or a+ * listener, which may also serve allowed peers: only the drop */+ np_u8 pad2[2];+ np_u64 cookie; /* the socket's cookie (bpf_get_socket_cookie), checked by SOCK_DESTROY */} NpEvent;typedef struct {@@ -238,4 +261,10 @@ NP_INLINE void np_decide(const NpApp *a, const NpList *l, int dir, int proto, inr->record = a->mode == NP_M_SILENT;}+/* Does a list of entries deny anything? (NpApp.can_deny, computed by userspace.) */+NP_INLINE int np_ents_deny(const NpEnt *e, np_u32 n) {+ for (np_u32 i = 0; i < n; i++) if (e[i].verdict == NP_V_DENY) return 1;+ return 0;+}+#endifdiff --git a/server/src/netperm/npcompile.c b/server/src/netperm/npcompile.cindex 4bd2bdb..f4b5336 100644--- a/server/src/netperm/npcompile.c+++ b/server/src/netperm/npcompile.c@@ -204,11 +204,12 @@ int np_compile_app(FwStore *s, const char *app, uint32_t idx, DnsMap *names, conconst FwRule **v = malloc(((size_t)R.n + 1) * sizeof *v);FwMap seen = { 0 };if (!v) goto done;- int k = 0;+ int k = 0, dom_deny = 0;for (int i = 0; i < R.n; i++) {FwDstKind kd = R.rules[i].kind;if (kd == FW_DST_ANY || kd == FW_DST_LAN || kd == FW_DST_INTERNET) v[k++] = &R.rules[i];if (kd == FW_DST_DOMAIN) out->has_domains = 1;+ if (kd == FW_DST_DOMAIN && R.rules[i].action == FW_DENY) dom_deny = 1; /* its addresses may come later (on_dns) */}out->truncated += fill(v, k, a->gen, &a->ngen, NP_GEN_MAX);@@ -239,9 +240,11 @@ 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);for (int i = 0; i < out->nnodes; i++) {if (out->nodes[i].fam == 4) a->has4 = 1;else a->has6 = 1;+ if (np_ents_deny(out->nodes[i].list.e, out->nodes[i].list.n)) a->can_deny = 1;}rc = 0;done:diff --git a/server/src/netperm/npdiag.c b/server/src/netperm/npdiag.cnew file mode 100644index 0000000..82e84fb--- /dev/null+++ b/server/src/netperm/npdiag.c@@ -0,0 +1,151 @@+/* npdiag.c — the sock_diag client of lord-netperm (see npdiag.h). */+#include "netperm/npdiag.h"++#include <errno.h>+#include <linux/inet_diag.h>+#include <linux/netlink.h>+#include <linux/sock_diag.h>+#include <netinet/in.h>+#include <string.h>+#include <sys/socket.h>+#include <sys/time.h>+#include <unistd.h>++int npdiag_open(void) {+ int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_SOCK_DIAG);+ if (fd < 0) return -1;+ struct timeval tv = { 0, 500000 };+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);+ return fd;+}++/* The next netlink message in buf[0..len) at *off, or NULL (checked without the NLMSG_OK macro,+ * which compares signed and unsigned). */+static const struct nlmsghdr *next_msg(const char *buf, size_t len, size_t *off) {+ if (*off + sizeof(struct nlmsghdr) > len) return NULL;+ const struct nlmsghdr *h = (const struct nlmsghdr *)(const void *)(buf + *off);+ if (h->nlmsg_len < sizeof *h || *off + h->nlmsg_len > len) return NULL;+ *off += NLMSG_ALIGN(h->nlmsg_len);+ return h;+}++typedef struct {+ struct nlmsghdr nh;+ struct inet_diag_req_v2 r;+} DiagReq;++static int send_req(int fd, uint16_t type, uint16_t flags, const struct inet_diag_req_v2 *r, uint32_t seq) {+ DiagReq q;+ memset(&q, 0, sizeof q);+ q.nh.nlmsg_len = sizeof q;+ q.nh.nlmsg_type = type;+ q.nh.nlmsg_flags = (uint16_t)(NLM_F_REQUEST | flags);+ q.nh.nlmsg_seq = seq;+ q.r = *r;+ struct sockaddr_nl sa = { .nl_family = AF_NETLINK };+ ssize_t n = sendto(fd, &q, sizeof q, 0, (struct sockaddr *)&sa, sizeof sa);+ return n == (ssize_t)sizeof q ? 0 : -(errno ? errno : EIO);+}++static void from_msg(const struct inet_diag_msg *m, int proto, NpSock *s) {+ memset(s, 0, sizeof *s);+ s->fam = m->idiag_family;+ s->proto = proto;+ s->state = m->idiag_state;+ size_t al = m->idiag_family == AF_INET ? 4 : 16;+ memcpy(s->src, m->id.idiag_src, al);+ memcpy(s->dst, m->id.idiag_dst, al);+ s->sport = ntohs(m->id.idiag_sport);+ s->dport = ntohs(m->id.idiag_dport);+ s->ifindex = m->id.idiag_if;+ s->cookie[0] = m->id.idiag_cookie[0];+ s->cookie[1] = m->id.idiag_cookie[1];+}++#ifndef INET_DIAG_CGROUP_ID+#define INET_DIAG_CGROUP_ID 21 /* Linux 5.9 */+#endif++static uint64_t cgroup_attr(const struct nlmsghdr *h) {+ size_t off = NLMSG_LENGTH(sizeof(struct inet_diag_msg));+ off = NLMSG_ALIGN(off);+ while (off + sizeof(struct nlattr) <= h->nlmsg_len) {+ const struct nlattr *a = (const struct nlattr *)(const void *)((const char *)h + off);+ if (a->nla_len < sizeof *a || off + a->nla_len > h->nlmsg_len) break;+ if ((a->nla_type & NLA_TYPE_MASK) == INET_DIAG_CGROUP_ID && a->nla_len >= NLA_HDRLEN + 8) {+ uint64_t v;+ memcpy(&v, (const char *)a + NLA_HDRLEN, 8);+ return v;+ }+ off += NLA_ALIGN(a->nla_len);+ }+ return 0;+}++int npdiag_each(int fd, int fam, int proto, uint32_t states, int (*cb)(void *u, const NpSock *s), void *u) {+ static uint32_t seq;+ struct inet_diag_req_v2 r;+ memset(&r, 0, sizeof r);+ r.sdiag_family = (uint8_t)fam;+ r.sdiag_protocol = (uint8_t)proto;+ r.idiag_states = states;+ uint32_t my = ++seq;+ int rc = send_req(fd, SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, &r, my);+ if (rc) return rc;+ static char buf[32768] __attribute__((aligned(8)));+ int seen = 0, stop = 0;+ for (;;) {+ ssize_t n = recv(fd, buf, sizeof buf, 0);+ if (n < 0) return errno == EINTR ? -EINTR : -errno;+ if (n == 0) return -EIO;+ size_t off = 0;+ for (const struct nlmsghdr *h; (h = next_msg(buf, (size_t)n, &off));) {+ if (h->nlmsg_seq != my) continue;+ if (h->nlmsg_type == NLMSG_DONE) return seen;+ if (h->nlmsg_type == NLMSG_ERROR) {+ const struct nlmsgerr *e = (const struct nlmsgerr *)NLMSG_DATA(h);+ return h->nlmsg_len >= NLMSG_LENGTH(sizeof *e) && e->error ? e->error : -EIO;+ }+ if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY || h->nlmsg_len < NLMSG_LENGTH(sizeof(struct inet_diag_msg)) || stop) continue;+ const struct inet_diag_msg *m = (const struct inet_diag_msg *)NLMSG_DATA(h);+ if (m->idiag_family != AF_INET && m->idiag_family != AF_INET6) continue;+ NpSock s;+ from_msg(m, proto, &s);+ s.cgid = cgroup_attr(h);+ seen++;+ if (cb(u, &s)) stop = 1; /* keep reading to the end of the dump */+ }+ }+}++int npdiag_destroy(int fd, const NpSock *s) {+ static uint32_t seq = 1u << 30;+ struct inet_diag_req_v2 r;+ memset(&r, 0, sizeof r);+ r.sdiag_family = (uint8_t)s->fam;+ r.sdiag_protocol = (uint8_t)s->proto;+ r.idiag_states = ~0u;+ size_t al = s->fam == AF_INET ? 4 : 16;+ memcpy(r.id.idiag_src, s->src, al);+ memcpy(r.id.idiag_dst, s->dst, al);+ r.id.idiag_sport = htons(s->sport);+ r.id.idiag_dport = htons(s->dport);+ r.id.idiag_if = s->ifindex;+ r.id.idiag_cookie[0] = s->cookie[0];+ r.id.idiag_cookie[1] = s->cookie[1];+ uint32_t my = ++seq;+ int rc = send_req(fd, SOCK_DESTROY, NLM_F_ACK, &r, my);+ if (rc) return rc;+ char buf[1024] __attribute__((aligned(8)));+ for (;;) {+ ssize_t n = recv(fd, buf, sizeof buf, 0);+ if (n < 0) return errno == EINTR ? -EINTR : -errno;+ if (n <= 0) return -EIO;+ size_t off = 0;+ for (const struct nlmsghdr *h; (h = next_msg(buf, (size_t)n, &off));) {+ if (h->nlmsg_seq != my || h->nlmsg_type != NLMSG_ERROR) continue;+ const struct nlmsgerr *e = (const struct nlmsgerr *)NLMSG_DATA(h);+ return h->nlmsg_len >= NLMSG_LENGTH(sizeof *e) ? e->error : -EIO;+ }+ }+}diff --git a/server/src/netperm/npdiag.h b/server/src/netperm/npdiag.hnew file mode 100644index 0000000..95b2a93--- /dev/null+++ b/server/src/netperm/npdiag.h@@ -0,0 +1,36 @@+/* npdiag.h — lord-netperm's sock_diag client (NETLINK_SOCK_DIAG): lists the machine's TCP and+ * UDP sockets with their cgroup and destroys one (SOCK_DESTROY, the kernel's tcp_abort /+ * udp_abort: the app gets ECONNABORTED and TCP sends a reset). It is how the firewall cuts the+ * connections an app opened before a rule changed (docs/FIREWALL.md, "Existing connections are+ * cut"). Netlink only: no AF_INET/AF_INET6 socket is created. Destroying needs CAP_NET_ADMIN and a+ * kernel with CONFIG_INET_DIAG_DESTROY (UDP also CONFIG_INET_UDP_DIAG); without it the packet+ * filter's drop still applies and npdiag_destroy returns -EOPNOTSUPP. */+#ifndef LORD_NP_DIAG_H+#define LORD_NP_DIAG_H++#include <stdint.h>++typedef struct {+ int fam; /* AF_INET / AF_INET6 */+ int proto; /* IPPROTO_TCP / IPPROTO_UDP */+ int state; /* the kernel's state (TCP_ESTABLISHED = 1, TCP_LISTEN = 10) */+ uint8_t src[16], dst[16]; /* local and remote address, network order (IPv4 in the first 4) */+ uint16_t sport, dport; /* local and remote port, host order */+ uint32_t ifindex;+ uint32_t cookie[2]; /* the socket's cookie, as sock_diag gives it */+ uint64_t cgid; /* its cgroup v2 id (INET_DIAG_CGROUP_ID), 0 if not reported */+} NpSock;++/* A NETLINK_SOCK_DIAG socket (close-on-exec, 500 ms receive timeout), or -1. */+int npdiag_open(void);++/* Calls cb for every socket of that family and protocol whose state bit is in states (1 << state).+ * cb returns nonzero to stop. Returns how many sockets it saw, or -errno. */+int npdiag_each(int fd, int fam, int proto, uint32_t states, int (*cb)(void *u, const NpSock *s), void *u);++/* Destroys the socket with that tuple and cookie (a different socket that reused the tuple has+ * another cookie and is left alone). 0, or -errno (-ENOENT: already gone; -EOPNOTSUPP: the kernel+ * can't). */+int npdiag_destroy(int fd, const NpSock *s);++#endifdiff --git a/server/src/netperm/npload.h b/server/src/netperm/npload.hindex b973a95..19fd189 100644--- a/server/src/netperm/npload.h+++ b/server/src/netperm/npload.h@@ -23,7 +23,8 @@X(np_counts, BPF_MAP_TYPE_HASH, 8, sizeof(NpCount), NP_MAX_APPS, 0) \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_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)/* 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@@ -47,7 +48,8 @@ static const NpProgSpec np_prog_specs[] = {{ "cgroup/sock_create", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, NULL, 0 },{ "cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE, BPF_CGROUP_DEVICE, NULL, 1 },{ "cgroup/lsm_socket_create", BPF_PROG_TYPE_LSM, BPF_LSM_CGROUP, "bpf_lsm_socket_create", 1 },- /* traffic accounting for the Monitor (bytes per app and per remote address); never denies */+ /* the packet filter: traffic accounting for the Monitor (bytes per app and per remote address)+ * and the cut of connections a rule now denies (it drops only those packets) */{ "cgroup/skb_ingress", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_INGRESS, NULL, 1 },{ "cgroup/skb_egress", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS, NULL, 1 },};diff --git a/server/src/netperm/npproto.h b/server/src/netperm/npproto.hindex 605c8fc..95acf72 100644--- a/server/src/netperm/npproto.h+++ b/server/src/netperm/npproto.h@@ -21,11 +21,13 @@* up to 4 names the resolver* gave for addr* NP_MSG_COUNTS {"desktop:x":[allowed,denied,rx,tx,rxRate,txRate,- * [["1.2.3.4",rx,tx,rxRate,txRate],...]],...}+ * [["1.2.3.4",rx,tx,rxRate,txRate,blocked],...],blocked],...}* every second, if it changed:* connections, bytes (the- * cgroup_skb accounting) and- * the busiest addresses */+ * cgroup_skb accounting), the+ * busiest addresses and the+ * bytes the filter dropped of+ * connections a rule now denies */#ifndef LORD_NP_PROTO_H#define LORD_NP_PROTO_Hdiff --git a/server/src/ui/js_fw.c b/server/src/ui/js_fw.cindex b4f5265..c2d2a7d 100644--- a/server/src/ui/js_fw.c+++ b/server/src/ui/js_fw.c@@ -21,7 +21,7 @@* addRule({ app, dir, dst, port, proto, action }) → id removeRule(id)* updateRule(id, { dir, dst, port, proto, action }) → id same app; the old rule comes back if* the new one can't be added- * poll() { counts: { app: [allowed, denied, rx, tx, rxRate, txRate, flows] },+ * poll() { counts: { app: [allowed, denied, rx, tx, rxRate, txRate, flows, blocked] },* events: [...] } (the bytes when lord-netperm's accounting is loaded,* see npproto.h)* blocks(app, restriction) true if the app is restricted in it (sdk/notify.js asks before showingdiff --git a/server/tests/netperm_cut.sh b/server/tests/netperm_cut.shnew file mode 100644index 0000000..311b823--- /dev/null+++ b/server/tests/netperm_cut.sh@@ -0,0 +1,210 @@+#!/bin/bash+# netperm_cut.sh — open connections are cut when a rule changes to deny (docs/FIREWALL.md,+# "Existing connections are cut"). Run it in the test VM as the session user (passwordless sudo),+# with lord-sessiond and lord-netperm running:+#+# bash server/tests/netperm_cut.sh [lan-host]+#+# Like netperm_integration.sh it builds an app scope by hand, writes ~/.config/lordos/firewall.json+# (restored at the end) and asks lord-netperm to reload. Inside the scope it starts long downloads+# (TCP, and QUIC with curl --http3-only when the network allows UDP 443) and an idle TCP session to+# the SSH port of lan-host, then changes the rules and measures how fast the traffic stops and what+# the app sees. Then the rule goes back to allow and a new connection must work at once.+set -u+LAN=${1:-172.17.0.2} # a LAN machine with an SSH port (only the banner is read)+TCP_URL=${TCP_URL:-https://deb.debian.org/debian/ls-lR.gz}+TCP_HOST=$(echo "$TCP_URL" | sed -E 's#https?://([^/]+)/.*#\1#')+QUIC_URL=${QUIC_URL:-"https://www.cloudflare.com/?n=[1-200]"} # any HTTP/3 site; a range is many requests on one QUIC connection+ISO_DIR=https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/+OTHER_URL=${OTHER_URL:-$ISO_DIR$(curl -s --max-time 8 "$ISO_DIR" | grep -o 'debian-[0-9.]*-amd64-netinst.iso' | head -1)}+U=$(id -u)+SESS=/sys/fs/cgroup/system.slice/lordos.service/lordos-session-$U+SC=$SESS/app-lordos-npcut.scope+FW=~/.config/lordos/firewall.json+APP=desktop:npcut+T=$(mktemp -d)+pass=0; fail=0+ok() { pass=$((pass+1)); echo " ok: $*"; }+bad() { fail=$((fail+1)); echo " BAD: $*"; }+[ -d "$SESS" ] || { echo "no LordOS session for uid $U ($SESS)"; exit 2; }+[ -S /run/lordos/netperm.sock ] || { echo "lord-netperm is not running"; exit 2; }+mkdir -p ~/.config/lordos+HAD=0+[ -f "$FW" ] && { cp "$FW" "$T/fw.backup"; HAD=1; }+# ends everything still running in the scope (cgroup.kill) and the background jobs+stopall() { echo 1 | sudo tee "$SC/cgroup.kill" >/dev/null 2>&1; jobs -p | xargs -r kill 2>/dev/null; wait 2>/dev/null; }+cleanup() {+ stopall+ if [ $HAD = 1 ]; then cp "$T/fw.backup" "$FW"; else rm -f "$FW"; fi+ reload+ sleep 0.3+ sudo rmdir "$SC" 2>/dev/null+ rm -rf "$T"+}+trap cleanup EXIT+sudo mkdir -p "$SC"++reload() {+ python3 -c 'import socket,struct,time;s=socket.socket(socket.AF_UNIX);s.connect("/run/lordos/netperm.sock");s.sendall(struct.pack("<IB",1,1));time.sleep(0.05);s.close()'+}+rules() { echo "$1" > "$FW"; reload; }+R0='{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[]}'+DENY='{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$APP'","dir":"out","dst":"any","action":"deny","when":0}]}'+ALLOW='{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$APP'","dir":"out","dst":"any","action":"allow","when":0}]}'++inscope() { sudo bash -c "echo \$\$ > $SC/cgroup.procs; exec setpriv --reuid=$U --regid=$(id -g) --init-groups \"\$@\"" inscope "$@"; }+now() { date +%s.%N; }+sub() { awk -v a="$1" -v b="$2" 'BEGIN { printf "%.2f", a - b }'; }+size() { stat -c %s "$1" 2>/dev/null || echo 0; }++# dl <name> <url> [curl args]: a rate-limited download in the scope, in the background (to stdout,+# so a URL range like "?n=[1-500]" is many requests on one connection)+dl() {+ local n=$1 url=$2; shift 2+ rm -f "$T/$n.data" "$T/$n.rc" "$T/$n.end" "$T/$n.err"+ ( inscope curl -4 -sSL --limit-rate 800k --max-time 120 "$@" "$url" > "$T/$n.data" 2> "$T/$n.err"; echo $? > "$T/$n.rc"; now > "$T/$n.end" ) &+}+# grows <name>: did the file grow over 1 s?+grows() { local a b; a=$(size "$T/$1.data"); sleep 1; b=$(size "$T/$1.data"); [ "$b" -gt "$a" ]; }+# stopped <name> <since>: the download ended (or stopped growing) after the rule changed+stopped() {+ local n=$1 t0=$2 i+ for i in $(seq 1 40); do [ -f "$T/$n.rc" ] && break; sleep 0.1; done+ if [ -f "$T/$n.rc" ]; then+ printf '%s %.2f s, curl exit %s: %s' "ended after" "$(sub "$(cat "$T/$n.end")" "$t0")" "$(cat "$T/$n.rc")" "$(tail -1 "$T/$n.err")"+ return 0+ fi+ local a b; a=$(size "$T/$n.data"); sleep 1; b=$(size "$T/$n.data")+ [ "$a" = "$b" ] && { printf 'no bytes for 1 s (still waiting)'; return 0; }+ printf 'still receiving'; return 1+}+# idle <name> <host> <port>: a TCP session that reads the banner and then waits; prints what recv got+idle() {+ local n=$1+ rm -f "$T/$n.out"+ ( inscope python3 -c '+import socket, sys, time, errno+s = socket.create_connection((sys.argv[1], int(sys.argv[2])), timeout=5); s.recv(64)+print("open", flush=True); s.settimeout(12)+try:+ d = s.recv(64); r = "eof" if not d else "data"+except socket.timeout: r = "timeout"+except OSError as e: r = errno.errorcode.get(e.errno, str(e.errno))+print(r, "%.3f" % time.time(), flush=True)' "$2" "$3" > "$T/$n.out" 2>&1 ) &+}+idle_wait() { local i; for i in $(seq 1 50); do grep -q open "$T/$1.out" 2>/dev/null && return 0; sleep 0.1; done; return 1; }+idle_result() { local i; for i in $(seq 1 ${2:-30}); do [ "$(wc -l < "$T/$1.out")" -ge 2 ] && break; sleep 0.1; done; sed -n 2p "$T/$1.out"; }++counts() { # allowed denied blocked of the app, from one COUNTS message+ python3 - "$APP" <<'PY'+import socket, struct, sys, json, time+s = socket.socket(socket.AF_UNIX); s.connect("/run/lordos/netperm.sock"); s.settimeout(3)+buf = b""; end = time.time() + 3+while time.time() < end:+ try: d = s.recv(1 << 20)+ except socket.timeout: break+ if not d: break+ buf += d+ while len(buf) >= 5:+ l, t = struct.unpack("<IB", buf[:5])+ if len(buf) < 4 + l: break+ body = buf[5:4 + l]; buf = buf[4 + l:]+ if t == 12:+ v = json.loads(body).get(sys.argv[1])+ if v: print(v[0], v[1], v[7] if len(v) > 7 else "-"); sys.exit(0)+print("- - -")+PY+}++rules "$R0"++echo "1. app-wide deny cuts a TCP download and an idle TCP session"+dl tcp "$TCP_URL"+idle ssh "$LAN" 22+sleep 2.5+grows tcp && ok "the TCP download runs" || bad "the TCP download doesn't run ($(cat "$T/tcp.err"))"+idle_wait ssh && ok "the idle session to $LAN:22 is open" || bad "no session to $LAN:22 ($(cat "$T/ssh.out"))"+t0=$(now); rules "$DENY"+r=$(stopped tcp "$t0") && ok "TCP download: $r" || bad "TCP download: $r"+grep -q "Software caused connection abort\|reset\|Recv failure" "$T/tcp.err" && ok "curl saw the connection end (not a stall)" || bad "curl: $(cat "$T/tcp.err")"+res=$(idle_result ssh)+case "$res" in ECONNABORTED*|ECONNRESET*) ok "idle session: $res (cut after $(sub "${res#* }" "$t0") s)";; *) bad "idle session: $res";; esac+read -r al de bl < <(counts)+[ "$de" != "-" ] && [ "$de" -ge 1 ] && ok "counters: allowed $al, denied $de, blocked $bl bytes" || bad "counters: $al $de $bl"+t1=$(now); rules "$ALLOW"+inscope curl -4 -sS -o /dev/null --max-time 8 -w '%{http_code}' "https://$TCP_HOST/" > "$T/again" 2>&1+grep -q '^[23]' "$T/again" && ok "allowed again: a new connection works at once ($(cat "$T/again"), $(sub "$(now)" "$t1") s)" || bad "allowed again: $(cat "$T/again")"++echo "2. QUIC (HTTP/3, connected UDP)"+rules "$R0"+dl quic "$QUIC_URL" --http3-only+sleep 3+if grows quic; then+ ok "the HTTP/3 download runs ($(ss -Huanp 2>/dev/null | grep -c ':443') UDP sockets to :443)"+ t0=$(now); rules "$DENY"+ r=$(stopped quic "$t0") && ok "HTTP/3 download: $r" || bad "HTTP/3 download: $r"+ rules "$ALLOW"+ inscope curl -4 -sS --http3-only -o /dev/null --max-time 8 -w '%{http_version}' "${QUIC_URL%%\?*}" > "$T/again" 2>&1+ grep -q '^3' "$T/again" && ok "allowed again: a new HTTP/3 request works" || bad "allowed again over HTTP/3: $(cat "$T/again")"+else+ echo " skipped: no HTTP/3 here ($(tail -1 "$T/quic.err"))"+fi+stopall++echo "3. a domain rule cuts only that destination"+rules "$R0"+dl tcp "$TCP_URL"+dl other "$OTHER_URL"+sleep 3+grows tcp && grows other && ok "both downloads run" || bad "the downloads don't run"+t0=$(now)+rules '{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$APP'","dir":"out","dst":"'$TCP_HOST'","action":"deny","when":0}]}'+r=$(stopped tcp "$t0") && ok "$TCP_HOST: $r" || bad "$TCP_HOST: $r"+[ ! -f "$T/other.rc" ] && grows other && ok "the other destination keeps downloading" || bad "the other destination stopped too ($(cat "$T/other.err"))"+stopall++echo "4. \"no internet\" cuts the internet, not the LAN"+rules "$R0"+dl tcp "$TCP_URL"+idle ssh "$LAN" 22+sleep 2.5+idle_wait ssh || bad "no session to $LAN:22"+t0=$(now)+rules '{"v":1,"mode":"silent","trusted":[],"restricted":{"'$APP'":["no-internet"]},"rules":[]}'+r=$(stopped tcp "$t0") && ok "internet download: $r" || bad "internet download: $r"+res=$(idle_result ssh 20)+[ -z "$res" ] && ok "the LAN session stays open" || bad "the LAN session ended: $res"+stopall++echo "5. a server in the scope keeps its LAN clients when outgoing internet is denied"+if [ -n "${CLIENT_SSH:-}" ]; then # e.g. CLIENT_SSH="ssh -i key [email protected]": a LAN machine runs the client+ rules "$R0"+ ( inscope python3 -c '+import socket+s = socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1); s.bind(("0.0.0.0", 18093)); s.listen()+c, _ = s.accept(); c.settimeout(8); n = 0+try:+ while True:+ d = c.recv(65536)+ if not d: break+ n += len(d)+except OSError: pass+print(n, flush=True)' > "$T/srv.out" 2>&1 ) &+ sleep 1+ IP=$(ip -4 -o addr show scope global | awk 'NR==1{split($4,a,"/");print a[1]}')+ $CLIENT_SSH "python3 -c 'import socket,time+c=socket.create_connection((\"$IP\",18093)); end=time.time()+4+while time.time()<end: c.sendall(b\"x\"*1000); time.sleep(0.01)+c.close()'" &+ sleep 1+ rules '{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$APP'","dir":"out","dst":"any","action":"deny","when":0}]}'+ for i in $(seq 1 120); do [ -s "$T/srv.out" ] && break; sleep 0.1; done+ n=$(cat "$T/srv.out" 2>/dev/null)+ [ "${n:-0}" -gt 300000 ] && ok "the server received ${n} bytes (4 s, the rule changed after 1 s)" || bad "the server got ${n:-nothing}"+ stopall+else+ echo " skipped: set CLIENT_SSH to a command that runs python3 on a LAN machine"+fi++echo "netperm-cut: $pass passed, $fail failed"+[ $fail = 0 ]diff --git a/tools/iso/rootfs/etc/systemd/system/lord-netperm.service b/tools/iso/rootfs/etc/systemd/system/lord-netperm.serviceindex eb687a3..5639752 100644--- a/tools/iso/rootfs/etc/systemd/system/lord-netperm.service+++ b/tools/iso/rootfs/etc/systemd/system/lord-netperm.service@@ -14,7 +14,7 @@ RuntimeDirectory=lordosRuntimeDirectoryPreserve=yesCapabilityBoundingSet=CAP_BPF CAP_NET_ADMIN CAP_PERFMON CAP_DAC_READ_SEARCH CAP_NET_RAWNoNewPrivileges=yes-RestrictAddressFamilies=AF_UNIX AF_PACKET+RestrictAddressFamilies=AF_UNIX AF_PACKET AF_NETLINKProtectSystem=strictProtectHome=read-onlyProtectControlGroups=yesdiff --git a/tools/rpi/build-kernel.sh b/tools/rpi/build-kernel.shindex 5e40b34..8f79748 100644--- a/tools/rpi/build-kernel.sh+++ b/tools/rpi/build-kernel.sh@@ -103,6 +103,11 @@ for FL in $FLAVOURS; do--enable SECCOMP --enable SECCOMP_FILTER \--enable CGROUPS --enable MEMCG --enable CGROUP_PIDS --enable CGROUP_SCHED \--enable BPF_SYSCALL --enable BPF_JIT --enable CGROUP_BPF+ # sock_diag SOCK_DESTROY (0.8.3): when a rule changes to deny, lord-netperm destroys the app's open+ # TCP connections and connected UDP sockets so the app sees them end instead of a stall+ # (docs/FIREWALL.md, "Existing connections are cut"); Raspberry Pi's defconfig leaves it off+ "$SRC/scripts/config" --file "$B/.config" \+ --module INET_DIAG --module INET_TCP_DIAG --module INET_UDP_DIAG --enable INET_DIAG_DESTROY# BPF LSM + BTF (0.8.1): lord-netperm's per-cgroup LSM program on socket_create (the per-app# "No Bluetooth" of Linux apps, docs/FIREWALL.md) needs the bpf LSM and /sys/kernel/btf/vmlinux# (its attach point is a BTF id). BTF is generated by pahole from the DWARF debug info; the debug@@ -119,7 +124,7 @@ for FL in $FLAVOURS; do--enable DEBUG_INFO_BTF_MODULES# shellcheck disable=SC2086make -s -C "$SRC" O="$B" ARCH=arm64 $CROSS olddefconfig- for o in SECURITY_LANDLOCK SECURITY_YAMA SECCOMP_FILTER CGROUP_BPF BPF_SYSCALL BPF_LSM DEBUG_INFO_BTF; do+ for o in SECURITY_LANDLOCK SECURITY_YAMA SECCOMP_FILTER CGROUP_BPF BPF_SYSCALL BPF_LSM DEBUG_INFO_BTF INET_DIAG_DESTROY; dogrep -q "^CONFIG_$o=y" "$B/.config" || { echo "build-kernel.sh: CONFIG_$o did not stick" >&2; exit 1; }donegrep -q '^CONFIG_LSM="landlock' "$B/.config" || { echo "build-kernel.sh: CONFIG_LSM did not stick" >&2; exit 1; }