Make it possible for non root user to restrict / filter the network communications of podman containers following custom rules #27099
Replies: 10 comments 3 replies
-
To do that you would need to join the network namespace and add the firewall rules inside container basically. That is however only safe as long as the container doesn't get CAP_NET_ADMIN. I would consider such request outside of the scope of podman. There is just to much complexity in creating custom firewall rules and how they get exposed to users. You can already do this yourself by using oci-hooks that trigger on before the start of the container to add the rules automatically or write your own netavark plugin that manages the network creating fully https://github.com/containers/netavark/blob/main/plugin-API.md |
Beta Was this translation helpful? Give feedback.
-
Thank you! I need to read a bit more to fully understand your answer :) . A few questions meanwhile:
But then would this rules live inside the container (I would prefer to avoid this), or outside of the container and somehow inside the virtual network?
This is the default, right? :)
But that will be injected inside the container, right? Or can these rules apply to the virtual network "outside" the container, to which the container connects? Do you have pointers to examples related to the network filtering / firewalling I am looking for? I search but could not find.
I could not find documentation about if / how one can filter traffic according to a user spec by configuring netavark; do you have pointers to examples? |
Beta Was this translation helpful? Give feedback.
-
I was looking into the netavark firewall code; do I understand correctly that this under the hood does call to iptables through the iptables crate? Then I suppose that I will there too have the problem that I wont be able to set up the rules I want without being root? |
Beta Was this translation helpful? Give feedback.
-
For the default case (--network pasta) there is only the container netns and the host netns, pasta doesn't have any sort of firewall so the only way (if you cannot add them on the host) is to add them into the container.
Yes unless you use --privileged or --cap-add CAP_NET_ADMIN to add them explicitly the container should not have it.
The outside is the host using normal layer4 tcp/udp sockets, there is no virtual network outside of the container really. Now there is a special case for named (custom) networks (alas stuff setup with netavark) which uses an intermediate namespace called "rootles-netns", see #22943 (comment) The best thing would be to podman unshare --rootless-netns add the rulese there and then sleep inf to keep the podman process alive forever which then keeps the namespace around
You need to implement the full network management yourself there, see the bridge driver with its firewall interfaces in the netavark repo. |
Beta Was this translation helpful? Give feedback.
-
No you have full privs over the rootless network namespace since we run in a usernamespace. (compare |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for your explanations, this is very interesting though way above my level of competence, I will read more and try to digest this / play around more :) . Should this be moved into a discussion? :) I feel like we may exchange back and forth a bit in this thread and there may be information useful to other readers :) . |
Beta Was this translation helpful? Give feedback.
-
I think I understand what you mean 🎉 🤯 🤓 🙇 . Writing the stuff below to check my understanding is correct :) .
This shows that:
This is a bit of a brainmelt for me, but I suppose this is just putting the finger on what container technology is all about i.e. leveraging namespaces, right? :) I had used / read quite a bit about container technology over the years and had some superficial understanding of it as an end user, but I think now I just got the revelation of what it looks like deep down and first time I can really put the finger on it myself and not as an end user - thank you so much! :) |
Beta Was this translation helpful? Give feedback.
-
Trying to summarize a bit the options available, and to make sure I understand everything correctly :) . Sorry for the repetition with stuff above, this is all very new to me :) . option 1: firewall rules on an individual container basis
So if I understand correctly, this would put the firewall rules inside the namespace of each individual container network, and the code running in each individual container will not be allowed to change these as long as there is no CAP_NET_ADMIN rights. This can be done either using oci-hooks (I need to find exactly how to do it, but sounds like this should already be possible to do out of the box with writing the correct script; this should look just like a usual bash iptables rules setting script though, so should be no special difficulty?), or with a netavark plugin but then I would need to write this myself (maybe more complicated?). This would allow to set up firewall rules on each container network individually, with different rules for each container possibly i.e. high granularity. Is this correct? :) If so, this is certainly the way I should try to go :) . If I understand correctly, even though the firewall (eg iptables) rules would live in the container network namespace, it would still not be possible for code running inside the container to change these from within the container at runtime (ie bypass these rules) if not CAP_NET_ADMIN is set? So basically no way for a container application to bypass these firewall rules, even though the rules are living in the container network namespace? option 2: firewall rules in the common rootless-netns namespace
The rootless-netns namespace is here as a "middleman" when using containers with named custom networks. I could put firewall rules there too with a bit of "hacking" around. However do I understand correctly that then the firewall rules will apply to all containers started with named custom networks? So I could not have disjoint, "incompatible" sets of rules for several containers running at the same time on the same host using this method, so no granularity? |
Beta Was this translation helpful? Give feedback.
-
Just sharing another idea. Connect to the network server beforehand and pass the file descriptor to the container with In one shell do the following steps:
In another shell do the following steps
|
Beta Was this translation helpful? Give feedback.
-
@Luap99 just brought this to my attention, so a quick cross-link here (no additional information, at least not at the moment): https://bugs.passt.top/show_bug.cgi?id=139 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Feature request description
I am running as a non-admin of my host machine a podman container that contains secrets that I need to protect.
This podman container never needs to accept any ingress, and only needs to have egress with a specific IP address that I fully trust. Therefore, I would like podman to filter the traffic in and out of my container, blocking all incoming traffic, and allowing outgoing traffic only to the trusted IP while blocking all other outgoing traffic. This is more restrictive than the firewall rules of the host machine. This way, even in the unlikely event where the container is compromised / someone pushes a malicious base image, the container cannot for example exfiltrate the secrets.
It looks like I could achieve this if I was root on the host machine, by setting up a virtual network, configuring iptable or similar rules for it that match my needs, and use this virtual network as the network for my container. However, I am not admin on the host, so I cannot configure iptable rules at the host level.
Any way podman itself could somehow filter network traffic in and out of the container, at the "boundary" of the container and in a way I can as a non root configure, independently (and in addition) of the host firewall that I cannot configure?
Suggest potential solution
I would like podman to filter traffic in and out of my container, at the "boundary" of the container or its virtual network in a way I can configure as a non admin user, independently of host level iptables and similar which I cannot myself configure.
Have you considered any alternatives?
If I had been admin on the host, I could set up iptable or similar rules on a custom virtual network and use this as the container network. However, I am no admin on the host.
Beta Was this translation helpful? Give feedback.
All reactions