Skip to contentSkip to conversation

Change in openvpn[master]: Implement initial packet reflection protection using bloom filter

Openvpn-devel plaisthos (Code Review) 1 participant 2 messages Sep 21, 2024 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1677175662000.I0a9274cab7fefce3b13c05052fb9a072e0bfa6b9@...2715...

#1 lore raw

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/30?usp=email

to review the following change.

Change subject: Implement initial packet reflection protection using bloom filter
......................................................................

Implement initial packet reflection protection using bloom filter

When an OpenVPN server is used/tried to be usedc in a reflection attack the protection with the simple --connect-freq-initial also block legimitate client from other networks that are not attacked by a reflection attack.

To allow a server to still reply to these clients, we need to make the counts rather more detailed and count per subnet or IP address. On the other hand when we keep all this state, we eliminate the advantage of having a stateless cookie based initial packet handshake.

As compromoise we use a bloom filter to store the information. This data structure is probabilistic and can have false positive and more packets being dropped but since it is a constant size and the size of this map is small enough for non-embedded systems (tests were done with a 2MB bloom filter), it is a good compromise.

The code is split into the bloom filter implementation and the actual logic implementing tracking the subnets, so the bloom filter should be relatively easily be exchangable by another data structure.

As hash funtion SIPHASH has been chosen since it was designed for this kind of application.

Change-Id I0a9274cab7fefce3b13c05052fb9a072e0bfa6b9

---
M .github/workflows/build.yaml
M CMakeLists.txt
M Changes.rst
M doc/man-sections/server-options.rst
M src/openvpn/Makefile.am
A src/openvpn/bloom.c
A src/openvpn/bloom.h
M src/openvpn/mudp.c
M src/openvpn/multi.c
M src/openvpn/options.c
M src/openvpn/options.h
M src/openvpn/reflect_filter.c
M src/openvpn/reflect_filter.h
M tests/unit_tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/test_reflect.c

15 files changed, 1,302 insertions(+), 15 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/30/30/9

.github/workflows/build.yaml +3 -0
index 361d457..93c67d1 100644
@@ -102,6 +102,9 @@
         env:
           srcdir: "${{ github.workspace }}/tests/unit_tests/openvpn"
 
+      - name: Run reflect unit test
+        run: ./unittests/test_reflect.exe
+
   ubuntu:
     strategy:
       fail-fast: false
CMakeLists.txt +17 -0
index ad620fa..5aba2d4 100644
@@ -369,6 +369,7 @@
     src/openvpn/base64.c
     src/openvpn/base64.h
     src/openvpn/basic.h
+    src/openvpn/bloom.c
     src/openvpn/buffer.c
     src/openvpn/buffer.h
     src/openvpn/circ_list.h
@@ -511,6 +512,8 @@
     src/openvpn/shaper.h
     src/openvpn/sig.c
     src/openvpn/sig.h
+    src/openvpn/siphash.h
+    src/openvpn/siphash_reference.c
     src/openvpn/socket.c
     src/openvpn/socket.h
     src/openvpn/socks.c
@@ -605,6 +608,7 @@
         "test_packet_id"
         "test_pkt"
         "test_provider"
+        "test_reflect"
         "test_ssl"
         "test_user_pass"
         )
@@ -770,6 +774,19 @@
         src/openvpn/session_id.c
         )
 
+    target_sources(test_reflect PRIVATE
+        src/openvpn/bloom.c
+        src/openvpn/reflect_filter.c
+        src/openvpn/reflect_filter.h
+        src/openvpn/siphash.h
+        src/openvpn/siphash_reference.c
+        src/openvpn/otime.c
+        src/openvpn/crypto_mbedtls.c
+        src/openvpn/crypto_openssl.c
+        src/openvpn/crypto.c
+        src/openvpn/packet_id.c
+        )
+
     target_sources(test_pkt PRIVATE
         tests/unit_tests/openvpn/mock_win32_execve.c
         src/openvpn/argv.c
Changes.rst +8 -0
index 439352a..af72678 100644
@@ -9,6 +9,14 @@
     the user experience as the client shows an error instead of running into
     a timeout when the server just stops responding completely.
 
+Bloom filter based reflection protection
+    To avoid the limitation of ``--connect-freq-initial`` to block legimitate
+    clients when an OpenVPN server is tried to be used in a reflection attack,
+    the new ``--connect-freq-initial-bloom-limit`` can impose limit on a
+    per-subnet basis. See the manual for more details. Note: this option
+    requires 2MB of extra memory in the default configuration.
+
+
 Deprecated features
 -------------------
 ``secret`` support has been removed by default.
doc/man-sections/server-options.rst +71 -0

77 lines not on this page · raw message

src/openvpn/Makefile.am +2 -0
index 3784a98..ca77718 100644
@@ -44,6 +44,7 @@
 	auth_token.c auth_token.h \
 	base64.c base64.h \
 	basic.h \
+    bloom.c bloom.h \
 	buffer.c buffer.h \
 	circ_list.h \
 	clinat.c clinat.h \
@@ -124,6 +125,7 @@
 	session_id.c session_id.h \
 	shaper.c shaper.h \
 	sig.c sig.h \
+	siphash_reference.c siphash.h \
 	socket.c socket.h \
 	socks.c socks.h \
 	ssl.c ssl.h  ssl_backend.h \
src/openvpn/bloom.c new file +253 -0

253 lines not on this page · raw message

src/openvpn/bloom.h new file +97 -0

97 lines not on this page · raw message

src/openvpn/mudp.c +3 -2
index 268b430..9fbffac 100644
@@ -84,7 +84,7 @@
     {
         /* Check if we are still below our limit for sending out
          * responses */
-        if (!reflect_filter_rate_limit_check(m->initial_rate_limiter))
+        if (!reflect_filter_check(m->initial_rate_limiter, from))
         {
             return false;
         }
@@ -254,7 +254,8 @@
                 {
                     /* 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);
+                    reflect_filter_rate_limit_decrease(m->initial_rate_limiter,
+                                                       &m->top.c2.from.dest);
 
                     mi = multi_create_instance(m, &real);
                     if (mi)
src/openvpn/multi.c +2 -1
index 0509911..b9de040 100644
@@ -368,7 +368,8 @@
     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);
+                                                      t->options.cf_initial_per,
+                                                      &t->options.initial_cf_bloom_config);
 
     /*
      * Allocate broadcast/multicast buffer list
src/openvpn/options.c +93 -0

111 lines not on this page · raw message

src/openvpn/options.h +3 -0
index f608cb8..8448071 100644
@@ -43,6 +43,7 @@
 #include "clinat.h"
 #include "crypto_backend.h"
 #include "dns.h"
+#include "reflect_filter.h"
 
 
 /*
@@ -515,6 +516,8 @@
     int cf_initial_max;
     int cf_initial_per;
 
+    struct bloom_filter_conf initial_cf_bloom_config;
+
     int max_clients;
     int max_routes_per_client;
     int stale_routes_check_interval;
src/openvpn/reflect_filter.c +356 -7

398 lines not on this page · raw message

src/openvpn/reflect_filter.h +50 -4

94 lines not on this page · raw message

tests/unit_tests/openvpn/Makefile.am +17 -1
index a4e6235..cd1c378 100644
@@ -11,7 +11,7 @@
 endif
 
 test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \
-	pkt_testdriver ssl_testdriver user_pass_testdriver
+	pkt_testdriver ssl_testdriver user_pass_testdriver reflect_testdriver
 
 if HAVE_LD_WRAP_SUPPORT
 if !WIN32
@@ -144,6 +144,22 @@
 	$(top_srcdir)/src/openvpn/win32-util.c \
 	$(top_srcdir)/src/openvpn/tls_crypt.c
 
+reflect_testdriver_CFLAGS  = @TEST_CFLAGS@ \
+	-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn
+reflect_testdriver_LDFLAGS = @TEST_LDFLAGS@
+reflect_testdriver_SOURCES = test_reflect.c mock_msg.c mock_msg.h \
+	$(top_srcdir)/src/openvpn/reflect_filter.c \
+	$(top_srcdir)/src/openvpn/bloom.c \
+	$(top_srcdir)/src/openvpn/buffer.c \
+	$(top_srcdir)/src/openvpn/crypto.c \
+	$(top_srcdir)/src/openvpn/crypto_mbedtls.c \
+	$(top_srcdir)/src/openvpn/crypto_openssl.c \
+	$(top_srcdir)/src/openvpn/otime.c \
+	$(top_srcdir)/src/openvpn/packet_id.c \
+	$(top_srcdir)/src/openvpn/platform.c \
+	$(top_srcdir)/src/openvpn/siphash_reference.c \
+	$(top_srcdir)/src/openvpn/win32-util.c
+
 if !WIN32
 tls_crypt_testdriver_CFLAGS  = \
 	-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
tests/unit_tests/openvpn/test_reflect.c new file +327 -0

327 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/30?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0a9274cab7fefce3b13c05052fb9a072e0bfa6b9
Gerrit-Change-Number: 30
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/30?usp=email )

Change subject: Implement initial packet reflection protection using bloom filter
......................................................................

Patch Set 9: Code-Review-1

(10 comments)

Patchset:

PS9:
I havne't reviewed the whole thing yet, but I found some stuff to complain about

File src/openvpn/Makefile.am:

http://gerrit.openvpn.net/c/openvpn/+/30/comment/5feab99c_f33c2318 :
PS8, Line 46: b
tabs vs spaces

File src/openvpn/bloom.h:

http://gerrit.openvpn.net/c/openvpn/+/30/comment/88db0086_921e0752 :
PS8, Line 8: 2022
Shouldn't this be 2024?

http://gerrit.openvpn.net/c/openvpn/+/30/comment/c0e46973_ad5f7ee1 :
PS8, Line 32: BLOOM_FILTER_BIT_COUNT
BITS

File src/openvpn/bloom.c:

http://gerrit.openvpn.net/c/openvpn/+/30/comment/ca1b18dc_b9ffbcbe :
PS8, Line 101: /**

             :  * Calculates the number of bytes we need for storing a bloom filter of size
             :  * size. We add + 1 to avoid rounding problems and too small allocation */
             : static inline
             : size_t
             : bloom_get_filter_byte_count(size_t size)
             : {
             :     static_assert(sizeof(bloom_counter_t) * 8 % BLOOM_FILTER_BITS_COUNT == 0,
             :                   "bloom_counter_t must be a multiple of BLOOM_FILTER_BIT_COUNT");
             :
             :     return size * sizeof(bloom_counter_t)/BLOOM_FILTER_BITS_COUNT + 1;
             : }

I'm really confused by what size means here. This is *not* the size field in the bloom filter struct, right? It's the total number of bits in the bloom filter, right?

http://gerrit.openvpn.net/c/openvpn/+/30/comment/88af9dc3_bf01868e :
PS8, Line 115: static inline

             : size_t
             : bloom_get_filter_bit_offset(size_t bucket)
             : {
             :     return (bucket * BLOOM_FILTER_BITS_COUNT) % sizeof(bloom_counter_t);
             : }

If this is a bit offset, don't we need 8 * sizeof(bloom_counter_t) here?

http://gerrit.openvpn.net/c/openvpn/+/30/comment/f0ce5e7f_d277c415 :
PS8, Line 122: static inline

             : size_t
             : bloom_get_filter_array_index(size_t bucket)
             : {
             :     return (bucket * BLOOM_FILTER_BITS_COUNT) / sizeof(bloom_counter_t);
             : }

This seems wrong to me. Every bucket is 2 bits wide. The bloom_counter_t is 32 bits wide, or 4 bytes, so sizeof(bloom_counter_t) == 4.

Bucket 3 is at a 6 bit offset, so it's in the first bloom_counter_t (array index 0). But this function gives 3 * 2 / 4 == 1.

http://gerrit.openvpn.net/c/openvpn/+/30/comment/688a1a4a_2c63f6ac :
PS8, Line 172:
whitespace

File src/openvpn/reflect_filter.c:

http://gerrit.openvpn.net/c/openvpn/+/30/comment/e570d1bc_a93710c4 :
PS8, Line 43: static bool
: reflect_filter_rate_l
Remove empty line

http://gerrit.openvpn.net/c/openvpn/+/30/comment/71c672e1_c93872de :
PS8, Line 116: /* we keep the count in the key instead of in the bloom filter table as
: * can then keep the counter in the bloom filter itself small (2 bits)
...as we can then...

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/30?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0a9274cab7fefce3b13c05052fb9a072e0bfa6b9
Gerrit-Change-Number: 30
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Sat, 21 Sep 2024 14:26:47 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment