From: "flichtenheld (Code Review)" Date: Wed, 4 Mar 2026 13:20:38 +0000 In-Reply-To: References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <356ea19e342fbd121365c12f8ea706f22098d925-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="jMUfHgMu/4o="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: socket: Never use negative buf->len in link_socket_read* List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --jMUfHgMu/4o= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1554?usp=3Demail to look at the new patch set (#3). Change subject: socket: Never use negative buf->len in link_socket_read* ...................................................................... socket: Never use negative buf->len in link_socket_read* We really don't want to use negative buf lengths in our code. So remove some places where we did so before. Change-Id: Iabe1992b2608661b862a81363574e957d18395f0 Signed-off-by: Frank Lichtenheld --- M src/openvpn/forward.c M src/openvpn/socket.c M src/openvpn/socket.h M src/openvpn/tun.h 4 files changed, 84 insertions(+), 69 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1554/3 diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 27cfd36..20b46d3 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -929,14 +929,12 @@ * Set up for recvfrom call to read datagram * sent to our TCP/UDP port. */ - int status; - /*ASSERT (!c->c2.to_tun.len);*/ c->c2.buf =3D c->c2.buffers->read_link_buf; ASSERT(buf_init(&c->c2.buf, c->c2.frame.buf.headroom)); - status =3D link_socket_read(sock, &c->c2.buf, &c->c2.from); + ssize_t status =3D link_socket_read(sock, &c->c2.buf, &c->c2.from); if (socket_connection_reset(sock, status)) { @@ -955,14 +953,14 @@ if (event_timeout_defined(&c->c2.explicit_exit_notification_in= terval)) { msg(D_STREAM_ERRORS, - "Connection reset during exit notification period, ign= oring [%d]", status); + "Connection reset during exit notification period, ign= oring [%zd]", status); management_sleep(1); } else { register_signal(c->sig, SIGUSR1, "connection-reset"); /* SOFT-SIGUSR1 -- TC= P connection reset */ - msg(D_STREAM_ERRORS, "Connection reset, restarting [%d]", = status); + msg(D_STREAM_ERRORS, "Connection reset, restarting [%zd]",= status); } } return; @@ -1026,7 +1024,7 @@ * Good, non-zero length packet received. * Commence multi-stage processing of packet, * such as authenticate, decrypt, decompress. - * If any stage fails, it sets buf.len to 0 or -1, + * If any stage fails, it sets buf.len to 0, * telling downstream stages to ignore the packet. */ if (c->c2.buf.len > 0) diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 2d5fc50..d2170a4 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1130,7 +1130,7 @@ static void stream_buf_close(struct stream_buf *sb); -static bool stream_buf_added(struct stream_buf *sb, int length_added); +static bool stream_buf_added(struct stream_buf *sb, ssize_t length_added); /* For stream protocols, allocate a buffer to build up packet. * Called after frame has been finalized. */ @@ -1792,6 +1792,10 @@ } } +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + void link_socket_close(struct link_socket *sock) { @@ -2159,12 +2163,13 @@ * @return true if \c sb->buf contains fully reassembled packet */ static bool -stream_buf_added(struct stream_buf *sb, int length_added) +stream_buf_added(struct stream_buf *sb, ssize_t length_added) { - dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=3D%d", length_added); + dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=3D%zd", length_added); if (length_added > 0) { - sb->buf.len +=3D length_added; + ASSERT(sb->buf.len + length_added <=3D INT_MAX); + sb->buf.len +=3D (int)length_added; } /* if length unknown, see if we can get the length prefix from @@ -2266,10 +2271,10 @@ * Socket Read Routines */ -int +ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf) { - int len =3D 0; + ssize_t len =3D 0; if (!sock->stream_buf.residual_fully_formed) { @@ -2299,7 +2304,8 @@ } if (len <=3D 0) { - return buf->len =3D len; + buf->len =3D 0; + return len; } } @@ -2331,14 +2337,14 @@ max_int(CMSG_SPACE(sizeof(struct in6_pktinfo)), CMSG_SPACE(sizeof(stru= ct in_addr))) #endif -static socklen_t +static ssize_t link_socket_read_udp_posix_recvmsg(struct link_socket *sock, struct buffer= *buf, - struct link_socket_actual *from) + struct link_socket_actual *from, sockle= n_t *fromlen) { struct iovec iov; uint8_t pktinfo_buf[PKTINFO_BUF_SIZE]; struct msghdr mesg =3D { 0 }; - socklen_t fromlen =3D sizeof(from->dest.addr); + *fromlen =3D sizeof(from->dest.addr); ASSERT(sock->sd >=3D 0); /* can't happen */ @@ -2347,62 +2353,67 @@ mesg.msg_iov =3D &iov; mesg.msg_iovlen =3D 1; mesg.msg_name =3D &from->dest.addr; - mesg.msg_namelen =3D fromlen; + mesg.msg_namelen =3D *fromlen; mesg.msg_control =3D pktinfo_buf; - mesg.msg_controllen =3D sizeof pktinfo_buf; - buf->len =3D recvmsg(sock->sd, &mesg, 0); - if (buf->len >=3D 0) + mesg.msg_controllen =3D (socklen_t)sizeof(pktinfo_buf); + ssize_t len =3D recvmsg(sock->sd, &mesg, 0); + if (len < 0) { - struct cmsghdr *cmsg; - fromlen =3D mesg.msg_namelen; - cmsg =3D CMSG_FIRSTHDR(&mesg); - if (cmsg !=3D NULL && CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL + buf->len =3D 0; + return len; + } + ASSERT(len <=3D INT_MAX); + buf->len =3D (int)len; + struct cmsghdr *cmsg; + *fromlen =3D mesg.msg_namelen; + cmsg =3D CMSG_FIRSTHDR(&mesg); + if (cmsg !=3D NULL && CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) - && cmsg->cmsg_level =3D=3D SOL_IP && cmsg->cmsg_type =3D=3D IP= _PKTINFO - && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(struct in_pktinfo))) + && cmsg->cmsg_level =3D=3D SOL_IP && cmsg->cmsg_type =3D=3D IP_PKT= INFO + && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(struct in_pktinfo))) #elif defined(IP_RECVDSTADDR) - && cmsg->cmsg_level =3D=3D IPPROTO_IP && cmsg->cmsg_type =3D= =3D IP_RECVDSTADDR - && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(struct in_addr))) + && cmsg->cmsg_level =3D=3D IPPROTO_IP && cmsg->cmsg_type =3D=3D IP= _RECVDSTADDR + && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(struct in_addr))) #else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */ #error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix= syshead.h) #endif - { + { #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) - struct in_pktinfo *pkti =3D (struct in_pktinfo *)CMSG_DATA(cms= g); - from->pi.in4.ipi_ifindex =3D - (sock->sockflags & SF_PKTINFO_COPY_IIF) ? pkti->ipi_ifinde= x : 0; - from->pi.in4.ipi_spec_dst =3D pkti->ipi_spec_dst; + struct in_pktinfo *pkti =3D (struct in_pktinfo *)CMSG_DATA(cmsg); + from->pi.in4.ipi_ifindex =3D + (sock->sockflags & SF_PKTINFO_COPY_IIF) ? pkti->ipi_ifindex : = 0; + from->pi.in4.ipi_spec_dst =3D pkti->ipi_spec_dst; #elif defined(IP_RECVDSTADDR) - from->pi.in4 =3D *(struct in_addr *)CMSG_DATA(cmsg); + from->pi.in4 =3D *(struct in_addr *)CMSG_DATA(cmsg); #else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */ #error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix= syshead.h) #endif - } - else if (cmsg !=3D NULL && CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL - && cmsg->cmsg_level =3D=3D IPPROTO_IPV6 && cmsg->cmsg_typ= e =3D=3D IPV6_PKTINFO - && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(struct in6_pktinfo= ))) - { - struct in6_pktinfo *pkti6 =3D (struct in6_pktinfo *)CMSG_DATA(= cmsg); - from->pi.in6.ipi6_ifindex =3D - (sock->sockflags & SF_PKTINFO_COPY_IIF) ? pkti6->ipi6_ifin= dex : 0; - from->pi.in6.ipi6_addr =3D pkti6->ipi6_addr; - } - else if (cmsg !=3D NULL) - { - msg(M_WARN, - "CMSG received that cannot be parsed (cmsg_level=3D%d, cms= g_type=3D%d, cmsg=3Dlen=3D%d)", - (int)cmsg->cmsg_level, (int)cmsg->cmsg_type, (int)cmsg->cm= sg_len); - } + } + else if (cmsg !=3D NULL && CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL + && cmsg->cmsg_level =3D=3D IPPROTO_IPV6 && cmsg->cmsg_type = =3D=3D IPV6_PKTINFO + && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(struct in6_pktinfo))) + { + struct in6_pktinfo *pkti6 =3D (struct in6_pktinfo *)CMSG_DATA(cmsg= ); + from->pi.in6.ipi6_ifindex =3D + (sock->sockflags & SF_PKTINFO_COPY_IIF) ? pkti6->ipi6_ifindex = : 0; + from->pi.in6.ipi6_addr =3D pkti6->ipi6_addr; + } + else if (cmsg !=3D NULL) + { + msg(M_WARN, + "CMSG received that cannot be parsed (cmsg_level=3D%d, cmsg_ty= pe=3D%d, cmsg=3Dlen=3D%d)", + (int)cmsg->cmsg_level, (int)cmsg->cmsg_type, (int)cmsg->cmsg_l= en); } - return fromlen; + return buf->len; } #endif /* if ENABLE_IP_PKTINFO */ -int +ssize_t link_socket_read_udp_posix(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from) { + ssize_t recvlen; socklen_t fromlen =3D sizeof(from->dest.addr); socklen_t expectedlen =3D af_addr_size(sock->info.af); addr_zero_host(&from->dest); @@ -2413,16 +2424,23 @@ /* Both PROTO_UDPv4 and PROTO_UDPv6 */ if (sock->info.proto =3D=3D PROTO_UDP && sock->sockflags & SF_USE_IP_P= KTINFO) { - fromlen =3D link_socket_read_udp_posix_recvmsg(sock, buf, from); + recvlen =3D link_socket_read_udp_posix_recvmsg(sock, buf, from, &f= romlen); } else #endif { - buf->len =3D recvfrom(sock->sd, BPTR(buf), buf_forward_capacity(bu= f), 0, &from->dest.addr.sa, - &fromlen); + recvlen =3D recvfrom(sock->sd, BPTR(buf), buf_forward_capacity(buf= ), 0, + &from->dest.addr.sa, &fromlen); } + if (recvlen < 0) + { + buf->len =3D 0; + return recvlen; + } + ASSERT(recvlen <=3D INT_MAX); + buf->len =3D (int)recvlen; /* FIXME: won't do anything when sock->info.af =3D=3D AF_UNSPEC */ - if (buf->len >=3D 0 && expectedlen && fromlen !=3D expectedlen) + if (expectedlen && fromlen !=3D expectedlen) { bad_address_length(fromlen, expectedlen); } @@ -2438,7 +2456,9 @@ ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct= link_socket_actual *to) { - packet_size_type len =3D (packet_size_type)BLENZ(buf); + const int blen =3D BLEN(buf); + ASSERT(blen >=3D 0 && blen <=3D PACKET_SIZE_MAX); + packet_size_type len =3D (packet_size_type)blen; dmsg(D_STREAM_DEBUG, "STREAM: WRITE %u offset=3D%d", len, buf->offset); ASSERT(len <=3D sock->stream_buf.maxlen); len =3D htonps(len); @@ -2450,10 +2470,6 @@ #endif } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - #if ENABLE_IP_PKTINFO ssize_t diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index cd4e8ed..8b49f75 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -54,6 +54,7 @@ */ typedef uint16_t packet_size_type; +#define PACKET_SIZE_MAX UINT16_MAX /* convert a packet_size_type from host to network order */ #define htonps(x) htons(x) @@ -459,7 +460,7 @@ #endif /* if PORT_SHARE */ static inline bool -socket_connection_reset(const struct link_socket *sock, int status) +socket_connection_reset(const struct link_socket *sock, ssize_t status) { if (link_socket_connection_oriented(sock)) { @@ -586,7 +587,7 @@ * Socket Read Routines */ -int link_socket_read_tcp(struct link_socket *sock, struct buffer *buf); +ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf); #ifdef _WIN32 @@ -606,20 +607,20 @@ #else /* ifdef _WIN32 */ -int link_socket_read_udp_posix(struct link_socket *sock, struct buffer *bu= f, - struct link_socket_actual *from); +ssize_t link_socket_read_udp_posix(struct link_socket *sock, struct buffer= *buf, + struct link_socket_actual *from); #endif /* ifdef _WIN32 */ /* read a TCP or UDP packet from link */ -static inline int +static inline ssize_t link_socket_read(struct link_socket *sock, struct buffer *buf, struct link= _socket_actual *from) { if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock)) /* unified UDPv4 and UDPv6, for DCO-WIN the kernel * will strip the length header */ { - int res; + ssize_t res; #ifdef _WIN32 res =3D link_socket_read_udp_win32(sock, buf, from); diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h index 4d6dfbb..f0c5c25 100644 --- a/src/openvpn/tun.h +++ b/src/openvpn/tun.h @@ -537,7 +537,7 @@ } static inline bool -tuntap_is_dco_win_timeout(struct tuntap *tt, int status) +tuntap_is_dco_win_timeout(struct tuntap *tt, ssize_t status) { return tuntap_is_dco_win(tt) && (status < 0) && (openvpn_errno() =3D= =3D ERROR_NETNAME_DELETED); } @@ -575,7 +575,7 @@ } static inline bool -tuntap_is_dco_win_timeout(struct tuntap *tt, int status) +tuntap_is_dco_win_timeout(struct tuntap *tt, ssize_t status) { return false; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1554?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Iabe1992b2608661b862a81363574e957d18395f0 Gerrit-Change-Number: 1554 Gerrit-PatchSet: 3 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --jMUfHgMu/4o= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

flichtenheld uploaded patch set #3 to this change.

<= p>View C= hange

socket: Never use negative buf->len in link_s=
ocket_read*

We really don't want to use negative buf lengths in<= br>our code. So remove some places where we did so before.

Change-Id= : Iabe1992b2608661b862a81363574e957d18395f0
Signed-off-by: Frank Lichten= held <frank@...2641...>
---
M src/openvpn/forward.c
M src/op= envpn/socket.c
M src/openvpn/socket.h
M src/openvpn/tun.h
4 files = changed, 84 insertions(+), 69 deletions(-)

git p=
ull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1554/3
d= iff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
i= ndex 27cfd36..20b46d3 100644
--- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -929,14 +929,1= 2 @@
* Set up for recvfrom call to read datagram
* sent to our TCP/UDP port.
*/
- int status;
= -
/*ASSERT (= !c->c2.to_tun.len);*/

c->c2.bu= f =3D c->c2.buffers->read_link_buf;
ASSERT(buf_i= nit(&c->c2.buf, c->c2.frame.buf.headroom));

- status =3D link_socke= t_read(sock, &c->c2.buf, &c->c2.from);
+ ssize_t status =3D link_socket_read(s= ock, &c->c2.buf, &c->c2.from);

if (socket_connection_reset(sock, status))

{<= /span>
@@ -955,14 +953,14 @@
if (even= t_timeout_defined(&c->c2.explicit_exit_notification_interval))
{
msg(D_STREAM_ERR= ORS,
- = "Connection reset during exit notification period, ignoring [%d]&qu= ot;, status);
+ = "Connection reset during exit notification period, ignor= ing [%zd]", status);
management_sleep= (1);
}
else
{
register_signal(= c->sig, SIGUSR1,
"= connection-reset"); /* SOFT-SIGUSR1 -- TCP connection reset */<= br>- msg(D_STREAM_= ERRORS, "Connection reset, restarting [%d]", status);
<= span style=3D"color: hsl(120, 100%, 40%);">+ msg(D_STREAM_ER= RORS, "Connection reset, restarting [%zd]", status);

}

}
retu= rn;
@@ -1026,7 +1024,7 @@
* Good, non= -zero length packet received.
* Commence multi-stage = processing of packet,
* such as authenticate, decrypt= , decompress.
- * I= f any stage fails, it sets buf.len to 0 or -1,
+ * If any stage fails, it sets buf.len to 0,=
* telling downstream stages to ignore the packet.
*/
if (c->c2.buf.len > 0)
diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c
index 2d5fc50..d2170a4 100644
--- a/src/openvpn= /socket.c
+++ b/src/openvpn/socket.c
@@ -11= 30,7 +1130,7 @@

static void stream_buf_c= lose(struct stream_buf *sb);

-static bool stream_buf_added(struct stream_buf *sb= , int length_added);
= +static bool stream_buf_added(struct stream_buf *sb, ssize_t length_added);=

/* For stream protocols, allocate a buf= fer to build up packet.
* Called after frame has been fin= alized. */
@@ -1792,6 +1792,10 @@
}
}

+#if defined(__GNUC__) || defined(__clang__)
+#pragma GCC diagnostic pop+#endif
+
void
l= ink_socket_close(struct link_socket *sock)
{
@@ -2159,12 +2163,13 @@

* @return true if \c sb->bu= f contains fully reassembled packet
*/
s= tatic bool
-stream_buf_= added(struct stream_buf *sb, int length_added)
+stream_buf_added(struct stream_buf *sb, ssize_t = length_added)
{
- dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=3D%d&quo= t;, length_added);
+ = dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=3D%zd", length_= added);
if (length_added > 0)
= {
- sb->buf.l= en +=3D length_added;
+ ASSERT(sb->buf.len + length_added <=3D INT_MAX);
= + sb->buf.len +=3D (i= nt)length_added;
}

= /* if length unknown, see if we can get the length prefix from@@ -2266,10 +2271,10 @@
* Socket Read Routines
*/

-int
+s= size_t
link_socket_read_tcp(struct link_socket *sock, stru= ct buffer *buf)
{
- int len =3D 0;
+ ssize_t len =3D 0;

i= f (!sock->stream_buf.residual_fully_formed)
{
@@ -2299,7 +2304,8 @@
}
if (len <=3D 0)

{
- return buf->len =3D len;
+ buf->l= en =3D 0;
+ = return len;
}
}
<= span>

@@ -2331,14 +2337,14 @@
max_int= (CMSG_SPACE(sizeof(struct in6_pktinfo)), CMSG_SPACE(sizeof(struct in_addr))= )
#endif

-static socklen_t
+static ssize_t
link_socket_read_udp_pos= ix_recvmsg(struct link_socket *sock, struct buffer *buf,
- struc= t link_socket_actual *from)
+ struct link_socket_actual *from,= socklen_t *fromlen)
{
struct iovec i= ov;
uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
<= span> struct msghdr mesg =3D { 0 };

- socklen_t fromlen =3D sizeof(from->dest.addr);
+ *fromlen =3D sizeof= (from->dest.addr);

ASSERT(sock-&g= t;sd >=3D 0); /* can't happen */

@= @ -2347,62 +2353,67 @@
mesg.msg_iov =3D &iov;
mesg.msg_iovlen =3D 1;
mesg.msg_name = =3D &from->dest.addr;
- mesg.msg_namelen =3D fromlen;
+ mesg.msg_namelen =3D *fromlen;
= mesg.msg_control =3D pktinfo_buf;
- mesg.msg_controllen =3D sizeof pktinfo_buf;
- buf->len =3D recvmsg(sock-&g= t;sd, &mesg, 0);

- = if (buf->len >=3D 0)
+ mesg.msg_controllen =3D (socklen_t)sizeof(pktinfo_buf);
+ ssize_t len =3D recvm= sg(sock->sd, &mesg, 0);
+ if (len < 0)
{
- struct cmsghdr *cmsg;
- fromlen =3D mesg.msg_namele= n;

- cmsg =3D CM= SG_FIRSTHDR(&mesg);
- if (cmsg !=3D NULL && CMSG_NXTHDR(&mesg, cmsg) =3D=3D= NULL
+ buf-&g= t;len =3D 0;
+ = return len;
+ }
+ ASSERT(len <= =3D INT_MAX);
+ bu= f->len =3D (int)len;
+ struct cmsghdr *cmsg;
+ *fromlen =3D mesg.msg_namelen;
+ cmsg =3D CMSG_FIRSTHDR(&mesg);
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (cmsg !=3D NULL &&am= p; CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL

#if defined(HA= VE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
- && cmsg->cmsg_level = =3D=3D SOL_IP && cmsg->cmsg_type =3D=3D IP_PKTINFO
- && cmsg->cms= g_len >=3D CMSG_LEN(sizeof(struct in_pktinfo)))

+ && cmsg->cmsg_level =3D= =3D SOL_IP && cmsg->cmsg_type =3D=3D IP_PKTINFO
+ && cmsg->cmsg_len= >=3D CMSG_LEN(sizeof(struct in_pktinfo)))
#elif define= d(IP_RECVDSTADDR)
- = && cmsg->cmsg_level =3D=3D IPPROTO_IP && cmsg-&g= t;cmsg_type =3D=3D IP_RECVDSTADDR
- && cmsg->cmsg_len >=3D CMSG_LEN(sizeo= f(struct in_addr)))
+= && cmsg->cmsg_level =3D=3D IPPROTO_IP && cmsg-&g= t;cmsg_type =3D=3D IP_RECVDSTADDR
+ && cmsg->cmsg_len >=3D CMSG_LEN(sizeof(= struct in_addr)))
#else /* if defined(HAVE_IN_PKTINFO) &am= p;& defined(HAVE_IPI_SPEC_DST) */
#error ENABLE_IP_PKT= INFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h) #endif
- = {
+ {
<= span> #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
- struct in_pk= tinfo *pkti =3D (struct in_pktinfo *)CMSG_DATA(cmsg);
- from->pi.in4.ipi_ifindex =3D=
- (sock= ->sockflags & SF_PKTINFO_COPY_IIF) ? pkti->ipi_ifindex : 0;
- from->pi.in4= .ipi_spec_dst =3D pkti->ipi_spec_dst;
+ struct in_pktinfo *pkti =3D (struct in_pktinfo= *)CMSG_DATA(cmsg);
+= from->pi.in4.ipi_ifindex =3D
+ (sock->sockflags & SF_PKTINFO_COPY_I= IF) ? pkti->ipi_ifindex : 0;
+ from->pi.in4.ipi_spec_dst =3D pkti->ipi_spec_dst;=
#elif defined(IP_RECVDSTADDR)
- from->pi.in4 =3D *(struct in_addr = *)CMSG_DATA(cmsg);
+ = from->pi.in4 =3D *(struct in_addr *)CMSG_DATA(cmsg);
#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_= DST) */

#error ENABLE_IP_PKTINFO is set without IP_PKTINFO= xor IP_RECVDSTADDR (fix syshead.h)
#endif
- }

- else if (cmsg !=3D NULL && CMSG_N= XTHDR(&mesg, cmsg) =3D=3D NULL
- && cmsg->cmsg_level =3D=3D IPPROTO= _IPV6 && cmsg->cmsg_type =3D=3D IPV6_PKTINFO
- && cmsg->cmsg= _len >=3D CMSG_LEN(sizeof(struct in6_pktinfo)))
- {
- struct in6_pktinfo *pkti6 =3D (struct in6_pkti= nfo *)CMSG_DATA(cmsg);
= - from->pi.in6.ipi6_ifindex =3D
- (sock->sockflags & SF_PKTINF= O_COPY_IIF) ? pkti6->ipi6_ifindex : 0;
- from->pi.in6.ipi6_addr =3D pkti6->ipi= 6_addr;
- }
- else if (cmsg !=3D= NULL)
- {
- msg(M_WARN,
- "CMS= G received that cannot be parsed (cmsg_level=3D%d, cmsg_type=3D%d, cmsg=3Dl= en=3D%d)",
- = (int)cmsg->cmsg_level, (int)cmsg->cmsg_type, (int)cmsg->= cmsg_len);
- }
+ }
+ else if (cmsg !=3D NULL &&a= mp; CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL

+ && cmsg->cmsg_level =3D=3D= IPPROTO_IPV6 && cmsg->cmsg_type =3D=3D IPV6_PKTINFO
<= span style=3D"color: hsl(120, 100%, 40%);">+ && cmsg-&g= t;cmsg_len >=3D CMSG_LEN(sizeof(struct in6_pktinfo)))

+ {
+ struct in6_pktinfo *pkti6 =3D (struct in6_pk= tinfo *)CMSG_DATA(cmsg);
+ from->pi.in6.ipi6_ifindex =3D
+ (sock->sockflags & SF_PKTINFO_= COPY_IIF) ? pkti6->ipi6_ifindex : 0;
+ from->pi.in6.ipi6_addr =3D pkti6->ipi6_ad= dr;
+ }
= + else if (cmsg !=3D NULL)
+ {
+ msg(M_WARN,

+ "CMSG received tha= t cannot be parsed (cmsg_level=3D%d, cmsg_type=3D%d, cmsg=3Dlen=3D%d)"= ,

+ (int)c= msg->cmsg_level, (int)cmsg->cmsg_type, (int)cmsg->cmsg_len);
}

- return fromlen;
+ return buf->len;
}
#= endif /* if ENABLE_IP_PKTINFO */

-int
+ssize_t
link_socket_read_udp_posix(struct lin= k_socket *sock, struct buffer *buf,
= struct link_socket_actual *from)
{
+ ssize_t recvlen;
= socklen_t fromlen =3D sizeof(from->dest.addr);
= socklen_t expectedlen =3D af_addr_size(sock->info.af);
= addr_zero_host(&from->dest);
@@ -2413,16 +2424,2= 3 @@
/* Both PROTO_UDPv4 and PROTO_UDPv6 */
= if (sock->info.proto =3D=3D PROTO_UDP && sock->soc= kflags & SF_USE_IP_PKTINFO)
{
- fromlen =3D link_socket_read_udp_p= osix_recvmsg(sock, buf, from);
+ recvlen =3D link_socket_read_udp_posix_recvmsg(sock, buf= , from, &fromlen);
}
else
#endif
{
- buf->len =3D recvfrom(sock->sd, BPTR(bu= f), buf_forward_capacity(buf), 0, &from->dest.addr.sa,
- &fr= omlen);

+ recv= len =3D recvfrom(sock->sd, BPTR(buf), buf_forward_capacity(buf), 0,
+ = &from->dest.addr.sa, &fromlen);
}=
+ if (recvlen < 0)
+ {
+ buf->len =3D 0;+ return recvlen;
+ }
+ ASSERT(recvlen <=3D INT_MAX);
+ buf->len =3D (i= nt)recvlen;
/* FIXME: won't do anything when sock-= >info.af =3D=3D AF_UNSPEC */
- if (buf->len >=3D 0 && expectedlen && f= romlen !=3D expectedlen)
+ if (expectedlen && fromlen !=3D expectedlen)
{

bad_address_length(fromlen, expectedlen)= ;
}
@@ -2438,7 +2456,9 @@
ssize_t

link_socket_write_tcp(struct link_socket *soc= k, struct buffer *buf, struct link_socket_actual *to)
{
- packet_size_type len= =3D (packet_size_type)BLENZ(buf);
+ const int blen =3D BLEN(buf);
+ ASSERT(blen >=3D 0 && blen <= =3D PACKET_SIZE_MAX);
+ packet_size_type len =3D (packet_size_type)blen;
= dmsg(D_STREAM_DEBUG, "STREAM: WRITE %u offset=3D%d", len, buf-&g= t;offset);
ASSERT(len <=3D sock->stream_buf.maxl= en);
len =3D htonps(len);
@@ -2450,10 = +2470,6 @@
#endif
}

-#if defined(__GNUC__) || = defined(__clang__)
-#pr= agma GCC diagnostic pop
-#endif
-
#if ENABLE_IP_PKTINFO


ssize_t
diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h<= br>index cd4e8ed..8b49f75 100644
--- a/src/openvpn/so= cket.h
+++ b/src/openvpn/socket.h
@@ -54,6 = +54,7 @@
*/

typedef ui= nt16_t packet_size_type;
+#define PACKET_SIZE_MAX UINT16_MAX

= /* convert a packet_size_type from host to network order */
#define htonps(x) htons(x)

@@ -459,7 +460,7 @@
= #endif /* if PORT_SHARE */

static= inline bool
-socket_co= nnection_reset(const struct link_socket *sock, int status)
+socket_connection_reset(const struct= link_socket *sock, ssize_t status)
{
= if (link_socket_connection_oriented(sock))
{@@ -586,7 +587,7 @@
* Socket Read Routines=
*/

-int link_socket_read_tcp(struct link_socket *sock, struct buffer= *buf);
+ssize_t link= _socket_read_tcp(struct link_socket *sock, struct buffer *buf);
<= span>

#ifdef _WIN32

@@ = -606,20 +607,20 @@

#else /* ifdef _WIN3= 2 */

= -int link_socket_read_udp_posix(struct link_socket *sock, struct buffer *bu= f,
- = struct link_socket_actual *from);
+ssize_t link_socket_read_udp_posix(struct link_so= cket *sock, struct buffer *buf,
+ struct link_socket_actual *f= rom);

#endif /* ifdef _WIN32 */
/* read a TCP or UDP packet from link */<= br>-static inline int
<= span style=3D"color: hsl(120, 100%, 40%);">+static inline ssize_t
link_socket_read(struct link_socket *sock, struct buffer *buf, stru= ct link_socket_actual *from)
{
if (pr= oto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
/* unified UDPv4 and UDPv6, for DCO-WIN the kernel

= * will strip the length header */
{
- int res;

+ ssize_t res;
<= /span>
#ifdef _WIN32
res =3D link_socket= _read_udp_win32(sock, buf, from);
diff --git a/src/openvpn/= tun.h b/src/openvpn/tun.h
index 4d6dfbb..f0c5c25 100644
--- a/src/openvpn/tun.h
+++ b/src/openvpn/tun.= h
@@ -537,7 +537,7 @@
}
<= /span>
static inline bool
-tuntap_is_dco_win_timeout(struct tuntap *tt, int status)
+tuntap_is_dco_win_timeou= t(struct tuntap *tt, ssize_t status)
{
= return tuntap_is_dco_win(tt) && (status < 0) && (openv= pn_errno() =3D=3D ERROR_NETNAME_DELETED);
}
@@ -575,7 +575,7 @@

}

= static inline bool
-tu= ntap_is_dco_win_timeout(struct tuntap *tt, int status)
+tuntap_is_dco_win_timeout(struct tuntap = *tt, ssize_t status)
{
return false;<= /span>
}

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

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Iabe1992b2608661b862a8136357= 4e957d18395f0
Gerrit-Change-Number: 1554
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --jMUfHgMu/4o=--