Add connect-freq-initial option to limit initial connection responses
PATCH 1/1 Add connect-freq-initial option to limit initial connection responses
This limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
The defaults of 100 attempts per 10s should work for most people, esepcially since completed three way handshakes are not counted. So the default will throttle connection attempts on server with high packet loss or that are actually under a DOS.
The 100 per 10s are similar in size to the old 2.5 and earlier behaviour where every initial connection attempt would take up a slot of the max-clients sessions and those would only expire after the TLS timeout. This roughly translates to 1024 connection attempts in 60s on an empty server.
OpenVPN will announce once per period how many packets it dropped:
Dropped 11 initial handshake packets due to --connect-freq-initial 100 10
to inform an amdin about the consequence of this feature.
--- Changes.rst | 4 ++ doc/man-sections/server-options.rst | 19 ++++++ src/openvpn/Makefile.am | 1 + src/openvpn/mudp.c | 14 +++++ src/openvpn/multi.c | 4 ++ src/openvpn/multi.h | 2 + src/openvpn/options.c | 24 ++++++++ src/openvpn/options.h | 5 ++ src/openvpn/reflect_filter.c | 91 +++++++++++++++++++++++++++++ src/openvpn/reflect_filter.h | 70 ++++++++++++++++++++++ 10 files changed, 234 insertions(+) create mode 100644 src/openvpn/reflect_filter.c create mode 100644 src/openvpn/reflect_filter.h
Changes.rst +4 -0
@@ -92,6 +92,10 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. + By default the rate of initial packet responses is limited to 100 per 10s + interval to avoid OpenVPN servers being abused in reflection attacks + (see ``--connect-freq-initial``). + Data channel offloading with ovpn-dco 2.6.0+ implements support for data-channel offloading where the data packets are directly processed and forwarded in kernel space thanks to the ovpn-dco
doc/man-sections/server-options.rst +19 -0
@@ -184,6 +184,25 @@ fast hardware. SSL/TLS authentication must be used in this mode. For the best protection against DoS attacks in server mode, use ``--proto udp`` and either ``--tls-auth`` or ``--tls-crypt``. +--connect-freq-initial args + (UDP only) Allow a maximum of ``n`` initial connection packet responses + per ``sec`` seconds from to clients. + + Valid syntax: + :: + + connect-freq-initial n sec + + OpenVPN starting at 2.6 is very efficient in responding to initial + connection packets. When not limiting the initial responses + an OpenVPN daemon can be abused in reflection attacks. + This option is designed to limit the rate OpenVPN will respond to initial + attacks. + + Connection attempts that complete the initial three-way handshake + will not be counted against the limit. The default is to allow + 100 initial connection per 10s. + --duplicate-cn Allow multiple clients with the same common name to concurrently connect. In the absence of this option, OpenVPN will disconnect a client
src/openvpn/Makefile.am +1 -0
@@ -113,6 +113,7 @@ openvpn_SOURCES = \ ps.c ps.h \ push.c push.h \ pushlist.h \ + reflect_filter.c reflect_filter.h \ reliable.c reliable.h \ route.c route.h \ run_command.c run_command.h \
src/openvpn/mudp.c +14 -0
@@ -82,6 +82,16 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; +if (verdict ==VERDICT_VALID_RESET_V3 || verdict ==VERDICT_VALID_RESET_V2 ) + { +/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + } + } +if (verdict ==VERDICT_VALID_RESET_V3 ) {/* Extract the packet id to check if it has the special format that
@@ -244,6 +254,10 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (frequency_limit_event_allowed (m->new_connection_limiter)) { +/* a successful three-way handshake only counts against +* connect-freq but not against connect-req-initial */ +reflect_filter_rate_limit_decrease (m->initial_rate_limiter); + mi =multi_create_instance (m, &real);if (mi) {
src/openvpn/multi.c +4 -0
@@ -52,6 +52,7 @@#include "crypto_backend.h" #include "ssl_util.h" #include "dco.h" +#include "reflect_filter.h" /*#define MULTI_DEBUG_EVENT_LOOP*/
@@ -368,6 +369,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode)*/ m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); + m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, + t->options.cf_initial_per);/* * Allocate broadcast/multicast buffer list
@@ -729,6 +732,7 @@ multi_uninit(struct multi_context *m)mbuf_free (m->mbuf);ifconfig_pool_free (m->ifconfig_pool);frequency_limit_free (m->new_connection_limiter); +initial_rate_limit_free (m->initial_rate_limiter);multi_reap_free (m->reaper);mroute_helper_free (m->route_helper);multi_tcp_free (m->mtcp);
src/openvpn/multi.h +2 -0
@@ -39,6 +39,7 @@#include "mtcp.h" #include "perf.h" #include "vlan.h" +#include "reflect_filter.h" #define MULTI_PREFIX_MAX_LENGTH 256
@@ -170,6 +171,7 @@ struct multi_context {* as external transport. */ struct ifconfig_pool *ifconfig_pool;struct frequency_limit *new_connection_limiter; +struct initial_packet_rate_limit *initial_rate_limiter;struct mroute_helper *route_helper;struct multi_reap *reaper;struct mroute_addr local;
src/openvpn/options.c +24 -0
@@ -480,6 +480,7 @@ static const char usage_message[] =" as well as pushes it to connecting clients.\n" "--learn-address cmd : Run command cmd to validate client virtual addresses.\n" "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n" +"--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n" "--max-clients n : Allow a maximum of n simultaneously connected clients.\n" "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n" "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
@@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ; o->max_routes_per_client =256 ; o->stale_routes_check_interval =0 ; o->ifconfig_pool_persist_refresh_freq =600 ;
@@ -1555,6 +1558,8 @@ show_p2mp_parms(const struct options *o)SHOW_BOOL (duplicate_cn);SHOW_INT (cf_max);SHOW_INT (cf_per); +SHOW_INT (cf_initial_max); +SHOW_INT (cf_initial_per);SHOW_INT (max_clients);SHOW_INT (max_routes_per_client);SHOW_STR (auth_user_pass_verify_script);
@@ -2613,6 +2618,10 @@ options_postprocess_verify_ce(const struct options *options, {msg (M_USAGE ,"--connect-freq only works with --mode server --proto udp. Try --max-clients instead." ); } +if (!proto_is_udp (ce->proto) && (options->cf_initial_max || options->cf_initial_per)) + { +msg (M_USAGE ,"--connect-freq only works with --mode server --proto udp. Try --max-clients instead." ); + }if (!(dev ==DEV_TYPE_TAP || (dev ==DEV_TYPE_TUN && options->topology ==TOP_SUBNET )) && options->ifconfig_pool_netmask) {msg (M_USAGE ,"The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode" );
@@ -7452,6 +7461,21 @@ add_option(struct options *options, options->cf_max = cf_max; options->cf_per = cf_per; } +else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +int cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); + cf_max =atoi (p[1 ]); + cf_per =atoi (p[2 ]); +if (cf_max <0 || cf_per <0 ) + { +msg (msglevel,"--connect-freq-initial parms must be > 0" ); + goto err; + } + options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }else if (streq (p[0 ],"max-clients" ) && p[1 ] && !p[2 ]) {int max_clients;
src/openvpn/options.h +5 -0
@@ -513,8 +513,13 @@ struct optionsbool push_ifconfig_ipv6_blocked;/* IPv6 */ bool enable_c2c;bool duplicate_cn; +int cf_max;int cf_per; + +int cf_initial_max; +int cf_initial_per; +int max_clients;int max_routes_per_client;int stale_routes_check_interval;
src/openvpn/reflect_filter.c new file +91 -0
91 lines not on this page · raw message
src/openvpn/reflect_filter.h new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.37.1 (Apple Git-137.1)
Hi,
first of all, feature-ACK, and "structure-ACK" - this is a lightweight implementation that can go into 2.6 with not much testing, which should "get the thing done" for most cases. A more sophisticated implementation with per-IP hashing (to avoid (ab-)using the rate-limiter to drown-out legitimate clients) can follwo for 2.7.
This said, most of the code looks good, except for this part:
src/openvpn/options.c init_options(struct options *o, const bool init_gc) · line 869
Quoted 9 lines · Arne Schwabe
index
ee3783046..cf8622f46100644src/openvpn/options.c +2
-0@@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ;
... so this is on-by-default...
src/openvpn/options.c options_postprocess_verify_ce(const struct options *options, · line 2624
Quoted 8 lines
src/openvpn/options.c +4
-0@@ -2613,6 +2618,10 @@ options_postprocess_verify_ce(const struct options *options, {msg (M_USAGE ,"--connect-freq only works with --mode server --proto udp. Try --max-clients instead." ); } +if (!proto_is_udp (ce->proto) && (options->cf_initial_max || options->cf_initial_per)) + { +msg (M_USAGE ,"--connect-freq only works with --mode server --proto udp. Try --max-clients instead." ); + }
... and if it is enabled for !UDP modes, we'll refuse cooperation.
But wouldn't that trigger for *all* TCP servers now, since it defaults to "active"?
Besides that, the "--connect-freq" there is wrong and needs to be "--connect-freq-initial" ;-)
[..]
src/openvpn/options.c add_option(struct options *options, · line 7471
Quoted 8 lines
src/openvpn/options.c +8
-0+else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +int cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); + cf_max =atoi (p[1 ]); + cf_per =atoi (p[2 ]); +if (cf_max <0 || cf_per <0 )
" < 0 " or " <= 0"?
src/openvpn/options.c add_option(struct options *options, · line 7475
Quoted 4 lines
src/openvpn/options.c +4
-0+ { +msg (msglevel,"--connect-freq-initial parms must be > 0" ); + goto err; + }
... because the inverse of ">0" is not "<0"...
src/openvpn/options.c add_option(struct options *options, · line 7478
Quoted 3 lines
src/openvpn/options.c +3
-0+ options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }
... now we could of course document "0 turns it off", but then the message needs to be ">= 0".
Quoted 8 lines
src/openvpn/options.c +8
-0+bool +reflect_filter_rate_limit_check (struct initial_packet_rate_limit *irl) +{ +if (now > irl->last_period_reset + irl->period_length) + { +int dropped =max_int (irl->curr_period_counter - irl->max_per_period,0 ); +if (dropped >0 ) + {
src/openvpn/options.c
Quoted 10 lines
src/openvpn/options.c +10
-0+msg (D_TLS_DEBUG_LOW ,"Dropped %d initial handshake packets due to " +"--connect-freq-initial %d %d" , + dropped, irl->max_per_period, irl->period_length); + } + irl->last_period_reset = now; + irl->curr_period_counter =0 ; + } +/* avoid overflow by capping the counter to INT_MAX -1. Still counting +* the number of packets during a period is still useful */ + irl->curr_period_counter =min_int (irl->curr_period_counter +1 ,INT_MAX -1 );
This looks uglysh.
With a 10-seconds bucket, an (32bit) "int" would overflow at about 430 million packets per seconds - which is, I think, way beyond what any reasonable system can handle (10G ethernet saturates at 15 Mpps).
But if you are really worried about overflows - why not use a (64bit) long here?
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]...PATCH v2 1/1 Add connect-freq-initial option to limit initial connection responses
In reply to Arne SchwabeThis limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
The defaults of 100 attempts per 10s should work for most people, esepcially since completed three way handshakes are not counted. So the default will throttle connection attempts on server with high packet loss or that are actually under a DOS.
The 100 per 10s are similar in size to the old 2.5 and earlier behaviour where every initial connection attempt would take up a slot of the max-clients sessions and those would only expire after the TLS timeout. This roughly translates to 1024 connection attempts in 60s on an empty server.
OpenVPN will announce once per period how many packets it dropped:
Dropped 11 initial handshake packets due to --connect-freq-initial 100 10
to inform an amdin about the consequence of this feature.
Patch v2: use strtol instead of atoi to be able to differentiate between
an error parsing and parsing 0. Use int64_t instead int to avoid overflow errors.
--- Changes.rst | 4 ++ doc/man-sections/server-options.rst | 19 ++++++ src/openvpn/Makefile.am | 1 + src/openvpn/mudp.c | 14 +++++ src/openvpn/multi.c | 4 ++ src/openvpn/multi.h | 2 + src/openvpn/options.c | 21 +++++++ src/openvpn/options.h | 5 ++ src/openvpn/reflect_filter.c | 93 +++++++++++++++++++++++++++++ src/openvpn/reflect_filter.h | 71 ++++++++++++++++++++++ 10 files changed, 234 insertions(+) create mode 100644 src/openvpn/reflect_filter.c create mode 100644 src/openvpn/reflect_filter.h
Changes.rst +4 -0
@@ -92,6 +92,10 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. + By default the rate of initial packet responses is limited to 100 per 10s + interval to avoid OpenVPN servers being abused in reflection attacks + (see ``--connect-freq-initial``). + Data channel offloading with ovpn-dco 2.6.0+ implements support for data-channel offloading where the data packets are directly processed and forwarded in kernel space thanks to the ovpn-dco
doc/man-sections/server-options.rst +19 -0
@@ -184,6 +184,25 @@ fast hardware. SSL/TLS authentication must be used in this mode. For the best protection against DoS attacks in server mode, use ``--proto udp`` and either ``--tls-auth`` or ``--tls-crypt``. +--connect-freq-initial args + (UDP only) Allow a maximum of ``n`` initial connection packet responses + per ``sec`` seconds from to clients. + + Valid syntax: + :: + + connect-freq-initial n sec + + OpenVPN starting at 2.6 is very efficient in responding to initial + connection packets. When not limiting the initial responses + an OpenVPN daemon can be abused in reflection attacks. + This option is designed to limit the rate OpenVPN will respond to initial + attacks. + + Connection attempts that complete the initial three-way handshake + will not be counted against the limit. The default is to allow + 100 initial connection per 10s. + --duplicate-cn Allow multiple clients with the same common name to concurrently connect. In the absence of this option, OpenVPN will disconnect a client
src/openvpn/Makefile.am +1 -0
@@ -113,6 +113,7 @@ openvpn_SOURCES = \ ps.c ps.h \ push.c push.h \ pushlist.h \ + reflect_filter.c reflect_filter.h \ reliable.c reliable.h \ route.c route.h \ run_command.c run_command.h \
src/openvpn/mudp.c +14 -0
@@ -82,6 +82,16 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; +if (verdict ==VERDICT_VALID_RESET_V3 || verdict ==VERDICT_VALID_RESET_V2 ) + { +/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + } + } +if (verdict ==VERDICT_VALID_RESET_V3 ) {/* Extract the packet id to check if it has the special format that
@@ -244,6 +254,10 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (frequency_limit_event_allowed (m->new_connection_limiter)) { +/* a successful three-way handshake only counts against +* connect-freq but not against connect-req-initial */ +reflect_filter_rate_limit_decrease (m->initial_rate_limiter); + mi =multi_create_instance (m, &real);if (mi) {
src/openvpn/multi.c +4 -0
@@ -52,6 +52,7 @@#include "crypto_backend.h" #include "ssl_util.h" #include "dco.h" +#include "reflect_filter.h" /*#define MULTI_DEBUG_EVENT_LOOP*/
@@ -368,6 +369,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode)*/ m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); + m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, + t->options.cf_initial_per);/* * Allocate broadcast/multicast buffer list
@@ -729,6 +732,7 @@ multi_uninit(struct multi_context *m)mbuf_free (m->mbuf);ifconfig_pool_free (m->ifconfig_pool);frequency_limit_free (m->new_connection_limiter); +initial_rate_limit_free (m->initial_rate_limiter);multi_reap_free (m->reaper);mroute_helper_free (m->route_helper);multi_tcp_free (m->mtcp);
src/openvpn/multi.h +2 -0
@@ -39,6 +39,7 @@#include "mtcp.h" #include "perf.h" #include "vlan.h" +#include "reflect_filter.h" #define MULTI_PREFIX_MAX_LENGTH 256
@@ -170,6 +171,7 @@ struct multi_context {* as external transport. */ struct ifconfig_pool *ifconfig_pool;struct frequency_limit *new_connection_limiter; +struct initial_packet_rate_limit *initial_rate_limiter;struct mroute_helper *route_helper;struct multi_reap *reaper;struct mroute_addr local;
src/openvpn/options.c +21 -0
@@ -480,6 +480,7 @@ static const char usage_message[] =" as well as pushes it to connecting clients.\n" "--learn-address cmd : Run command cmd to validate client virtual addresses.\n" "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n" +"--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n" "--max-clients n : Allow a maximum of n simultaneously connected clients.\n" "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n" "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
@@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ; o->max_routes_per_client =256 ; o->stale_routes_check_interval =0 ; o->ifconfig_pool_persist_refresh_freq =600 ;
@@ -1555,6 +1558,8 @@ show_p2mp_parms(const struct options *o)SHOW_BOOL (duplicate_cn);SHOW_INT (cf_max);SHOW_INT (cf_per); +SHOW_INT (cf_initial_max); +SHOW_INT (cf_initial_per);SHOW_INT (max_clients);SHOW_INT (max_routes_per_client);SHOW_STR (auth_user_pass_verify_script);
@@ -7452,6 +7457,22 @@ add_option(struct options *options, options->cf_max = cf_max; options->cf_per = cf_per; } +else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +long cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); +char *e1, *e2; + cf_max =strtol (p[1 ], &e1,10 ); + cf_per =strtol (p[2 ], &e2,10 ); +if (cf_max <0 || cf_per <0 || *e1 !='\0' || *e2 !='\0' ) + { +msg (msglevel,"--connect-freq-initial parameters must be integers and >= 0" ); + goto err; + } + options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }else if (streq (p[0 ],"max-clients" ) && p[1 ] && !p[2 ]) {int max_clients;
src/openvpn/options.h +5 -0
@@ -513,8 +513,13 @@ struct optionsbool push_ifconfig_ipv6_blocked;/* IPv6 */ bool enable_c2c;bool duplicate_cn; +int cf_max;int cf_per; + +int cf_initial_max; +int cf_initial_per; +int max_clients;int max_routes_per_client;int stale_routes_check_interval;
src/openvpn/reflect_filter.c new file +93 -0
93 lines not on this page · raw message
src/openvpn/reflect_filter.h new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.37.1 (Apple Git-137.1)
Hi,
Quoted 3 lines · Arne Schwabe
Patch v2: use strtol instead of atoi to be able to differentiate between
an error parsing and parsing 0. Useint64_tinstead int to avoid overflow errors.
I find this easier to read, so thanks.
This said...
src/openvpn/mudp.c do_pre_decrypt_check(struct multi_context *m, · line 92
Quoted 6 lines
src/openvpn/mudp.c +6
-0+/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + }
This does "we kill the packet if reflect_filter returns false(!)".
[..]
Quoted 3 lines
src/openvpn/reflect_filter.c new file +3
-0+bool +reflect_filter_rate_limit_check (struct initial_packet_rate_limit *irl) +{
[..]
Review
Quoted 2 lines
(quoted diff) +2
-0+ return irl->curr_period_counter > irl->max_per_period; +}
This does "return false *while under the limit*".
So it kills all UDP for me.
2023-01-09 09:03:01 us=694540 rrl: curr_period_counter=1, max_pp=100 2023-01-09 09:03:01 us=694598 connection killed by reflect_filter 2023-01-09 09:03:17 us=494917 rrl: curr_period_counter=1, max_pp=100 2023-01-09 09:03:17 us=495003 connection killed by reflect_filter 2023-01-09 09:03:22 us=773712 rrl: curr_period_counter=2, max_pp=100 2023-01-09 09:03:22 us=773773 connection killed by reflect_filter 2023-01-09 09:03:25 us=243757 rrl: curr_period_counter=3, max_pp=100 2023-01-09 09:03:25 us=243827 connection killed by reflect_filter
Turning around the condition in reflect_filter.c
+ return irl->curr_period_counter < irl->max_per_period;
makes things well-behaved - as in, normal connections succeed, excess RESET_V3 fail - tested with your sendOvpn.py
$ python3.9 sendOvpn.py 1000 generating packets creating sockets Round 0 sending packets sleeping 99 packets received
2023-01-09 09:22:47 us=429929 Connection Attempt Dropped 900 initial handshake packets due to --connect-freq-initial 100 10
While at it, I wonder if it would be a useful addition to do a single message if the rate limiter kicks in, like this
if ( irl->curr_period_counter == irl->max_per_period )
{
msg( M_WARN, "connect-freq-initial rate limiter activated, dropping incoming UDP for the next %d seconds",
(irl->last_period_reset + irl->period_length)-now );
}we do have a log entry after the end of the period, but that might be an arbitrary time ("when the next packet comes in") further down the log, with no indication why OpenVPN decided to play dead at this point.
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]...Quoted 16 lines · Arne Schwabe
This limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
The defaults of 100 attempts per 10s should work for most people, esepcially since completed three way handshakes are not counted. So the default will throttle connection attempts on server with high packet loss or that are actually under a DOS.
The 100 per 10s are similar in size to the old 2.5 and earlier behaviour where every initial connection attempt would take up a slot of the max-clients sessions and those would only expire after the TLS timeout. This roughly translates to 1024 connection attempts in 60s on an empty server.
[...]
doc/man-sections/server-options.rst fast hardware. SSL/TLS authentication must be used in this mode. · line 185
Quoted 7 lines
doc/man-sections/server-options.rst +0
-0@@ -184,6 +184,25 @@ fast hardware. SSL/TLS authentication must be used in this mode. For the best protection against DoS attacks in server mode, use ``--proto udp`` and either ``--tls-auth`` or ``--tls-crypt``.
I think we should add a sentence about the relationship between connect-freq and connect-freq-initial. Because users will wonder. Specifically are there attacks that connect-freq protects against that connect-freq-initial does not?
doc/man-sections/server-options.rst fast hardware. SSL/TLS authentication must be used in this mode. · line 189
Quoted 4 lines
doc/man-sections/server-options.rst +3
-0+--connect-freq-initial args + (UDP only) Allow a maximum of ``n`` initial connection packet responses + per ``sec`` seconds from to clients.
Remove "from"?
Quoted 19 lines
doc/man-sections/server-options.rst +16
-0+ + Valid syntax: + :: + + connect-freq-initial n sec + + OpenVPN starting at 2.6 is very efficient in responding to initial + connection packets. When not limiting the initial responses + an OpenVPN daemon can be abused in reflection attacks. + This option is designed to limit the rate OpenVPN will respond to initial + attacks. + + Connection attempts that complete the initial three-way handshake + will not be counted against the limit. The default is to allow + 100 initial connection per 10s. + --duplicate-cn Allow multiple clients with the same common name to concurrently connect. In the absence of this option, OpenVPN will disconnect a client
[...]
Quoted 22 lines
src/openvpn/mudp.c +10
-0@@ -82,6 +82,16 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; +if (verdict ==VERDICT_VALID_RESET_V3 || verdict ==VERDICT_VALID_RESET_V2 ) + { +/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + } + } +if (verdict ==VERDICT_VALID_RESET_V3 ) {/* Extract the packet id to check if it has the special format that
src/openvpn/mudp.c multi_get_create_instance_udp(struct multi_context *m, bool *floated) · line 258
Quoted 5 lines
src/openvpn/mudp.c +2
-0@@ -244,6 +254,10 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (frequency_limit_event_allowed (m->new_connection_limiter)) { +/* a successful three-way handshake only counts against +* connect-freq but not against connect-req-initial */
"connect-freq-initial"
Quoted 34 lines
src/openvpn/mudp.c +2
-0+reflect_filter_rate_limit_decrease (m->initial_rate_limiter); + mi =multi_create_instance (m, &real);if (mi) {src/openvpn/multi.c +4
-0@@ -52,6 +52,7 @@#include "crypto_backend.h" #include "ssl_util.h" #include "dco.h" +#include "reflect_filter.h" /*#define MULTI_DEBUG_EVENT_LOOP*/ @@ -368,6 +369,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode)*/ m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); + m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, + t->options.cf_initial_per);/* * Allocate broadcast/multicast buffer list @@ -729,6 +732,7 @@ multi_uninit(struct multi_context *m)mbuf_free (m->mbuf);ifconfig_pool_free (m->ifconfig_pool);frequency_limit_free (m->new_connection_limiter); +initial_rate_limit_free (m->initial_rate_limiter);multi_reap_free (m->reaper);mroute_helper_free (m->route_helper);multi_tcp_free (m->mtcp);
[...]
Quoted 43 lines
src/openvpn/options.c +14
-0@@ -480,6 +480,7 @@ static const char usage_message[] =" as well as pushes it to connecting clients.\n" "--learn-address cmd : Run command cmd to validate client virtual addresses.\n" "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n" +"--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n" "--max-clients n : Allow a maximum of n simultaneously connected clients.\n" "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n" "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n" @@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ; o->max_routes_per_client =256 ; o->stale_routes_check_interval =0 ; o->ifconfig_pool_persist_refresh_freq =600 ;@@ -1555,6 +1558,8 @@ show_p2mp_parms(const struct options *o)SHOW_BOOL (duplicate_cn);SHOW_INT (cf_max);SHOW_INT (cf_per); +SHOW_INT (cf_initial_max); +SHOW_INT (cf_initial_per);SHOW_INT (max_clients);SHOW_INT (max_routes_per_client);SHOW_STR (auth_user_pass_verify_script);@@ -7452,6 +7457,22 @@ add_option(struct options *options, options->cf_max = cf_max; options->cf_per = cf_per; } +else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +long cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); +char *e1, *e2; + cf_max =strtol (p[1 ], &e1,10 ); + cf_per =strtol (p[2 ], &e2,10 ); +if (cf_max <0 || cf_per <0 || *e1 !='\0' || *e2 !='\0' )
src/openvpn/options.c add_option(struct options *options, · line 7478
Quoted 10 lines
src/openvpn/options.c +7
-0+ { +msg (msglevel,"--connect-freq-initial parameters must be integers and >= 0" ); + goto err; + } + options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }else if (streq (p[0 ],"max-clients" ) && p[1 ] && !p[2 ]) {int max_clients;
Regards,
Signature · 1 line
Frank Lichtenheld
PATCH v3 1/1 Add connect-freq-initial option to limit initial connection responses
In reply to Arne SchwabeThis limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
The defaults of 100 attempts per 10s should work for most people, esepcially since completed three way handshakes are not counted. So the default will throttle connection attempts on server with high packet loss or that are actually under a DOS.
The 100 per 10s are similar in size to the old 2.5 and earlier behaviour where every initial connection attempt would take up a slot of the max-clients sessions and those would only expire after the TLS timeout. This roughly translates to 1024 connection attempts in 60s on an empty server.
OpenVPN will announce once per period when starting dropping packets and ultimatively how many packets it dropped:
Connection Attempt Note: --connect-freq-initial 100 10 rate limit exceeded, dropping initial handshake packets for the next 10 seconds
Connection Attempt Dropped 217 initial handshake packets due to --connect-freq-initial 100 10
to inform an amdin about the consequence of this feature.
Patch v2: use strtol instead of atoi to be able to differentiate between
an error parsing and parsing 0. Use int64_t instead int to avoid overflow errors.
Patch v3: Add message when we start dropping. Add a few fixes to the logic.
improve docs
--- Changes.rst | 4 + doc/man-sections/server-options.rst | 24 ++++++ src/openvpn/Makefile.am | 1 + src/openvpn/mudp.c | 14 ++++ src/openvpn/multi.c | 4 + src/openvpn/multi.h | 2 + src/openvpn/options.c | 21 ++++++ src/openvpn/options.h | 5 ++ src/openvpn/reflect_filter.c | 111 ++++++++++++++++++++++++++++ src/openvpn/reflect_filter.h | 75 +++++++++++++++++++ 10 files changed, 261 insertions(+) create mode 100644 src/openvpn/reflect_filter.c create mode 100644 src/openvpn/reflect_filter.h
Changes.rst +4 -0
@@ -92,6 +92,10 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. + By default the rate of initial packet responses is limited to 100 per 10s + interval to avoid OpenVPN servers being abused in reflection attacks + (see ``--connect-freq-initial``). + Data channel offloading with ovpn-dco 2.6.0+ implements support for data-channel offloading where the data packets are directly processed and forwarded in kernel space thanks to the ovpn-dco
doc/man-sections/server-options.rst +24 -0
@@ -178,12 +178,36 @@ fast hardware. SSL/TLS authentication must be used in this mode. with connection requests using certificates which will ultimately fail to authenticate. + This limit applies after ``--connect-freq-initial`` and + only applies to client that have completed the three-way handshake + or client that use ``--tls-crypt-v2`` without cookie support + (``allow-noncookie`` argument to ``--tls-crypt-v2``). + This is an imperfect solution however, because in a real DoS scenario, legitimate connections might also be refused. For the best protection against DoS attacks in server mode, use ``--proto udp`` and either ``--tls-auth`` or ``--tls-crypt``. +--connect-freq-initial args + (UDP only) Allow a maximum of ``n`` initial connection packet responses + per ``sec`` seconds from the OpenVPN server to clients. + + Valid syntax: + :: + + connect-freq-initial n sec + + OpenVPN starting at 2.6 is very efficient in responding to initial + connection packets. When not limiting the initial responses + an OpenVPN daemon can be abused in reflection attacks. + This option is designed to limit the rate OpenVPN will respond to initial + attacks. + + Connection attempts that complete the initial three-way handshake + will not be counted against the limit. The default is to allow + 100 initial connection per 10s. + --duplicate-cn Allow multiple clients with the same common name to concurrently connect. In the absence of this option, OpenVPN will disconnect a client
src/openvpn/Makefile.am +1 -0
@@ -113,6 +113,7 @@ openvpn_SOURCES = \ ps.c ps.h \ push.c push.h \ pushlist.h \ + reflect_filter.c reflect_filter.h \ reliable.c reliable.h \ route.c route.h \ run_command.c run_command.h \
src/openvpn/mudp.c +14 -0
@@ -82,6 +82,16 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; +if (verdict ==VERDICT_VALID_RESET_V3 || verdict ==VERDICT_VALID_RESET_V2 ) + { +/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + } + } +if (verdict ==VERDICT_VALID_RESET_V3 ) {/* Extract the packet id to check if it has the special format that
@@ -244,6 +254,10 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (frequency_limit_event_allowed (m->new_connection_limiter)) { +/* a successful three-way handshake only counts against +* connect-freq but not against connect-freq-initial */ +reflect_filter_rate_limit_decrease (m->initial_rate_limiter); + mi =multi_create_instance (m, &real);if (mi) {
src/openvpn/multi.c +4 -0
@@ -52,6 +52,7 @@#include "crypto_backend.h" #include "ssl_util.h" #include "dco.h" +#include "reflect_filter.h" /*#define MULTI_DEBUG_EVENT_LOOP*/
@@ -368,6 +369,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode)*/ m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); + m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, + t->options.cf_initial_per);/* * Allocate broadcast/multicast buffer list
@@ -729,6 +732,7 @@ multi_uninit(struct multi_context *m)mbuf_free (m->mbuf);ifconfig_pool_free (m->ifconfig_pool);frequency_limit_free (m->new_connection_limiter); +initial_rate_limit_free (m->initial_rate_limiter);multi_reap_free (m->reaper);mroute_helper_free (m->route_helper);multi_tcp_free (m->mtcp);
src/openvpn/multi.h +2 -0
@@ -39,6 +39,7 @@#include "mtcp.h" #include "perf.h" #include "vlan.h" +#include "reflect_filter.h" #define MULTI_PREFIX_MAX_LENGTH 256
@@ -170,6 +171,7 @@ struct multi_context {* as external transport. */ struct ifconfig_pool *ifconfig_pool;struct frequency_limit *new_connection_limiter; +struct initial_packet_rate_limit *initial_rate_limiter;struct mroute_helper *route_helper;struct multi_reap *reaper;struct mroute_addr local;
src/openvpn/options.c +21 -0
@@ -480,6 +480,7 @@ static const char usage_message[] =" as well as pushes it to connecting clients.\n" "--learn-address cmd : Run command cmd to validate client virtual addresses.\n" "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n" +"--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n" "--max-clients n : Allow a maximum of n simultaneously connected clients.\n" "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n" "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
@@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ; o->max_routes_per_client =256 ; o->stale_routes_check_interval =0 ; o->ifconfig_pool_persist_refresh_freq =600 ;
@@ -1555,6 +1558,8 @@ show_p2mp_parms(const struct options *o)SHOW_BOOL (duplicate_cn);SHOW_INT (cf_max);SHOW_INT (cf_per); +SHOW_INT (cf_initial_max); +SHOW_INT (cf_initial_per);SHOW_INT (max_clients);SHOW_INT (max_routes_per_client);SHOW_STR (auth_user_pass_verify_script);
@@ -7452,6 +7457,22 @@ add_option(struct options *options, options->cf_max = cf_max; options->cf_per = cf_per; } +else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +long cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); +char *e1, *e2; + cf_max =strtol (p[1 ], &e1,10 ); + cf_per =strtol (p[2 ], &e2,10 ); +if (cf_max <0 || cf_per <0 || *e1 !='\0' || *e2 !='\0' ) + { +msg (msglevel,"--connect-freq-initial parameters must be integers and >= 0" ); + goto err; + } + options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }else if (streq (p[0 ],"max-clients" ) && p[1 ] && !p[2 ]) {int max_clients;
src/openvpn/options.h +5 -0
@@ -513,8 +513,13 @@ struct optionsbool push_ifconfig_ipv6_blocked;/* IPv6 */ bool enable_c2c;bool duplicate_cn; +int cf_max;int cf_per; + +int cf_initial_max; +int cf_initial_per; +int max_clients;int max_routes_per_client;int stale_routes_check_interval;
src/openvpn/reflect_filter.c new file +111 -0
111 lines not on this page · raw message
src/openvpn/reflect_filter.h new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
2.37.1 (Apple Git-137.1)
PATCH v4 1/1 Add connect-freq-initial option to limit initial connection responses
In reply to Arne SchwabeThis limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
The defaults of 100 attempts per 10s should work for most people, esepcially since completed three way handshakes are not counted. So the default will throttle connection attempts on server with high packet loss or that are actually under a DOS.
The 100 per 10s are similar in size to the old 2.5 and earlier behaviour where every initial connection attempt would take up a slot of the max-clients sessions and those would only expire after the TLS timeout. This roughly translates to 1024 connection attempts in 60s on an empty server.
OpenVPN will announce once per period when starting dropping packets and ultimatively how many packets it dropped:
Connection Attempt Note: --connect-freq-initial 100 10 rate limit exceeded, dropping initial handshake packets for the next 10 seconds
Connection Attempt Dropped 217 initial handshake packets due to --connect-freq-initial 100 10
to inform an amdin about the consequence of this feature.
Patch v2: use strtol instead of atoi to be able to differentiate between
an error parsing and parsing 0. Use int64_t instead int to avoid overflow errors.
Patch v3: Add message when we start dropping. Add a few fixes to the logic.
improve docs
Patch v4: missing missing return statement.
--- Changes.rst | 4 ++ doc/man-sections/server-options.rst | 24 +++++++ src/openvpn/Makefile.am | 1 + src/openvpn/mudp.c | 14 ++++ src/openvpn/multi.c | 4 ++ src/openvpn/multi.h | 2 + src/openvpn/options.c | 21 ++++++ src/openvpn/options.h | 5 ++ src/openvpn/reflect_filter.c | 107 ++++++++++++++++++++++++++++ src/openvpn/reflect_filter.h | 75 +++++++++++++++++++ 10 files changed, 257 insertions(+) create mode 100644 src/openvpn/reflect_filter.c create mode 100644 src/openvpn/reflect_filter.h
Changes.rst +4 -0
@@ -92,6 +92,10 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. + By default the rate of initial packet responses is limited to 100 per 10s + interval to avoid OpenVPN servers being abused in reflection attacks + (see ``--connect-freq-initial``). + Data channel offloading with ovpn-dco 2.6.0+ implements support for data-channel offloading where the data packets are directly processed and forwarded in kernel space thanks to the ovpn-dco
doc/man-sections/server-options.rst +24 -0
@@ -178,12 +178,36 @@ fast hardware. SSL/TLS authentication must be used in this mode. with connection requests using certificates which will ultimately fail to authenticate. + This limit applies after ``--connect-freq-initial`` and + only applies to client that have completed the three-way handshake + or client that use ``--tls-crypt-v2`` without cookie support + (``allow-noncookie`` argument to ``--tls-crypt-v2``). + This is an imperfect solution however, because in a real DoS scenario, legitimate connections might also be refused. For the best protection against DoS attacks in server mode, use ``--proto udp`` and either ``--tls-auth`` or ``--tls-crypt``. +--connect-freq-initial args + (UDP only) Allow a maximum of ``n`` initial connection packet responses + per ``sec`` seconds from the OpenVPN server to clients. + + Valid syntax: + :: + + connect-freq-initial n sec + + OpenVPN starting at 2.6 is very efficient in responding to initial + connection packets. When not limiting the initial responses + an OpenVPN daemon can be abused in reflection attacks. + This option is designed to limit the rate OpenVPN will respond to initial + attacks. + + Connection attempts that complete the initial three-way handshake + will not be counted against the limit. The default is to allow + 100 initial connection per 10s. + --duplicate-cn Allow multiple clients with the same common name to concurrently connect. In the absence of this option, OpenVPN will disconnect a client
src/openvpn/Makefile.am +1 -0
@@ -113,6 +113,7 @@ openvpn_SOURCES = \ ps.c ps.h \ push.c push.h \ pushlist.h \ + reflect_filter.c reflect_filter.h \ reliable.c reliable.h \ route.c route.h \ run_command.c run_command.h \
src/openvpn/mudp.c +14 -0
@@ -82,6 +82,16 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; +if (verdict ==VERDICT_VALID_RESET_V3 || verdict ==VERDICT_VALID_RESET_V2 ) + { +/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + } + } +if (verdict ==VERDICT_VALID_RESET_V3 ) {/* Extract the packet id to check if it has the special format that
@@ -244,6 +254,10 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (frequency_limit_event_allowed (m->new_connection_limiter)) { +/* a successful three-way handshake only counts against +* connect-freq but not against connect-freq-initial */ +reflect_filter_rate_limit_decrease (m->initial_rate_limiter); + mi =multi_create_instance (m, &real);if (mi) {
src/openvpn/multi.c +4 -0
@@ -52,6 +52,7 @@#include "crypto_backend.h" #include "ssl_util.h" #include "dco.h" +#include "reflect_filter.h" /*#define MULTI_DEBUG_EVENT_LOOP*/
@@ -368,6 +369,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode)*/ m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); + m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, + t->options.cf_initial_per);/* * Allocate broadcast/multicast buffer list
@@ -729,6 +732,7 @@ multi_uninit(struct multi_context *m)mbuf_free (m->mbuf);ifconfig_pool_free (m->ifconfig_pool);frequency_limit_free (m->new_connection_limiter); +initial_rate_limit_free (m->initial_rate_limiter);multi_reap_free (m->reaper);mroute_helper_free (m->route_helper);multi_tcp_free (m->mtcp);
src/openvpn/multi.h +2 -0
@@ -39,6 +39,7 @@#include "mtcp.h" #include "perf.h" #include "vlan.h" +#include "reflect_filter.h" #define MULTI_PREFIX_MAX_LENGTH 256
@@ -170,6 +171,7 @@ struct multi_context {* as external transport. */ struct ifconfig_pool *ifconfig_pool;struct frequency_limit *new_connection_limiter; +struct initial_packet_rate_limit *initial_rate_limiter;struct mroute_helper *route_helper;struct multi_reap *reaper;struct mroute_addr local;
src/openvpn/options.c +21 -0
@@ -480,6 +480,7 @@ static const char usage_message[] =" as well as pushes it to connecting clients.\n" "--learn-address cmd : Run command cmd to validate client virtual addresses.\n" "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n" +"--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n" "--max-clients n : Allow a maximum of n simultaneously connected clients.\n" "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n" "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
@@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ; o->max_routes_per_client =256 ; o->stale_routes_check_interval =0 ; o->ifconfig_pool_persist_refresh_freq =600 ;
@@ -1555,6 +1558,8 @@ show_p2mp_parms(const struct options *o)SHOW_BOOL (duplicate_cn);SHOW_INT (cf_max);SHOW_INT (cf_per); +SHOW_INT (cf_initial_max); +SHOW_INT (cf_initial_per);SHOW_INT (max_clients);SHOW_INT (max_routes_per_client);SHOW_STR (auth_user_pass_verify_script);
@@ -7452,6 +7457,22 @@ add_option(struct options *options, options->cf_max = cf_max; options->cf_per = cf_per; } +else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +long cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); +char *e1, *e2; + cf_max =strtol (p[1 ], &e1,10 ); + cf_per =strtol (p[2 ], &e2,10 ); +if (cf_max <0 || cf_per <0 || *e1 !='\0' || *e2 !='\0' ) + { +msg (msglevel,"--connect-freq-initial parameters must be integers and >= 0" ); + goto err; + } + options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }else if (streq (p[0 ],"max-clients" ) && p[1 ] && !p[2 ]) {int max_clients;
src/openvpn/options.h +5 -0
@@ -513,8 +513,13 @@ struct optionsbool push_ifconfig_ipv6_blocked;/* IPv6 */ bool enable_c2c;bool duplicate_cn; +int cf_max;int cf_per; + +int cf_initial_max; +int cf_initial_per; +int max_clients;int max_routes_per_client;int stale_routes_check_interval;
src/openvpn/reflect_filter.c new file +107 -0
107 lines not on this page · raw message
src/openvpn/reflect_filter.h new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
2.37.1 (Apple Git-137.1)
Hi,
Quoted 4 lines · Arne Schwabe
This limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
v4 works perfectly well for unixy platforms, but breaks Windows compilation, due to "there is a new source module that MSVC does not know about".
https://github.com/cron2/openvpn/actions/runs/3878016110/jobs/6613714099
:(
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]...PATCH v5 1/1 Add connect-freq-initial option to limit initial connection responses
In reply to Arne SchwabeThis limits the nubmer of packets OpenVPN will respond to. This avoid OpenVPN server being abused for refelection attacks in a large scale as we gotten a lot more efficient with the cookie approach in our initial connection approach.
The defaults of 100 attempts per 10s should work for most people, esepcially since completed three way handshakes are not counted. So the default will throttle connection attempts on server with high packet loss or that are actually under a DOS.
The 100 per 10s are similar in size to the old 2.5 and earlier behaviour where every initial connection attempt would take up a slot of the max-clients sessions and those would only expire after the TLS timeout. This roughly translates to 1024 connection attempts in 60s on an empty server.
OpenVPN will announce once per period when starting dropping packets and ultimatively how many packets it dropped:
Connection Attempt Note: --connect-freq-initial 100 10 rate limit exceeded, dropping initial handshake packets for the next 10 seconds
Connection Attempt Dropped 217 initial handshake packets due to --connect-freq-initial 100 10
to inform an amdin about the consequence of this feature.
Patch v2: use strtol instead of atoi to be able to differentiate between
an error parsing and parsing 0. Use int64_t instead int to avoid overflow errors.
Patch v3: Add message when we start dropping. Add a few fixes to the logic.
improve docs
Patch v4: missing missing return statement.
Patch v5: add build files for msvc build
--- Changes.rst | 4 ++ doc/man-sections/server-options.rst | 24 +++++++ src/openvpn/Makefile.am | 1 + src/openvpn/mudp.c | 14 ++++ src/openvpn/multi.c | 4 ++ src/openvpn/multi.h | 2 + src/openvpn/openvpn.vcxproj | 2 + src/openvpn/options.c | 21 ++++++ src/openvpn/options.h | 5 ++ src/openvpn/reflect_filter.c | 107 ++++++++++++++++++++++++++++ src/openvpn/reflect_filter.h | 75 +++++++++++++++++++ 11 files changed, 259 insertions(+) create mode 100644 src/openvpn/reflect_filter.c create mode 100644 src/openvpn/reflect_filter.h
Changes.rst +4 -0
@@ -92,6 +92,10 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. + By default the rate of initial packet responses is limited to 100 per 10s + interval to avoid OpenVPN servers being abused in reflection attacks + (see ``--connect-freq-initial``). + Data channel offloading with ovpn-dco 2.6.0+ implements support for data-channel offloading where the data packets are directly processed and forwarded in kernel space thanks to the ovpn-dco
doc/man-sections/server-options.rst +24 -0
@@ -178,12 +178,36 @@ fast hardware. SSL/TLS authentication must be used in this mode. with connection requests using certificates which will ultimately fail to authenticate. + This limit applies after ``--connect-freq-initial`` and + only applies to client that have completed the three-way handshake + or client that use ``--tls-crypt-v2`` without cookie support + (``allow-noncookie`` argument to ``--tls-crypt-v2``). + This is an imperfect solution however, because in a real DoS scenario, legitimate connections might also be refused. For the best protection against DoS attacks in server mode, use ``--proto udp`` and either ``--tls-auth`` or ``--tls-crypt``. +--connect-freq-initial args + (UDP only) Allow a maximum of ``n`` initial connection packet responses + per ``sec`` seconds from the OpenVPN server to clients. + + Valid syntax: + :: + + connect-freq-initial n sec + + OpenVPN starting at 2.6 is very efficient in responding to initial + connection packets. When not limiting the initial responses + an OpenVPN daemon can be abused in reflection attacks. + This option is designed to limit the rate OpenVPN will respond to initial + attacks. + + Connection attempts that complete the initial three-way handshake + will not be counted against the limit. The default is to allow + 100 initial connection per 10s. + --duplicate-cn Allow multiple clients with the same common name to concurrently connect. In the absence of this option, OpenVPN will disconnect a client
src/openvpn/Makefile.am +1 -0
@@ -113,6 +113,7 @@ openvpn_SOURCES = \ ps.c ps.h \ push.c push.h \ pushlist.h \ + reflect_filter.c reflect_filter.h \ reliable.c reliable.h \ route.c route.h \ run_command.c run_command.h \
src/openvpn/mudp.c +14 -0
@@ -82,6 +82,16 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; +if (verdict ==VERDICT_VALID_RESET_V3 || verdict ==VERDICT_VALID_RESET_V2 ) + { +/* Check if we are still below our limit for sending out +* responses */ +if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) + { +return false; + } + } +if (verdict ==VERDICT_VALID_RESET_V3 ) {/* Extract the packet id to check if it has the special format that
@@ -244,6 +254,10 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (frequency_limit_event_allowed (m->new_connection_limiter)) { +/* a successful three-way handshake only counts against +* connect-freq but not against connect-freq-initial */ +reflect_filter_rate_limit_decrease (m->initial_rate_limiter); + mi =multi_create_instance (m, &real);if (mi) {
src/openvpn/multi.c +4 -0
@@ -52,6 +52,7 @@#include "crypto_backend.h" #include "ssl_util.h" #include "dco.h" +#include "reflect_filter.h" /*#define MULTI_DEBUG_EVENT_LOOP*/
@@ -368,6 +369,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode)*/ m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); + m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, + t->options.cf_initial_per);/* * Allocate broadcast/multicast buffer list
@@ -729,6 +732,7 @@ multi_uninit(struct multi_context *m)mbuf_free (m->mbuf);ifconfig_pool_free (m->ifconfig_pool);frequency_limit_free (m->new_connection_limiter); +initial_rate_limit_free (m->initial_rate_limiter);multi_reap_free (m->reaper);mroute_helper_free (m->route_helper);multi_tcp_free (m->mtcp);
src/openvpn/multi.h +2 -0
@@ -39,6 +39,7 @@#include "mtcp.h" #include "perf.h" #include "vlan.h" +#include "reflect_filter.h" #define MULTI_PREFIX_MAX_LENGTH 256
@@ -170,6 +171,7 @@ struct multi_context {* as external transport. */ struct ifconfig_pool *ifconfig_pool;struct frequency_limit *new_connection_limiter; +struct initial_packet_rate_limit *initial_rate_limiter;struct mroute_helper *route_helper;struct multi_reap *reaper;struct mroute_addr local;
src/openvpn/openvpn.vcxproj +2 -0
@@ -322,6 +322,7 @@ <ClCompile Include="proxy.c" /> <ClCompile Include="ps.c" /> <ClCompile Include="push.c" /> + <ClCompile Include="reflect_filter.c" /> <ClCompile Include="reliable.c" /> <ClCompile Include="route.c" /> <ClCompile Include="run_command.c" />
@@ -417,6 +418,7 @@ <ClInclude Include="ps.h" /> <ClInclude Include="push.h" /> <ClInclude Include="pushlist.h" /> + <ClInclude Include="reflect_filter.h" /> <ClInclude Include="reliable.h" /> <ClInclude Include="ring_buffer.h" /> <ClInclude Include="route.h" />
src/openvpn/options.c +21 -0
@@ -480,6 +480,7 @@ static const char usage_message[] =" as well as pushes it to connecting clients.\n" "--learn-address cmd : Run command cmd to validate client virtual addresses.\n" "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n" +"--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n" "--max-clients n : Allow a maximum of n simultaneously connected clients.\n" "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n" "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
@@ -864,6 +865,8 @@ init_options(struct options *o, const bool init_gc) o->n_bcast_buf =256 ; o->tcp_queue_limit =64 ; o->max_clients =1024 ; + o->cf_initial_per =10 ; + o->cf_initial_max =100 ; o->max_routes_per_client =256 ; o->stale_routes_check_interval =0 ; o->ifconfig_pool_persist_refresh_freq =600 ;
@@ -1555,6 +1558,8 @@ show_p2mp_parms(const struct options *o)SHOW_BOOL (duplicate_cn);SHOW_INT (cf_max);SHOW_INT (cf_per); +SHOW_INT (cf_initial_max); +SHOW_INT (cf_initial_per);SHOW_INT (max_clients);SHOW_INT (max_routes_per_client);SHOW_STR (auth_user_pass_verify_script);
@@ -7452,6 +7457,22 @@ add_option(struct options *options, options->cf_max = cf_max; options->cf_per = cf_per; } +else if (streq (p[0 ],"connect-freq-initial" ) && p[1 ] && p[2 ] && !p[3 ]) + { +long cf_max, cf_per; + +VERIFY_PERMISSION (OPT_P_GENERAL ); +char *e1, *e2; + cf_max =strtol (p[1 ], &e1,10 ); + cf_per =strtol (p[2 ], &e2,10 ); +if (cf_max <0 || cf_per <0 || *e1 !='\0' || *e2 !='\0' ) + { +msg (msglevel,"--connect-freq-initial parameters must be integers and >= 0" ); + goto err; + } + options->cf_initial_max = cf_max; + options->cf_initial_per = cf_per; + }else if (streq (p[0 ],"max-clients" ) && p[1 ] && !p[2 ]) {int max_clients;
src/openvpn/options.h +5 -0
@@ -513,8 +513,13 @@ struct optionsbool push_ifconfig_ipv6_blocked;/* IPv6 */ bool enable_c2c;bool duplicate_cn; +int cf_max;int cf_per; + +int cf_initial_max; +int cf_initial_per; +int max_clients;int max_routes_per_client;int stale_routes_check_interval;
src/openvpn/reflect_filter.c new file +107 -0
107 lines not on this page · raw message
src/openvpn/reflect_filter.h new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
2.37.1 (Apple Git-137.1)
The following tests were all done on v4. diff v4->v5 is only the extra MSVC build files, so I did not completely re-test v5.
Stare-at-code looks good now (thanks). Fixed a bit of wording in the commit message and reflect_filter.h comments.
Tested on the Linux server test rig, with "normal" traffic, "gremlins" (~100 UDP gremlins) and "just bombarding with RESET packets". Everything worked fine, and the new logs nicely show what is happening:
Jan 9 20:47:48 gentoo tun-udp-p2mp[1718]: Connection Attempt Note: --connect-freq-initial 100 10 rate limit exceeded, dropping initial handshake packets for the next 1 seconds Jan 9 20:47:57 gentoo tun-udp-p2mp[1718]: Dropped 920 initial handshake packets due to --connect-freq-initial 100 10
I have also tested actually using the new option to change defaults :-)
2023-01-09 22:38:31 us=987098 Connection Attempt Note: --connect-freq-initial 200 30 rate limit exceeded, dropping initial handshake packets for the next 30 seconds
(and it does that)
MSVC builds succeed now (GHA), with v5, and for good measure I've thrown in a "make distcheck" ("new source files") which succeeds as well.
Your patch has been applied to the master and release/2.6 branch.
commit b520c68c67b6e52cd71b16675f1c436abf18d4dc (master) commit 93d8d92319816a36f53ef8772bd6705f6abc2df9 (release/2.6)
Author: Arne Schwabe Date: Tue Jan 10 02:59:01 2023 +0100
Add connect-freq-initial option to limit initial connection responses
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg25938.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering