From: "cron2 (Code Review)" Date: Thu, 19 Oct 2023 16:58:42 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <55aae1b6c1a669f1a9dc54b0f24e10640bdc4a6e-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="rj+nzlbHAbg="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: mss/mtu: make all size calculations use size_t List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel , plaisthos --rj+nzlbHAbg= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/26= 9?usp=3Demail ) Change subject: mss/mtu: make all size calculations use size_t ...................................................................... mss/mtu: make all size calculations use size_t Half of them used unsigned int, the other half size_t. Standardize on one. Could've also standardized on the other, both are much too big for the expected numbers anyway. Add a new utility function clamp_size_to_int for cases we need to change from size_t to int (there are a lot of those all over our codebase). Resolves some -Wconversion warnings. Change-Id: Ic996eca227d9e68279a454db93fcbc86a7bd0380 Signed-off-by: Frank Lichtenheld Acked-by: Arne Schwabe Message-Id: <20231008104022.20200-1-frank@...2641...> URL: https://www.mail-archive.com/search?l=3Dmid&q=3D20231008104022.20200-1= -frank@...2641... Signed-off-by: Gert Doering --- M src/openvpn/integer.h M src/openvpn/mss.c M src/openvpn/mtu.c 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/openvpn/integer.h b/src/openvpn/integer.h index 215c159..a012524 100644 --- a/src/openvpn/integer.h +++ b/src/openvpn/integer.h @@ -36,6 +36,13 @@ ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >= > 32)) #endif +static inline int +clamp_size_to_int(size_t size) +{ + ASSERT(size <=3D INT_MAX); + return (int)size; +} + /* * min/max functions */ @@ -188,8 +195,8 @@ /** * Rounds down num to the nearest multiple of multiple */ -static inline unsigned int -round_down_uint(unsigned int num, unsigned int multiple) +static inline size_t +round_down_size(size_t num, size_t multiple) { return (num / multiple) * multiple; } diff --git a/src/openvpn/mss.c b/src/openvpn/mss.c index 4227047..1566c64 100644 --- a/src/openvpn/mss.c +++ b/src/openvpn/mss.c @@ -207,8 +207,8 @@ } } =20 -static inline unsigned int -adjust_payload_max_cbc(const struct key_type *kt, unsigned int target) +static inline size_t +adjust_payload_max_cbc(const struct key_type *kt, size_t target) { if (!cipher_kt_mode_cbc(kt->cipher)) { @@ -221,13 +221,13 @@ /* With CBC we need at least one extra byte for padding and then n= eed * to ensure that the resulting CBC ciphertext length, which is al= ways * a multiple of the block size, is not larger than the target val= ue */ - unsigned int block_size =3D cipher_kt_block_size(kt->cipher); - target =3D round_down_uint(target, block_size); + size_t block_size =3D cipher_kt_block_size(kt->cipher); + target =3D round_down_size(target, block_size); return target - 1; } } -static unsigned int +static size_t get_ip_encap_overhead(const struct options *options, const struct link_socket_info *lsi) { @@ -258,7 +258,7 @@ struct link_socket_info *lsi) { #if defined(ENABLE_FRAGMENT) - unsigned int overhead; + size_t overhead; overhead =3D frame_calculate_protocol_header_size(kt, options, false); @@ -267,12 +267,12 @@ overhead +=3D get_ip_encap_overhead(options, lsi); } - unsigned int target =3D options->ce.fragment - overhead; + size_t target =3D options->ce.fragment - overhead; /* The 4 bytes of header that fragment adds itself. The other extra pa= yload * bytes (Ethernet header/compression) are handled by the fragment code * just as part of the payload and therefore automatically taken into * account if the packet needs to fragmented */ - frame->max_fragment_size =3D adjust_payload_max_cbc(kt, target) - 4; + frame->max_fragment_size =3D clamp_size_to_int(adjust_payload_max_cbc(= kt, target)) - 4; if (cipher_kt_mode_cbc(kt->cipher)) { @@ -296,7 +296,7 @@ return; } - unsigned int overhead, payload_overhead; + size_t overhead, payload_overhead; overhead =3D frame_calculate_protocol_header_size(kt, options, false); @@ -325,7 +325,7 @@ * by ce.mssfix */ /* This is the target value our payload needs to be smaller */ - unsigned int target =3D options->ce.mssfix - overhead; + size_t target =3D options->ce.mssfix - overhead; frame->mss_fix =3D (uint16_t)(adjust_payload_max_cbc(kt, target) - pay= load_overhead); diff --git a/src/openvpn/mtu.c b/src/openvpn/mtu.c index 81851d3..e1e8e02 100644 --- a/src/openvpn/mtu.c +++ b/src/openvpn/mtu.c @@ -167,7 +167,7 @@ */ const char *ciphername =3D o->ciphername; - unsigned int overhead =3D 0; + size_t overhead =3D 0; if (strcmp(o->ciphername, "BF-CBC") =3D=3D 0) { @@ -185,7 +185,7 @@ * the ciphers are actually valid for non tls in occ calucation */ init_key_type(&occ_kt, ciphername, o->authname, true, false); - unsigned int payload =3D frame_calculate_payload_size(frame, o, &occ_k= t); + size_t payload =3D frame_calculate_payload_size(frame, o, &occ_kt); overhead +=3D frame_calculate_protocol_header_size(&occ_kt, o, true); return payload + overhead; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/269?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ic996eca227d9e68279a454db93fcbc86a7bd0380 Gerrit-Change-Number: 269 Gerrit-PatchSet: 5 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --rj+nzlbHAbg= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

mss/mtu: make all size calculations use size_t=


Half of them used unsigned int, the other half size_t.
Standardi= ze on one. Could've also standardized on the
other, both are much to= o big for the expected numbers
anyway.

Add a new utility function= clamp_size_to_int for
cases we need to change from size_t to int (there=
are a lot of those all over our codebase).

Resolves some -Wconve= rsion warnings.

Change-Id: Ic996eca227d9e68279a454db93fcbc86a7bd0380=
Signed-off-by: Frank Lichtenheld <frank@...2641...>
Acked-by: = Arne Schwabe <arne-openvpn@...1227...>
Message-Id: <20231008104= 022.20200-1-frank@...2641...>
URL: https://www.mail-archive.com/searc= h?l=3Dmid&q=3D20231008104022.20200-1-frank@...2641...
Signed-off-by:= Gert Doering <gert@...1296...>
---
M src/openvpn/integer.h
= M src/openvpn/mss.c
M src/openvpn/mtu.c
3 files changed, 21 insertion= s(+), 14 deletions(-)

diff --git a/src/openvpn/integer.h b/src/openvpn/integer.h
index 215c159..a012524 100644
--- a/src/openvpn/integer.= h
+++ b/src/openvpn/integer.h
@@ -36,6 +36,= 13 @@
((uint64_t)ntohl((x) & 0xFFFF= FFFF) << 32) | ntohl((x) >> 32))
#endif=

+static i= nline int
+clamp_size= _to_int(size_t size)
= +{
+ ASSERT(size &= lt;=3D INT_MAX);
+ = return (int)size;
+}=
+
/= *
* min/max functions
*/
@@ -188,8 +195,8 @@
/**
* Rounds down = num to the nearest multiple of multiple
*/
-static inline unsigned int-round_down_uint(unsigned int nu= m, unsigned int multiple)
+static inline size_t
+round_down_size(size_t num, size_t multiple)
{

return (num / multiple) * multiple;
}

diff --git a/src/openvpn/mss.c b/src/openvpn/mss.c
<= span>index 4227047..1566c64 100644
--- a/src/openvpn/mss.c<= /span>
+++ b/src/openvpn/mss.c
@@ -207,8 +207,8 @@=
}
}

-static inline unsigned int
-adjust_payload_max_cbc(const st= ruct key_type *kt, unsigned int target)
+static inline size_t
+adjust_payload_max_cbc(const struct key_type *kt, size= _t target)
{
if (!cipher_kt_mode_cbc(= kt->cipher))
{
@@ -221,13 +221,13 @= @
/* With CBC we need at least one extra byte for = padding and then need
* to ensure that the result= ing CBC ciphertext length, which is always
* a mu= ltiple of the block size, is not larger than the target value */
= - unsigned int block_size = =3D cipher_kt_block_size(kt->cipher);
- target =3D round_down_uint(target, block_size);<= /span>
+ size_t block= _size =3D cipher_kt_block_size(kt->cipher);
+ target =3D round_down_size(target, block= _size);
return target - 1;
}<= /span>
}

-static unsigned int
+static size_t
get_ip_encap_overhead(const s= truct options *options,
const struct= link_socket_info *lsi)
{
@@ -258,7 +258,7= @@
struct link_socket_info *lsi)=
{
#if defined(ENABLE_FRAGMENT)- unsigned int overhead;
+ size_t overhead;


overhead =3D frame_calculate_protocol_h= eader_size(kt, options, false);

@@ -267,1= 2 +267,12 @@
overhead +=3D get_ip_encap_overhead(o= ptions, lsi);
}

- unsigned int target =3D options->ce= .fragment - overhead;
+ size_t target =3D options->ce.fragment - overhead;
/* The 4 bytes of header that fragment adds itself. The other extra p= ayload
* bytes (Ethernet header/compression) are hand= led by the fragment code
* just as part of the payloa= d and therefore automatically taken into
* account if= the packet needs to fragmented */
- frame->max_fragment_size =3D adjust_payload_max_cbc(kt,= target) - 4;
+ fr= ame->max_fragment_size =3D clamp_size_to_int(adjust_payload_max_cbc(kt, = target)) - 4;

if (cipher_kt_mode_cbc= (kt->cipher))
{
@@ -296,7 +296,7 @@=
return;
}
<= /span>
- unsigned int overh= ead, payload_overhead;
- unsigned int target =3D options->ce.mssfix= - overhead;
+ siz= e_t target =3D options->ce.mssfix - overhead;
frame= ->mss_fix =3D (uint16_t)(adjust_payload_max_cbc(kt, target) - payload_ov= erhead);


diff --git a/s= rc/openvpn/mtu.c b/src/openvpn/mtu.c
index 81851d3..e1e8e02= 100644
--- a/src/openvpn/mtu.c
+++ b/src/o= penvpn/mtu.c
@@ -167,7 +167,7 @@
*/
const char *ciphername =3D o->ciphername;
=
- unsigned i= nt overhead =3D 0;
+ = size_t overhead =3D 0;

if (strcmp= (o->ciphername, "BF-CBC") =3D=3D 0)
{
@@ -185,7 +185,7 @@
* the ciphers are ac= tually valid for non tls in occ calucation */
init_key= _type(&occ_kt, ciphername, o->authname, true, false);

- unsigned int p= ayload =3D frame_calculate_payload_size(frame, o, &occ_kt);
<= span style=3D"color: hsl(120, 100%, 40%);">+ size_t payload =3D frame_ca= lculate_payload_size(frame, o, &occ_kt);

overhead = +=3D frame_calculate_protocol_header_size(&occ_kt, o, true);
=
return payload + overhead;

To view, visit change 269. To unsubscribe, or for help writing = mail filters, visit settings= .

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ic996eca227d9e68279a454db93f= cbc86a7bd0380
Gerrit-Change-Number: 269
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: merged
--rj+nzlbHAbg=--