Skip to contentSkip to conversation

Make buffer related function conversion explicit when narrowing

PATCH In review Arne Schwabe 2 participants 8 messages Mar 24, 2021 – Mar 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Make buffer related function conversion explicit when narrowing

Clang and gcc do report many of the narrowing conversion that MSVC reports, like these:

warning C4267: 'function': conversion from 'size_t' to 'int', possible loss of data

This commit changes int to size_t where it is safe (e.g. checked by buf_size_valid) and add casts where necessary.

In the function buffer_read_from_file the return value of fread is size_t (at least on Linux/Windows/macOS and cppreference), so fix the check to actually make sense.

---
 src/openvpn/buffer.c | 20 ++++++++++----------
 src/openvpn/buffer.h | 16 ++++++++--------
 2 files changed, 18 insertions(+), 18 deletions(-)
src/openvpn/buffer.c +10 -10
index 35d9ecdc..48bf25d5 100644
@@ -661,7 +661,7 @@ rm_trailing_chars(char *str, const char *what_to_delete)
     bool modified;
     do
     {
-        const int len = strlen(str);
+        const size_t len = strlen(str);
         modified = false;
         if (len > 0)
         {
@@ -687,7 +687,7 @@ string_alloc(const char *str, struct gc_arena *gc)
 {
     if (str)
     {
-        const int n = strlen(str) + 1;
+        const size_t n = strlen(str) + 1;
         char *ret;
 
         if (gc)
@@ -814,7 +814,7 @@ string_alloc_buf(const char *str, struct gc_arena *gc)
 bool
 buf_string_match_head_str(const struct buffer *src, const char *match)
 {
-    const int size = strlen(match);
+    const size_t size = strlen(match);
     if (size < 0 || size > src->len)
     {
         return false;
@@ -827,7 +827,7 @@ buf_string_compare_advance(struct buffer *src, const char *match)
 {
     if (buf_string_match_head_str(src, match))
     {
-        buf_advance(src, strlen(match));
+        buf_advance(src, (int)strlen(match));
         return true;
     }
     else
@@ -1244,7 +1244,7 @@ buffer_list_push(struct buffer_list *ol, const char *str)
         struct buffer_entry *e = buffer_list_push_data(ol, str, len+1);
         if (e)
         {
-            e->buf.len = len; /* Don't count trailing '\0' as part of length */
+            e->buf.len = (int)len; /* Don't count trailing '\0' as part of length */
         }
     }
 }
@@ -1293,7 +1293,7 @@ void
 buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len,
                                 const char *sep)
 {
-    const int sep_len = strlen(sep);
+    const size_t sep_len = strlen(sep);
     struct buffer_entry *more = bl->head;
     size_t size = 0;
     int count = 0;
@@ -1400,7 +1400,7 @@ buffer_read_from_file(const char *filename, struct gc_arena *gc)
 {
     struct buffer ret = { 0 };
 
-    platform_stat_t file_stat = {0};
+    platform_stat_t file_stat = { 0 };
     if (platform_stat(filename, &file_stat) < 0)
     {
         return ret;
@@ -1414,13 +1414,13 @@ buffer_read_from_file(const char *filename, struct gc_arena *gc)
 
     const size_t size = file_stat.st_size;
     ret = alloc_buf_gc(size + 1, gc); /* space for trailing \0 */
-    ssize_t read_size = fread(BPTR(&ret), 1, size, fp);
-    if (read_size < 0)
+    size_t read_size = fread(BPTR(&ret), 1, size, fp);
+    if (read_size == 0)
     {
         free_buf_gc(&ret, gc);
         goto cleanup;
     }
-    ASSERT(buf_inc_len(&ret, read_size));
+    ASSERT(buf_inc_len(&ret, (int)read_size));
     buf_null_terminate(&ret);
 
 cleanup:
src/openvpn/buffer.h +8 -8
index 1722ffd5..9ea70284 100644
@@ -332,13 +332,13 @@ buf_set_write(struct buffer *buf, uint8_t *data, int size)
 }
 
 static inline void
-buf_set_read(struct buffer *buf, const uint8_t *data, int size)
+buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
 {
     if (!buf_size_valid(size))
     {
         buf_size_error(size);
     }
-    buf->len = buf->capacity = size;
+    buf->len = buf->capacity = (int)size;
     buf->offset = 0;
     buf->data = (uint8_t *)data;
 }
@@ -538,10 +538,10 @@ struct buffer buf_sub(struct buffer *buf, int size, bool prepend);
  */
 
 static inline bool
-buf_safe(const struct buffer *buf, int len)
+buf_safe(const struct buffer *buf, size_t len)
 {
     return buf_valid(buf) && buf_size_valid(len)
-           && buf->offset + buf->len + len <= buf->capacity;
+           && buf->offset + buf->len + (int)len <= buf->capacity;
 }
 
 static inline bool
@@ -549,7 +549,7 @@ buf_safe_bidir(const struct buffer *buf, int len)
 {
     if (buf_valid(buf) && buf_size_valid_signed(len))
     {
-        const int newlen = buf->len + len;
+        int newlen = buf->len + len;
         return newlen >= 0 && buf->offset + newlen <= buf->capacity;
     }
     else
@@ -653,7 +653,7 @@ buf_advance(struct buffer *buf, int size)
  */
 
 static inline uint8_t *
-buf_write_alloc(struct buffer *buf, int size)
+buf_write_alloc(struct buffer *buf, size_t size)
 {
     uint8_t *ret;
     if (!buf_safe(buf, size))
@@ -661,7 +661,7 @@ buf_write_alloc(struct buffer *buf, int size)
         return NULL;
     }
     ret = BPTR(buf) + buf->len;
-    buf->len += size;
+    buf->len += (int)size;
     return ret;
 }
 
@@ -686,7 +686,7 @@ buf_read_alloc(struct buffer *buf, int size)
 }
 
 static inline bool
-buf_write(struct buffer *dest, const void *src, int size)
+buf_write(struct buffer *dest, const void *src, size_t size)
 {
     uint8_t *cp = buf_write_alloc(dest, size);
     if (!cp)
Signature · 1 line
2.30.1
#1 lore raw

PATCH 2/4 Fix unchecked signess conversions reported by MSVC

Whenever possible the types have been aligned in the various parts of OpenVPN. If that was not possible, an explicit cast to a narrower type has been added.

---
 src/openvpn/console_builtin.c | 2 +-
 src/openvpn/dhcp.c            | 2 +-
 src/openvpn/multi.h           | 2 +-
 src/openvpn/options.h         | 2 +-
 src/openvpn/packet_id.h       | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
src/openvpn/console_builtin.c +1 -1
index 445928bf..74a0e2b6 100644
@@ -74,7 +74,7 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
     if (in != INVALID_HANDLE_VALUE
         && err != INVALID_HANDLE_VALUE
         && !win32_service_interrupt(&win32_signal)
-        && WriteFile(err, prompt, strlen(prompt), &len, NULL))
+        && WriteFile(err, prompt, (DWORD)strlen(prompt), &len, NULL))
     {
         bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
         DWORD flags_save = 0;
src/openvpn/dhcp.c +1 -1
index c19370eb..1e79ccc4 100644
@@ -113,7 +113,7 @@ do_extract(struct dhcp *dhcp, int optlen)
                         const int owlen = len + 2;        /* len of data to overwrite */
                         uint8_t *src = dest + owlen;
                         uint8_t *end = p + optlen;
-                        const int movlen = end - src;
+                        const size_t movlen = end - src;
                         if (movlen > 0)
                         {
                             memmove(dest, src, movlen);   /* overwrite router option */
src/openvpn/multi.h +1 -1
index 7669508c..65f64659 100644
@@ -181,7 +181,7 @@ struct multi_context {
     struct mroute_addr local;
     bool enable_c2c;
     int max_clients;
-    int tcp_queue_limit;
+    unsigned int tcp_queue_limit;
     int status_file_version;
     int n_clients; /* current number of authenticated clients */
 
src/openvpn/options.h +1 -1
index f3208c71..d8e91fbc 100644
@@ -457,7 +457,7 @@ struct options
     bool ccd_exclusive;
     bool disable;
     int n_bcast_buf;
-    int tcp_queue_limit;
+    unsigned int tcp_queue_limit;
     struct iroute *iroutes;
     struct iroute_ipv6 *iroutes_ipv6;                   /* IPv6 */
     bool push_ifconfig_defined;
src/openvpn/packet_id.h +1 -1
index 3b58da22..0d2e094d 100644
@@ -316,7 +316,7 @@ packet_id_close_to_wrapping(const struct packet_id_send *p)
 static inline bool
 check_timestamp_delta(time_t remote, unsigned int max_delta)
 {
-    unsigned int abs;
+    time_t abs;
     const time_t local_now = now;
 
     if (local_now >= remote)
Signature · 1 line
2.30.1
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/4] Fix unchecked signess conversions reported by MSVC

Hi,

src/openvpn/dhcp.c do_extract(struct dhcp *dhcp, int optlen) · line 117

Quoted 11 lines · Arne Schwabe
src/openvpn/dhcp.c +1 -1
index c19370eb..1e79ccc4 100644
@@ -113,7 +113,7 @@ do_extract(struct dhcp *dhcp, int optlen)
                         const int owlen = len + 2;        /* len of data to overwrite */
                         uint8_t *src = dest + owlen;
                         uint8_t *end = p + optlen;
-                        const int movlen = end - src;
+                        const size_t movlen = end - src;
                         if (movlen > 0)

This code scares me... I think it is correct because of all these checks around, but I like the version with a signed int really more "should all these assumptions about i<optlen, len<=(room+2), .. no longer hold, and movlen ever be negative, the old code would not copy anything, and the new code would overwrite all memory".

Based on that, I'd do a "(size_t)movlen" cast here, if positive.

The rest of the patch looks good.

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]...
#3 lore raw

PATCH 3/4 Fix socket related functions using int instead of socket_descriptor_t

In reply to Arne Schwabe

On windows the SOCKET type is a UINT_PTR, which is a 64 bit pointer, so using an int worked so far but is actually quite wrong.

---
 src/openvpn/fdmisc.c |  8 ++++----
 src/openvpn/fdmisc.h | 10 +++++-----
 src/openvpn/mtu.c    |  2 +-
 src/openvpn/mtu.h    |  2 +-
 src/openvpn/socket.c | 16 ++++++++--------
 5 files changed, 19 insertions(+), 19 deletions(-)
src/openvpn/fdmisc.c +4 -4
index 1cea505c..e0d198e5 100644
@@ -36,7 +36,7 @@
 
 /* Set a file descriptor to non-blocking */
 bool
-set_nonblock_action(int fd)
+set_nonblock_action(socket_descriptor_t fd)
 {
 #ifdef _WIN32
     u_long arg = 1;
@@ -55,7 +55,7 @@ set_nonblock_action(int fd)
 
 /* Set a file descriptor to not be passed across execs */
 bool
-set_cloexec_action(int fd)
+set_cloexec_action(socket_descriptor_t fd)
 {
 #ifndef _WIN32
     if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
@@ -68,7 +68,7 @@ set_cloexec_action(int fd)
 
 /* Set a file descriptor to non-blocking */
 void
-set_nonblock(int fd)
+set_nonblock(socket_descriptor_t fd)
 {
     if (!set_nonblock_action(fd))
     {
@@ -78,7 +78,7 @@ set_nonblock(int fd)
 
 /* Set a file descriptor to not be passed across execs */
 void
-set_cloexec(int fd)
+set_cloexec(socket_descriptor_t fd)
 {
     if (!set_cloexec_action(fd))
     {
src/openvpn/fdmisc.h +5 -5
index 0fb8b934..f4b67482 100644
@@ -28,16 +28,16 @@
 #include "error.h"
 #include "syshead.h"
 
-bool set_nonblock_action(int fd);
+bool set_nonblock_action(socket_descriptor_t fd);
 
-bool set_cloexec_action(int fd);
+bool set_cloexec_action(socket_descriptor_t fd);
 
-void set_nonblock(int fd);
+void set_nonblock(socket_descriptor_t fd);
 
-void set_cloexec(int fd);
+void set_cloexec(socket_descriptor_t fd);
 
 static inline void
-openvpn_fd_set(int fd, fd_set *setp)
+openvpn_fd_set(socket_descriptor_t fd, fd_set *setp)
 {
 #ifndef _WIN32 /* The Windows FD_SET() implementation does not overflow */
     ASSERT(fd >= 0 && fd < FD_SETSIZE);
src/openvpn/mtu.c +1 -1
index 3ddeac76..3317c884 100644
@@ -166,7 +166,7 @@ frame_print(const struct frame *frame,
 #define MTUDISC_NOT_SUPPORTED_MSG "--mtu-disc is not supported on this OS"
 
 void
-set_mtu_discover_type(int sd, int mtu_type, sa_family_t proto_af)
+set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af)
 {
     if (mtu_type >= 0)
     {
src/openvpn/mtu.h +1 -1
index 549c319b..0c8bdf8b 100644
@@ -209,7 +209,7 @@ void frame_print(const struct frame *frame,
                  int level,
                  const char *prefix);
 
-void set_mtu_discover_type(int sd, int mtu_type, sa_family_t proto_af);
+void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af);
 
 int translate_mtu_discover_type_name(const char *name);
 
src/openvpn/socket.c +8 -8
index ebe8c85c..0d9b793c 100644
@@ -846,7 +846,7 @@ mac_addr_safe(const char *mac_addr)
 }
 
 static int
-socket_get_sndbuf(int sd)
+socket_get_sndbuf(socket_descriptor_t sd)
 {
 #if defined(HAVE_GETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_SNDBUF)
     int val;
@@ -863,7 +863,7 @@ socket_get_sndbuf(int sd)
 }
 
 static void
-socket_set_sndbuf(int sd, int size)
+socket_set_sndbuf(socket_descriptor_t sd, int size)
 {
 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_SNDBUF)
     if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (void *) &size, sizeof(size)) != 0)
@@ -874,7 +874,7 @@ socket_set_sndbuf(int sd, int size)
 }
 
 static int
-socket_get_rcvbuf(int sd)
+socket_get_rcvbuf(socket_descriptor_t sd)
 {
 #if defined(HAVE_GETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_RCVBUF)
     int val;
@@ -891,7 +891,7 @@ socket_get_rcvbuf(int sd)
 }
 
 static bool
-socket_set_rcvbuf(int sd, int size)
+socket_set_rcvbuf(socket_descriptor_t sd, int size)
 {
 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_RCVBUF)
     if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (void *) &size, sizeof(size)) != 0)
@@ -904,7 +904,7 @@ socket_set_rcvbuf(int sd, int size)
 }
 
 static void
-socket_set_buffers(int fd, const struct socket_buffer_size *sbs)
+socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs)
 {
     if (sbs)
     {
@@ -934,7 +934,7 @@ socket_set_buffers(int fd, const struct socket_buffer_size *sbs)
  */
 
 static bool
-socket_set_tcp_nodelay(int sd, int state)
+socket_set_tcp_nodelay(socket_descriptor_t sd, int state)
 {
 #if defined(_WIN32) || (defined(HAVE_SETSOCKOPT) && defined(IPPROTO_TCP) && defined(TCP_NODELAY))
     if (setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (void *) &state, sizeof(state)) != 0)
@@ -954,7 +954,7 @@ socket_set_tcp_nodelay(int sd, int state)
 }
 
 static inline void
-socket_set_mark(int sd, int mark)
+socket_set_mark(socket_descriptor_t sd, int mark)
 {
 #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
     if (mark && setsockopt(sd, SOL_SOCKET, SO_MARK, (void *) &mark, sizeof(mark)) != 0)
@@ -965,7 +965,7 @@ socket_set_mark(int sd, int mark)
 }
 
 static bool
-socket_set_flags(int sd, unsigned int sockflags)
+socket_set_flags(socket_descriptor_t sd, unsigned int sockflags)
 {
     if (sockflags & SF_TCP_NODELAY)
     {
Signature · 1 line
2.30.1
Acked-by on v1 3/4
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix socket related functions using int instead of socket_descriptor_t

On non-windows, socket_descriptor_t is typedef'ed to "int", so this is no "generated code change", just ensuring consistency in our types. On Windows, this is a "SOCKET", which might or might not fit into an int, but is certainly not interchangeable. So, makes sense.

Compile-tested on MinGW, lightly client-tested on FreeBSD and Linux.

Your patch has been applied to the master branch.

commit e756e12adb33e93f712281361ae61a1522d3df93

Author: Arne Schwabe
Date:   Wed Mar 24 23:23:29 2021 +0100
     Fix socket related functions using int instead of socket_descriptor_t

     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]/msg21806.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#5 lore raw

PATCH 4/4 Use correct types for OpenSSL and Windows APIs

In reply to Arne Schwabe

The error code of OpenSSL is a long. On most Unics systems (mac, Linux...) this happens to be the same as size_t. But on Windows as LP64, long is a 32 bit type and size_t is a 64 bit type. So use the same type as OpenSSL.

When calling the Windows API use DWORD for the functions that want a DWORD.

---
 src/openvpn/crypto_openssl.c | 2 +-
 src/openvpn/cryptoapi.c      | 2 +-
 src/openvpn/route.c          | 9 +++------
 3 files changed, 5 insertions(+), 8 deletions(-)
src/openvpn/crypto_openssl.c +1 -1
index 4486d246..573beaed 100644
@@ -199,7 +199,7 @@ crypto_clear_error(void)
 void
 crypto_print_openssl_errors(const unsigned int flags)
 {
-    size_t err = 0;
+    unsigned long err = 0;
 
     while ((err = ERR_get_error()))
     {
src/openvpn/cryptoapi.c +1 -1
index a992441b..ded8c914 100644
@@ -997,7 +997,7 @@ pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
     }
 
     msg(D_LOW, "cryptoapicert: calling priv_enc_CNG with alg = %ls", alg);
-    *siglen = priv_enc_CNG(cd, alg, tbs, (int)tbslen, sig, *siglen,
+    *siglen = priv_enc_CNG(cd, alg, tbs, (int)tbslen, sig, (int)*siglen,
                            cng_padding_type(padding), (DWORD)saltlen);
 
     return (*siglen == 0) ? 0 : 1;
src/openvpn/route.c +3 -6
index 5e1dca67..c6b3dc58 100644
@@ -2701,12 +2701,11 @@ get_default_gateway_row(const MIB_IPFORWARDTABLE *routes)
     struct gc_arena gc = gc_new();
     DWORD lowest_metric = MAXDWORD;
     const MIB_IPFORWARDROW *ret = NULL;
-    int i;
     int best = -1;
 
     if (routes)
     {
-        for (i = 0; i < routes->dwNumEntries; ++i)
+        for (DWORD i = 0; i < routes->dwNumEntries; ++i)
         {
             const MIB_IPFORWARDROW *row = &routes->table[i];
             const in_addr_t net = ntohl(row->dwForwardDest);
@@ -3167,14 +3166,13 @@ void
 show_routes(int msglev)
 {
     struct gc_arena gc = gc_new();
-    int i;
 
     const MIB_IPFORWARDTABLE *rt = get_windows_routing_table(&gc);
 
     msg(msglev, "SYSTEM ROUTING TABLE");
     if (rt)
     {
-        for (i = 0; i < rt->dwNumEntries; ++i)
+        for (DWORD i = 0; i < rt->dwNumEntries; ++i)
         {
             msg(msglev, "%s", format_route_entry(&rt->table[i], &gc));
         }
@@ -4023,8 +4021,7 @@ test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi)
     const MIB_IPFORWARDTABLE *rt = get_windows_routing_table(&gc);
     if (rt)
     {
-        int i;
-        for (i = 0; i < rt->dwNumEntries; ++i)
+        for (DWORD i = 0; i < rt->dwNumEntries; ++i)
         {
             const MIB_IPFORWARDROW *row = &rt->table[i];
             const in_addr_t net = ntohl(row->dwForwardDest);
Signature · 1 line
2.30.1
Acked-by on v1 4/4
#6 lore raw

[Openvpn-devel] [PATCH applied] Re: Use correct types for OpenSSL and Windows APIs

"Because it makes sense". The DWORD changes for the loop variables confused me a bit, because these are not "passed to a windows API" - but they are *compared* to "i < rt->dwNumEntries", and since *that* is a DWORD, I can see why it might warn.

Again, compile tested on MinGW, lightly client side tested on Linux and FreeBSD.

Your patch has been applied to the master branch.

commit 467b16dc65df711c0f1b3a8640b1fadc09c56803

Author: Arne Schwabe
Date:   Wed Mar 24 23:23:30 2021 +0100
     Use correct types for OpenSSL and Windows APIs

     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]/msg21803.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 1/4
#7 lore raw
In reply to Arne Schwabe

Read through the code changes, seem to all make sense. Verified the fread() change according to FreeBSD man page. The only hunk that slightly confuses me is that the patch removes a "const" which seems to be "correctly before the change" - but not sure this is relevant at all. Compile tested on Ubuntu/MinGW, lightly tested on FreeBSD and Linux.

Your patch has been applied to the master branch.

commit 7fc608da4ec388c9209bd009cd5053ac0ff7df38

Author: Arne Schwabe
Date:   Wed Mar 24 23:23:27 2021 +0100
     Make buffer related function conversion explicit when narrowing

     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]/msg21805.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering