Skip to content

Systemd firewall integration suggestions #7327

Open

Description

@ipuustin

Submission type

  • Request for enhancement (RFE)

Systemd firewall integration: some suggestions

I saw the recent IPAddressDeny, IPAddressAllow and IPAccounting changes, but I think they could be improved. I have some suggestions which build on each other, so that they could be implemented even partially.

Zone declarations in systemd.network

Dealing with IP addresses in IPAddress* is difficult especially when doing system-level configuration for things like IoT devices, where the IP address ranges are not known beforehand. It would be easier to deal with interfaces and zones. I suggest the following change to the systemd.network file config:

[Firewall]

Zones=

  A comma-separated list of the firewall zones that the matching network
  interfaces belong to. All interfaces belong to zone "all". The loopback
  interface belongs to zone "local". Other zone names can be defined by
  just adding them as values to the Zones= keyword.

Then we could extend IPAddressDeny and friends to look (something) like this:

IPAddressDeny=zone:all
IPAddressAllow=zone:local,zone:lan

The big benefit would be that we could say that a service would be open to connections from ethernet interface or VPN interface, while not serving any clients trying to connect from the WLAN interface. Using zones is a pretty standard way for designing firewall rules.

Nftables integration

Even though eBPF is fast and allows for very fine-grained control, many (most) real-life hosts use a netfilter-based firewall. The key problem is to have a modular firewall ruleset that can be realiably extended from systemd. Nftables offers a much better support for this than iptables by having real firewall scripting ( nft), atomicity (the firewall is always in a known "complete" state), and data structures. Systemd could use this functionality for implementing service-level support for controlling port opening (and other things) in the firewall. This functionality would be orthogonal to the already supported eBPF IP address filtering.

Note that we would not get support for rules like "open port 22 to ssh.service". We would instead have rules like "open port 22 in the firewall".

OpenPorts= keyword

The syntax in systemd.service files could look like this:

OpenPorts=

  Which ports to open in the firewall for the service. Syntax is either:

     OpenPorts=22,200,300-305
  or
     OpenPorts=tcp:22,udp:200,udp:300-305
  or
     OpenPorts=zone:all:tcp:22,zone:local:udp:200,udp:300-305

  If no “udp” or “tcp” qualifier is given, the ports are opened in the firewall
  for both protocols. The requested ports are opened in the firewall when
  the service is started and closed when the service is stopped.

The benefit of this would be that opening ports in the firewall would be tied to service lifecycle -- no ports would be opened if the service was not running. The previously-introduced zones would be used here. Probably just supporting tcp and udp protocols would already be enough for the vast majority of services, and the others would need to be handled via the firewall ruleset itself (by adding a ruleset fragment to a pre-configured .d directory). The OpenPorts= keyword might be implemented in several ways, but below I'm considering how to do it in nftables.

Hooking systemd services to nftables ruleset

In order to use OpenPorts= , systemd needs to control the firewall ruleset. One way to define this would be to let systemd control the firewall script by introducing a new file type, say systemd.firewall. This would be directly tied to corresponding service file, so that starting (say) nftables.service would trigger loading of nftables.firewall.

The systemd.firewall file might look like this (other stuff like logging, counters, port forwarding, etc. could be added later):

Policy=

  Allowed values are "reject", "drop", or “accept”. This is the policy for the input
  chain that handles the incoming packets. If a packet goes through the processing
  chain without any matching rule, this verdict is applied to it. Having policy "accept"
  means that OpenPorts= keyword is a no-op.

RulesetFile=

  Which firewall ruleset file is loaded as the global configuration. Systemd defines a
  set of variables to be used in the ruleset file.

The ruleset file, if controlled by systemd, would need to have some pre-defined syntax so that systemd could hook the services to it. Using maps for both tcp- and udp-based services might be a clean and fast approach:

#!/usr/sbin/nft

table inet filter {

    map tcp_service_map {
        type inet_service : verdict
    }

    map udp_service_map {
        type inet_service : verdict
    }

    chain input {
        type filter hook input priority 1; policy $policy;

        tcp dport vmap @tcp_service_map;
        udp dport vmap @udp_service_map;
    }
}

It's possible to make this suitable for all protocols (like ICMP) using packet marking, but there is a related performance penalty.

How do we add systemd services OpenPort= declarations to the ruleset? There are several alternatives to this with varying complexity. One way is to generate firewall fragments and add them to the ruleset using nft's include keyword. Reloading the firewall ruleset using nft will add the fragments atomically. A fragment to open ssh port for zone "local" could look like this:

#!/usr/sbin/nft

table inet filter {
    include "zones.ruleset"
    chain sshd {
        iif @ZONE_LOCAL accept;
   }
}

add element inet filter tcp_service_map {22 : jump sshd};

(This assumes that zone definitions are in zones.ruleset.)

A heavier approach would be to write a "systemd-nft" application, which could parse nft ruleset file, consider the firewall settings in systemd unit files and create the atomic firewall transaction (possibly using libnftnl). Keeping compatibility with nft scripting makes sense, because it's developed in lock-step with kernel nftables support and it has been documented well.

Activity

  1. boucman commented on Nov 14, 2017

    @boucman
    Contributor

    I see two major problems with that approch

    • we link systemd and networkd in a way that make other network managers lose functionnalities or have to integrate deeply with systemd

    • the whole idea of the current approch is to not be a firewall. it's a simple per-service filtering. It's an extra layer independant of the real firwall and based on a different part of the kernel (that last point is important from a security point of view)

    I agree that the syntax is a little rough, and having a way to declare "bunch of IP adresses" in a cross-service way could be usefull, but replacing the eBPF approchi with something that integrates with netfilter is kinda against the idea of the feature...

  2. poettering commented on Nov 14, 2017

    @poettering
    Member

    When we started working on this we originally focussed on integrating this with iptables or nftables, but that's nasty, as doing ingress filtering by cgroup is messy then, as cgroup information is only available late in the packet path, but iptables/nftables are located much earlier in the path. This also fucks up accounting, as packets that are delivered to multiple cgroups have to be accounted for each service, but nftables/iptables are inherently a per-system concept, where they would be accounted only once.

    Long story short: nftables/iptables integration is not going to happen, and that's not really within systemd to do anything about, it's a technical necessity of the Linux kernel really. Sorry.

  3. poettering commented on Nov 14, 2017

    @poettering
    Member

    I am very open to add a couple of more hooks to systemd's bpf code, for example to restrict what ports a service can bind or connect to, or which network interfaces it can send/recv packets on.

    I am not a big fan of such a "zones" concept I must say. They tend to be defined within some specific, limited domain, and usually an a crappy, incomplete way (for example: changes to zone definitions are not immediately propagated to the the firewall). UNIX had its own way for this (/etc/networks) — while it is probably the most readily available concept of such, it's so badly defined it's pretty much useless.

    But anyway, I am not totally against "zones", but I must say, we should be very careful when introducing them, and do it properly if we do, and in some universal, properly dynamic way, and I am not sure how that could work IRL. (one idea that comes to mind would be to take benefit of ebpf's "map" feature, i.e. that they exist as a concept outside of systemd's own management, but you'd get the ability to link systemd to define "maps" externally.

  4. added
    RFE 🎁
    on Nov 14, 2017
  5. ipuustin commented on Nov 15, 2017

    @ipuustin
    ContributorAuthor

    @boucman: I guess the "zone" concept could be opened to other connection managers as well. Maybe there could be a D-Bus API for adding an interface to a zone? Or even a udev property for network devices? However, I don't really understand the "not a firewall" comment. If a piece of software does packet filtering for security reasons it is in my book a firewall. :-) The question is that how to make systemd service-level packet filtering work in tandem with iptables/nftables host-level packet filtering, or if systemd packet filtering could be extended to cover the common use cases, so that the host firewall could be left out. More about this below.

    @poettering: Ok, I'll not pursue the netfilter approach any further...

    However, how complete is the systemd packet filtering support planned eventually to be? My worry is that having some packet filtering done in systemd eBPF filters and some in nftables/iptables will be confusing and error-prone. In my view, an eBPF-based service firewalling might already be a pretty complete solution for edge hosts for which systemd manages all services. A few extra things would be needed (in addition to the zone support :-) :

    1. Host-wide default policy. Will the packets matching IPAddressDeny be dropped or rejected? How to control this?
    2. Connection tracking (and stateful firewalling in general). I think we should somehow distinguish between incoming and outgoing traffic. Let's say that I set in a root slice IPAddressDeny=all to prevent incoming connections from the Internet. Still, I would like to have a desktop application be able to connect to a service and receive packets in reply (say, browse the web). For this, something like IPAddressDenyIncoming=all would be nice, along with connection tracking.
    3. Global protocols. For example, some ICMPv6 packets should be dropped and some accepted (see https://tools.ietf.org/html/rfc4890).
    4. D-Bus control. It would be cool if I could (for example) add an IP address or zone to IPAddressDeny list without having to restart the service. This is more of a wish-list item however...

    I think the port and interface control would be nice additions to the eBPF code. Is anyone already working on them or are they something that patches would be welcome for?

  6. poettering commented on Nov 15, 2017

    @poettering
    Member

    Host-wide default policy. Will the packets matching IPAddressDeny be dropped or rejected? How to control this?

    cgroup/ebpf only allows dropping packets at the moment. Rejecting packets would need kernel work first.

    If you want to define host-wide drop/accept rules, then simply set them on the root slice i.e. on -.slice (or system.slice, if you want to apply it only to system services). The firewall applied to each unit is after all the combination of the unit's own firewall plus the firewall of all its parent slices.

    Connection tracking (and stateful firewalling in general). I think we should somehow distinguish between incoming and outgoing traffic. Let's say that I set in a root slice IPAddressDeny=all to prevent incoming connections from the Internet. Still, I would like to have a desktop application be able to connect to a service and receive packets in reply (say, browse the web). For this, something like IPAddressDenyIncoming=all would be nice, along with connection tracking.

    I figure this is something we probably could add: a filter that drop connection attempts, but let's other packets through.

    Global protocols. For example, some ICMPv6 packets should be dropped and some accepted (see https://tools.ietf.org/html/rfc4890).

    Such global filtering is probably left to nftables/iptables, I don't think this needs to be done by systemd.

    D-Bus control. It would be cool if I could (for example) add an IP address or zone to IPAddressDeny list without having to restart the service. This is more of a wish-list item however...

    This already exists: "systemctl set-property foo.service IPAddressDeny=1.2.3.0/24" will be applied instantly, and is done through a D-Bus call

  7. poettering commented on Nov 15, 2017

    @poettering
    Member

    I think the port and interface control would be nice additions to the eBPF code. Is anyone already working on them or are they something that patches would be welcome for?

    I am not aware of anyone working on that yet.

  8. topimiettinen commented on Nov 15, 2017

    @topimiettinen
    Contributor

    I'd like look at this later when CONFIG_CGROUP_BPF is enabled in Debian kernel.

  9. g0tar commented on Dec 23, 2021

    @g0tar
    Contributor

    Using zones is a pretty standard way for designing firewall rules

    No, it's not. It's just one of a many ways, the others including ipsets.

    The more elegant way of implementing "zones" is using realms (/etc/iproute2/rt_realms) or interface groups (/etc/iproute2/group) as they are standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFE 🎁pid1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions