dco: process messages immediately after read
Some checks are pending
Build / Check code style with clang-format (push) Waiting to run
Build / Android - arm64-v8a (push) Waiting to run
Build / gcc-mingw - x64 - OSSL (push) Waiting to run
Build / gcc-mingw - x86 - OSSL (push) Waiting to run
Build / mingw unittest argv - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest auth_token - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest buffer - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest crypto - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest cryptoapi - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest misc - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest ncp - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest options_parse - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest packet_id - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest pkt - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest provider - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest ssl - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest tls_crypt - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest user_pass - x64 - OSSL (push) Blocked by required conditions
Build / mingw unittest argv - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest auth_token - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest buffer - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest crypto - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest cryptoapi - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest misc - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest ncp - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest options_parse - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest packet_id - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest pkt - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest provider - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest ssl - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest tls_crypt - x86 - OSSL (push) Blocked by required conditions
Build / mingw unittest user_pass - x86 - OSSL (push) Blocked by required conditions
Build / gcc - ubuntu-24.04 - OpenSSL 3.0.13 --enable-pkcs11 (push) Waiting to run
Build / gcc - ubuntu-22.04 - OpenSSL 3.0.2 --enable-pkcs11 (push) Waiting to run
Build / gcc - ubuntu-22.04 - mbed TLS (push) Waiting to run
Build / gcc - ubuntu-24.04 - mbed TLS (push) Waiting to run
Build / clang-asan - ubuntu-22.04 - mbedtls (push) Waiting to run
Build / clang-asan - ubuntu-22.04 - openssl (push) Waiting to run
Build / clang-asan - ubuntu-24.04 - mbedtls (push) Waiting to run
Build / clang-asan - ubuntu-24.04 - openssl (push) Waiting to run
Build / macos-14 - libressl - asan (push) Waiting to run
Build / macos-14 - openssl@3 - asan (push) Waiting to run
Build / macos-15 - libressl - asan (push) Waiting to run
Build / macos-15 - openssl@3 - asan (push) Waiting to run
Build / macos-26 - libressl - asan (push) Waiting to run
Build / macos-26 - openssl@3 - asan (push) Waiting to run
Build / macos-14 - libressl - normal (push) Waiting to run
Build / macos-14 - openssl@3 - normal (push) Waiting to run
Build / macos-15 - libressl - normal (push) Waiting to run
Build / macos-15 - openssl@3 - normal (push) Waiting to run
Build / macos-26 - libressl - normal (push) Waiting to run
Build / macos-26 - openssl@3 - normal (push) Waiting to run
Build / msbuild - amd64 - openssl (push) Waiting to run
Build / msbuild - amd64-clang - openssl (push) Waiting to run
Build / msbuild - arm64 - openssl (push) Waiting to run
Build / msbuild - x86 - openssl (push) Waiting to run
Build / msbuild - x86-clang - openssl (push) Waiting to run
Build / clang asan - ubuntu-22.04 - libressl (push) Waiting to run
Build / gcc normal - ubuntu-22.04 - libressl (push) Waiting to run
Build / clang asan - ubuntu-22.04 - mbedtls3 (push) Waiting to run
Build / gcc normal - ubuntu-22.04 - mbedtls3 (push) Waiting to run
Build / clang asan - ubuntu-24.04 - awslc (push) Waiting to run
Build / gcc normal - ubuntu-24.04 - awslc (push) Waiting to run
Deploy Doxygen documentation to Pages / build (push) Waiting to run
Deploy Doxygen documentation to Pages / deploy (push) Blocked by required conditions

Currently, reading and processing of incoming DCO messages are
decoupled: notifications are read, parsed, and the relevant information
is stored in fields of dco_context_t for later processing (with the only
exception being stats). This approach is problematic on Linux, since
libnl does not allow reading a single netlink message at a time, which
can result in loss of information when multiple notifications are
available.

This change adopts a read -> parse -> process paradigm. On Linux,
processing is now invoked directly from within the parsing callback,
which libnl calls for each received netlink packet. The other interfaces
are adapted accordingly to unify the processing model across all
platforms.

On Linux, however, a DEL_PEER notification from the kernel triggers a
GET_PEER request from userspace, which clutters the netlink
communication logic and can lead to errors or even process exit when
multiple simultaneous DEL_PEER notifications are received. To avoid
this, introduce a lock that prevents requesting stats while we are still
busy parsing other messages.

Reported-by: Stefan Baranoff <stefan.baranoff@trinitycyber.com>
Github: OpenVPN/openvpn#900
Github: OpenVPN/openvpn#918
Github: fixes OpenVPN/openvpn#919

Change-Id: Iefc251cb4483c0b9fb9d6a5207db4445cd884d52
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1403
Message-Id: <20251128112705.12613-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34785.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
This commit is contained in:
Ralf Lici 2025-11-28 12:26:59 +01:00 committed by Gert Doering
parent 0effd6cae3
commit 7791f5358a
9 changed files with 88 additions and 44 deletions

View file

@ -127,12 +127,13 @@ int open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev);
void close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx);
/**
* Read data from the DCO communication channel (i.e. a control packet)
* Read and process data from the DCO communication channel
* (i.e. a control packet)
*
* @param dco the DCO context
* @return 0 on success or a negative error code otherwise
*/
int dco_do_read(dco_context_t *dco);
int dco_read_and_process(dco_context_t *dco);
/**
* Install a DCO in the main event loop
@ -305,7 +306,7 @@ close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx)
}
static inline int
dco_do_read(dco_context_t *dco)
dco_read_and_process(dco_context_t *dco)
{
ASSERT(false);
return 0;

View file

@ -578,7 +578,7 @@ dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t *n
}
int
dco_do_read(dco_context_t *dco)
dco_read_and_process(dco_context_t *dco)
{
struct ifdrv drv;
uint8_t buf[4096];
@ -684,11 +684,21 @@ dco_do_read(dco_context_t *dco)
default:
msg(M_WARN, "%s: unknown kernel notification %d", __func__, type);
dco->dco_message_type = 0;
break;
}
nvlist_destroy(nvl);
if (dco->c->mode == CM_TOP)
{
multi_process_incoming_dco(dco);
}
else
{
process_incoming_dco(dco);
}
return 0;
}

View file

@ -49,6 +49,15 @@
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
/* When parsing multiple DEL_PEER notifications, openvpn tries to request stats
* for each DEL_PEER message (see setenv_stats). This triggers a GET_PEER
* request-reply while we are still parsing the rest of the initial
* notifications, which can lead to NLE_BUSY or even NLE_NOMEM.
*
* This basic lock ensures we don't bite our own tail by issuing a dco_get_peer
* while still busy receiving and parsing other messages.
*/
static bool __is_locked = false;
/* libnl < 3.5.0 does not set the NLA_F_NESTED on its own, therefore we
* have to explicitly do it to prevent the kernel from failing upon
@ -127,7 +136,9 @@ nla_put_failure:
static int
ovpn_nl_recvmsgs(dco_context_t *dco, const char *prefix)
{
__is_locked = true;
int ret = nl_recvmsgs(dco->nl_sock, dco->nl_cb);
__is_locked = false;
switch (ret)
{
@ -1094,29 +1105,34 @@ ovpn_handle_msg(struct nl_msg *msg, void *arg)
* message, that stores the type-specific attributes.
*
* the "dco" object is then filled accordingly with the information
* retrieved from the message, so that the rest of the OpenVPN code can
* react as need be.
* retrieved from the message, so that *process_incoming_dco can react
* as need be.
*/
int ret;
switch (gnlh->cmd)
{
case OVPN_CMD_PEER_GET:
{
/* return directly, there are no messages to pass to *process_incoming_dco() */
return ovpn_handle_peer(dco, attrs);
}
case OVPN_CMD_PEER_DEL_NTF:
{
return ovpn_handle_peer_del_ntf(dco, attrs);
ret = ovpn_handle_peer_del_ntf(dco, attrs);
break;
}
case OVPN_CMD_PEER_FLOAT_NTF:
{
return ovpn_handle_peer_float_ntf(dco, attrs);
ret = ovpn_handle_peer_float_ntf(dco, attrs);
break;
}
case OVPN_CMD_KEY_SWAP_NTF:
{
return ovpn_handle_key_swap_ntf(dco, attrs);
ret = ovpn_handle_key_swap_ntf(dco, attrs);
break;
}
default:
@ -1125,11 +1141,25 @@ ovpn_handle_msg(struct nl_msg *msg, void *arg)
return NL_STOP;
}
if (ret != NL_OK)
{
return ret;
}
if (dco->c->mode == CM_TOP)
{
multi_process_incoming_dco(dco);
}
else
{
process_incoming_dco(dco);
}
return NL_OK;
}
int
dco_do_read(dco_context_t *dco)
dco_read_and_process(dco_context_t *dco)
{
msg(D_DCO_DEBUG, __func__);
@ -1141,6 +1171,12 @@ dco_get_peer(dco_context_t *dco, int peer_id, const bool raise_sigusr1_on_err)
{
ASSERT(dco);
if (__is_locked)
{
msg(D_DCO_DEBUG, "%s: cannot request peer stats while parsing other messages", __func__);
return 0;
}
/* peer_id == -1 means "dump all peers", but this is allowed in MP mode only.
* If it happens in P2P mode it means that the DCO peer was deleted and we
* can simply bail out

View file

@ -690,7 +690,7 @@ dco_handle_overlapped_success(dco_context_t *dco, bool queued)
}
int
dco_do_read(dco_context_t *dco)
dco_read_and_process(dco_context_t *dco)
{
if (dco->ifmode != DCO_MODE_MP)
{
@ -727,6 +727,15 @@ dco_do_read(dco_context_t *dco)
break;
}
if (dco->c->mode == CM_TOP)
{
multi_process_incoming_dco(dco);
}
else
{
process_incoming_dco(dco);
}
return 0;
}

View file

@ -1243,19 +1243,11 @@ extract_dco_float_peer_addr(const sa_family_t socket_family, struct openvpn_sock
}
}
static void
process_incoming_dco(struct context *c)
void
process_incoming_dco(dco_context_t *dco)
{
#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
dco_context_t *dco = &c->c1.tuntap->dco;
dco_do_read(dco);
/* no message for us to handle - platform specific code has logged details */
if (dco->dco_message_type == 0)
{
return;
}
struct context *c = dco->c;
/* FreeBSD currently sends us removal notifcation with the old peer-id in
* p2p mode with the ping timeout reason, so ignore that one to not shoot
@ -2369,7 +2361,7 @@ process_io(struct context *c, struct link_socket *sock)
{
if (!IS_SIG(c))
{
process_incoming_dco(c);
dco_read_and_process(&c->c1.tuntap->dco);
}
}
}

View file

@ -209,6 +209,13 @@ void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi
void extract_dco_float_peer_addr(sa_family_t socket_family, struct openvpn_sockaddr *out_osaddr,
const struct sockaddr *float_sa);
/**
* Process an incoming DCO message (from kernel space).
*
* @param dco - Pointer to the structure representing the DCO context.
*/
void process_incoming_dco(dco_context_t *dco);
/**
* Write a packet to the external network interface.
* @ingroup external_multiplexer

View file

@ -3263,14 +3263,12 @@ process_incoming_del_peer(struct multi_context *m, struct multi_instance *mi, dc
multi_signal_instance(m, mi, SIGTERM);
}
bool
multi_process_incoming_dco(struct multi_context *m)
void
multi_process_incoming_dco(dco_context_t *dco)
{
dco_context_t *dco = &m->top.c1.tuntap->dco;
ASSERT(dco->c->multi);
struct multi_instance *mi = NULL;
int ret = dco_do_read(&m->top.c1.tuntap->dco);
struct multi_context *m = dco->c->multi;
int peer_id = dco->dco_message_peer_id;
@ -3279,12 +3277,12 @@ multi_process_incoming_dco(struct multi_context *m)
*/
if (peer_id < 0)
{
return ret > 0;
return;
}
if ((peer_id < m->max_clients) && (m->instances[peer_id]))
{
mi = m->instances[peer_id];
struct multi_instance *mi = m->instances[peer_id];
set_prefix(mi);
if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
{
@ -3325,11 +3323,6 @@ multi_process_incoming_dco(struct multi_context *m)
"type %d, del_peer_reason %d",
peer_id, dco->dco_message_type, dco->dco_del_peer_reason);
}
dco->dco_message_type = 0;
dco->dco_message_peer_id = -1;
dco->dco_del_peer_reason = -1;
return ret > 0;
}
#endif /* if defined(ENABLE_DCO) */
@ -4462,4 +4455,4 @@ multi_check_push_ifconfig_ipv6_extra_route(struct multi_instance *mi,
return (!ipv6_net_contains_host(&ifconfig_local, o->ifconfig_ipv6_netbits,
dest));
}
}

View file

@ -305,13 +305,9 @@ bool multi_process_post(struct multi_context *m, struct multi_instance *mi,
/**
* Process an incoming DCO message (from kernel space).
*
* @param m - The single \c multi_context structure.
*
* @return
* - True, if the message was received correctly.
* - False, if there was an error while reading the message.
* @param dco - Pointer to the structure representing the DCO context.
*/
bool multi_process_incoming_dco(struct multi_context *m);
void multi_process_incoming_dco(dco_context_t *dco);
/**************************************************************************/
/**

View file

@ -505,7 +505,7 @@ multi_io_process_io(struct multi_context *m)
/* incoming data on DCO? */
else if (e->arg == MULTI_IO_DCO)
{
multi_process_incoming_dco(m);
dco_read_and_process(&m->top.c1.tuntap->dco);
}
#endif
/* signal received? */