Skip to content

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.

lord-netperm: connection notices carry the resolver's names for the address

Federico Pereira740b4aa1519a
diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.c
index c90993c..a96f4a4 100644
--- a/server/src/netperm/netperm.c
+++ b/server/src/netperm/netperm.c
@@ -595,12 +595,27 @@ static void on_event(void *u, const void *data, uint32_t len) {
return;
}
const char *flag = ev.flag ? fw_flag_name(ev.flag) : "";
- char app[FW_APP_MAX * 2 + 4], j[700];
+ /* the names the system resolver gave for that address (at most 4, so the Monitor can offer
+ * "block this domain" with the real name) */
+ char hosts[4 * (FW_NAME_MAX * 2 + 4) + 4] = "[]";
+ if (ev.fam == 4 || ev.fam == 6) {
+ char names[4][FW_NAME_MAX];
+ int nh = dnsmap_lookup(G.names, NAMES_APP, ev.fam, ev.addr, mono(), names, 4);
+ size_t w = 0;
+ hosts[w++] = '[';
+ for (int i = 0; i < nh; i++) {
+ char q[FW_NAME_MAX * 2 + 4];
+ json_quote(q, sizeof q, names[i]);
+ w += (size_t)snprintf(hosts + w, sizeof hosts - w, "%s%s", i ? "," : "", q);
+ }
+ snprintf(hosts + w, sizeof hosts - w, "]");
+ }
+ char app[FW_APP_MAX * 2 + 4], j[700 + sizeof hosts];
json_quote(app, sizeof app, sc->app);
- int n = snprintf(j, sizeof j, "{\"app\":%s,\"v\":\"%s\",\"dir\":\"%s\",\"proto\":\"%s\",\"addr\":\"%s\",\"port\":%u,\"by\":\"%s\",\"rule\":%u,\"flag\":\"%s\",\"record\":%d}",
+ int n = snprintf(j, sizeof j, "{\"app\":%s,\"v\":\"%s\",\"dir\":\"%s\",\"proto\":\"%s\",\"addr\":\"%s\",\"port\":%u,\"by\":\"%s\",\"rule\":%u,\"flag\":\"%s\",\"record\":%d,\"hosts\":%s}",
app, ev.verdict == NP_V_DENY ? "deny" : "allow", ev.dir == NP_D_IN ? "in" : "out",
ev.proto == NP_P_TCP ? "tcp" : ev.proto == NP_P_UDP ? "udp" : "other", addr, ev.port, by_name(ev.reason),
- ev.rule_id, flag ? flag : "", ev.record);
+ 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",
diff --git a/server/src/netperm/npproto.h b/server/src/netperm/npproto.h
index c40e0dc..8cdf788 100644
--- a/server/src/netperm/npproto.h
+++ b/server/src/netperm/npproto.h
@@ -15,7 +15,10 @@
* From lord-netperm to lord-server (compact JSON in the data):
* NP_MSG_STATUS {"bpf":true,"dns":"resolved","error":""} on connect and when it changes
* NP_MSG_EVENT {"app":"desktop:x","v":"deny","dir":"out","proto":"tcp","addr":"1.2.3.4",
- * "port":443,"by":"rule","rule":7,"flag":"","record":0} a new connection
+ * "port":443,"by":"rule","rule":7,"flag":"","record":0,
+ * "hosts":["example.com"]} a new connection; hosts:
+ * up to 4 names the resolver
+ * gave for addr
* NP_MSG_COUNTS {"desktop:x":[allowed,denied],...} every second, if it changed */
#ifndef LORD_NP_PROTO_H
#define LORD_NP_PROTO_H
diff --git a/server/tests/netperm_integration.sh b/server/tests/netperm_integration.sh
index 700dd4e..2d85152 100644
--- a/server/tests/netperm_integration.sh
+++ b/server/tests/netperm_integration.sh
@@ -113,6 +113,31 @@ rules '{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$AP
got=$(python3 -c 'import socket;socket.create_connection(("'$GW'",53),timeout=4);print("ok")' 2>&1 | tail -1)
[ "$got" = ok ] && ok "outside the scope → ok" || bad "outside the scope → $got"
+echo "9. connection notices carry the names the resolver gave (hosts)"
+rules '{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$APP'","dir":"out","dst":"example.com","action":"deny","when":0}]}'
+python3 - "$APP" > /tmp/np-hosts-$U.txt <<'PY' &
+import socket, struct, sys, time, json
+s = socket.socket(socket.AF_UNIX); s.connect('/run/lordos/netperm.sock'); s.settimeout(8)
+buf = b''; end = time.time() + 8
+while time.time() < end:
+ try: d = s.recv(65536)
+ 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 == 11:
+ ev = json.loads(body)
+ if ev.get('app') == sys.argv[1] and ev.get('hosts'): print(' '.join(ev['hosts'])); sys.exit(0)
+PY
+sleep 1
+try https example.com 443 >/dev/null
+wait
+grep -q "example.com" /tmp/np-hosts-$U.txt && ok "the notice names example.com ($(cat /tmp/np-hosts-$U.txt))" || bad "no host names in the notice"
+rm -f /tmp/np-hosts-$U.txt
+
echo "netperm-integration: $pass passed, $fail failed"
sudo rmdir "$SC" 2>/dev/null
[ $fail = 0 ]