Firewall (UFW) #107

Merged
samsapti merged 3 commits from :main into main 2022-11-22 20:05:01 +00:00
Owner

Add firewall setup using UFW

Add firewall setup using UFW
samsapti added 1 commit 2022-11-10 20:51:08 +00:00
samsapti added 1 commit 2022-11-10 21:03:56 +00:00
samsapti force-pushed main from 86589ad065 to 731b3f8877 2022-11-12 13:39:47 +00:00 Compare
samsapti force-pushed main from 731b3f8877 to fcfb13dcef 2022-11-12 13:53:24 +00:00 Compare
Author
Owner

(rebased onto latest main)

(rebased onto latest main)
samsapti added the
Security Hardening
label 2022-11-12 15:13:32 +00:00
samsapti force-pushed main from fcfb13dcef to d597a956ff 2022-11-12 18:42:14 +00:00 Compare
reynir reviewed 2022-11-13 10:49:39 +00:00
reynir left a comment
Owner

I have bad experience with using nftables and docker on the same host. Maybe ufw works better?

I have bad experience with using nftables and docker on the same host. Maybe ufw works better?
@ -0,0 +6,4 @@
- name: Allow necessary ports
community.general.ufw:
rule: allow
port: "{{ item }}"
Owner

Udp? Tcp? Both?

Udp? Tcp? Both?
Author
Owner

Do any of the services need UDP? Otherwise I can set them all to allow TCP only.

Do any of the services need UDP? Otherwise I can set them all to allow TCP only.
Owner

I think we may need UDP to receive responses to DNS requests. Otherwise I don't think we use UDP. My question was also what protocol(s) this covers.

I think we may need UDP to receive responses to DNS requests. Otherwise I don't think we use UDP. My question was also what protocol(s) this covers.
Author
Owner

Default is both.

Default is both.
Author
Owner

I think we may need UDP to receive responses to DNS requests.

No, firewall is only for blocking incoming requests. Responses to outgoing requests are not affected.

> I think we may need UDP to receive responses to DNS requests. No, firewall is only for blocking incoming requests. Responses to outgoing requests are not affected.
Owner

Yes, but you need to listen for the response :-)

Yes, but you need to listen for the response :-)
Author
Owner

Still, DNS requests work even if UFW blocks UDP, since it only blocks incoming requests. A DNS response is not a request ;)

Still, DNS requests work even if UFW blocks UDP, since it only blocks incoming _requests_. A DNS response is not a request ;)
Owner

Well, UDP is connectionless and doesn't know about requests and responses. The reply is sent to the sender on the sending port. It may be that the rule generated by ufw tracks the state and allows replies, but it is not immediately obvious from these invocations.

For other UDP uses it may not be desirable: for example, if the datagrams are metrics reports we don't expect a reply and it would be undesirable to allow incoming packets in that case.

Well, UDP is connectionless and doesn't know about requests and responses. The reply is sent to the sender on the sending port. It may be that the rule generated by ufw tracks the state and allows replies, but it is not immediately obvious from these invocations. For other UDP uses it may not be desirable: for example, if the datagrams are metrics reports we don't expect a reply and it would be undesirable to allow incoming packets in that case.
Author
Owner

You're right, perhaps I wasn't clear enough. Yes, Iptables/Nftables (which is UFW's backend(s)) tracks the state, and thus allows replies. It will only block clients trying to connect to the denied port on the host, even if a service is listening on the port (but not in the case of Docker described below). Unless you deny outgoing traffic, DNS lookups will work fine.

Also, I've never had problems with UFW blocking DNS lookups with this configuration.

You're right, perhaps I wasn't clear enough. Yes, Iptables/Nftables (which is UFW's backend(s)) tracks the state, and thus allows replies. It will only block clients trying to connect to the denied port on the host, even if a service is listening on the port (but not in the case of Docker described below). Unless you deny outgoing traffic, DNS lookups will work fine. Also, I've never had problems with UFW blocking DNS lookups with this configuration.
Author
Owner

I've changed it such that it only allows TCP on the specified ports.

I've changed it such that it only allows TCP on the specified ports.
samsapti marked this conversation as resolved
@ -0,0 +11,4 @@
- 22 # Gitea SSH
- 80 # HTTP
- 443 # HTTPS
- 389 # OpenLDAP
Owner

I'm not sure we actually want to expose ldap

I'm not sure we actually want to expose ldap
Author
Owner

Why is that? Do we only need it to be reachable internally on the host? If so, we should then also prefix the ports with 127.0.0.1: in the container config. Or if the only thing that needs to communicate with it is phpldapadmin, it shouldn't expose ports at all since they're on the same Docker network. If that's the case, I can fix it in another PR.

Why is that? Do we only need it to be reachable internally on the host? If so, we should then also prefix the ports with `127.0.0.1:` in the container config. Or if the only thing that needs to communicate with it is `phpldapadmin`, it shouldn't expose ports at all since they're on the same Docker network. If that's the case, I can fix it in another PR.
Owner

Yes, I think it's only used internally. Whether we have configured things correctly to use internal networks is another question. I don't know all that much about LDAP, and especially regarding security considerations, but I think we probably don't want to expose it. Maybe @valberg has an opinion.

Yes, I think it's only used internally. Whether we have configured things correctly to use internal networks is another question. I don't know all that much about LDAP, and especially regarding security considerations, but I think we probably don't want to expose it. Maybe @valberg has an opinion.
samsapti marked this conversation as resolved
Author
Owner

I have bad experience with using nftables and docker on the same host. Maybe ufw works better?

From my own experience, UFW works perfectly fine with Docker. Just be aware, that exposing container ports like 80:80 omits the firewall, so if you don't want it to do that you need to prefix it with localhost like so: 127.0.0.1:80:80.

> I have bad experience with using nftables and docker on the same host. Maybe ufw works better? From my own experience, UFW works perfectly fine with Docker. Just be aware, that exposing container ports like `80:80` omits the firewall, so if you don't want it to do that you need to prefix it with localhost like so: `127.0.0.1:80:80`.
Owner

What does "omits the firewall" mean?

If we can undo the change and won't lock ourselves out I think we should try it.

What does "omits the firewall" mean? If we can undo the change and won't lock ourselves out I think we should try it.
Author
Owner

What does "omits the firewall" mean?

It means that if UFW denies port 80, but a Docker container exposes 80:80, it won't be blocked. This is due to Docker interacting with iptables/nftables directly (which is UFW's backend).

If we can undo the change and won't lock ourselves out I think we should try it.

That should be possible. We can also keep an SSH session open in another terminal while we test.

> What does "omits the firewall" mean? It means that if UFW denies port 80, but a Docker container exposes `80:80`, it won't be blocked. This is due to Docker interacting with iptables/nftables directly (which is UFW's backend). > If we can undo the change and won't lock ourselves out I think we should try it. That should be possible. We can also keep an SSH session open in another terminal while we test.
Owner

What does "omits the firewall" mean?

It means that if UFW denies port 80, but a Docker container exposes 80:80, it won't be blocked. This is due to Docker interacting with iptables/nftables directly (which is UFW's backend).

Ah, that's a little disappointing. Then we can't rely on the firewall rules alone to work :(

> > What does "omits the firewall" mean? > > It means that if UFW denies port 80, but a Docker container exposes `80:80`, it won't be blocked. This is due to Docker interacting with iptables/nftables directly (which is UFW's backend). Ah, that's a little disappointing. Then we can't rely on the firewall rules alone to work :(
Author
Owner

Ah, that's a little disappointing. Then we can't rely on the firewall rules alone to work :(

Yes, unfortunately. But it still works in all other cases, so it's better than nothing.

> Ah, that's a little disappointing. Then we can't rely on the firewall rules alone to work :( Yes, unfortunately. But it still works in all other cases, so it's better than nothing.
samsapti added 1 commit 2022-11-15 19:42:25 +00:00
valberg approved these changes 2022-11-22 19:49:28 +00:00
samsapti merged commit d53c6d41dc into main 2022-11-22 20:05:01 +00:00
Sign in to join this conversation.
No description provided.