Skip to contentSkip to conversation

Automatically restart Linux systemd OpenVPN client service on failure

Openvpn-devel Melvin Vermeeren 4 participants 7 messages May 13, 2023 – May 15, 2023

lore
Share & tools
lore
#0 lore raw

Hi all,

Today I changed some OpenVPN server configuration and restarted the service, thinking all clients will reconnect just fine as usual. Unlike other days however, all Linux clients ended up exploding due to unexpected tun-device recreation and lack of permission to do so, resulting in fatal exit with service failure. Quite a surprise to me, and a bit of a mess to fix!

After first researching Debian's OpenVPN package, I find the service is provided by OpenVPN itself. Looking further, I find a PR on GitHub from 2018.
https://github.com/OpenVPN/openvpn/pull/104

After doing some thinking I shared my thoughts there as well.
https://github.com/OpenVPN/openvpn/pull/104#issuecomment-1546384702

However, mailing list seems preferred for OpenVPN, so here it is! I'll quote my message on GitHub here for convenience, I'll also add a ref from GitHub to this mail on the mailing list archives once I see it live.

Quoted 14 lines

Today I was bitten by this issue. I adjusted some seemingly irrelevant options on a OpenVPN server instance and restarted it, expecting the clients to handle it gracefully. Because the clients drop a bunch of privileges however and OpenVPN decided recreation of the tun device was needed it ended in an explosion where all clients fatal exited and as a result the openvpn- [email protected]... for them failed. Quite a mess I had to manually untangle to get critical systems talking to each other again.

Personally I think the default should be to retry. A system service should do it's best to keep working, as it usually means it's part of a server or managed workstation, etc, and not a human user interacting with a VPN GUI or similar. Especially nasty if you rely on the VPN for remote access to the other machines, luckily didn't happen for me today.

Quoted 5 lines

I genuinely don't see a use case where you don't want it to automatically restart and try again on failure! Imagine if a DHCP client would stop trying if it encountered a very weird situation and fatal exited, resulting in a host without network connectivity. This with OpenVPN client feels very similar in principle to that.

It has been quite some years since 2018, and with things like remote work etc getting more common client VPN reliability I feel is much more important than before. Losing the client VPN could mean having to physically travel to the machine in question in order to fix it, as remote access could be impossible!

Thoughts on changing this by default?

Thanks,

Signature · 2 lines
Melvin Vermeeren
Systems engineer
#1 lore raw

Am 13.05.23 um 16:24 schrieb Melvin Vermeeren:

Quoted 7 lines

Hi all,

Today I changed some OpenVPN server configuration and restarted the service, thinking all clients will reconnect just fine as usual. Unlike other days however, all Linux clients ended up exploding due to unexpected tun-device recreation and lack of permission to do so, resulting in fatal exit with service failure. Quite a surprise to me, and a bit of a mess to fix!

Can you provide some more detail here? Otherwise this seem a bit nebulously to me what exactly explodes and goes wrong.

Arne

#2 lore raw

Hi Arne,

Quoted 2 lines · On Saturday, 13 May 2023 16:28:29 CEST Arne Schwabe wrote:

Can you provide some more detail here? Otherwise this seem a bit nebulously to me what exactly explodes and goes wrong.

I changed the --keepalive setting on the server, lowering the timeout from 120 to 60 seconds to make things somewhat more responsive. Figured a small push change like this wouldn't really cause trouble.

This triggered a TUN device close/reopen, which makes sense in hindsight. Clients are configured to drop privilege with --user and --group after connecting, so they could not make the change needed.

Quoted 5 lines

NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device.
...
ERROR: Cannot ioctl TUNSETIFF tun_vpn: Operation not permitted (errno=1) Exiting due to fatal error

This is not the only case though, I also had some problems with DCO on a Debian testing client recently, resulting in failure after network was offline for quite a while. Official Debian package DCO 0.0+git20230324-1.

Quoted 4 lines

TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)
TLS Error: TLS handshake failed
... A lot of the same ...

Quoted 8 lines

TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)
TLS Error: TLS handshake failed
TLS: tls_multi_process: killed expiring key
No encryption key found. Purging data channel keys
dco_del_key: netlink out of memory error: No buffer space available (errno=105)
Exiting due to fatal error

In some cases this is arguably user/admin configuration error, but in other cases it is genuinely bugs or problems that can cause OpenVPN to fail.

From a security point of view, it's also better to drop privileges after connection and if things really change a lot error, fail, exit and get restarted by systemd, with privileges, set things up and drop them again. The alternative is to forever run OpenVPN client process as root.

In conclusion I don't see a reason to *not* automatically restart and retry when configured as a system service through systemd. GUI clients might be a different matter, though even there I'd argue for auto-reconnect until the human user tells the program to stop.

Cheers,

Signature · 2 lines
Melvin Vermeeren
Systems engineer
#3 lore raw

Am 13.05.23 um 16:47 schrieb Melvin Vermeeren:

Quoted 20 lines

Hi Arne,

Quoted 2 lines · On Saturday, 13 May 2023 16:28:29 CEST Arne Schwabe wrote:

Can you provide some more detail here? Otherwise this seem a bit nebulously to me what exactly explodes and goes wrong.

I changed the --keepalive setting on the server, lowering the timeout from 120 to 60 seconds to make things somewhat more responsive. Figured a small push change like this wouldn't really cause trouble.

This triggered a TUN device close/reopen, which makes sense in hindsight. Clients are configured to drop privilege with --user and --group after connecting, so they could not make the change needed.

Quoted 5 lines

NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device.
...
ERROR: Cannot ioctl TUNSETIFF tun_vpn: Operation not permitted (errno=1) Exiting due to fatal error

Quoted 4 lines

This is not the only case though, I also had some problems with DCO on a Debian testing client recently, resulting in failure after network was offline for quite a while. Official Debian package DCO 0.0+git20230324-1.

Yes the option we ignore to check if we have to reopen the tun device is quite short. We should probably turn this into a positive list instead of assuming that all options need to trigger to a tun reopen/close.

Arne

#4 lore raw

Hi,

Quoted 3 lines · Arne Schwabe

Yes the option we ignore to check if we have to reopen the tun device is quite short. We should probably turn this into a positive list instead of assuming that all options need to trigger to a tun reopen/close.

True, but even so - shouldn't the systemd unit file just restart the openvpn client anyway?

(I see myself having to restart the OpenSolaris Community VPN client ever so often...)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#5 lore raw
Quoted 7 lines · Gert Doering

Hi,

Quoted 3 lines · Arne Schwabe

Yes the option we ignore to check if we have to reopen the tun device is quite short. We should probably turn this into a positive list instead of assuming that all options need to trigger to a tun reopen/close.

Quoted 5 lines

True, but even so - shouldn't the systemd unit file just restart the openvpn client anyway?

(I see myself having to restart the OpenSolaris Community VPN client ever so often...)

We had this discussion back in the days when we added the automatic restart on servers. In the unit file for [email protected]... we added:

RestartSec=5s
Restart=on-failure

We ended up only enabling this on the server config by default, as it was some good points (which I don't recall right now) about not restarting the client configs automatically. It might have been due to avoid DDoS the server in larger deployments, if a bad option would be pushed to all clients or something like that.

This is anyhow very easily added on a per-config bases using a systemd feature:

    # systemctl edit [email protected]...

In the file opened in the editor, just add "[Service]" and those two lines mentioned earlier. You might want to have a bit longer "Restart" timer, but that's up to the local sysadmin to judge best.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#6 lore raw

Hi all,

Quoted 5 lines · On Sunday, 14 May 2023 21:12:08 CEST David Sommerseth wrote:

We ended up only enabling this on the server config by default, as it was some good points (which I don't recall right now) about not restarting the client configs automatically. It might have been due to avoid DDoS the server in larger deployments, if a bad option would be pushed to all clients or something like that.

These are some good points. For server-side currently it is RestartSec=5s, which I agree makes sense for server. To avoid too aggressive connection spamming can we not set the client restart interval to a much higher value?

If OpenVPN itself does not fatal error/crash, the back-off interval mechanism is used for reconnect with the --connect-retry option. The default maximum wait time after a bunch of attempts is 300 seconds (5 minutes).

Knowing this is the worst case scenario for all common setups, I think we safely can set RestartSec=300s without any risk of DDoS or similar, as this timeout matches the slowest reconnection interval of OpenVPN internally.

Personally I think RestartSec=60s or RestartSec=120s is also fine, but I understand being conservative with a change like this. In the end I don't really care if it takes "one" or "a few" minutes as long as the client does reconnect if something went wrong inside or outside the OpenVPN process.

Thoughts on this?

Thanks!

Signature · 2 lines
Melvin Vermeeren
Systems engineer