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.

Native components as their own firewall apps: Mail, printers, Store, Terminal and blocklists in their own scopes

Federico Pereiraf87380b42ab6
diff --git a/server/Makefile b/server/Makefile
index a4028e4..e24756d 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -69,7 +69,7 @@ DOC_SRC := src/doc/doc.c src/doc/wire.c src/ui/js_doc.c src/print/ipp.c src/prin
src/print/escl.c src/print/print.c src/ui/js_print.c
SRC += $(DOC_SRC)
# Per-app firewall (src/fw, docs/FIREWALL.md): rules, decision and names learned from DNS
-FW_SRC := src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/fw/fwlists.c src/ui/js_fw.c
+FW_SRC := src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/fw/fwlists.c src/ui/js_fw.c src/sec/netconn.c
SRC += $(FW_SRC)
OBJ := $(SRC:src/%.c=build/obj/%.o)
HDR := $(wildcard src/*.h src/gpu/*.h src/ui/*.h src/term/*.h src/wl/*.h src/wasm/*.h src/sound/*.h src/pkg/*.h src/net/*.h src/install/*.h src/remote/*.h src/users/*.h src/sec/*.h src/monitor/*.h src/media/*.h src/app/*.h src/doc/*.h src/print/*.h) $(WL_GEN) $(APP_CLIENT_HDR)
@@ -200,9 +200,9 @@ build/wasm-test: tests/wasm_test.c $(WASM_SRC) src/wasm/wasm.h src/wasm/wasm_int
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/wasm_test.c $(WASM_SRC) -lm
PKG_SRC := src/pkg/crypto.c src/pkg/pgp.c src/pkg/xz.c src/pkg/deb.c src/pkg/idx.c src/pkg/status.c src/pkg/resolve.c src/pkg/http.c src/pkg/pkg.c
-build/pkg-test: tests/pkg_test.c $(PKG_SRC) build/obj/ui/stb.o src/admin.h $(wildcard src/pkg/*.h)
+build/pkg-test: tests/pkg_test.c $(PKG_SRC) src/sec/cgroup.c build/obj/ui/stb.o src/admin.h $(wildcard src/pkg/*.h)
@mkdir -p build
- $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pkg_test.c $(PKG_SRC) build/obj/ui/stb.o -lm
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pkg_test.c $(PKG_SRC) src/sec/cgroup.c build/obj/ui/stb.o -lm
PKG_READ_SRC := src/pkg/deb.c src/pkg/idx.c src/pkg/status.c src/pkg/resolve.c src/pkg/crypto.c
build/pkg-js-test: tests/pkg_js_test.c src/ui/js_pkg.c $(PKG_READ_SRC) $(QJS_OBJ) $(wildcard src/pkg/*.h)
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pkg_js_test.c src/ui/js_pkg.c $(PKG_READ_SRC) $(QJS_OBJ) -lm -lpthread
@@ -536,9 +536,17 @@ build/arcpath-test: tests/arcpath_test.c src/doc/arcpath.c src/doc/arcpath.h
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/arcpath_test.c src/doc/arcpath.c
# IPP encoder/decoder, mDNS and page ranges (tests/ipp_test.c)
-build/ipp-test: tests/ipp_test.c src/print/ipp.c src/print/mdns.c src/print/cups.c src/print/phttp.c src/doc/wire.c src/sec/json.c $(wildcard src/print/*.h)
+# (0.8.4) the components' network connectors (src/sec/netconn.c) and what they need, for the tests
+NETCONN_SRC := src/sec/netconn.c src/sec/cgroup.c src/fw/rules.c src/fw/fwmap.c
+# the connectors: requests, sockets, the real connector process and its fail-closed start
+test: build/netconn-test
+build/netconn-test: tests/netconn_test.c $(NETCONN_SRC) src/sec/json.c src/sec/netconn.h src/sec/cgroup.h
@mkdir -p build
- $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/ipp_test.c src/print/ipp.c src/print/mdns.c src/print/cups.c src/print/phttp.c src/doc/wire.c src/sec/json.c
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/netconn_test.c $(NETCONN_SRC) src/sec/json.c -lpthread
+ ./$@ || { rm -f $@; exit 1; }
+build/ipp-test: tests/ipp_test.c src/print/ipp.c src/print/mdns.c src/print/cups.c src/print/phttp.c src/doc/wire.c src/sec/json.c $(NETCONN_SRC) $(wildcard src/print/*.h)
+ @mkdir -p build
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/ipp_test.c src/print/ipp.c src/print/mdns.c src/print/cups.c src/print/phttp.c src/doc/wire.c src/sec/json.c $(NETCONN_SRC) -lpthread
# lord-doc end to end: malicious archives, PDF and the lockdown (needs build/lord-doc)
build/doc-test: tests/doc_test.c src/doc/wire.c src/sec/json.c $(wildcard src/doc/*.h)
@mkdir -p build
@@ -550,7 +558,7 @@ include $(sort $(wildcard mk/*.mk))
# lord-netd (docs/NETWORK.md, "The network belongs to the system"): admin.c with LORD_NETD (net-* actions
# only) plus networking and the package engine (net-vpn-enable). libc only; lord-sessiond runs it.
all: build/lord-netd
-NETD_OBJ := build/obj/netd/admin.o build/obj/netd/netd.o $(NET_SRC:src/%.c=build/obj/%.o) $(PKG_SRC:src/%.c=build/obj/%.o) build/obj/ui/stb.o
+NETD_OBJ := build/obj/netd/admin.o build/obj/netd/netd.o $(NET_SRC:src/%.c=build/obj/%.o) $(PKG_SRC:src/%.c=build/obj/%.o) build/obj/sec/cgroup.o build/obj/ui/stb.o
build/obj/netd/admin.o: src/admin.c $(HDR)
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) -DLORD_NETD $(CFLAGS) $(WARN) -std=c11 -c -o $@ $<
diff --git a/server/mk/mail.mk b/server/mk/mail.mk
index a9fbc4e..1cc016e 100644
--- a/server/mk/mail.mk
+++ b/server/mk/mail.mk
@@ -21,9 +21,9 @@ build/lord-server: $(MAILC_OBJ)
$(MAILC_OBJ): $(wildcard src/mail/*.h) src/ui/js_mail.h
# the connector's policy: allowed endpoints and the public-address check for remote images
test: build/mailc-test
-build/mailc-test: tests/mailc_test.c src/mail/mailc.c src/mail/mailc.h src/doc/wire.c src/sec/json.c
+build/mailc-test: tests/mailc_test.c src/mail/mailc.c src/mail/mailc.h src/doc/wire.c src/sec/json.c $(NETCONN_SRC)
@mkdir -p build
- $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/mailc_test.c src/mail/mailc.c src/doc/wire.c src/sec/json.c -lpthread -lm
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/mailc_test.c src/mail/mailc.c src/doc/wire.c src/sec/json.c $(NETCONN_SRC) -lpthread -lm
./$@ || { rm -f $@; exit 1; }
# hostile servers: HTTP chunk sizes / Content-Length / header limits and IMAP literal sizes,
# over a socketpair (no network). Needs the OpenSSL headers (net.c), like lord-maild.
@@ -56,6 +56,6 @@ build/lord-maild: $(LMD_SRC) build/obj/mail/mimg.o $(MAIL_HDR) src/sec/sandbox.h
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ $(LMD_SRC) build/obj/mail/mimg.o $(shell pkg-config --libs openssl) -lpthread -lm
# manual driver for lord-maild (not in `make test`): build/maild-cli '{"op":"status"}' --wait 5
-build/maild-cli: tests/maild_cli.c src/mail/mailc.c src/doc/wire.c src/sec/json.c src/mail/mailc.h
+build/maild-cli: tests/maild_cli.c src/mail/mailc.c src/doc/wire.c src/sec/json.c src/mail/mailc.h $(NETCONN_SRC)
@mkdir -p build
- $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/maild_cli.c src/mail/mailc.c src/doc/wire.c src/sec/json.c -lpthread -lm
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/maild_cli.c src/mail/mailc.c src/doc/wire.c src/sec/json.c $(NETCONN_SRC) -lpthread -lm
diff --git a/server/src/fw/rules.c b/server/src/fw/rules.c
index afaefcc..f8dbcb7 100644
--- a/server/src/fw/rules.c
+++ b/server/src/fw/rules.c
@@ -78,6 +78,19 @@ int fw_component_ok(const char *name) {
return 0;
}
+int fw_component_scope(const char *scope, char *app, size_t n) {
+ size_t l = scope ? strlen(scope) : 0;
+ if (l <= 17 || strncmp(scope, "lordos-net-", 11) || strcmp(scope + l - 6, ".scope")) return -1;
+ char comp[32];
+ size_t cl = l - 11 - 6;
+ if (cl >= sizeof comp) return -1;
+ memcpy(comp, scope + 11, cl);
+ comp[cl] = 0;
+ if (!fw_component_ok(comp)) return -1;
+ int w = snprintf(app, n, "lordos:%s", comp);
+ return w < 0 || (size_t)w >= n ? -1 : 0;
+}
+
int fw_app_canon(const char *in, char *out, size_t n) {
if (!in || !out || !n) return -1;
size_t len = strnlen(in, FW_APP_MAX);
diff --git a/server/src/fw/rules.h b/server/src/fw/rules.h
index 9d4056c..f867f9e 100644
--- a/server/src/fw/rules.h
+++ b/server/src/fw/rules.h
@@ -125,6 +125,8 @@ int fw_app_canon(const char *in, char *out, size_t n);
* their own rows"): their firewall id is "lordos:<name>". 1 if name is one of them. */
#define FW_COMPONENTS { "mail", "store", "print", "terminal", "lists", NULL }
int fw_component_ok(const char *name);
+/* "lordos-net-<name>.scope" of a known component → "lordos:<name>" in app. 0 ok, -1 otherwise. */
+int fw_component_scope(const char *scope, char *app, size_t n);
/* Destination as text ("any", "lan", "internet", "example.com", "*.example.com", "10.0.0.0/8",
* "2001:db8::1") → kind/wildcard/domain/fam/addr/prefix of r. 0 ok, -1 invalid. */
diff --git a/server/src/mail/mailc.c b/server/src/mail/mailc.c
index 6c0b7d4..d36a3b9 100644
--- a/server/src/mail/mailc.c
+++ b/server/src/mail/mailc.c
@@ -1,5 +1,6 @@
/* mailc.c — see mailc.h. */
#include "mailc.h"
+#include "../sec/netconn.h"
#include "proto.h"
#include "../doc/proto.h"
#include "../doc/wire.h"
@@ -505,18 +506,16 @@ static int send_msg(uint32_t op, uint32_t id, const char *json, int fd) {
return r;
}
+/* The socket is made and connected by Mail's own connector, in lordos-net-mail.scope (0.8.4,
+ * sec/netconn.h): Mail and the calendar sync are their own app for the firewall and the Monitor. */
static int connect_timeout(const struct addrinfo *ai, int ms) {
- int fd = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC | SOCK_NONBLOCK, ai->ai_protocol);
+ int fd = netconn_socket(NC_MAIL, ai->ai_family, SOCK_STREAM, ai->ai_addr, ai->ai_addrlen);
if (fd < 0) return -1;
- int r = connect(fd, ai->ai_addr, ai->ai_addrlen);
- if (r && errno != EINPROGRESS) { close(fd); return -1; }
- if (r) {
- struct pollfd p = { fd, POLLOUT, 0 };
- if (poll(&p, 1, ms) != 1) { close(fd); return -1; }
- int err = 0;
- socklen_t el = sizeof err;
- if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &el) || err) { close(fd); return -1; }
- }
+ struct pollfd p = { fd, POLLOUT, 0 };
+ if (poll(&p, 1, ms) != 1) { close(fd); return -1; }
+ int err = 0;
+ socklen_t el = sizeof err;
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &el) || err) { close(fd); return -1; }
int fl = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
return fd;
diff --git a/server/src/main.c b/server/src/main.c
index d04874f..d6499be 100644
--- a/server/src/main.c
+++ b/server/src/main.c
@@ -52,6 +52,7 @@
#include "outputs.h"
#include "monitor/netrec.h"
#include "sec/codesig.h"
+#include "sec/netconn.h"
#include <libgen.h>
#include <limits.h>
@@ -1517,6 +1518,8 @@ int main(int argc, char **argv) {
sigaction(SIGPIPE, &pipe_sa, NULL);
/* guardián de una sesión de terminal (ver term/hold.c): no abre pantalla ni input */
if (argc > 1 && !strcmp(argv[1], "--term-holder")) return hold_main(argc, argv);
+ /* (0.8.4) a component's network connector (sec/netconn.h): only creates sockets in its scope */
+ if (argc > 1 && !strcmp(argv[1], "--net-connector")) return netconn_main(argc, argv);
if (argc > 1 && !strcmp(argv[1], "--check-code")) return check_code(argc, argv);
/* «modo app»: lord-server --app <id> corre una app .lpk como cliente Wayland aislado
* (app/appclient.c). El id ya viene validado del propio lord-server (no de JS). */
diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.c
index bc51fa8..a53d83d 100644
--- a/server/src/netperm/netperm.c
+++ b/server/src/netperm/netperm.c
@@ -536,16 +536,7 @@ static int scope_app(const char *name, char *app) {
if (!strcmp(name, "server")) { snprintf(app, FW_APP_MAX, "lordos"); return 0; }
size_t n = strlen(name);
/* (0.8.4) LordOS's own components that use the network: lordos-net-<name>.scope → lordos:<name> */
- if (n > 17 && !strncmp(name, "lordos-net-", 11) && !strcmp(name + n - 6, ".scope")) {
- char comp[32];
- size_t cl = n - 11 - 6;
- if (cl >= sizeof comp) return -1;
- memcpy(comp, name + 11, cl);
- comp[cl] = 0;
- if (!fw_component_ok(comp)) return -1;
- snprintf(app, FW_APP_MAX, "lordos:%s", comp);
- return 0;
- }
+ if (!strncmp(name, "lordos-net-", 11)) return fw_component_scope(name, app, FW_APP_MAX);
if (n < 18 || strncmp(name, "app-lordos-", 11) || strcmp(name + n - 6, ".scope")) return -1;
return fw_app_canon(name, app, FW_APP_MAX);
}
diff --git a/server/src/pkg/http.c b/server/src/pkg/http.c
index 5fd25a4..0d20059 100644
--- a/server/src/pkg/http.c
+++ b/server/src/pkg/http.c
@@ -8,6 +8,7 @@
#include "http.h"
#include "../errcode.h"
#include "crypto.h"
+#include "../sec/cgroup.h"
#include <ctype.h>
#include <errno.h>
@@ -379,6 +380,12 @@ int fetch_batch(FetchItem *items, int n, FetchProgress cb, void *ud) {
syscall(SYS_close_range, 4, ~0u, 0);
signal(SIGPIPE, SIG_IGN);
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 (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/print/mdns.c b/server/src/print/mdns.c
index 36bc8d0..67d7535 100644
--- a/server/src/print/mdns.c
+++ b/server/src/print/mdns.c
@@ -1,5 +1,6 @@
/* mdns.c — see mdns.h. */
#include "mdns.h"
+#include "../sec/netconn.h"
#include <arpa/inet.h>
#include <errno.h>
@@ -203,7 +204,7 @@ const char *mdns_txt(const MdnsSvc *s, const char *key, char *out, size_t n) {
}
int mdns_browse(const char *const *types, int ntypes, int ms, MdnsSvc *out, int max) {
- int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ int fd = netconn_socket(NC_PRINT, AF_INET, SOCK_DGRAM, NULL, 0); /* the printers' own (sec/netconn.h) */
if (fd < 0) return 0;
unsigned char ttl = 255, loop = 1;
setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof ttl);
diff --git a/server/src/print/phttp.c b/server/src/print/phttp.c
index 990663e..4891ccd 100644
--- a/server/src/print/phttp.c
+++ b/server/src/print/phttp.c
@@ -1,5 +1,6 @@
/* phttp.c — see phttp.h. */
#include "phttp.h"
+#include "../sec/netconn.h"
#include <arpa/inet.h>
#include <errno.h>
@@ -41,9 +42,9 @@ int phttp_lan_ipv4(const char *ip) {
int phttp_tcp(const char *ip, int port, int timeout_ms) {
struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons((uint16_t)port) };
if (port < 1 || port > 65535 || inet_pton(AF_INET, ip, &a.sin_addr) != 1) return -1;
- int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ /* made and connected by the printers' connector, in lordos-net-print.scope (sec/netconn.h) */
+ int fd = netconn_socket(NC_PRINT, AF_INET, SOCK_STREAM, (struct sockaddr *)&a, sizeof a);
if (fd < 0) return -1;
- if (connect(fd, (struct sockaddr *)&a, sizeof a) && errno != EINPROGRESS) { close(fd); return -1; }
struct pollfd p = { fd, POLLOUT, 0 };
int err = 0;
socklen_t el = sizeof err;
diff --git a/server/src/sec/netconn.c b/server/src/sec/netconn.c
new file mode 100644
index 0000000..7942270
--- /dev/null
+++ b/server/src/sec/netconn.c
@@ -0,0 +1,235 @@
+/* netconn.c — sockets for LordOS's own components, created in their own scopes (see netconn.h). */
+#include "netconn.h"
+#include "cgroup.h"
+#include "../fw/rules.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <poll.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/prctl.h>
+#include <sys/resource.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static const char *const NAMES[NC_COUNT] = { "mail", "print", "lists" };
+
+const char *netconn_name(NcComponent c) { return (unsigned)c < NC_COUNT ? NAMES[c] : NULL; }
+
+/* ---- the request ---- */
+
+int netconn_req_ok(const NcReq *r) {
+ if (!r || r->magic != NC_MAGIC || r->pad || r->pad2) return -1;
+ if (r->fam != 4 && r->fam != 6) return -1;
+ if (r->type != 1 && r->type != 2) return -1;
+ if (r->connect > 1) return -1;
+ if (r->connect && !r->port) return -1;
+ if (!r->connect && (r->port || r->scope_id)) return -1;
+ if (r->fam == 4) for (int i = 4; i < 16; i++) if (r->addr[i]) return -1;
+ if (r->fam == 4 && r->scope_id) return -1;
+ return 0;
+}
+
+int netconn_req_make(NcReq *r, int fam, int type, const struct sockaddr *to, socklen_t tolen) {
+ memset(r, 0, sizeof *r);
+ r->magic = NC_MAGIC;
+ if (fam == AF_INET) r->fam = 4;
+ else if (fam == AF_INET6) r->fam = 6;
+ else return -1;
+ if (type == SOCK_STREAM) r->type = 1;
+ else if (type == SOCK_DGRAM) r->type = 2;
+ else return -1;
+ if (to) {
+ if (to->sa_family != fam) return -1;
+ r->connect = 1;
+ if (fam == AF_INET) {
+ if (tolen < (socklen_t)sizeof(struct sockaddr_in)) return -1;
+ const struct sockaddr_in *a = (const struct sockaddr_in *)(const void *)to;
+ memcpy(r->addr, &a->sin_addr, 4);
+ r->port = ntohs(a->sin_port);
+ } else {
+ if (tolen < (socklen_t)sizeof(struct sockaddr_in6)) return -1;
+ const struct sockaddr_in6 *a = (const struct sockaddr_in6 *)(const void *)to;
+ memcpy(r->addr, &a->sin6_addr, 16);
+ r->port = ntohs(a->sin6_port);
+ r->scope_id = a->sin6_scope_id;
+ }
+ }
+ return netconn_req_ok(r);
+}
+
+int netconn_serve_one(const NcReq *r) {
+ if (netconn_req_ok(r)) return -EINVAL;
+ int fam = r->fam == 4 ? AF_INET : AF_INET6;
+ int fd = socket(fam, (r->type == 1 ? SOCK_STREAM : SOCK_DGRAM) | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+ if (fd < 0) return -errno;
+ if (r->connect) {
+ int rc;
+ if (fam == AF_INET) {
+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(r->port) };
+ memcpy(&a.sin_addr, r->addr, 4);
+ rc = connect(fd, (struct sockaddr *)&a, sizeof a);
+ } else {
+ struct sockaddr_in6 a = { .sin6_family = AF_INET6, .sin6_port = htons(r->port), .sin6_scope_id = r->scope_id };
+ memcpy(&a.sin6_addr, r->addr, 16);
+ rc = connect(fd, (struct sockaddr *)&a, sizeof a);
+ }
+ if (rc && errno != EINPROGRESS) { int e = errno; close(fd); return -e; }
+ }
+ return fd;
+}
+
+/* ---- lord-server's side ---- */
+
+static struct { int fd; pid_t pid; } conn[NC_COUNT] = { { -1, 0 }, { -1, 0 }, { -1, 0 } };
+static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
+
+static void drop(NcComponent c) {
+ if (conn[c].fd >= 0) close(conn[c].fd);
+ conn[c].fd = -1;
+ if (conn[c].pid > 0) { kill(conn[c].pid, SIGKILL); waitpid(conn[c].pid, NULL, 0); }
+ conn[c].pid = 0;
+}
+
+static int spawn(NcComponent c) {
+ int sv[2];
+ if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv)) return -1;
+ char argv0[32];
+ snprintf(argv0, sizeof argv0, "lord-net-%s", NAMES[c]);
+ char envs[2][64];
+ char *envp[3];
+ int ne = 0;
+ if (getenv("LORDOS_SESSIOND")) { snprintf(envs[ne], sizeof envs[ne], "LORDOS_SESSIOND=1"); envp[ne] = envs[ne]; ne++; }
+ snprintf(envs[ne], sizeof envs[ne], "LANG=C.UTF-8");
+ envp[ne] = envs[ne];
+ ne++;
+ envp[ne] = NULL;
+ char *args[] = { argv0, "--net-connector", (char *)NAMES[c], NULL };
+ pid_t p = fork();
+ if (p == 0) {
+ /* only the channel (fd 3) and stderr (the session log); no DRM, input or other fds */
+ int ch = fcntl(sv[1], F_DUPFD, 10);
+ dup2(ch, 3);
+ int null = open("/dev/null", O_RDWR);
+ if (null >= 0) { dup2(null, 0); dup2(null, 1); }
+ syscall(SYS_close_range, 4, ~0u, 0);
+ sigset_t none;
+ sigemptyset(&none);
+ sigprocmask(SIG_SETMASK, &none, NULL);
+ execve("/proc/self/exe", args, envp);
+ _exit(127);
+ }
+ close(sv[1]);
+ if (p < 0) { close(sv[0]); return -1; }
+ conn[c].fd = sv[0];
+ conn[c].pid = p;
+ return 0;
+}
+
+/* One round trip; 0 with *out the fd, or -errno. */
+static int ask(NcComponent c, const NcReq *r, int *out) {
+ if (send(conn[c].fd, r, sizeof *r, MSG_NOSIGNAL) != (ssize_t)sizeof *r) return -EPIPE;
+ struct pollfd p = { conn[c].fd, POLLIN, 0 };
+ if (poll(&p, 1, 5000) != 1) return -ETIMEDOUT; /* a first request also waits for its placement */
+ int err = 0;
+ struct iovec iov = { &err, sizeof err };
+ union { struct cmsghdr h; char b[CMSG_SPACE(sizeof(int))]; } cm;
+ struct msghdr m = { .msg_iov = &iov, .msg_iovlen = 1, .msg_control = cm.b, .msg_controllen = sizeof cm.b };
+ ssize_t n = recvmsg(conn[c].fd, &m, MSG_CMSG_CLOEXEC);
+ if (n != (ssize_t)sizeof err) return -EPIPE;
+ int fd = -1;
+ for (struct cmsghdr *h = CMSG_FIRSTHDR(&m); h; h = CMSG_NXTHDR(&m, h))
+ if (h->cmsg_level == SOL_SOCKET && h->cmsg_type == SCM_RIGHTS && h->cmsg_len == CMSG_LEN(sizeof(int))) memcpy(&fd, CMSG_DATA(h), sizeof fd);
+ if (err) { if (fd >= 0) close(fd); return err < 0 ? err : -EIO; }
+ if (fd < 0) return -EIO;
+ /* what came back must be the socket that was asked for */
+ int type = 0, dom = 0;
+ socklen_t tl = sizeof type, dl = sizeof dom;
+ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &tl) || getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &dom, &dl) ||
+ type != (r->type == 1 ? SOCK_STREAM : SOCK_DGRAM) || dom != (r->fam == 4 ? AF_INET : AF_INET6)) {
+ close(fd);
+ return -EIO;
+ }
+ *out = fd;
+ return 0;
+}
+
+int netconn_socket(NcComponent comp, int fam, int type, const struct sockaddr *to, socklen_t tolen) {
+ NcReq r;
+ if ((unsigned)comp >= NC_COUNT || netconn_req_make(&r, fam, type, to, tolen)) { errno = EINVAL; return -1; }
+ /* outside a LordOS session there are no scopes to put the sockets in (development, the
+ * tests): the socket is made here, as before */
+ if (!getenv("LORDOS_SESSIOND")) {
+ int fd = netconn_serve_one(&r);
+ if (fd < 0) { errno = -fd; return -1; }
+ return fd;
+ }
+ pthread_mutex_lock(&mu);
+ int fd = -1, rc = -EPIPE;
+ for (int attempt = 0; attempt < 2 && (rc == -EPIPE || rc == -ETIMEDOUT); attempt++) {
+ if (conn[comp].fd < 0 && spawn(comp)) { rc = -EAGAIN; break; }
+ rc = ask(comp, &r, &fd);
+ if (rc == -EPIPE || rc == -ETIMEDOUT) {
+ fprintf(stderr, "netconn: the %s connector did not answer (%s)%s\n", NAMES[comp], strerror(-rc), attempt ? "" : "; starting it again");
+ drop(comp);
+ }
+ }
+ pthread_mutex_unlock(&mu);
+ if (rc) { errno = -rc; return -1; }
+ return fd;
+}
+
+/* ---- the connector ---- */
+
+int netconn_main(int argc, char **argv) {
+ if (argc != 3 || !fw_component_ok(argv[2])) { fprintf(stderr, "lord-server --net-connector: unknown component\n"); return 2; }
+ int known = 0;
+ for (int i = 0; i < NC_COUNT; i++) known |= !strcmp(argv[2], NAMES[i]);
+ if (!known) return 2;
+ prctl(PR_SET_PDEATHSIG, SIGKILL);
+ if (getppid() == 1) return 0;
+ prctl(PR_SET_DUMPABLE, 0);
+ prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+ signal(SIGPIPE, SIG_IGN);
+ struct rlimit nf = { 64, 64 };
+ setrlimit(RLIMIT_NOFILE, &nf);
+ int type = 0;
+ socklen_t tl = sizeof type;
+ if (getsockopt(3, SOL_SOCKET, SO_TYPE, &type, &tl) || type != SOCK_SEQPACKET) { fprintf(stderr, "lord-server --net-connector: started by lord-server only\n"); return 2; }
+ char scope[64];
+ snprintf(scope, sizeof scope, "lordos-net-%s.scope", argv[2]);
+ CgroupLimits lim = { .mem_high_mb = 0, .mem_max_mb = 64, .cpu_percent = 0 };
+ if (cgroup_enter(scope, &lim) && getenv("LORDOS_SESSIOND")) {
+ fprintf(stderr, "netconn: could not place the %s connector in %s: its connections are refused\n", argv[2], scope);
+ return 3; /* fail closed: lord-server's requests get EPIPE */
+ }
+ for (;;) {
+ NcReq r;
+ ssize_t n = recv(3, &r, sizeof r, 0);
+ if (n == 0) return 0;
+ if (n < 0) { if (errno == EINTR) continue; return 1; }
+ int fd = n == (ssize_t)sizeof r ? netconn_serve_one(&r) : -EINVAL;
+ int err = fd < 0 ? fd : 0;
+ struct iovec iov = { &err, sizeof err };
+ union { struct cmsghdr h; char b[CMSG_SPACE(sizeof(int))]; } cm;
+ struct msghdr m = { .msg_iov = &iov, .msg_iovlen = 1 };
+ if (fd >= 0) {
+ memset(&cm, 0, sizeof cm);
+ m.msg_control = cm.b;
+ m.msg_controllen = sizeof cm.b;
+ struct cmsghdr *h = CMSG_FIRSTHDR(&m);
+ h->cmsg_level = SOL_SOCKET;
+ h->cmsg_type = SCM_RIGHTS;
+ h->cmsg_len = CMSG_LEN(sizeof(int));
+ memcpy(CMSG_DATA(h), &fd, sizeof fd);
+ }
+ if (sendmsg(3, &m, MSG_NOSIGNAL) < 0 && errno == EPIPE) return 0;
+ if (fd >= 0) close(fd);
+ }
+}
diff --git a/server/src/sec/netconn.h b/server/src/sec/netconn.h
new file mode 100644
index 0000000..0857a56
--- /dev/null
+++ b/server/src/sec/netconn.h
@@ -0,0 +1,61 @@
+/* netconn.h — sockets for LordOS's own components, created in their own scopes (0.8.4).
+ *
+ * lord-server opens connections for several of its parts: Mail and the calendar sync (for
+ * lord-maild, which has no sockets of its own), printers and scanners, and the blocklist
+ * downloads. The kernel ties a socket to the cgroup of the process that created it, so each of
+ * those parts gets a small connector process of its own: lord-server re-executed as
+ * `lord-server --net-connector <component>` (component from a fixed list, fw_component_ok), which
+ * asks lord-sessiond to place it in lordos-net-<component>.scope and then only creates sockets on
+ * request. lord-netperm sees that scope as the firewall app "lordos:<component>": its rules, its
+ * traffic and its row in Monitor → Network are its own, enforced by the kernel.
+ *
+ * The connector only does what lord-server could do itself (same user): socket(), and connect()
+ * to the address lord-server already chose (non-blocking: lord-server waits for the result). It
+ * takes no paths or names, runs nothing, and its request is a fixed-size struct validated on its
+ * side too. In a LordOS session a connector that can't be placed in its scope exits, and the
+ * request fails (fail closed: otherwise the traffic would count, and be decided, as lord-server's
+ * own). Outside a LordOS session (no LORDOS_SESSIOND: development, the tests) there are no scopes,
+ * and netconn_socket() makes the socket in lord-server itself, as before. */
+#ifndef LORD_SEC_NETCONN_H
+#define LORD_SEC_NETCONN_H
+
+#include <sys/socket.h>
+
+typedef enum { NC_MAIL = 0, NC_PRINT = 1, NC_LISTS = 2, NC_COUNT } NcComponent;
+
+/* The connector's name ("mail", "print", "lists"), the same as its firewall id after "lordos:". */
+const char *netconn_name(NcComponent c);
+
+/* A new non-blocking, close-on-exec socket of family fam (AF_INET / AF_INET6) and type
+ * (SOCK_STREAM / SOCK_DGRAM), created by comp's connector. With to, the connector also called
+ * connect() (EINPROGRESS for TCP): the caller polls for POLLOUT and reads SO_ERROR as usual; a
+ * connection the firewall denies fails with EPERM. Returns the fd, or -1 with errno set.
+ * Thread-safe. */
+int netconn_socket(NcComponent comp, int fam, int type, const struct sockaddr *to, socklen_t tolen);
+
+/* `lord-server --net-connector <component>`: the connector process (main.c). Never returns
+ * normally. */
+int netconn_main(int argc, char **argv);
+
+/* ---- the request (exposed for tests/netconn_test.c) ---- */
+#define NC_MAGIC 0x4c4e4331u /* "LNC1" */
+typedef struct {
+ unsigned magic;
+ unsigned char fam; /* 4 or 6 */
+ unsigned char type; /* 1 stream, 2 datagram */
+ unsigned char connect; /* 1: connect to addr:port */
+ unsigned char pad;
+ unsigned short port; /* host order */
+ unsigned short pad2;
+ unsigned char addr[16];
+ unsigned scope_id; /* IPv6 link-local */
+} NcReq;
+
+/* 0 if a connector may act on it (checked again on the connector's side). */
+int netconn_req_ok(const NcReq *r);
+/* Builds the request from a sockaddr; 0 ok. */
+int netconn_req_make(NcReq *r, int fam, int type, const struct sockaddr *to, socklen_t tolen);
+/* The connector's side: does one request (socket, connect) and returns the fd or -errno. */
+int netconn_serve_one(const NcReq *r);
+
+#endif
diff --git a/server/src/term/pty.c b/server/src/term/pty.c
index e0c9a83..b56d8a1 100644
--- a/server/src/term/pty.c
+++ b/server/src/term/pty.c
@@ -4,6 +4,7 @@
* login shell (the one in /etc/passwd), just like any terminal emulator.
* The child inherits no capabilities, blocked signals or descriptors from the server. */
#include "term.h"
+#include "../sec/cgroup.h"
#include "lord_version.h" /* build/gen: LordOS version (Makefile, from package.json) */
#include <errno.h>
@@ -51,6 +52,14 @@ static void child(int fd, const char *cwd, int id) {
memset(caps, 0, sizeof caps);
syscall(SYS_capset, &hdr, caps);
+ /* (0.8.4) the shell and every program it runs in lordos-net-terminal.scope: one "Terminal" app
+ * for the firewall and Monitor → Network (lordos:terminal), each connection with its program's
+ * name. Best effort: a terminal that can't be placed still opens. */
+ if (getenv("LORDOS_SESSIOND")) {
+ CgroupLimits lim = { 0, 0, 0 };
+ cgroup_enter("lordos-net-terminal.scope", &lim);
+ }
+
struct passwd *pw = getpwuid(getuid());
const char *home = getenv("HOME");
if (!home && pw) home = pw->pw_dir;
diff --git a/server/tests/fw_rules_test.c b/server/tests/fw_rules_test.c
index 0b5769c..6ffd048 100644
--- a/server/tests/fw_rules_test.c
+++ b/server/tests/fw_rules_test.c
@@ -66,6 +66,14 @@ static void test_canon(void) {
const char *bad[] = { "", "desktop:", ":x", "a b", "a\"b", "/usr/../bin/sh", "/usr//bin", "/usr/bin/", "rel/path",
"app-lordos-lpk-a:b.scope", "x\n", "\xc3\xa1" };
for (size_t i = 0; i < sizeof bad / sizeof *bad; i++) CHECK(fw_app_canon(bad[i], o, sizeof o) == -1);
+ /* (0.8.4) the components' scopes */
+ CHECK(!fw_component_scope("lordos-net-mail.scope", o, sizeof o) && !strcmp(o, "lordos:mail"));
+ CHECK(!fw_component_scope("lordos-net-terminal.scope", o, sizeof o) && !strcmp(o, "lordos:terminal"));
+ CHECK(!fw_app_canon("lordos:store", o, sizeof o) && !strcmp(o, "lordos:store"));
+ const char *badsc[] = { "lordos-net-evil.scope", "lordos-net-.scope", "lordos-net-mail", "lordos-net-mail.scope.scope",
+ "lordos-maild.scope", "app-lordos-mail.scope", "lordos-net-Mail.scope", NULL };
+ for (int i = 0; badsc[i]; i++) CHECK(fw_component_scope(badsc[i], o, sizeof o) == -1);
+ CHECK(fw_component_ok("lists") && !fw_component_ok("") && !fw_component_ok(NULL) && !fw_component_ok("vpn"));
char longid[200];
memset(longid, 'a', sizeof longid - 1);
longid[sizeof longid - 1] = 0;
diff --git a/server/tests/netconn_test.c b/server/tests/netconn_test.c
new file mode 100644
index 0000000..5497165
--- /dev/null
+++ b/server/tests/netconn_test.c
@@ -0,0 +1,156 @@
+/* netconn_test.c — the components' network connectors (src/sec/netconn.c, 0.8.4):
+ * request validation on both sides, the sockets the connector makes, the real connector process
+ * (this binary re-executed with --net-connector, as lord-server is) and its fail-closed start. */
+#define _GNU_SOURCE
+#include "sec/netconn.h"
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static int fails, checks;
+#define CHECK(c) do { checks++; if (!(c)) { fails++; fprintf(stderr, "%s:%d: failed: %s\n", __FILE__, __LINE__, #c); } } while (0)
+
+static NcReq good(void) {
+ NcReq r;
+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(443) };
+ inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
+ netconn_req_make(&r, AF_INET, SOCK_STREAM, (struct sockaddr *)&a, sizeof a);
+ return r;
+}
+
+static void test_requests(void) {
+ NcReq r = good();
+ CHECK(!netconn_req_ok(&r) && r.fam == 4 && r.type == 1 && r.connect == 1 && r.port == 443);
+ NcReq x = r; x.magic ^= 1; CHECK(netconn_req_ok(&x));
+ x = r; x.fam = 5; CHECK(netconn_req_ok(&x));
+ x = r; x.type = 3; CHECK(netconn_req_ok(&x));
+ x = r; x.connect = 2; CHECK(netconn_req_ok(&x));
+ x = r; x.port = 0; CHECK(netconn_req_ok(&x)); /* connect needs a port */
+ x = r; x.addr[9] = 1; CHECK(netconn_req_ok(&x)); /* IPv4 with bytes past the 4th */
+ x = r; x.scope_id = 2; CHECK(netconn_req_ok(&x));
+ x = r; x.pad = 1; CHECK(netconn_req_ok(&x));
+ x = r; x.connect = 0; CHECK(netconn_req_ok(&x)); /* no connect: no port either */
+ x.port = 0; CHECK(!netconn_req_ok(&x));
+ CHECK(netconn_req_ok(NULL));
+ NcReq y;
+ CHECK(netconn_req_make(&y, AF_UNIX, SOCK_STREAM, NULL, 0)); /* only internet sockets */
+ CHECK(netconn_req_make(&y, AF_INET, SOCK_RAW, NULL, 0)); /* no raw sockets */
+ struct sockaddr_in6 a6 = { .sin6_family = AF_INET6, .sin6_port = htons(80) };
+ CHECK(netconn_req_make(&y, AF_INET, SOCK_STREAM, (struct sockaddr *)&a6, sizeof a6)); /* family mismatch */
+ CHECK(netconn_req_make(&y, AF_INET6, SOCK_STREAM, (struct sockaddr *)&a6, 4)); /* short address */
+ CHECK(!netconn_req_make(&y, AF_INET6, SOCK_DGRAM, (struct sockaddr *)&a6, sizeof a6) && y.fam == 6 && y.type == 2);
+ x = r; x.magic = 0;
+ CHECK(netconn_serve_one(&x) == -EINVAL); /* the connector checks again */
+ CHECK(!strcmp(netconn_name(NC_MAIL), "mail") && !strcmp(netconn_name(NC_LISTS), "lists") && !netconn_name(NC_COUNT));
+}
+
+static int listener(int *port) {
+ int l = socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in a = { .sin_family = AF_INET };
+ inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
+ socklen_t al = sizeof a;
+ if (bind(l, (struct sockaddr *)&a, sizeof a) || listen(l, 4) || getsockname(l, (struct sockaddr *)&a, &al)) return -1;
+ *port = ntohs(a.sin_port);
+ return l;
+}
+
+static int connected(int fd) {
+ struct pollfd p = { fd, POLLOUT, 0 };
+ int err = 1;
+ socklen_t el = sizeof err;
+ return poll(&p, 1, 3000) == 1 && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &el) && !err;
+}
+
+static void test_sockets(int via_process) {
+ int port, l = listener(&port);
+ CHECK(l >= 0);
+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons((uint16_t)port) };
+ inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
+ int fd = netconn_socket(NC_MAIL, AF_INET, SOCK_STREAM, (struct sockaddr *)&a, sizeof a);
+ CHECK(fd >= 0);
+ if (fd >= 0) {
+ CHECK(connected(fd));
+ CHECK(fcntl(fd, F_GETFL) & O_NONBLOCK);
+ CHECK(fcntl(fd, F_GETFD) & FD_CLOEXEC);
+ int c = accept(l, NULL, NULL);
+ CHECK(c >= 0);
+ CHECK(write(fd, "x", 1) == 1);
+ char b;
+ CHECK(c >= 0 && read(c, &b, 1) == 1 && b == 'x');
+ if (c >= 0) close(c);
+ close(fd);
+ }
+ int u = netconn_socket(NC_PRINT, AF_INET, SOCK_DGRAM, NULL, 0);
+ int t = 0;
+ socklen_t tl = sizeof t;
+ CHECK(u >= 0 && !getsockopt(u, SOL_SOCKET, SO_TYPE, &t, &tl) && t == SOCK_DGRAM);
+ if (u >= 0) close(u);
+ /* a refused connection comes back as a socket whose SO_ERROR says so */
+ close(l);
+ fd = netconn_socket(NC_LISTS, AF_INET, SOCK_STREAM, (struct sockaddr *)&a, sizeof a);
+ CHECK(fd < 0 || !connected(fd));
+ if (fd >= 0) close(fd);
+ errno = 0;
+ CHECK(netconn_socket(NC_COUNT, AF_INET, SOCK_STREAM, NULL, 0) == -1 && errno == EINVAL);
+ printf("netconn-test: sockets %s: ok\n", via_process ? "from the connector processes" : "made in place (no LordOS session)");
+}
+
+/* the connector's own checks: only a known component, only with a SEQPACKET channel on fd 3 */
+static void test_connector_start(void) {
+ const char *bad[] = { "evil", "terminal", "store", "", NULL }; /* terminal and store have no connector */
+ for (int i = 0; bad[i]; i++) {
+ pid_t p = fork();
+ if (p == 0) {
+ char *argv[] = { "lord-net-x", "--net-connector", (char *)bad[i], NULL };
+ _exit(netconn_main(3, argv));
+ }
+ int st;
+ waitpid(p, &st, 0);
+ CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 2);
+ }
+ pid_t p = fork();
+ if (p == 0) {
+ close(3);
+ int f = open("/dev/null", O_RDONLY);
+ if (f != 3) dup2(f, 3);
+ char *argv[] = { "lord-net-mail", "--net-connector", "mail", NULL };
+ _exit(netconn_main(3, argv));
+ }
+ int st;
+ waitpid(p, &st, 0);
+ CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 2); /* fd 3 isn't a channel */
+}
+
+int main(int argc, char **argv) {
+ if (argc > 1 && !strcmp(argv[1], "--net-connector")) return netconn_main(argc, argv);
+ test_requests();
+ test_connector_start();
+ unsetenv("LORDOS_SESSIOND");
+ test_sockets(0);
+ /* in a "session" the sockets come from real connector processes (this binary re-executed);
+ * they can't be placed in a scope from here (no lord-sessiond for this process): with
+ * LORD_NETCONN_TEST_PLACED unset the test checks that the request fails closed */
+ setenv("LORDOS_SESSIOND", "1", 1);
+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(9) };
+ inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
+ int fd = netconn_socket(NC_MAIL, AF_INET, SOCK_STREAM, (struct sockaddr *)&a, sizeof a);
+ if (fd >= 0) {
+ /* placed after all (development cgroup delegation): the full path works */
+ close(fd);
+ test_sockets(1);
+ } else {
+ CHECK(errno == EPIPE || errno == ETIMEDOUT);
+ printf("netconn-test: a connector that can't enter its scope refuses (fail closed): ok\n");
+ }
+ printf("netconn-test: %d/%d passed\n", checks - fails, checks);
+ return fails ? 1 : 0;
+}