From d6c5c010f3764e2a7bc7c6b654ef7986d704199b Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 02:28:41 +0000 Subject: per-VM egress whitelists Whitelists are now named, any number per user, in /var/lib/fc-egress/USER/LIST, and each VM uses one of them: run-firecracker-vm --egress LIST (default: default). The run script writes the list's name to /run/fc-egress/TAP when it takes the tap, and empties it when the VM exits; a list that doesn't exist allows nothing. A user's VMs can only use that user's lists. So the unit of policy is the tap (i.e. the running VM) rather than the user: nftables sets allow_TAP, nginx decrypt sockets decrypt-TAP.sock, egress_taps.lua maps guest addresses to tap and user. fcbr0-firewall updates a tap when its file in /run/fc-egress changes, and all of a user's taps when anything in the user's directory does. nginx's connection limit stays per user. fc-egress takes -l LIST (default: default), and has new commands lists and vms (running VMs' taps and their lists). Log lines now say user, tap and (nginx's) list: "ALLOW|DENY USER TAP LIST ...", kernel: "fc-egress ALLOW|DENY USER TAP". The old rules file, /var/lib/fc-egress/USER/allowed, is now just a list named "allowed"; rename it to "default" to keep using it. --- modules/host/egress.lua | 67 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 20 deletions(-) (limited to 'modules/host/egress.lua') diff --git a/modules/host/egress.lua b/modules/host/egress.lua index 6c507a1..c4c92eb 100644 --- a/modules/host/egress.lua +++ b/modules/host/egress.lua @@ -1,8 +1,9 @@ -- The guests' egress policy, for nginx (OpenResty), see default.nix. -- --- Per user: whose a guest is, egress_users.lua says by its address (which --- the bridge makes sure of, see default.nix). The user's rules are in --- RULES/USER/allowed, one per line, read on every connection and request: +-- Per VM: which tap (and so whose) a guest is, egress_taps.lua says by its +-- address (which the bridge makes sure of, see default.nix). VMS/TAP has +-- the name of the VM's list, RULES/USER/LIST its rules, one per line, both +-- read on every connection and request: -- example.com example.com and its subdomains, passed through -- without decrypting -- example.com/foo/ only requests whose path starts with /foo/, @@ -15,19 +16,36 @@ local _M = {} -local RULES = os.getenv("FC_EGRESS_RULES") -- RULES/USER/allowed +local RULES = os.getenv("FC_EGRESS_RULES") -- RULES/USER/LIST +local VMS = os.getenv("FC_EGRESS_VMS") -- VMS/TAP: LIST local STATE = os.getenv("FC_EGRESS_STATE") -- ca.key, ca.pem -local RUN = os.getenv("FC_EGRESS_RUN") -- RUN/decrypt-USER.sock: the http server -local users = require("egress_users") -- guest address -> user +local RUN = os.getenv("FC_EGRESS_RUN") -- RUN/decrypt-TAP.sock: the http server +local guests = require("egress_taps") -- guest address -> { tap, user } +local by_tap = {} +for _, g in pairs(guests) do + by_tap[g.tap] = g +end local function valid_name(s) return s ~= nil and s:match("^[a-z0-9-]+[a-z0-9.-]*$") ~= nil and not s:find("%.%.") and s:sub(-1) ~= "." end -local function rules(user) +-- The name of the guest's list, nil if none (or a bad one), and its +-- description for the log: USER TAP LIST. +local function list(guest) + local f = io.open(VMS .. "/" .. guest.tap) + local name = f and f:read(256) + if f then + f:close() + end + name = name and name:match("^([A-Za-z0-9_-]+)\n?$") + return name, guest.user .. " " .. guest.tap .. " " .. (name or "-") +end + +local function rules(guest, name) local domains, paths = {}, {} - local f = io.open(RULES .. "/" .. user .. "/allowed") + local f = name and io.open(RULES .. "/" .. guest.user .. "/" .. name) if not f then return domains, paths end @@ -49,12 +67,12 @@ local function rules(user) end -- nil (deny), "pass", or the list of allowed path prefixes -local function policy(host, user) +local function policy(host, guest, name) host = host and host:lower() - if not user or not valid_name(host) then + if not guest or not name or not valid_name(host) then return nil end - local domains, paths = rules(user) + local domains, paths = rules(guest, name) local prefixes = {} local suffix = host while suffix do @@ -74,15 +92,20 @@ end -- stream, preread: pass through, decrypt, or close function _M.preread() local sni = ngx.var.ssl_preread_server_name - local user = users[ngx.var.remote_addr] - local p = policy(sni, user) + local guest = guests[ngx.var.remote_addr] + local name, who + if guest then + name, who = list(guest) + end + local p = policy(sni, guest, name) + who = who or "- - -" if p == "pass" then ngx.var.egress_upstream = sni:lower() .. ":443" - ngx.var.egress_log = "ALLOW " .. user .. " " .. sni:lower() + ngx.var.egress_log = "ALLOW " .. who .. " " .. sni:lower() elseif p then - ngx.var.egress_upstream = "unix:" .. RUN .. "/decrypt-" .. user .. ".sock" + ngx.var.egress_upstream = "unix:" .. RUN .. "/decrypt-" .. guest.tap .. ".sock" else - ngx.var.egress_log = "DENY " .. (user or "-") .. " " .. (sni ~= "" and sni or "-") + ngx.var.egress_log = "DENY " .. who .. " " .. (sni ~= "" and sni or "-") return ngx.exit(403) end end @@ -130,7 +153,7 @@ local function make_cert(name) end -- Whose the connection is isn't known here; but only connections that --- some user's path rules allow get here (see preread), and each request +-- some VM's path rules allow get here (see preread), and each request -- gets checked in access. function _M.certificate() local ssl = require("ngx.ssl") @@ -151,12 +174,16 @@ end -- http, access: check host and path of decrypted requests function _M.access() - local user = (ngx.var.server_addr or ""):match("decrypt%-(.+)%.sock$") - ngx.var.egress_user = user or "-" + local guest = by_tap[(ngx.var.server_addr or ""):match("decrypt%-(.+)%.sock$")] + local name, who + if guest then + name, who = list(guest) + end + ngx.var.egress_guest = who or "- - -" local sni = (ngx.var.ssl_server_name or ""):lower() local host = (ngx.var.host or ""):lower() local path = ngx.unescape_uri((ngx.var.request_uri or ""):match("^[^?]*")) - local p = policy(sni, user) + local p = policy(sni, guest, name) local ok = type(p) == "table" and host == sni and path:sub(1, 1) == "/" if ok then for segment in path:gmatch("[^/]+") do -- cgit v1.3.1