From: "flichtenheld (Code Review)"
Date: Tue, 3 Mar 2026 16:47:40 +0000
In-Reply-To:
References:
Reply-To: frank@...2641..., arne-openvpn@...1227...,
openvpn-devel@lists.sourceforge.net
Message-ID:
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
Content-Type: multipart/alternative; boundary="hWpanwhRc58="; charset=UTF-8
Subject: [Openvpn-devel] [M] Change in openvpn[master]: socket: Fix various
conversion warnings
List-Id:
List-Unsubscribe: ,
List-Archive:
List-Post:
List-Help:
List-Subscribe: ,
To: plaisthos
Cc: openvpn-devel
--hWpanwhRc58=
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/+/1476?usp=3Demail
to look at the new patch set (#7).
Change subject: socket: Fix various conversion warnings
......................................................................
socket: Fix various conversion warnings
- We assume that addrinfo->ai_family always fits
into a sa_family_t, so we do not check those.
- Reduce places where we explicitly pass ai_family
and prefer sock->info.af.
- Make sure that --bind-dev has a suitable length.
- Treat PROTO_* as uint8_t where possible (however,
much code uses proto =3D -1 for various error handling).
Change-Id: I7be7427480d3540d43dd818eddb6eb5860956459
Signed-off-by: Frank Lichtenheld
---
M src/openvpn/options.c
M src/openvpn/socket.c
M src/openvpn/socket_util.c
3 files changed, 30 insertions(+), 40 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76/1476/7
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index fdbc678..e1c62bd 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -6492,7 +6492,15 @@
else if (streq(p[0], "bind-dev") && p[1])
{
VERIFY_PERMISSION(OPT_P_SOCKFLAGS);
- options->bind_dev =3D p[1];
+ if (strlen(p[1]) <=3D IFNAMSIZ)
+ {
+ options->bind_dev =3D p[1];
+ }
+ else
+ {
+ msg(msglevel, "argument to --bind-dev is longer than allowed %=
u", IFNAMSIZ);
+ goto err;
+ }
}
#endif
else if (streq(p[0], "txqueuelen") && p[1] && !p[2])
@@ -6578,11 +6586,9 @@
}
else if (streq(p[0], "proto") && p[1] && !p[2])
{
- int proto;
- sa_family_t af;
VERIFY_PERMISSION(OPT_P_GENERAL | OPT_P_CONNECTION);
- proto =3D ascii2proto(p[1]);
- af =3D ascii2af(p[1]);
+ int proto =3D ascii2proto(p[1]);
+ sa_family_t af =3D ascii2af(p[1]);
if (proto < 0)
{
msg(msglevel, "Bad protocol: '%s'. Allowed protocols with --pr=
oto option: %s", p[1],
diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c
index 0b72684..33c12db 100644
--- a/src/openvpn/socket.c
+++ b/src/openvpn/socket.c
@@ -649,28 +649,23 @@
}
static void
-bind_local(struct link_socket *sock, const sa_family_t ai_family)
+bind_local(struct link_socket *sock)
{
/* bind to local address/port */
if (sock->bind_local)
{
if (sock->socks_proxy && sock->info.proto =3D=3D PROTO_UDP)
{
- socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, ai_fami=
ly, "SOCKS", false);
+ socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->i=
nfo.af, "SOCKS", false);
}
else
{
- socket_bind(sock->sd, sock->info.lsa->bind_local, ai_family, "=
TCP/UDP",
+ socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.a=
f, "TCP/UDP",
sock->info.bind_ipv6_only);
}
}
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
static void
create_socket(struct link_socket *sock, struct addrinfo *addr)
{
@@ -702,7 +697,7 @@
}
/* Set af field of sock->info, so it always reflects the address family
* of the created socket */
- sock->info.af =3D addr->ai_family;
+ sock->info.af =3D (sa_family_t)addr->ai_family;
/* set socket buffers based on --sndbuf and --rcvbuf options */
socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);
@@ -714,8 +709,9 @@
if (sock->bind_dev)
{
msg(M_INFO, "Using bind-dev %s", sock->bind_dev);
+ /* Note: We verify strlen of bind_dev in options parsing */
if (setsockopt(sock->sd, SOL_SOCKET, SO_BINDTODEVICE, sock->bind_d=
ev,
- strlen(sock->bind_dev) + 1)
+ (socklen_t)(strlen(sock->bind_dev) + 1))
!=3D 0)
{
msg(M_WARN | M_ERRNO, "WARN: setsockopt SO_BINDTODEVICE=3D%s f=
ailed", sock->bind_dev);
@@ -723,13 +719,9 @@
}
#endif
- bind_local(sock, addr->ai_family);
+ bind_local(sock);
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
#ifdef TARGET_ANDROID
static void
protect_fd_nonlocal(int fd, const struct sockaddr *addr)
@@ -1161,13 +1153,8 @@
}
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
static void
-resolve_bind_local(struct link_socket *sock, const sa_family_t af)
+resolve_bind_local(struct link_socket *sock)
{
struct gc_arena gc =3D gc_new();
@@ -1183,12 +1170,12 @@
}
/* will return AF_{INET|INET6}from local_host */
- status =3D get_cached_dns_entry(sock->dns_cache, sock->local_host,=
sock->local_port, af,
+ status =3D get_cached_dns_entry(sock->dns_cache, sock->local_host,=
sock->local_port, sock->info.af,
flags, &sock->info.lsa->bind_local);
if (status)
{
- status =3D openvpn_getaddrinfo(flags, sock->local_host, sock->=
local_port, 0, NULL, af,
+ status =3D openvpn_getaddrinfo(flags, sock->local_host, sock->=
local_port, 0, NULL, sock->info.af,
&sock->info.lsa->bind_local);
}
@@ -1209,7 +1196,7 @@
/* the resolved 'local entry' might have a different family th=
an
* what was globally configured
*/
- sock->info.af =3D sock->info.lsa->bind_local->ai_family;
+ sock->info.af =3D (sa_family_t)sock->info.lsa->bind_local->ai_=
family;
}
}
@@ -1405,7 +1392,8 @@
sock->mark =3D o->mark;
sock->bind_dev =3D o->bind_dev;
- sock->info.proto =3D proto;
+ ASSERT(proto >=3D 0 && proto < PROTO_N);
+ sock->info.proto =3D (uint8_t)proto;
sock->info.af =3D o->ce.af;
sock->info.remote_float =3D o->ce.remote_float;
sock->info.lsa =3D &c->c1.link_socket_addrs[sock_index];
@@ -1471,7 +1459,7 @@
{
if (sock->bind_local)
{
- resolve_bind_local(sock, sock->info.af);
+ resolve_bind_local(sock);
}
resolve_remote(sock, 1, NULL);
}
@@ -1732,9 +1720,9 @@
* and we should not connect a remote */
if (sock->info.af =3D=3D AF_UNSPEC)
{
+ sock->info.af =3D (sa_family_t)sock->info.lsa->bind_local-=
>ai_family;
msg(M_WARN, "Could not determine IPv4/IPv6 protocol. Using=
%s",
- addr_family_name(sock->info.lsa->bind_local->ai_family=
));
- sock->info.af =3D sock->info.lsa->bind_local->ai_family;
+ addr_family_name(sock->info.af));
}
create_socket(sock, sock->info.lsa->bind_local);
}
@@ -1794,10 +1782,6 @@
}
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
void
link_socket_close(struct link_socket *sock)
{
@@ -2374,8 +2358,8 @@
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);
+ "CMSG received that cannot be parsed (cmsg_level=3D%d, cmsg_ty=
pe=3D%d, cmsg=3Dlen=3D%zu)",
+ cmsg->cmsg_level, cmsg->cmsg_type, (size_t)cmsg->cmsg_len);
}
return buf->len;
diff --git a/src/openvpn/socket_util.c b/src/openvpn/socket_util.c
index 0194f38..913ac69 100644
--- a/src/openvpn/socket_util.c
+++ b/src/openvpn/socket_util.c
@@ -356,7 +356,7 @@
const char *short_form;
const char *display_form;
sa_family_t proto_af;
- int proto;
+ uint8_t proto;
};
/* Indexed by PROTO_x */
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1476?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: I7be7427480d3540d43dd818eddb6eb5860956459
Gerrit-Change-Number: 1476
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld
Gerrit-Reviewer: flichtenheld
Gerrit-Reviewer: plaisthos
Gerrit-CC: openvpn-devel
Gerrit-Attention: plaisthos
--hWpanwhRc58=
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Attention is cur=
rently required from: plaisthos.
flichtenheld uploaded patch set #7 to this change.
<=
p>View C=
hange
socket: Fix various conversion warnings
-=
We assume that addrinfo->ai_family always fits
into a sa_family_t,=
so we do not check those.
- Reduce places where we explicitly pass ai_f=
amily
and prefer sock->info.af.
- Make sure that --bind-dev has =
a suitable length.
- Treat PROTO_* as uint8_t where possible (however, much code uses proto =3D -1 for various error handling).
Change-=
Id: I7be7427480d3540d43dd818eddb6eb5860956459
Signed-off-by: Frank Licht=
enheld <frank@...2641...>
---
M src/openvpn/options.c
M src/=
openvpn/socket.c
M src/openvpn/socket_util.c
3 files changed, 30 inse=
rtions(+), 40 deletions(-)
git pull ssh://gerrit=
.openvpn.net:29418/openvpn refs/changes/76/1476/7
diff --git a/src/=
openvpn/options.c b/src/openvpn/options.c
index fdbc678..e1=
c62bd 100644
--- a/src/openvpn/options.c
++=
+ b/src/openvpn/options.c
@@ -6492,7 +6492,15 @@
=
else if (streq(p[0], "bind-dev") && p[1])
{
VERIFY_PERMISSION(OPT_P_SOCKFLA=
GS);
- options-&=
gt;bind_dev =3D p[1];
+ if (strlen(p[1]) <=3D IFNAMSIZ)
+ {
+ options->bind_dev =3D p[1];
+ }
+ else
+ {
+ msg(msglevel, "argument to --bind-dev is longer=
than allowed %u", IFNAMSIZ);
+ goto err;
+ }
}
#endif=
span>
else if (streq(p[0], "txqueuelen") &&=
p[1] && !p[2])
@@ -6578,11 +6586,9 @@
}
else if (streq(p[0], "proto") &am=
p;& p[1] && !p[2])
{
- int proto;
- sa_family_t af;
=
VERIFY_PERMISSION(OPT_P_GENERAL | OPT_P_CONNECTION);
- proto =3D ascii2proto(p[1]);
- af =3D ascii2af(p[=
1]);
+ int pro=
to =3D ascii2proto(p[1]);
+ sa_family_t af =3D ascii2af(p[1]);
i=
f (proto < 0)
{
ms=
g(msglevel, "Bad protocol: '%s'. Allowed protocols with --prot=
o option: %s", p[1],
diff --git a/src/openvpn/socket.c=
b/src/openvpn/socket.c
index 0b72684..33c12db 100644
--- a/src/openvpn/socket.c
+++ b/src/openvpn/soc=
ket.c
@@ -649,28 +649,23 @@
}
static void
-bind_local(struct link_socket *sock, const sa_family_t ai_fami=
ly)
+bind_local(struc=
t link_socket *sock)
{
/* bind to loc=
al address/port */
if (sock->bind_local)
=
{
if (sock->socks_proxy && s=
ock->info.proto =3D=3D PROTO_UDP)
{
<=
span style=3D"color: hsl(0, 100%, 40%);">- socket_bind(sock->=
ctrl_sd, sock->info.lsa->bind_local, ai_family, "SOCKS", fa=
lse);
+ so=
cket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info=
.af, "SOCKS", false);
}
=
else
{
- socket_bind(sock->sd, sock->info.lsa-&=
gt;bind_local, ai_family, "TCP/UDP",
+ socket_bind(sock->sd, sock->in=
fo.lsa->bind_local, sock->info.af, "TCP/UDP",
sock->info.bind_ipv6_only);
=
}
}
}
-#if defined(__GNUC__) || d=
efined(__clang__)
-#pra=
gma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
static void
create_=
socket(struct link_socket *sock, struct addrinfo *addr)
{<=
/span>
@@ -702,7 +697,7 @@
}
=
/* Set af field of sock->info, so it always reflects the address fa=
mily
* of the created socket */
- sock->info.af =3D addr->ai_family=
;
+ sock->info.=
af =3D (sa_family_t)addr->ai_family;
=
/* set socket buffers based on --sndbuf and --rcvbuf options */<=
br> socket_set_buffers(sock->sd, &sock->socket_buffer_s=
izes, true);
@@ -714,8 +709,9 @@
if (s=
ock->bind_dev)
{
msg(M_INF=
O, "Using bind-dev %s", sock->bind_dev);
+ /* Note: We verify strlen of bind=
_dev in options parsing */
if (setsockopt(sock->=
;sd, SOL_SOCKET, SO_BINDTODEVICE, sock->bind_dev,
- strlen(sock->bind=
_dev) + 1)
+ =
(socklen_t)(strlen(sock->bind_dev) + 1))
=
!=3D 0)
{
=
msg(M_WARN | M_ERRNO, "WARN: setsockopt SO_BINDTODEVICE=3D%s failed&q=
uot;, sock->bind_dev);
@@ -723,13 +719,9 @@
}
#endif
- bind_local(sock, addr->ai_family);=
span>
+ bind_local(sock);=
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
=
-#endif
-
#ifdef TARGET_ANDROID<=
br> static void
protect_fd_nonlocal(int fd, const st=
ruct sockaddr *addr)
@@ -1161,13 +1153,8 @@
}
}
-#if defined(__GNUC__) || defined(__clang__)-#pragma GCC diagnostic push
-#pragma GCC diagnostic i=
gnored "-Wconversion"
-#endif
-
static void
-resolve_bind_local(struct link_socket *sock, const sa_family_t af)
+resolve_bind_local(struct=
link_socket *sock)
{
struct gc_arena=
gc =3D gc_new();
@@ -1183,12 +1170,12 @@=
}
/* w=
ill return AF_{INET|INET6}from local_host */
- status =3D get_cached_dns_entry(sock->dns=
_cache, sock->local_host, sock->local_port, af,
+ status =3D get_cached_dns_entry(s=
ock->dns_cache, sock->local_host, sock->local_port, sock->info.=
af,
flags, &sock=
->info.lsa->bind_local);
i=
f (status)
{
- status =3D openvpn_getaddrinfo(flags, sock->=
local_host, sock->local_port, 0, NULL, af,
+ status =3D openvpn_getaddrinfo(flags,=
sock->local_host, sock->local_port, 0, NULL, sock->info.af,
&sock->info.lsa=
->bind_local);
}
@@ -1209,7 +1196,7 @@
/* the resolved '=
;local entry' might have a different family than
=
* what was globally configured
*/
- sock->info=
.af =3D sock->info.lsa->bind_local->ai_family;
+ sock->info.af =3D (sa_fa=
mily_t)sock->info.lsa->bind_local->ai_family;
=
}
}
@@ -1405,7 =
+1392,8 @@
sock->mark =3D o->m=
ark;
sock->bind_dev =3D o->bind_dev;
<=
span style=3D"color: hsl(0, 100%, 40%);">- sock->info.proto =3D proto=
;
+ ASSERT(proto &=
gt;=3D 0 && proto < PROTO_N);
+ sock->info.proto =3D (uint8_t)proto;
=
sock->info.af =3D o->ce.af;
sock->=
info.remote_float =3D o->ce.remote_float;
sock->=
info.lsa =3D &c->c1.link_socket_addrs[sock_index];
@=
@ -1471,7 +1459,7 @@
{
if (so=
ck->bind_local)
{
- resolve_bind_local(sock, sock->info.=
af);
+ res=
olve_bind_local(sock);
}
=
resolve_remote(sock, 1, NULL);
}
@@ -1=
732,9 +1720,9 @@
* and we should not connect =
a remote */
if (sock->info.af =3D=3D AF_UNS=
PEC)
{
+ sock->info.af =3D (sa_family_t)sock->=
info.lsa->bind_local->ai_family;
msg=
(M_WARN, "Could not determine IPv4/IPv6 protocol. Using %s",
- addr_f=
amily_name(sock->info.lsa->bind_local->ai_family));
- sock->info.af =3D=
sock->info.lsa->bind_local->ai_family;
+ addr_family_name(sock->=
info.af));
}
crea=
te_socket(sock, sock->info.lsa->bind_local);
=
}
@@ -1794,10 +1782,6 @@
}
=
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
void
link_socket_close(=
struct link_socket *sock)
{
@@ -2374,8 +23=
58,8 @@
else if (cmsg !=3D NULL)
=
{
msg(M_WARN,
- "CMSG received that cannot be parsed (cms=
g_level=3D%d, cmsg_type=3D%d, cmsg=3Dlen=3D%d)",
- (int)cmsg->cmsg_level, (int)=
cmsg->cmsg_type, (int)cmsg->cmsg_len);
+ "CMSG received that cannot be par=
sed (cmsg_level=3D%d, cmsg_type=3D%d, cmsg=3Dlen=3D%zu)",
+ cmsg->cmsg_level,=
cmsg->cmsg_type, (size_t)cmsg->cmsg_len);
}
return buf->len;
dif=
f --git a/src/openvpn/socket_util.c b/src/openvpn/socket_util.c
<=
span>index 0194f38..913ac69 100644
--- a/src/openvpn/socket=
_util.c
+++ b/src/openvpn/socket_util.c
@@ =
-356,7 +356,7 @@
const char *short_form;
const char *display_form;
sa_family_t proto_af=
;
- int proto;
+ uint8_t proto;=
};
/* Indexed by PROTO_x */=
span>
To view, visit change 1476. To unsubscribe, =
or for help writing mail filters, visit settings.
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I7be7427480d3540d43dd818eddb=
6eb5860956459
Gerrit-Change-Number: 1476
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld <frank@...2723...=
.>
Gerrit-Reviewer: flichtenheld <frank@...2716...=
1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...=
.1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...=
ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...=
..1227...>
=
--hWpanwhRc58=--