From: "flichtenheld (Code Review)" Date: Tue, 3 Mar 2026 16:41:54 +0000 In-Reply-To: References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <6a890d6e41ffa2d64299dfc4cf4d99b3abd4909a-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="6a2TCTb77ik="; 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 --6a2TCTb77ik= 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 (#2). 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, 85 insertions(+), 70 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1554/2 diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 5004e35..081e79a 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 033444e..0b72684 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1132,7 +1132,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. */ @@ -1794,6 +1794,10 @@ } } +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + void link_socket_close(struct link_socket *sock) { @@ -2130,12 +2134,13 @@ } 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 @@ -2238,10 +2243,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) { @@ -2272,7 +2277,8 @@ } if (len <=3D 0) { - return buf->len =3D len; + buf->len =3D 0; + return len; } } @@ -2304,14 +2310,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 */ @@ -2320,62 +2326,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); @@ -2386,16 +2397,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); } @@ -2411,8 +2429,10 @@ ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct= link_socket_actual *to) { - packet_size_type len =3D BLEN(buf); - dmsg(D_STREAM_DEBUG, "STREAM: WRITE %d offset=3D%d", (int)len, buf->of= fset); + 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); ASSERT(buf_write_prepend(buf, &len, sizeof(len))); @@ -2423,10 +2443,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 3f46dc6..f11634e 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 =20 /* convert a packet_size_type from host to network order */ #define htonps(x) htons(x) @@ -441,7 +442,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)) { @@ -568,7 +569,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 @@ -588,20 +589,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 34f4929..cf5eb3b 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: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --6a2TCTb77ik= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

flichtenheld uploaded patch set #2 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, 85 insertions(+), 70 deletions(-)

git p=
ull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1554/2
d= iff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
i= ndex 5004e35..081e79a 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 033444e..0b72684 100644
--- a/src/openvpn= /socket.c
+++ b/src/openvpn/socket.c
@@ -11= 32,7 +1132,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. */
@@ -1794,6 +1794,10 @@
}
}

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

}

<= span> static bool

-stre= am_buf_added(struct stream_buf *sb, int length_added)
+stream_buf_added(struct stream_buf *sb, s= size_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.le= n +=3D (int)length_added;
}
/* if length unknown, see if we can get the length prefix from=
@@ -2238,10 +2243,10 @@
* Socket Read Ro= utines
*/

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

if (!sock->stream_buf.residual_fully_formed)

= {
@@ -2272,7 +2277,8 @@
}=
if (len <=3D 0)
{
= - return buf->len = =3D len;
+ = buf->len =3D 0;
+= return len;
}
}

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

-static socklen_t
+static ssize_t
link_socket_re= ad_udp_posix_recvmsg(struct link_socket *sock, struct buffer *buf,- = struct link_socket_actual *from)
+ struct link_socket_act= ual *from, socklen_t *fromlen)
{
stru= ct 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);

ASSE= RT(sock->sd >=3D 0); /* can't happen */
<= br>@@ -2320,62 +2326,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 recvm= sg(sock->sd, &mesg, 0);
- if (buf->len >=3D 0)
+ mesg.msg_controllen =3D (socklen_t)sizeof(pktinfo_b= uf);
+ ssize_t len= =3D recvmsg(sock->sd, &mesg, 0);
+ if (len < 0)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- struct cmsghdr *cmsg;
- fromlen =3D mesg.= msg_namelen;
- c= msg =3D CMSG_FIRSTHDR(&mesg);
- if (cmsg !=3D NULL && CMSG_NXTHDR(&mesg, cm= sg) =3D=3D NULL
+ = buf->len =3D 0;
+ 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 d= efined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
- && cmsg->cms= g_level =3D=3D SOL_IP && cmsg->cmsg_type =3D=3D IP_PKTINFO
- && cmsg= ->cmsg_len >=3D CMSG_LEN(sizeof(struct in_pktinfo)))
+ && cmsg->cmsg_lev= el =3D=3D SOL_IP && cmsg->cmsg_type =3D=3D IP_PKTINFO
= + && cmsg->cm= sg_len >=3D CMSG_LEN(sizeof(struct in_pktinfo)))
#elif = defined(IP_RECVDSTADDR)
- && cmsg->cmsg_level =3D=3D IPPROTO_IP && c= msg->cmsg_type =3D=3D IP_RECVDSTADDR
- && cmsg->cmsg_len >=3D CMSG_LEN= (sizeof(struct in_addr)))
+ && cmsg->cmsg_level =3D=3D IPPROTO_IP && c= msg->cmsg_type =3D=3D IP_RECVDSTADDR
+ && cmsg->cmsg_len >=3D CMSG_LEN(s= izeof(struct in_addr)))
#else /* if defined(HAVE_IN_PKTINF= O) && 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_D= ST)
- struct= in_pktinfo *pkti =3D (struct in_pktinfo *)CMSG_DATA(cmsg);
- from->pi.in4.ipi_ifind= ex =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_p= ktinfo *)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_spe= c_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_P= KTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif- }
- else if (cmsg !=3D NULL && = CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL
- && cmsg->cmsg_level =3D=3D I= PPROTO_IPV6 && cmsg->cmsg_type =3D=3D IPV6_PKTINFO
- && cmsg-&g= t;cmsg_len >=3D CMSG_LEN(sizeof(struct in6_pktinfo)))

- {
- struct in6_pktinfo *pkti6 =3D (struct in= 6_pktinfo *)CMSG_DATA(cmsg);
- from->pi.in6.ipi6_ifindex =3D
- (sock->sockflags & S= F_PKTINFO_COPY_IIF) ? pkti6->ipi6_ifindex : 0;
- from->pi.in6.ipi6_addr =3D pkti6= ->ipi6_addr;
- = }
- else if (c= msg !=3D NULL)
- = {
- msg(M_W= ARN,
- &= quot;CMSG received that cannot be parsed (cmsg_level=3D%d, cmsg_type=3D%d, = cmsg=3Dlen=3D%d)",
- (int)cmsg->cmsg_level, (int)cmsg->cmsg_type, (int)c= msg->cmsg_len);
- = }
+ }<= br>+ else if (cmsg !=3D NULL= && CMSG_NXTHDR(&mesg, cmsg) =3D=3D NULL
+ && cmsg->cmsg_lev= el =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 (st= ruct 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 r= eceived that cannot be parsed (cmsg_level=3D%d, cmsg_type=3D%d, cmsg=3Dlen= =3D%d)",
+ = (int)cmsg->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_posi= x(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);
@@ -238= 6,16 +2397,23 @@
/* Both PROTO_UDPv4 and PROTO_UDPv6 *= /
if (sock->info.proto =3D=3D PROTO_UDP && = sock->sockflags & SF_USE_IP_PKTINFO)
{- fromlen =3D link_socke= t_read_udp_posix_recvmsg(sock, buf, from);
+ recvlen =3D link_socket_read_udp_posix_recvm= sg(sock, buf, from, &fromlen);
}
= else
#endif
{
- buf->len =3D recvfrom(sock->= ;sd, BPTR(buf), buf_forward_capacity(buf), 0, &from->dest.addr.sa,
- = &fromlen);
+= recvlen =3D recvfrom(sock->sd, BPTR(buf), buf_forward_capacity(b= uf), 0,
+ = &from->dest.addr.sa, &fromlen);
= }
+ if (recvle= n < 0)
+ {
+ buf->len =3D = 0;
+ return re= cvlen;
+ }<= br>+ ASSERT(recvlen <=3D = INT_MAX);
+ buf-&g= t;len =3D (int)recvlen;
/* FIXME: won't do anythin= g 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);
}
@@ -2411,8 +2429,10 @@=
ssize_t
link_socket_write_tcp(struct lin= k_socket *sock, struct buffer *buf, struct link_socket_actual *to) {
- packet_= size_type len =3D BLEN(buf);
- dmsg(D_STREAM_DEBUG, "STREAM: WRITE %d offset=3D%d", (= int)len, buf->offset);
+ const int blen =3D BLEN(buf);
+ ASSERT(blen >=3D 0 && blen <=3D PACKE= T_SIZE_MAX);
+ pac= ket_size_type len =3D (packet_size_type)blen;
+ dmsg(D_STREAM_DEBUG, "STREAM: WRITE %u o= ffset=3D%d", len, buf->offset);
ASSERT(len <= ;=3D sock->stream_buf.maxlen);
len =3D htonps(len);=
ASSERT(buf_write_prepend(buf, &len, sizeof(len)))= ;
@@ -2423,10 +2443,6 @@
#endif
= }

-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
#if ENABLE_IP_PKTINFO
<= /span>
ssize_t
diff --git a/src/openvpn/socket.h = b/src/openvpn/socket.h
index 3f46dc6..f11634e 100644=
--- a/src/openvpn/socket.h
+++ b/src/openvpn/sock= et.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 ne= twork order */
#define htonps(x) htons(x)
= @@ -441,7 +442,7 @@
#endif /* if PORT_SHARE */
<= span>

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))
{
@@ -568,7 +569,7 @@
= * Socket Read Routines
*/

-int link_socket_read_tcp(struct li= nk_socket *sock, struct buffer *buf);

+ssize_t link_socket_read_tcp(struct link_socket *sock, st= ruct buffer *buf);

#ifdef _WIN32<= br>
@@ -588,20 +589,20 @@
#else /* ifdef _WIN32 */

-int link_socket_read_udp_posix(struct link_= socket *sock, struct buffer *buf,
- struct link_socket_actual *from)= ;
+ssize_t link_socke= t_read_udp_posix(struct link_socket *sock, struct buffer *buf,
+ = struct link_socket_actual *from);


#en= dif /* 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-WI= N 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
in= dex 34f4929..cf5eb3b 100644
--- a/src/openvpn/tun.h<= br>+++ b/src/openvpn/tun.h
@@ -537,7 +537,7 @@=
}

static inline bool-tuntap_is_dco_win_timeout(stru= ct tuntap *tt, int status)
+tuntap_is_dco_win_timeout(struct tuntap *tt, ssize_t status)<= br> {
return tuntap_is_dco_win(tt) && (s= tatus < 0) && (openvpn_errno() =3D=3D ERROR_NETNAME_DELETED);
}
@@ -575,7 +575,7 @@
}

static inline bool
-tuntap_is_dco_win_timeout(struct tuntap *tt, i= nt status)
+tuntap_is= _dco_win_timeout(struct tuntap *tt, ssize_t status)
{
return false;
}

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: 2
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...>
= --6a2TCTb77ik=--