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
Lighter blocklists and menu bar item
Federico Pereiracbd9de8c0007
diff --git a/apps/monitor/NetTree.jsx b/apps/monitor/NetTree.jsxindex f90a59c..220ecce 100644--- a/apps/monitor/NetTree.jsx+++ b/apps/monitor/NetTree.jsx@@ -108,21 +108,26 @@ export function ViewButton({ view, setView, extra, tr }) {</View>{open && (<Overlay anchor={btn}>- <View ref={menu} style={{ position: 'absolute', top: 28, [direction() === 'rtl' ? 'right' : 'left']: 0, width: 236, paddingVertical: 5, paddingHorizontal: 5,+ <View ref={menu} style={{ position: 'absolute', top: 28, [direction() === 'rtl' ? 'right' : 'left']: 0, width: 420, paddingVertical: 6, paddingHorizontal: 5,borderRadius: t.menuRadius, backgroundColor: t.popupBg || (t.mode === 'dark' ? '#2c2c31' : '#ffffff'), border: `1px solid ${t.separator}`,boxShadow: '0 10px 30px rgba(0,0,0,0.35)' }}>- {SECTIONS.map(([key, values], si) => (- <View key={key}>- {si ? <View style={{ height: 1, marginVertical: 4, marginHorizontal: 6, backgroundColor: t.separator }} /> : null}- <Text style={{ fontSize: 10, fontWeight: 600, color: t.textDim, paddingHorizontal: 8, paddingVertical: 3 }}>{tr(`monitor.net.view.${key}`)}</Text>- {values.map((v) => <MenuItem key={v} label={tr(`monitor.net.view.${key}.${v}`)} checked={view[key] === v} onClick={() => setView({ [key]: v })} />)}- </View>- ))}+ {/* two columns: what the list is (grouping, order) and what it shows (direction, protocol, status) */}+ <View style={{ flexDirection: 'row', gap: 4 }}>+ {[['group', 'sort'], ['dir', 'proto', 'status']].map((col, ci) => (+ <View key={ci} style={{ flex: 1, minWidth: 0 }}>+ {col.map((key, si) => (+ <View key={key}>+ {si ? <View style={{ height: 1, marginVertical: 3, marginHorizontal: 6, backgroundColor: t.separator }} /> : null}+ <Text style={{ fontSize: 10, fontWeight: 600, color: t.textDim, paddingHorizontal: 8, paddingVertical: 2 }}>{tr(`monitor.net.view.${key}`)}</Text>+ {SECTIONS.find(([k]) => k === key)[1].map((v) => <MenuItem key={v} label={tr(`monitor.net.view.${key}.${v}`)} checked={view[key] === v} onClick={() => setView({ [key]: v })} />)}+ </View>+ ))}+ </View>+ ))}+ </View>+ {(extra || []).length ? <View style={{ height: 1, marginVertical: 4, marginHorizontal: 6, backgroundColor: t.separator }} /> : null}{(extra || []).map((it) => (- <View key={it.label}>- <View style={{ height: 1, marginVertical: 4, marginHorizontal: 6, backgroundColor: t.separator }} />- <MenuItem label={it.label} checked={it.checked} onClick={() => { setOpen(false); it.onClick(); }} />- </View>+ <MenuItem key={it.label} label={it.label} checked={it.checked} onClick={() => { setOpen(false); it.onClick(); }} />))}</View></Overlay>@@ -137,7 +142,7 @@ function MenuItem({ label, checked, onClick }) {const on = hover && t.menuHighlight !== 'subtle';return (<View onClick={onClick} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}- style={{ height: 22, paddingHorizontal: 8, borderRadius: t.menuItemRadius, flexDirection: 'row', alignItems: 'center', gap: 6,+ style={{ height: 21, paddingHorizontal: 8, borderRadius: t.menuItemRadius, flexDirection: 'row', alignItems: 'center', gap: 6,backgroundColor: on ? t.accent : hover ? t.hover : 'transparent' }}><Text style={{ width: 12, fontSize: 11, color: on ? '#fff' : t.text }}>{checked ? '✓' : ''}</Text><Text style={{ flex: 1, fontSize: 12, whiteSpace: 'nowrap', color: on ? '#fff' : t.text, textAlign: 'start' }}>{label}</Text>@@ -233,17 +238,17 @@ export function Child({ node, depth, path, ctx }) {const destName = node.kind === 'domain' ? node.name : null;const destAddr = node.addr || node.flows[0]?.addr;const canSwitch = appOf && (node.kind === 'domain' || node.kind === 'ip') && !ctx.isSystem(appOf);- const inset = 14 + depth * 14;+ const inset = depth ? 14 + depth * 14 : 4;return (<View>- <View onClick={() => ctx.onPick(node, appOf)} style={{ flexDirection: 'row', alignItems: 'center', gap: 5, minHeight: 22, borderRadius: 5,+ <View onClick={() => ctx.onPick(node, appOf)} style={{ flexDirection: 'row', alignItems: 'center', gap: 5, minHeight: depth ? 22 : 32, borderRadius: 5,...(direction() === 'rtl' ? { paddingRight: inset } : { paddingLeft: inset }), backgroundColor: sel ? (t.mode === 'dark' ? 'rgba(255,255,255,0.06)' : 'rgba(0,0,0,0.05)') : 'transparent' }}><View onClick={(e) => { if (!kids.length) return; e.stopPropagation(); ctx.toggle(key); }} style={{ width: 10, alignItems: 'center' }}><Text style={{ fontSize: 8, color: t.textDim }}>{kids.length ? (open ? '▾' : direction() === 'rtl' ? '◂' : '▸') : ''}</Text></View>{node.kind === 'app' ? <AppGlyph item={ctx.itemOf(node)} size={13} /> : <View style={{ width: 6, height: 6, borderRadius: 3, backgroundColor: dot }} />}<View style={{ flex: 1, minWidth: 0 }}>- <Text style={{ fontSize: 11, color: t.text, whiteSpace: 'nowrap', overflow: 'hidden', fontFamily: node.kind === 'app' || node.kind === 'country' ? undefined : 'monospace',+ <Text style={{ fontSize: depth ? 11 : 12, fontWeight: depth ? 400 : 600, color: t.text, whiteSpace: 'nowrap', overflow: 'hidden', fontFamily: node.kind === 'app' || node.kind === 'country' ? undefined : 'monospace',direction: node.kind === 'app' || node.kind === 'country' ? undefined : 'ltr', textAlign: 'start' }}>{label}{node.kind === 'domain' && new Set(node.flows.map((f) => f.addr)).size > 1 ? ` (${new Set(node.flows.map((f) => f.addr)).size})` : ''}</Text>diff --git a/apps/monitor/Network.jsx b/apps/monitor/Network.jsxindex b8126ce..b4f9a16 100644--- a/apps/monitor/Network.jsx+++ b/apps/monitor/Network.jsx@@ -42,14 +42,23 @@ const placeOf = (tr, h) => (h.cc ? countryName(h.cc) : h.scope ? tr(`monitor.net// Names the system resolver gave for each address, if lord-netperm's notices carry them ("host" or// "hosts"). The names belong to the system, not to one app (docs/FIREWALL.md, "Where the names come// from"), so one map serves every app. Without them a row offers a rule for its address instead.+// (0.8.4) An address the resolver reached through a CNAME chain has several names (deb.debian.org →+// debian.map.fastlydns.net): the row shows the shortest, usually the one the app asked for, and+// allNamesOf keeps them all for the search chips and the blocklists.+const allNames = new Map();function namesOf(events) {const out = new Map();for (const e of events || []) {- const n = Array.isArray(e.hosts) ? e.hosts[0] : e.host;- if (e.addr && typeof n === 'string' && n) out.set(e.addr, n);+ const list = (Array.isArray(e.hosts) ? e.hosts : e.host ? [e.host] : []).filter((n) => typeof n === 'string' && n);+ if (!e.addr || !list.length) continue;+ const best = [...list].sort((a, b) => a.split('.').length - b.split('.').length || a.length - b.length)[0];+ out.set(e.addr, best);+ if (allNames.size > 4096 && !allNames.has(e.addr)) allNames.delete(allNames.keys().next().value);+ allNames.set(e.addr, list);}return out;}+const allNamesOf = (addr, name) => allNames.get(addr) || (name ? [name] : []);// The sheet's choices for a name: the exact name and, a level up, its parent with every subdomain.function nameChoices(name) {@@ -435,6 +444,12 @@ function Summary({ groups, conns, last, tr, fw, names, range, sysHist, launcher,hosts.sort((a, b) => b.sent + b.recv - (a.sent + a.recv));if (hosts.some((h) => h.sent + h.recv)) hosts.splice(0, hosts.length, ...hosts.filter((h) => h.sent + h.recv)); // with traffic, only those that moved bytesconst denied = (fw?.events || []).filter((e) => e.v === 'deny' && e.dir === 'out' && (!e.at || Date.now() - e.at < 10 * 60 * 1000));+ const byDst = new Map(); // the latest of each (app, destination), with how many+ for (const e of denied) {+ const k = `${e.app}|${(e.hosts && e.hosts[0]) || e.addr}`;+ byDst.set(k, { e, n: (byDst.get(k)?.n || 0) + 1 });+ }+ const deniedRecent = [...byDst.values()].slice(-4).reverse();return (<><Section id="traffic" title={tr('monitor.net.traffic')}>@@ -486,9 +501,10 @@ function Summary({ groups, conns, last, tr, fw, names, range, sysHist, launcher,</Section>) : null}<Section id="denied" title={tr('monitor.net.recentlyDenied')}>- {denied.length ? denied.slice(-4).reverse().map((e, i) => (+ {denied.length ? deniedRecent.map(({ e, n }, i) => (<View key={i} style={{ gap: 1 }}>- <Text style={{ fontSize: 11, color: t.text, fontFamily: 'monospace', direction: 'ltr', textAlign: 'start', lineHeight: 1.3 }} numberOfLines={2}>{(e.hosts && e.hosts[0]) || e.addr || '?'}</Text>+ <Text style={{ fontSize: 11, color: t.text, fontFamily: 'monospace', direction: 'ltr', textAlign: 'start', lineHeight: 1.3 }} numberOfLines={2}>+ {`${(e.hosts && e.hosts[0]) || e.addr || '?'}${n > 1 ? ` ×${n}` : ''}`}</Text><Text style={{ fontSize: 10, color: t.textDim, lineHeight: 1.3, textAlign: 'start' }} numberOfLines={2}>{`${labelOf(tr, groups.find((g) => g.key === e.app) || { key: e.app, name: e.app })} · ${tr(`monitor.net.by.${e.by === 'list' || e.by === 'restricted' || e.by === 'mode' ? e.by : 'rule'}`)}`}</Text>@@ -672,9 +688,9 @@ function seen(key, active) {function flowsOf(apps, names, tr) {const out = [];const listedCache = new Map();- const listed = (name, addr) => {- const k = `${name || ''}|${addr}`;- if (!listedCache.has(k)) listedCache.set(k, firewall.listMatch(name ? [name] : [], addr));+ const listed = (list, addr) => {+ const k = `${list.join(',')}|${addr}`;+ if (!listedCache.has(k)) listedCache.set(k, firewall.listMatch(list, addr));return listedCache.get(k);};for (const g of apps) {@@ -684,7 +700,8 @@ function flowsOf(apps, names, tr) {if (!h.remote) continue;any = true;const name = names.get(h.remote) || null;- const common = { ...base, name, addr: h.remote, cc: h.cc || '', scope: h.scope || '', denied: h.denied ? 1 : 0, blocked: h.blocked || 0, listed: listed(name, h.remote) };+ const all = allNamesOf(h.remote, name);+ const common = { ...base, name, names: all, addr: h.remote, cc: h.cc || '', scope: h.scope || '', denied: h.denied ? 1 : 0, blocked: h.blocked || 0, listed: listed(all, h.remote) };const ps = (h.portStats || []).filter((p) => Array.isArray(p) && p.length >= 7);if (ps.length) {const share = Math.max(1, ps.length);diff --git a/sdk/netmodel.js b/sdk/netmodel.jsindex b8d0ba6..1487d3e 100644--- a/sdk/netmodel.js+++ b/sdk/netmodel.js@@ -4,7 +4,8 @@// The input is a flat list of flows, one per (app, destination, port):// { app, appName, name, addr, port, proto: 'tcp'|'udp'|'other', dir: 'out'|'in', rx, tx, rateRx, rateTx,// cc, scope, conns, denied, last, listed, system }-// name: the name the system resolver gave for addr (or null); last: ms of the last activity;+// name: the name the system resolver gave for addr (or null), names: all of them (a CNAME chain);+// last: ms of the last activity;// listed: the id of a subscribed blocklist that covers it (or null); system: an essential system// service (name resolution, time, the network service), shown but never blockable.//@@ -154,7 +155,10 @@ const lc = (s) => String(s || '').toLowerCase();function chipMatches(f, c, countryName) {switch (c.type) {case 'app': return lc(f.appName).includes(lc(c.value)) || lc(f.app).includes(lc(c.value));- case 'domain': { const n = lc(f.name), v = lc(c.value); return !!n && (n === v || n.endsWith(`.${v}`) || (!v.includes('.') && n.includes(v))); }+ case 'domain': {+ const v = lc(c.value);+ return (f.names && f.names.length ? f.names : [f.name]).some((x) => { const n = lc(x); return !!n && (n === v || n.endsWith(`.${v}`) || (!v.includes('.') && n.includes(v))); });+ }case 'ip': { const x = parseCidr(c.value); return !!f.addr && (x ? cidrContains(x, f.addr) : lc(f.addr).startsWith(lc(c.value))); }case 'port': return Number(f.port) === Number(c.value);case 'proto': return f.proto === c.value;@@ -169,7 +173,7 @@ export function matchesSearch(f, { chips = [], rest = '' } = {}, countryName) {for (const c of chips) byType.set(c.type, [...(byType.get(c.type) || []), c]);for (const list of byType.values()) if (!list.some((c) => chipMatches(f, c, countryName))) return false;if (rest) {- const hay = lc([f.appName, f.app, f.name, f.addr, f.port, f.proto, f.cc, countryName?.(f.cc)].join(' '));+ const hay = lc([f.appName, f.app, f.name, ...(f.names || []), f.addr, f.port, f.proto, f.cc, countryName?.(f.cc)].join(' '));for (const w of lc(rest).split(/\s+/)) if (w && !hay.includes(w)) return false;}return true;diff --git a/sdk/netstatus.jsx b/sdk/netstatus.jsxindex a63580e..7d9cdec 100644--- a/sdk/netstatus.jsx+++ b/sdk/netstatus.jsx@@ -26,8 +26,12 @@ import { useLauncherApps } from './apps.js';import { COMPONENTS } from './netmodel.js';const SAMPLES = 300; // one per second: the last five minutes-export const netStatus = createStore({ rx: 0, tx: 0, hist: [] }); // hist: [[rxRate, txRate], …]-let timer = null, prev = null;+// What the bar shows ({ rx, tx } as short text): it changes only when the rounded rates do, so an+// idle network redraws nothing. The rates and the five minutes behind them stay out of any store;+// the panel, while open, reads them once per second.+export const netStatus = createStore({ rx: '0', tx: '0' });+const live = { rx: 0, tx: 0, hist: [] }; // hist: [[rxRate, txRate], …]+let timer = null, prev = null, panelOpen = false, ticks = 0;const shown = () => settings.get()?.monitor?.menuItem === true;@@ -36,28 +40,37 @@ function tick() {try { s = typeof lord !== 'undefined' && lord.monitor?.netTotals ? lord.monitor.netTotals() : null; } catch (e) { s = null; }if (s && prev) {const dt = Math.max(0.2, (s.t - prev.t) / 1000);- const rx = Math.max(0, (s.rx - prev.rx) / dt), tx = Math.max(0, (s.tx - prev.tx) / dt);- netStatus.set((o) => ({ rx, tx, hist: [...o.hist.slice(o.hist.length >= SAMPLES ? 1 : 0), [rx, tx]] }));+ live.rx = Math.max(0, (s.rx - prev.rx) / dt);+ live.tx = Math.max(0, (s.tx - prev.tx) / dt);+ live.hist.push([live.rx, live.tx]);+ if (live.hist.length > SAMPLES) live.hist.shift();+ // the bar's text every other second (the panel's numbers every second)+ if (++ticks % 2 === 0) {+ const o = netStatus.get(), n = live.hist.slice(-2), rx = short(n.reduce((a, p) => a + p[0], 0) / n.length),+ tx = short(n.reduce((a, p) => a + p[1], 0) / n.length);+ if (o.rx !== rx || o.tx !== tx) netStatus.set({ rx, tx });+ }}if (s) prev = s;- firewall.refresh();+ if (panelOpen) firewall.refresh(); // lord-netperm's notices only while the panel shows them}function sync() {const on = shown();if (on && !timer) { prev = null; tick(); timer = setInterval(tick, 1000); }- else if (!on && timer) { clearInterval(timer); timer = null; prev = null; netStatus.set({ rx: 0, tx: 0, hist: [] }); }+ else if (!on && timer) { clearInterval(timer); timer = null; prev = null; live.hist = []; netStatus.set({ rx: '0', tx: '0' }); }}const rate = (n) => t('monitor.rate', { v: formatBytes(Math.max(0, n || 0)) });-// a compact rate for the bar: "1.2M", "340K", "12"+// a compact rate for the bar: "1.2M", "340K", "0" (under 1 KB/s: background chatter); changing the+// text relays out the bar, so it changes only when this rounded value doesfunction short(n) {const v = Math.max(0, n || 0);if (v >= 1e9) return `${(v / 1e9).toFixed(1)}G`;if (v >= 1e6) return `${(v / 1e6).toFixed(v >= 1e7 ? 0 : 1)}M`;if (v >= 1e3) return `${Math.round(v / 1e3)}K`;- return `${Math.round(v)}`;+ return '0';}// ---- the panel ----@@ -98,7 +111,14 @@ function appLabel(id, launcher) {function Panel({ onClose }) {const th = useTheme();- const st = useStore(netStatus);+ const [, setTick] = useState(0);+ useEffect(() => {+ panelOpen = true;+ firewall.refresh();+ const id = setInterval(() => setTick((x) => x + 1), 1000);+ return () => { panelOpen = false; clearInterval(id); };+ }, []);+ const st = live;const fw = useStore(firewallState);const launcher = useLauncherApps();const mode = firewall.state().mode || 'silent';@@ -108,6 +128,12 @@ function Panel({ onClose }) {.sort((a, b) => b[1][4] + b[1][5] - (a[1][4] + a[1][5])).slice(0, 5);const now = Date.now();const denied = (fw.events || []).filter((e) => e.v === 'deny' && e.at && now - e.at < 10 * 60 * 1000);+ const seenDst = new Map(); // the latest of each (app, destination), with how many+ for (const e of denied) {+ const k = `${e.app}|${(e.hosts && e.hosts[0]) || e.addr}`;+ seenDst.set(k, { e, n: (seenDst.get(k)?.n || 0) + 1 });+ }+ const recent = [...seenDst.values()].slice(-3).reverse();const label = (k) => <Text style={{ fontSize: 11, fontWeight: 600, color: th.textDim, textAlign: 'start' }}>{k}</Text>;const row = (k, v, color) => (<View style={{ flexDirection: 'row', gap: 8 }}>@@ -138,8 +164,8 @@ function Panel({ onClose }) {<Text style={{ fontSize: 10, fontWeight: 700, color: denied.length ? '#ff453a' : th.textDim }}>{formatNumber(denied.length, 0)}</Text></View></View>- {denied.slice(-3).reverse().map((e, i) => (- <View key={i}>{row((e.hosts && e.hosts[0]) || e.addr || '?', appLabel(e.app, launcher))}</View>+ {recent.map(({ e, n }, i) => (+ <View key={i}>{row(`${(e.hosts && e.hosts[0]) || e.addr || '?'}${n > 1 ? ` ×${n}` : ''}`, appLabel(e.app, launcher))}</View>))}<View style={{ height: 1, backgroundColor: th.separator }} />{label(t('monitor.status.mode'))}@@ -179,8 +205,8 @@ function NetStatusItem({ color, height }) {return (<View ref={btn} onClick={() => setOpen((o) => !o)} style={{ height, paddingHorizontal: 6, justifyContent: 'center', direction: 'ltr' }}><View style={{ width: 44 }}>- <Text style={{ fontSize: 9, lineHeight: 1.05, color, fontFamily: 'monospace', textAlign: 'end' }}>{`↑${short(st.tx)}`}</Text>- <Text style={{ fontSize: 9, lineHeight: 1.05, color, fontFamily: 'monospace', textAlign: 'end' }}>{`↓${short(st.rx)}`}</Text>+ <Text style={{ fontSize: 9, lineHeight: 1.05, color, fontFamily: 'monospace', textAlign: 'end' }}>{`↑${st.tx}`}</Text>+ <Text style={{ fontSize: 9, lineHeight: 1.05, color, fontFamily: 'monospace', textAlign: 'end' }}>{`↓${st.rx}`}</Text></View>{open && (<Overlay anchor={btn}>diff --git a/server/src/fw/fwlists.c b/server/src/fw/fwlists.cindex a0979d4..729b691 100644--- a/server/src/fw/fwlists.c+++ b/server/src/fw/fwlists.c@@ -8,25 +8,32 @@#include <stdlib.h>#include <string.h>+/* The set keeps no names: a name is its 64-bit hash (fw_hash, seeded per process, so nobody can+ * craft a name that collides on purpose), sorted for binary search, with the tag of the list it+ * came from. A list of 380,000 names takes about 4 MB instead of 50. Networks are kept whole. */+typedef struct { uint64_t h; uint32_t seq; uint16_t tag; uint16_t pad; } NameEnt;+typedef struct { unsigned char k[18]; uint16_t tag; uint32_t seq; } NetEnt;+struct FwLists {- FwMap exact, sub, nets; /* name → tag; name → tag; (fam, prefix, network) → tag */+ NameEnt *ex, *sub; /* exact names; names with every subdomain */+ size_t nex, nsub, capex, capsub;+ NetEnt *net;+ size_t nnet, capnet;+ uint32_t seq;+ int sorted; /* 0: entries were added since the last sort */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->ex);+ free(l->sub);+ free(l->net);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;@@ -35,7 +42,7 @@ FwlFormat fwl_format_from_name(const char *n) {return 0;}-/* ---- the set --------------------------------------------------------------------------------- */+/* ---- the set ---- */static size_t net_key(int fam, const uint8_t *addr, int prefix, unsigned char *k) {memset(k, 0, 18);@@ -47,14 +54,58 @@ static size_t net_key(int fam, const uint8_t *addr, int prefix, unsigned char *kunsigned char m = bits >= 8 ? 0xff : bits <= 0 ? 0 : (unsigned char)(0xff << (8 - bits));k[2 + i] = addr[i] & m;}- return (size_t)(2 + bytes);+ return 18;+}++static int name_cmp(const void *a, const void *b) {+ const NameEnt *x = a, *y = b;+ if (x->h != y->h) return x->h < y->h ? -1 : 1;+ return x->seq < y->seq ? -1 : x->seq > y->seq;+}++static int net_cmp(const void *a, const void *b) {+ const NetEnt *x = a, *y = b;+ int c = memcmp(x->k, y->k, 18);+ if (c) return c;+ return x->seq < y->seq ? -1 : x->seq > y->seq;+}++/* Sorts and drops repeated entries (the first one added keeps its tag). Returns how many went. */+static size_t uniq_names(NameEnt *v, size_t *n) {+ if (*n < 2) return 0;+ qsort(v, *n, sizeof *v, name_cmp);+ size_t w = 1;+ for (size_t i = 1; i < *n; i++) if (v[i].h != v[w - 1].h) v[w++] = v[i];+ size_t gone = *n - w;+ *n = w;+ return gone;}-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;+static size_t finish(FwLists *l) {+ if (!l || l->sorted) return 0;+ size_t gone = uniq_names(l->ex, &l->nex) + uniq_names(l->sub, &l->nsub);+ if (l->nnet > 1) {+ qsort(l->net, l->nnet, sizeof *l->net, net_cmp);+ size_t w = 1;+ for (size_t i = 1; i < l->nnet; i++) if (memcmp(l->net[i].k, l->net[w - 1].k, 18)) l->net[w++] = l->net[i];+ gone += l->nnet - w;+ l->nnet = w;+ }+ l->sorted = 1;+ return gone;+}++size_t fwl_finish(FwLists *l) { return finish(l); }+size_t fwl_domains(const FwLists *l) { if (!l) return 0; finish((FwLists *)l); return l->nex + l->nsub; }+size_t fwl_nets(const FwLists *l) { if (!l) return 0; finish((FwLists *)l); return l->nnet; }++static int grow(void **v, size_t *cap, size_t n, size_t sz) {+ if (n < *cap) return 0;+ size_t c = *cap ? *cap * 2 : 1024;+ void *p = realloc(*v, c * sz);+ if (!p) return -1;+ *v = p;+ *cap = c;return 0;}@@ -85,33 +136,52 @@ static int net_usable(const FwRule *r) {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;+/* What a line is: 'd'/'s' with the name in name, 'n' with the network in r, or 0 if invalid. */+static int check_line(const char *line, size_t len, char *name, FwRule *r) {+ if (!line || len < 3 || line[1] != ' ') return 0;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 (good_name(v, vn, name) || strlen(name) != vn || memcmp(name, v, vn)) return 0; /* already normalized */+ return line[0];}- 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;+ if (line[0] == 'n') return !parse_net(v, vn, r) && net_usable(r) ? 'n' : 0;+ return 0;+}++int fwl_check_line(const char *line, size_t len) {+ char name[FW_NAME_MAX];+ FwRule r;+ return check_line(line, len, name, &r) ? 0 : -1;+}++int fwl_add_line(FwLists *l, const char *line, size_t len, uint16_t tag) {+ char name[FW_NAME_MAX];+ FwRule r;+ if (!l || !tag) return -1;+ int kind = check_line(line, len, name, &r);+ if (!kind) return -1;+ if (kind == 'n') {+ if (l->nnet >= FWL_MAX_NETS * 2 || grow((void **)&l->net, &l->capnet, l->nnet, sizeof *l->net)) return -2;+ NetEnt *e = &l->net[l->nnet++];+ net_key(r.fam, r.addr, r.prefix, e->k);+ e->tag = tag;+ e->seq = l->seq++;+ if (r.fam == 4) l->has4[r.prefix] = 1;+ else l->has6[r.prefix] = 1;+ } else {+ int sub = kind == 's';+ NameEnt **v = sub ? &l->sub : &l->ex;+ size_t *n = sub ? &l->nsub : &l->nex, *cap = sub ? &l->capsub : &l->capex;+ if (l->nex + l->nsub >= FWL_MAX_DOMAINS * 2 || grow((void **)v, cap, *n, sizeof **v)) return -2;+ NameEnt *e = &(*v)[(*n)++];+ e->h = fw_hash(name, strlen(name));+ e->tag = tag;+ e->seq = l->seq++;+ e->pad = 0;}- return -1;+ l->sorted = 0;+ return 0;}long fwl_add_text(FwLists *l, const char *text, size_t len, uint16_t tag) {@@ -122,11 +192,16 @@ long fwl_add_text(FwLists *l, const char *text, size_t len, uint16_t tag) {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++;+ if (r == -2) break;+ added++;}i = e + 1;}+ finish(l);+ /* the caps are on unique entries */+ if (l->nex + l->nsub > FWL_MAX_DOMAINS) l->nsub = l->nex >= FWL_MAX_DOMAINS ? 0 : FWL_MAX_DOMAINS - l->nex;+ if (l->nex > FWL_MAX_DOMAINS) l->nex = FWL_MAX_DOMAINS;+ if (l->nnet > FWL_MAX_NETS) l->nnet = FWL_MAX_NETS;return added;}@@ -149,64 +224,32 @@ FwLists *fwl_load_text(const char *text, size_t len, char *err, size_t errlen) {}i = e + 1;}+ finish(l);+ if (l->nex + l->nsub > FWL_MAX_DOMAINS || l->nnet > FWL_MAX_NETS) { snprintf(err, errlen, "too many entries"); fwl_free(l); return NULL; }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);+static uint16_t find_name(const NameEnt *v, size_t n, uint64_t h) {+ size_t lo = 0, hi = n;+ while (lo < hi) {+ size_t m = lo + (hi - lo) / 2;+ if (v[m].h < h) lo = m + 1;+ else hi = m;}- if (o.err) { free(o.p); return NULL; }- *len = o.n;- return o.p;+ return lo < n && v[lo].h == h ? v[lo].tag : 0;}-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;+ if (!l || !in) return 0;+ finish((FwLists *)l);+ if (!l->nex && !l->nsub) 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;+ uint16_t t = find_name(l->ex, l->nex, fw_hash(name, strlen(name)));+ if (t) return t;for (const char *sfx = name; sfx;) {- v = fwmap_get(&l->sub, sfx, strlen(sfx));- if (v) return (uint16_t)(uintptr_t)v;+ t = find_name(l->sub, l->nsub, fw_hash(sfx, strlen(sfx)));+ if (t) return t;sfx = strchr(sfx, '.');if (sfx) sfx++;}@@ -214,7 +257,9 @@ uint16_t fwl_match_name(const FwLists *l, const char *in) {}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;+ if (!l || !addr || (fam != 4 && fam != 6)) return 0;+ finish((FwLists *)l);+ if (!l->nnet) 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); }@@ -223,19 +268,25 @@ uint16_t fwl_match_addr(const FwLists *l, int fam, const uint8_t *addr) {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;+ net_key(fam, a, p, k);+ size_t lo = 0, hi = l->nnet;+ while (lo < hi) {+ size_t m = lo + (hi - lo) / 2;+ if (memcmp(l->net[m].k, k, 18) < 0) lo = m + 1;+ else hi = m;+ }+ if (lo < l->nnet && !memcmp(l->net[lo].k, k, 18)) return l->net[lo].tag;}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) {+ finish((FwLists *)l);+ for (size_t i = 0; i < l->nnet; i++) {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;+ memcpy(a, l->net[i].k + 2, l->net[i].k[0] == 4 ? 4 : 16);+ if (cb(u, l->net[i].k[0], a, l->net[i].k[1])) return;}}@@ -274,8 +325,69 @@ static int set_err(char *err, size_t errlen, const char *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) {+/* What the parser writes: the normalized lines, each once (a set of their hashes). */+typedef struct {+ char *p;+ size_t n, cap;+ uint64_t *set;+ size_t nset, capset; /* capset: a power of 2 */+ int err;+} Raw;++static void raw_put(Raw *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 : 65536;+ 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;+}++/* 1 if new (and now remembered), 0 if already there, -1 out of memory. */+static int raw_seen(Raw *o, uint64_t h) {+ if (!h) h = 1;+ if ((o->nset + 1) * 2 > o->capset) {+ size_t c = o->capset ? o->capset * 2 : 4096;+ uint64_t *v = calloc(c, sizeof *v);+ if (!v) return -1;+ for (size_t i = 0; i < o->capset; i++) {+ if (!o->set[i]) continue;+ size_t k = (size_t)o->set[i] & (c - 1);+ while (v[k]) k = (k + 1) & (c - 1);+ v[k] = o->set[i];+ }+ free(o->set);+ o->set = v;+ o->capset = c;+ }+ size_t k = (size_t)h & (o->capset - 1);+ while (o->set[k]) {+ if (o->set[k] == h) return 0;+ k = (k + 1) & (o->capset - 1);+ }+ o->set[k] = h;+ o->nset++;+ return 1;+}++static int raw_line(Raw *o, const char *line, size_t n, size_t *count, FwlStats *st) {+ int r = raw_seen(o, fw_hash(line, n));+ if (r < 0) { o->err = 1; return -1; }+ if (!r) { st->dups++; return 0; }+ raw_put(o, line, n);+ raw_put(o, "\n", 1);+ (*count)++;+ return 1;+}++/* One name from a list: written (d/s) or counted as why not. 1 written, 0 not, -1 stop. */+static int raw_name(Raw *o, 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; }@@ -287,31 +399,21 @@ static int raw_name(FwLists *l, const char *s, size_t n, int sub, FwlStats *st)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; }+ if (st->domains >= FWL_LIST_DOMAINS) return -1;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;+ return raw_line(o, line, (size_t)ln, &st->domains, st);}-static int raw_net(FwLists *l, const char *s, size_t n, FwlStats *st) {+static int raw_net(Raw *o, 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; }+ if (st->nets >= FWL_LIST_NETS) return -1;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;+ return raw_line(o, line, (size_t)ln, &st->nets, st);}int fwl_parse_raw(const char *raw, size_t len, FwlFormat fmt, int sub, char **out, size_t *outlen,@@ -324,8 +426,8 @@ int fwl_parse_raw(const char *raw, size_t len, FwlFormat fmt, int sub, char **ouif (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");+ Raw o = { 0 };+ Raw *l = &o;int full = 0;size_t i = 0;/* a UTF-8 byte order mark at the start */@@ -333,7 +435,7 @@ int fwl_parse_raw(const char *raw, size_t len, FwlFormat fmt, int sub, char **ouwhile (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"); }+ if (e - i > FWL_LINE_MAX) { free(o.p); free(o.set); return set_err(err, errlen, "line"); }const char *s = raw + i;size_t n = e - i;i = e + 1;@@ -370,13 +472,13 @@ int fwl_parse_raw(const char *raw, size_t len, FwlFormat fmt, int sub, char **ou} 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 (r < 0) { full = 1; st->truncated = !o.err; }}- 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;+ free(o.set);+ if (o.err) { free(o.p); return set_err(err, errlen, "oom"); }+ if (st->bad > 20 && st->bad * 10 > st->lines) { free(o.p); return set_err(err, errlen, "format"); }+ if (!st->domains && !st->nets) { free(o.p); return set_err(err, errlen, "empty"); }+ *out = o.p;+ *outlen = o.n;return 0;}diff --git a/server/src/fw/fwlists.h b/server/src/fw/fwlists.hindex 24a9332..6ea267b 100644--- a/server/src/fw/fwlists.h+++ b/server/src/fw/fwlists.h@@ -10,8 +10,9 @@* (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.+ * (which list it came from, for the Monitor's "blocked by the list …"). Names are kept as+ * 64-bit hashes (seeded per process) in sorted arrays: about 10 bytes a name. Name lookups walk+ * the labels right to left; address lookups try each prefix length present (binary search).** 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,@@ -62,17 +63,20 @@ 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. */+/* Adds one normalized line ("d name", "s name", "n cidr") with a tag (1..65535). 0 added (a+ * repeated entry goes at the next lookup or fwl_finish, 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). */+/* Only checks a line, as fwl_add_line would (lord-server compiles the file without keeping it). */+int fwl_check_line(const char *line, size_t len);+/* Sorts and drops repeated entries; returns how many went. Lookups and counts do it on their own. */+size_t fwl_finish(FwLists *l);+/* Adds every line of a normalized text (the lines fwl_parse_raw writes). Returns the number of+ * lines 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);diff --git a/server/src/fw/fwsub.c b/server/src/fw/fwsub.cindex dccee1d..7ed7945 100644--- a/server/src/fw/fwsub.c+++ b/server/src/fw/fwsub.c@@ -14,6 +14,7 @@#include <poll.h>#include <pthread.h>#include <signal.h>+#include <malloc.h>#include <spawn.h>#include <stdio.h>#include <stdlib.h>@@ -434,25 +435,51 @@ static void *worker(void *arg) {}helper_stop(&h);- /* compile every enabled list that has a copy */+ /* compile every enabled list that has a copy: their lines go to fwlists.txt as they are+ * (each checked again; lord-netperm drops repeated ones), and into the set lord-server keeps+ * for the Monitor (hashes only: a few bytes a name) */FwLists *s = fwl_new();char ids[FWS_MAX_LISTS][32];memset(ids, 0, sizeof ids);- for (int i = 0; s && i < n; i++) {+ char tmp[PATH_MAX + 8];+ snprintf(tmp, sizeof tmp, "%s.tmp", txt_path);+ int ofd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);+ int ok = ofd >= 0 && write(ofd, FWL_HEADER "\n", strlen(FWL_HEADER) + 1) == (ssize_t)strlen(FWL_HEADER) + 1;+ size_t total = 0;+ for (int i = 0; s && ok && i < n; i++) {if (!copy[i].on) continue;char p[PATH_MAX];cache_path(copy[i].id, "txt", p, sizeof p);size_t len;char *txt = read_file(p, FWL_FILE_MAX, &len);if (!txt) continue;+ int good = 1;+ for (size_t k = 0; k < len && good;) {+ const char *nl = memchr(txt + k, '\n', len - k);+ size_t e = nl ? (size_t)(nl - txt) : len;+ if (e > k && fwl_check_line(txt + k, e - k)) good = 0;+ k = e + 1;+ }+ if (!good || total + len > FWL_FILE_MAX - 64) {+ fprintf(stderr, "lord.fw: the copy of blocklist %s is %s: left out\n", copy[i].id, good ? "too large" : "invalid");+ free(txt);+ continue;+ }memcpy(ids[i], copy[i].id, sizeof ids[i]);- if (fwl_add_text(s, txt, len, (uint16_t)(i + 1)) < 0) fprintf(stderr, "lord.fw: the copy of blocklist %s is invalid\n", copy[i].id);+ fwl_add_text(s, txt, len, (uint16_t)(i + 1));+ for (size_t off = 0; ok && off < len;) {+ ssize_t w = write(ofd, txt + off, len - off);+ if (w <= 0) ok = 0;+ else off += (size_t)w;+ }+ if (ok && len && txt[len - 1] != '\n') ok = write(ofd, "\n", 1) == 1;+ total += len + 1;free(txt);}- size_t tl = 0;- char *text = s ? fwl_to_text(s, &tl) : NULL;- if (!text || write_file(txt_path, text, tl)) fprintf(stderr, "lord.fw: could not write %s\n", txt_path);- free(text);+ if (ofd >= 0 && (fsync(ofd) || close(ofd))) ok = 0;+ if (!ok || rename(tmp, txt_path)) { unlink(tmp); fprintf(stderr, "lord.fw: could not write %s\n", txt_path); }+ if (s) fwl_finish(s);+ malloc_trim(0); /* the copies were read on this thread: give their memory back */pthread_mutex_lock(&mu);fwl_free(next_set);diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.cindex a53d83d..3848d01 100644--- a/server/src/netperm/netperm.c+++ b/server/src/netperm/netperm.c@@ -36,6 +36,7 @@#include <arpa/inet.h>#include <dirent.h>+#include <malloc.h>#include <errno.h>#include <fcntl.h>#include <linux/bpf.h>@@ -250,6 +251,7 @@ static void load_lists(Session *s) {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));+ malloc_trim(0); /* the file's text and the set's growth: back to the system */}/* ---- maps ------------------------------------------------------------------------------------ */@@ -343,7 +345,10 @@ 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 */+ /* the lists deny new connections in connect()/sendmsg(); an open connection to an address that+ * just joined a list is cut by the sweep after the change (sync_blk), so the per-packet filter+ * doesn't decide for every app because lists are on (it would cost every packet a decision) */+ if (s->lists) c.app.blk = s->blkset;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@@ -401,6 +406,8 @@ static int blk_map(uint32_t set, int fam, const uint8_t *addr, int prefix, int dreturn del ? bpfs_map_delete(G.m_np_blk6, &k) : bpfs_map_update(G.m_np_blk6, &k, &one, BPF_ANY);}+static int blk_added; /* entries sync_blk installed (a sweep follows) */+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);@@ -420,6 +427,7 @@ static void blk_want(Session *s, int fam, const uint8_t *addr, int prefix, int le->mark = G.blk_pass;s->nlearned += learned;*slot = e;+ blk_added++;}/* An address the resolver now also gives for an unlisted name: no longer blocked. */@@ -443,9 +451,12 @@ static int blk_dns_cb(void *u, int fam, const uint8_t *addr, const char (*names)return 0;}+static void sweep(unsigned uid);+static void sync_blk(Session *s) {if (!G.bpf_ok) return;G.blk_pass++;+ blk_added = 0;if (s->lists) {if (!s->blkset) s->blkset = ++G.next_blkset;fwl_net_each(s->lists, blk_net_cb, s);@@ -458,6 +469,7 @@ static void sync_blk(Session *s) {s->nlearned -= b->learned;free(fwmap_take(&s->blkin, e->key, e->klen));}+ if (blk_added) sweep(s->uid); /* open connections to what was just added */}/* ---- user@<uid>.service: the systemd-run escape ------------------------------------------- */@@ -834,7 +846,7 @@ static int remote_of(const NpSock *k, uint8_t *a) {* 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;+ if (!a->can_deny && !a->blk) return 0;*fam = remote_of(k, addr);int proto = k->proto == IPPROTO_TCP ? NP_P_TCP : NP_P_UDP, lan;NpList l;diff --git a/server/src/pkg/http.c b/server/src/pkg/http.cindex 0d20059..c667d81 100644--- a/server/src/pkg/http.c+++ b/server/src/pkg/http.c@@ -382,10 +382,7 @@ int fetch_batch(FetchItem *items, int n, FetchProgress cb, void *ud) {prctl(PR_SET_PDEATHSIG, SIGKILL);/* (0.8.4) in lordos-net-store.scope, so the Store and the updates are their own app for the* firewall and Monitor → Network (lordos:store); outside a LordOS session, where it is */- if (getenv("LORDOS_SESSIOND")) {- CgroupLimits lim = { 0, 0, 0 };- cgroup_enter("lordos-net-store.scope", &lim);- }+ if (getenv("LORDOS_SESSIOND")) cgroup_enter_root("lordos-net-store.scope");if (drop_to_nobody()) _exit(2);Conn c = { .fd = -1, .out = 3 };for (int i = 0; i < n; i++) get_one(&c, &items[i], (uint32_t)i);diff --git a/server/src/sec/cgroup.c b/server/src/sec/cgroup.cindex ef75be4..f977416 100644--- a/server/src/sec/cgroup.c+++ b/server/src/sec/cgroup.c@@ -226,6 +226,28 @@ int cgroup_service_init(void) {return 0;}+int cgroup_enter_root(const char *name) {+ if (geteuid() != 0 || !cgroup_scope_name_ok(name)) return -1;+ const char *self = cgroup_self(); /* ".../lordos-session-<uid>/server" */+ char dir[PATH_MAX];+ if (!self[0] || (size_t)snprintf(dir, sizeof dir, CG_ROOT "%s", self) >= sizeof dir) return -1;+ char *sl = strrchr(dir, '/');+ unsigned uid;+ char tail;+ if (!sl || strcmp(sl, "/server")) return -1;+ *sl = 0;+ const char *sess = strrchr(dir, '/');+ if (!sess || sscanf(sess + 1, "lordos-session-%u%c", &uid, &tail) != 1) return -1;+ int fd = open(dir, 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_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];diff --git a/server/src/sec/cgroup.h b/server/src/sec/cgroup.hindex cac6a81..33c5c8a 100644--- a/server/src/sec/cgroup.h+++ b/server/src/sec/cgroup.h@@ -48,6 +48,12 @@ int cgroup_service_init(void);* service's own traffic (DHCP, the VPN daemons) apart from lord-sessiond's. Best effort: 0 placed. */int cgroup_service_leaf(const char *name);+/* (0.8.4) A root process of a session (the admin helper's download child, before it drops to+ * nobody) moves itself into scope `name` of its own session's subtree: lord-sessiond's placement+ * service only places session users, and root may write the root-owned cgroup.procs itself. Only+ * from the session's «server» leaf, only itself. 0 placed. */+int cgroup_enter_root(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/tests/fwlists_test.c b/server/tests/fwlists_test.cindex 1172796..8893bd4 100644--- a/server/tests/fwlists_test.c+++ b/server/tests/fwlists_test.c@@ -111,7 +111,7 @@ static void test_ips(void) {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, "d tracker.example", 17, 2) == 0); /* repeated: 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);@@ -137,11 +137,12 @@ static void test_set(void) {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);+ CHECK(fwl_check_line("d tracker.example", 17) == 0 && fwl_check_line("d Tracker.example", 17) == -1 && fwl_check_line("n 10.0.0.0/8", 12) == -1);+ /* the compiled file: round trip (repeated lines are fine), and any bad line rejects it */+ const char *t = "lordos-fwlists 1\nd tracker.example\ns ads.example\nn 203.0.113.0/24\nd tracker.example\nn 2001:db8::/32\n";+ size_t n = strlen(t);char err[64];- FwLists *b = t ? fwl_load_text(t, n, err, sizeof err) : NULL;+ FwLists *b = fwl_load_text(t, n, err, sizeof err);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",@@ -152,7 +153,6 @@ static void test_set(void) {b = fwl_load_text("lordos-fwlists 1", 16, err, sizeof err);CHECK(b && !fwl_domains(b));fwl_free(b);- free(t);fwl_free(l);}@@ -207,8 +207,8 @@ static void bench(void) {}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);+ printf("fwlists-test: 200000-name hosts list parsed in %.0f ms, loaded in %.0f ms, 1e6 lookups in %.0f ms (the set: %zu entries)\n",+ (t1 - t0) * 1e3, (t2 - t1) * 1e3, (t3 - t2) * 1e3, fwl_domains(l));fwl_free(l);free(out);free(raw);diff --git a/server/tests/netmodel_test.mjs b/server/tests/netmodel_test.mjsindex 83e9c61..ee617b1 100644--- a/server/tests/netmodel_test.mjs+++ b/server/tests/netmodel_test.mjs@@ -76,6 +76,7 @@ eq('search: two chips of one type: either', only({}, parseSearch('port:22 port:9eq('search: network chip', only({}, parseSearch('ip:192.0.2.0/24')), 2);eq('search: country by code and by name', [only({}, parseSearch('country:AR')), only({}, parseSearch('country:argen'), { countryName: (cc) => (cc === 'AR' ? 'Argentina' : cc) })], [2, 2]);eq('search: free text', only({}, { chips: [], rest: 'imap' }), 1);+eq('search: any name of a CNAME chain', matchesSearch(F({ name: 'deb.debian.org', names: ['deb.debian.org', 'debian.map.cdn.example'] }), parseSearch('domain:cdn.example')), true);eq('search: free text needs every word', only({}, { chips: [], rest: 'firefox 8080' }), 1);eq('search: nothing', matchesSearch(flows[0], { chips: [{ type: 'port', value: '1' }] }), false);