|
|
@@ -0,0 +1,118 @@
|
|
|
+[Unit]
|
|
|
+Description=Smartbotic VectorAPI Function Runner
|
|
|
+Documentation=https://git.smartbotics.ai/fszontagh/smartbotic-vectorapi
|
|
|
+# Same DB ordering as the server, and for the same reason: After= only, never
|
|
|
+# Requires=. The database may legitimately live on another host, and the binary
|
|
|
+# retries the connection for 30s before giving up, which Restart=on-failure
|
|
|
+# then turns into a slow retry loop instead of a dead service.
|
|
|
+After=network-online.target smartbotic-database.service
|
|
|
+Wants=network-online.target
|
|
|
+# Deliberately NO ordering against smartbotic-vectorapi.service in either
|
|
|
+# direction. The API must come up and serve everything else with no runner
|
|
|
+# present (it only refuses to STORE a function while no runner is healthy), and
|
|
|
+# a Before= here would make the API's Type=notify start wait on our database
|
|
|
+# connect loop at boot. RunnerPool health-checks us and re-checks after a
|
|
|
+# cooldown, so a runner that arrives late or restarts needs no unit ordering.
|
|
|
+
|
|
|
+[Service]
|
|
|
+# The binary calls sd_notify(READY=1) once the HTTP listener thread is up and
|
|
|
+# pings WATCHDOG=1 every 200ms (when built with libsystemd - the Debian build
|
|
|
+# base installs libsystemd-dev, so it is). If ever built without it, sd_notify
|
|
|
+# is a no-op macro and this must become Type=exec.
|
|
|
+Type=notify
|
|
|
+NotifyAccess=main
|
|
|
+# Comfortably above the 200ms ping interval: this exists to catch a runner
|
|
|
+# wedged by a pathological function, not to police scheduling jitter.
|
|
|
+WatchdogSec=30
|
|
|
+User=smartbotic-vectorapi-runner
|
|
|
+Group=smartbotic-vectorapi-runner
|
|
|
+ExecStart=/usr/bin/smartbotic-vectorapi-runner --config /etc/smartbotic-vectorapi/runner.json
|
|
|
+Restart=on-failure
|
|
|
+RestartSec=3
|
|
|
+# No state of its own: bytecode cache and engine pool are in-memory, and it
|
|
|
+# must not be able to write to the API's data dir. Hence "/" and no
|
|
|
+# ReadWritePaths at all - see the hardening block.
|
|
|
+WorkingDirectory=/
|
|
|
+LimitNOFILE=65536
|
|
|
+UMask=0077
|
|
|
+
|
|
|
+# Per-project function secrets (ctx.env) land here. NOT shipped in the .deb and
|
|
|
+# NOT a conffile: postinst creates an empty 0640 file if none exists, so dpkg
|
|
|
+# has no md5sum for it and an upgrade can never prompt about, reset the mode of,
|
|
|
+# or replace an operator's secrets. The leading "-" keeps us starting if an
|
|
|
+# operator deletes it outright.
|
|
|
+EnvironmentFile=-/etc/smartbotic-vectorapi/runner.env
|
|
|
+
|
|
|
+# ---------------------------------------------------------------------------
|
|
|
+# Security hardening
|
|
|
+#
|
|
|
+# Mirrors smartbotic-vectorapi.service, then goes further, because this process
|
|
|
+# executes untrusted tenant-authored JavaScript. Everything below the first
|
|
|
+# block is defense in depth for one scenario only: a bug in QuickJS or in the
|
|
|
+# ctx bindings lets a function reach native execution. In that case the JS
|
|
|
+# sandbox is already lost, and these are what is left.
|
|
|
+# ---------------------------------------------------------------------------
|
|
|
+NoNewPrivileges=true
|
|
|
+ProtectSystem=strict
|
|
|
+ProtectHome=true
|
|
|
+PrivateTmp=true
|
|
|
+PrivateDevices=true
|
|
|
+ProtectKernelTunables=true
|
|
|
+ProtectKernelModules=true
|
|
|
+ProtectControlGroups=true
|
|
|
+RestrictRealtime=true
|
|
|
+RestrictSUIDSGID=true
|
|
|
+RestrictNamespaces=true
|
|
|
+LockPersonality=true
|
|
|
+SystemCallFilter=@system-service
|
|
|
+SystemCallErrorNumber=EPERM
|
|
|
+
|
|
|
+# No ReadWritePaths: unlike the server there is no /var/lib to grant, so
|
|
|
+# ProtectSystem=strict leaves the whole filesystem read-only. An escaped
|
|
|
+# function therefore has nowhere to drop a payload, and in particular no write
|
|
|
+# access to the API's data dir.
|
|
|
+
|
|
|
+# QuickJS (quickjs-ng) is a bytecode interpreter - it never generates native
|
|
|
+# code - so denying writable-executable memory costs nothing and removes the
|
|
|
+# standard way a heap corruption bug in an engine becomes code execution.
|
|
|
+# Drop this ONLY if a future dependency genuinely needs W+X (a JIT would).
|
|
|
+MemoryDenyWriteExecute=true
|
|
|
+
|
|
|
+# The runner needs IP for the database (gRPC) and for ctx.fetch/embeddings; it
|
|
|
+# has no business with netlink, packet sockets or anything exotic. AF_UNIX
|
|
|
+# stays for the systemd notify socket and local resolvers.
|
|
|
+RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
|
|
+SystemCallArchitectures=native
|
|
|
+
|
|
|
+# Defense in depth behind functions/net_guard.hpp, which already refuses
|
|
|
+# loopback/RFC1918/link-local destinations in ctx.fetch. The prize for an SSRF
|
|
|
+# here is the cloud metadata service (169.254.169.254 on the production
|
|
|
+# droplet), i.e. the host's credentials, so it is worth blocking a second time
|
|
|
+# at the cgroup. NOTE: a host whose DNS resolver is itself link-local (some
|
|
|
+# clouds use 169.254.169.253) needs that address added to IPAddressAllow.
|
|
|
+IPAddressDeny=link-local multicast
|
|
|
+
|
|
|
+# A function must not be able to read the process table, the kernel log or the
|
|
|
+# host identity even after an escape - those are reconnaissance for the next
|
|
|
+# step, and nothing the runner itself needs.
|
|
|
+ProtectProc=invisible
|
|
|
+ProcSubset=pid
|
|
|
+ProtectClock=true
|
|
|
+ProtectHostname=true
|
|
|
+ProtectKernelLogs=true
|
|
|
+RemoveIPC=true
|
|
|
+
|
|
|
+# Backstops, not the primary limits: per-run memory/stack/wall-time ceilings are
|
|
|
+# enforced inside the engine (runner.json "runtime"). These only catch a bug in
|
|
|
+# that enforcement. MemoryMax must stay well above pool_size *
|
|
|
+# memory_limit_bytes (4 * 128 MiB by default) - raise both together, or a legit
|
|
|
+# pool growth turns into a cgroup OOM kill that takes every tenant's run with
|
|
|
+# it. CPUWeight rather than CPUQuota on purpose: a busy loop is already bounded
|
|
|
+# by the wall-time ceiling, so we only want the API to win the CPU, not to
|
|
|
+# throttle runs into failing.
|
|
|
+MemoryMax=1G
|
|
|
+TasksMax=128
|
|
|
+CPUWeight=50
|
|
|
+
|
|
|
+[Install]
|
|
|
+WantedBy=multi-user.target
|