linux icon indicating copy to clipboard operation
linux copied to clipboard

Restricting network access to Internet-global addresses only

Open Changaco opened this issue 6 months ago • 9 comments

Hello everyone,

After looking into many Linux sandboxing tools, I've found that some of them don't prevent applications which are granted Internet access from accessing the local network, because doing so without CAP_NET_ADMIN requires a userspace network stack (slirp4netns or pasta).

Since Landlock already does some network access control, and work is ongoing to add more (#6, #10, #57), I'd like to know what you think about the idea of creating a Landlock rule to only allow network traffic that any other computer directly or indirectly connected to the Internet could send.

Changaco avatar Jun 05 '26 14:06 Changaco

This would also be really useful as a way to easily prevent SSRF without the application having to be careful with DNS resolution etc, so I support the use case, although I think this should realistically be implemented in Landlock as a "allow this list of IP blocks" with the application being responsible for providing the list of allowed IPs (i.e. all public IP addresses).

micromaomao avatar Jun 05 '26 14:06 micromaomao

@micromaomao Asking each application to provide the list of allowed IP address blocks seems quite unrealistic and wasteful to me. IP addresses are considered global by default, ranges for local/private/carrier use are the exceptions, so checking whether an IP address is Internet-global means checking that it isn't in one of the reserved ranges. I think it would be fine to use a static list of reserved ranges compiled into the kernel.

Changaco avatar Jun 06 '26 10:06 Changaco

I implemented something close to this in userspace (sandlock, seccomp user-notification rather than Landlock, so I'm not proposing it as a substitute for the kernel feature) and I think you probably are already aware of.

The practical limit I hit doing this in userspace is the one motivating this issue: it costs a supervising process and a per-connect/sendto round-trip. A Landlock rule with the reserved ranges built in would avoid both and get the edge cases right once for everyone, so I'd support it.

congwang-mk avatar Jun 09 '26 04:06 congwang-mk

Since Landlock already does some network access control, and work is ongoing to add more (#6, #10, #57), I'd like to know what you think about the idea of creating a Landlock rule to only allow network traffic that any other computer directly or indirectly connected to the Internet could send.

The idea is to be able to control TCP and UDP (#10) connect/bind wrt ports, unix socket according to their path (#36), and to be able to deny any other socket type creation (#6). With these features, we should be able to create minimal app firewalls.

What do you mean exactly by "connected to the Internet"?

This would also be really useful as a way to easily prevent SSRF without the application having to be careful with DNS resolution etc, so I support the use case, although I think this should realistically be implemented in Landlock as a "allow this list of IP blocks" with the application being responsible for providing the list of allowed IPs (i.e. all public IP addresses).

There are legitimate use cases to handle IP addresses but it's difficult to manage them and keep them up-to-date. However, we could think of other ways to identify the peer (e.g. local or not, scoped to the sandbox...), but it is challenging to properly deal with inet/inet6 withou race conditions (I worked on a prototype).

The practical limit I hit doing this in userspace is the one motivating this issue: it costs a supervising process and a per-connect/sendto round-trip. A Landlock rule with the reserved ranges built in would avoid both and get the edge cases right once for everyone, so I'd support it.

Using seccomp notify is a workaround/hack and it makes sense as long as Landlock doesn't have such feature. Anyway, such supervisor is only ok for a sandboxer, but one of Landlock's goal is to create built-in sandboxing, which cannot rely on such a supervisor.

l0kod avatar Jun 09 '26 15:06 l0kod

Since Landlock already does some network access control, and work is ongoing to add more (#6, #10, #57), I'd like to know what you think about the idea of creating a Landlock rule to only allow network traffic that any other computer directly or indirectly connected to the Internet could send.

The idea is to be able to control TCP and UDP (#10) connect/bind wrt ports, unix socket according to their path (#36), and to be able to deny any other socket type creation (#6). With these features, we should be able to create minimal app firewalls.

What do you mean exactly by "connected to the Internet"?

Sorry for my English, I meant the same as the " Internet-global addresses" in $subject.

This would also be really useful as a way to easily prevent SSRF without the application having to be careful with DNS resolution etc, so I support the use case, although I think this should realistically be implemented in Landlock as a "allow this list of IP blocks" with the application being responsible for providing the list of allowed IPs (i.e. all public IP addresses).

There are legitimate use cases to handle IP addresses but it's difficult to manage them and keep them up-to-date. However, we could think of other ways to identify the peer (e.g. local or not, scoped to the sandbox...), but it is challenging to properly deal with inet/inet6 withou race conditions (I worked on a prototype).

Right, I think this is another reason why it is better to stay in user-space.

The practical limit I hit doing this in userspace is the one motivating this issue: it costs a supervising process and a per-connect/sendto round-trip. A Landlock rule with the reserved ranges built in would avoid both and get the edge cases right once for everyone, so I'd support it.

Using seccomp notify is a workaround/hack and it makes sense as long as Landlock doesn't have such feature. Anyway, such supervisor is only ok for a sandboxer, but one of Landlock's goal is to create built-in sandboxing, which cannot rely on such a supervisor.

My point is that if the user-space already has a mechanism to implement it, re-implementing it in-kernel might not have a big advantage (unless of course performance). More importantly, modifying and upgrading kernel takes months.

In case of any misunderstanding, this is not an objection, I am just trying to explain how to use it without changing kernel, for those who could not easily update kernels.

congwang-mk avatar Jun 11 '26 04:06 congwang-mk

With these features, we should be able to create minimal app firewalls.

A firewall should deny access to whatever might be on the local network. Landlock's existing and upcoming features don't provide a solution for that, hence my suggestion of a new rule.

After thinking about this some more and watching the recent talk of the Flatpak developers, specifically the part about network sandboxing, I think this hypothetical new rule should probably support limiting network access to any combination of localhost, LAN and Internet.

Changaco avatar Jun 11 '26 10:06 Changaco

I've put together lists of IP address ranges that could be used in an initial implementation of this feature. They may not be exactly right and should be reviewed by actual network experts. The data sources are the IANA registries:

localhost / loopback

  • 127.0.0.0/8
  • ::1/128

other non-global (LAN, CGNAT, etc)

  • 0.0.0.0/8 (“this network”)
  • 10.0.0.0/8 (private use)
  • 100.64.0.0/10 (carrier grade network address translation)
  • 169.254.0.0/16 (link-local)
  • 172.16.0.0/12 (private use)
  • 192.0.0.0/24 (reserved), except:
    • 192.0.0.9/32 (Port Control Protocol Anycast)
    • 192.0.0.10/32 (Traversal Using Relays around NAT Anycast)
  • 192.0.2.0/24 (documentation)
  • 192.88.99.2/32 (6a44-relay anycast address)
  • 192.168.0.0/16 (private use)
  • 198.18.0.0/15 (benchmarking)
  • 198.51.100.0/24 (documentation)
  • 203.0.113.0/24 (documentation)
  • 224.0.0.0/24 (local multicast)
  • 239.0.0.0/8 (organization multicast)
  • 240.0.0.0/4 (reserved)
  • 255.255.255.255/32 (broadcast)
  • ::/128 (unspecified address)
  • ::ffff:0:0/96 if the embedded IPv4 address is non-global
  • 64:ff9b::/96 if the embedded IPv4 address is non-global
  • 64:ff9b:1::/48 (local IPv4-IPv6 translation)
  • 100::/64 (black hole)
  • 100:0:0:1::/64 (dummy)
  • 2001::/23 (reserved)
  • 2001:2::/48 (benchmarking)
  • 2002::/16 if the embedded IPv4 address is non-global
  • 5f00::/16 (segment routing)
  • fc00::/7 (unique-local)
  • fe80::/10 (link-local)
  • fec0::/10 (deprecated site-local)
  • ff00::/8 (multicast), except:
    • ff0e::/16 (global multicast)

global

Everything else.

For IPv6, could also be a positive check on:

  • ::ffff:0:0/96 if the embedded IPv4 address is global
  • 64:ff9b::/96 if the embedded IPv4 address is global
  • 2001::/3, except:
    • 2001::/23 (reserved)
    • 2001:2::/48 (benchmarking)
  • 2002::/16 if the embedded IPv4 address is global
  • ff0e::/16 (global multicast)

unclear

I'm not sure how these ranges should be categorized.

  • 2001::/32 (Teredo)
  • 3ffe:831f::/32 (deprecated Teredo)

irrelevant

These ranges probably aren't worth treating in a specific way since they aren't supposed to be used anyway.

  • 2001:db8::/32 (documentation)
  • 3fff::/20 (documentation)

Changaco avatar Jun 21 '26 20:06 Changaco

I've put together lists of IP address ranges that could be used in an initial implementation of this feature. They may not be exactly right and should be reviewed by actual network experts. The data sources are the IANA registries:

localhost / loopback

  • 127.0.0.0/8
  • ::1/128

other non-global (LAN, CGNAT, etc)

  • 0.0.0.0/8 (“this network”)
  • 10.0.0.0/8 (private use)
  • 100.64.0.0/10 (carrier grade network address translation)
  • 169.254.0.0/16 (link-local)
  • 172.16.0.0/12 (private use)
  • 192.0.0.0/24 (reserved), except:
    • 192.0.0.9/32 (Port Control Protocol Anycast)
    • 192.0.0.10/32 (Traversal Using Relays around NAT Anycast)
  • 192.0.2.0/24 (documentation)
  • 192.88.99.2/32 (6a44-relay anycast address)
  • 192.168.0.0/16 (private use)
  • 198.18.0.0/15 (benchmarking)
  • 198.51.100.0/24 (documentation)
  • 203.0.113.0/24 (documentation)
  • 224.0.0.0/24 (local multicast)
  • 239.0.0.0/8 (organization multicast)
  • 240.0.0.0/4 (reserved)
  • 255.255.255.255/32 (broadcast)
  • ::/128 (unspecified address)
  • ::ffff:0:0/96 if the embedded IPv4 address is non-global
  • 64:ff9b::/96 if the embedded IPv4 address is non-global
  • 64:ff9b:1::/48 (local IPv4-IPv6 translation)
  • 100::/64 (black hole)
  • 100:0:0:1::/64 (dummy)
  • 2001::/23 (reserved)
  • 2001:2::/48 (benchmarking)
  • 2002::/16 if the embedded IPv4 address is non-global
  • 5f00::/16 (segment routing)
  • fc00::/7 (unique-local)
  • fe80::/10 (link-local)
  • fec0::/10 (deprecated site-local)
  • ff00::/8 (multicast), except:
    • ff0e::/16 (global multicast)

global

Everything else.

For IPv6, could also be a positive check on:

  • ::ffff:0:0/96 if the embedded IPv4 address is global
  • 64:ff9b::/96 if the embedded IPv4 address is global
  • 2001::/3, except:
    • 2001::/23 (reserved)
    • 2001:2::/48 (benchmarking)
  • 2002::/16 if the embedded IPv4 address is global
  • ff0e::/16 (global multicast)

unclear

I'm not sure how these ranges should be categorized.

  • 2001::/32 (Teredo)
  • 3ffe:831f::/32 (deprecated Teredo)

irrelevant

These ranges probably aren't worth treating in a specific way since they aren't supposed to be used anyway.

  • 2001:db8::/32 (documentation)
  • 3fff::/20 (documentation)

It seems to me like hardcoding this kind of policy (IP address lists) directly in the kernel is ill advised.

If space is a concern, IP address ranges (both v6 and v4) can be efficiently stored as a trie, a prefix matching data structure that allows constant time lookup, and efficient space storage. So there is no reason to encode any sort of address list in the kernel. BPF does the same thing through BPF_MAP_TYPE_LPM_TRIE.

In practice, these kind of policy definition should be left to userspace, with the kernel itself only responsible for loading and enforcing whatever the userspace policy (your ip address lists) is. Your "local addresses only" might be different than mine. Do you include ULAs? What about ipv6-v4 translation mechanisms? This is not something we should be hardcoding

This feature is also harder to implement kernel side than you may expect. For example, certain types of (CAP_NET_RAW) sockets AF_PACKET AF_XDP bypass much of the kernel's networking stack, making filtering harder. How do you handle already open sockets at enforcement time? How do you deal with connectionless protocols (udp)?

I also would like to pose whether a network namespace based solution, like https://github.com/rootless-containers/slirp4netns, would address the use case better.

RazeLighter777 avatar Jun 24 '26 14:06 RazeLighter777

I agree with @RazeLighter777, such IP blocks should be handled by user space. However, an alternative might be to enforce restrictions acconding to the network interface used for destination (e.g. only allow connections to local ports). The way packet routing works in the Linux kernel might be a challenge though.

l0kod avatar Jul 08 '26 09:07 l0kod