Securing Embedded Web Portals & nftables on Yocto
Technical note on avoiding exposed backend ports behind Nginx on Embedded Linux devices (such as a Raspberry Pi running Yocto) using loopback binding and nftables.
Symptom
A device web portal uses Nginx (port 80) with auth_basic as a reverse proxy to a Flask backend (for example, port 5000). Because the backend binds 0.0.0.0:5000 with no authentication and CORS *, any host on the LAN or SoftAP can call http://<pi-ip>:5000 directly to bypass login and trigger CSRF requests.
Root Cause
- Authentication exists only in Nginx, while the backend listens on all network interfaces (
0.0.0.0). - Minimal Embedded Linux images usually do not ship an active firewall (
iptables/nftables).
Fix 1: Bind the Backend to 127.0.0.1 (Recommended)
Since Nginx proxies locally to localhost, restrict the backend to loopback and remove CORS * (frontend and API share one origin through Nginx):
app.run(debug=False, host="127.0.0.1", port=5000)
This closes the exposure without changing the OS image. Also verify other local services (for example, Redis defaults to bind 127.0.0.1 and protected-mode yes).
Fix 2: Add an nftables Layer in Yocto (Defense-in-Depth)
1. Check Kernel Config & Modules on the Board
zcat /proc/config.gz | grep -E "NF_TABLES|NFT_"
ls /lib/modules/$(uname -r)/kernel/net/netfilter/ | grep -E "nf_tables|nfnetlink"
Minimum required options: CONFIG_NF_TABLES (=y or =m) and CONFIG_NF_TABLES_INET. If CONFIG_NFT_REJECT is disabled, use drop instead of reject.
2. Keep nftables Slim in Yocto
nftables lives in meta-networking. Its default PACKAGECONFIG pulls in python; trim it in local.conf or your distro config:
PACKAGECONFIG:pn-nftables = "readline json"
3. Sample Recipe (recipes-security/pi-firewall/pi-firewall.bb)
SUMMARY = "Minimal nftables firewall rules for Embedded Linux"
LICENSE = "CLOSED"
SRC_URI = "file://pi-firewall.nft file://pi-firewall.service"
inherit systemd
SYSTEMD_SERVICE:${PN} = "pi-firewall.service"
RDEPENDS:${PN} = "nftables kernel-module-nf-tables kernel-module-nfnetlink"
do_install() {
install -Dm 0644 ${WORKDIR}/pi-firewall.nft ${D}${sysconfdir}/pi-firewall.nft
install -Dm 0644 ${WORKDIR}/pi-firewall.service ${D}${systemd_system_unitdir}/pi-firewall.service
}
Ruleset pi-firewall.nft (drops external access to internal ports while keeping other traffic working):
flush ruleset
table inet pi_filter {
chain input {
type filter hook input priority 0; policy accept;
iif "lo" accept
tcp dport { 5000, 6379 } drop
}
}
Unit file pi-firewall.service:
[Unit]
Description=Embedded Linux nftables firewall
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/nft -f /etc/pi-firewall.nft
ExecStop=/usr/sbin/nft flush ruleset
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Note: Check nft list ruleset before using flush ruleset so you do not wipe existing NAT/masquerade or iptables-nft rules.