From: "plaisthos (Code Review)" Date: Wed, 22 Nov 2023 13:08:59 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <430a734e15d164a4e98b0849d40330116b802646-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="G6JadVbR+9A="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: cron2 , flichtenheld Cc: openvpn-devel --G6JadVbR+9A= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello cron2, flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=3Demail to look at the new patch set (#2). Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr ...................................................................... Fix unaligned access in macOS, FreeBSD, Solaris hwaddr The undefined behaviour USAN clang checker found this. This fix is a bit messy but so are the original structures. Since the API on Solaris/Illuminos does not return the AF_LINK sockaddr type we are interested in, there is little value in fixing the code on that platform to iterate through a list that does not contain the element we are looking for. Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8 Signed-off-by: Arne Schwabe --- M src/openvpn/route.c 1 file changed, 38 insertions(+), 11 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/2 diff --git a/src/openvpn/route.c b/src/openvpn/route.c index ff64938..f8cfa53 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -3639,11 +3639,15 @@ rgi->flags |=3D RGI_NETMASK_DEFINED; } +#if !defined(TARGET_SOLARIS) + /* Illumos/Solaris does not provide AF_LINK entries when calling the + * SIOCGIFCONF API, so there is little sense to trying to figure out a + * MAC address from an API that does not provide that information */ + /* try to read MAC addr associated with interface that owns default ga= teway */ if (rgi->flags & RGI_IFACE_DEFINED) { struct ifconf ifc; - struct ifreq *ifr; const int bufsize =3D 4096; char *buffer; @@ -3668,22 +3672,44 @@ for (cp =3D buffer; cp <=3D buffer + ifc.ifc_len - sizeof(struct i= freq); ) { - ifr =3D (struct ifreq *)cp; -#if defined(TARGET_SOLARIS) - const size_t len =3D sizeof(ifr->ifr_name) + sizeof(ifr->ifr_a= ddr); -#else - const size_t len =3D sizeof(ifr->ifr_name) + max(sizeof(ifr->i= fr_addr), ifr->ifr_addr.sa_len); -#endif + struct ifreq ifr =3D { 0 }; + /* this is not always using an 8 byte alignment that struct ifr + * requires */ + memcpy(&ifr, cp, sizeof(struct ifreq)); + const size_t len =3D sizeof(ifr.ifr_name) + max(sizeof(ifr.ifr= _addr), ifr.ifr_addr.sa_len); - if (!ifr->ifr_addr.sa_family) + if (!ifr.ifr_addr.sa_family) { break; } - if (!strncmp(ifr->ifr_name, rgi->iface, IFNAMSIZ)) + if (!strncmp(ifr.ifr_name, rgi->iface, IFNAMSIZ)) { - if (ifr->ifr_addr.sa_family =3D=3D AF_LINK) + if (ifr.ifr_addr.sa_family =3D=3D AF_LINK) { - struct sockaddr_dl *sdl =3D (struct sockaddr_dl *)&ifr= ->ifr_addr; + /* This is a confusing member access on multiple level= s. + * + * struct sockaddr_dl is 20 bytes in size and has + * 12 bytes space for the hw address (6 bytes) + * and Ethernet interface name (max 16 bytes) + * + * So if the interface name is more than 6 byte, it + * extends beyond the struct. + * + * This struct is embedded into ifreq that has + * 16 bytes for a sockaddr and also expects this + * struct to potentially extend beyond the bounds of + * the struct. + * + * Since we only copied 32 bytes from cp to ifr but sdl + * might extend after ifr's end, we need to copy from + * cp directly to avoid missing out on extra bytes + * behind the struct + */ + const size_t sock_dl_len =3D max_int((int) (sizeof(str= uct sockaddr_dl)), + (int) (ifr.ifr_addr= .sa_len)); + + struct sockaddr_dl *sdl =3D gc_malloc(sock_dl_len, tru= e, &gc); + memcpy(sdl, cp + offsetof(struct ifreq, ifr_addr), soc= k_dl_len); memcpy(rgi->hwaddr, LLADDR(sdl), 6); rgi->flags |=3D RGI_HWADDR_DEFINED; } @@ -3691,6 +3717,7 @@ cp +=3D len; } } +#endif /* if !defined(TARGET_SOLARIS) */ done: if (sockfd >=3D 0) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?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: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8 Gerrit-Change-Number: 454 Gerrit-PatchSet: 2 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --G6JadVbR+9A= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

plaisthos uploaded patch set #2 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/454?usp=3Demail">View Chang= e

Fix unaligned access in macOS, FreeBSD, Solaris hwaddr

The und= efined behaviour USAN clang checker found this.

This fix is a bit me= ssy but so are the original structures.

Since the API on Solaris/Ill= uminos does not return the AF_LINK
sockaddr type we are interested in, t= here is little value in
fixing the code on that platform to iterate thro= ugh a list
that does not contain the element we are looking for.

= Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Signed-off-by: Arne= Schwabe <arne@...1227...>
---
M src/openvpn/route.c
1 file = changed, 38 insertions(+), 11 deletions(-)

git pull ssh://gerrit.=
openvpn.net:29418/openvpn refs/changes/54/454/2
diff --git a/src/op=
envpn/route.c b/src/openvpn/route.c
index ff64938..f8cfa53 = 100644
--- a/src/openvpn/route.c
+++ b/src/= openvpn/route.c
@@ -3639,11 +3639,15 @@
= rgi->flags |=3D RGI_NETMASK_DEFINED;
}=

+#if !def= ined(TARGET_SOLARIS)
= + /* Illumos/Solaris does not provide AF_LINK entries when calling the
+ * SIOCGIFCONF AP= I, so there is little sense to trying to figure out a
+ * MAC address from an API that does = not provide that information */
+
/* try to read MAC addr associated with i= nterface that owns default gateway */
if (rgi->flag= s & RGI_IFACE_DEFINED)
{
= struct ifconf ifc;
- = struct ifreq *ifr;
const int bufsize =3D 4096= ;
char *buffer;

= @@ -3668,22 +3672,44 @@

for (cp = =3D buffer; cp <=3D buffer + ifc.ifc_len - sizeof(struct ifreq); )
{
-= ifr =3D (struct ifreq *)cp;
-#if defined(TARGET_SOLARIS)
- const size_t len =3D sizeof(ifr->ifr= _name) + sizeof(ifr->ifr_addr);
-#else
- = const size_t len =3D sizeof(ifr->ifr_name) + max(sizeof(ifr->= ;ifr_addr), ifr->ifr_addr.sa_len);
-#endif
+ /* this is not always using an 8 byte al= ignment that struct ifr
+ * requires */
+ memcpy(&ifr, cp, sizeof(struct ifreq));+ const size_t len= =3D sizeof(ifr.ifr_name) + max(sizeof(ifr.ifr_addr), ifr.ifr_addr.sa_len);=

- = if (!ifr->ifr_addr.sa_family)
+ if (!ifr.ifr_addr.sa_family)
{

break;
= }
- = if (!strncmp(ifr->ifr_name, rgi->iface, IFNAMSIZ))
+ if (!strncmp(ifr.ifr_= name, rgi->iface, IFNAMSIZ))

{
- if (ifr->ifr_ad= dr.sa_family =3D=3D AF_LINK)
+ if (ifr.ifr_addr.sa_family =3D=3D AF_LINK)<= br> {
- struct sockaddr_dl *sdl =3D (struct sockaddr_dl *= )&ifr->ifr_addr;
+ /* This is a confusing member access on multiple le= vels.
+ = *
+ = * struct sockaddr_dl is 20 bytes in size and has
+ * 12 bytes spac= e for the hw address (6 bytes)
+ * and Ethernet interface name (max 16 bytes= )
+ = *
+ = * So if the interface name is more than 6 byte, it
+ * extends beyond = the struct.
+ = *
+ = * This struct is embedded into ifreq that has
+ * 16 bytes f= or a sockaddr and also expects this
+ * struct to potentially extend beyond = the bounds of
+ = * the struct.
+ *
+ * Since we only copied 32 bytes from cp to= ifr but sdl
+ = * might extend after ifr's end, we need to copy from

+ * = cp directly to avoid missing out on extra bytes
+ * behind the struct=
+ */

+ co= nst size_t sock_dl_len =3D max_int((int) (sizeof(struct sockaddr_dl)),
+ = (int) (ifr.ifr_addr.sa_len));
+

+ struct sockaddr_dl *sdl =3D gc_ma= lloc(sock_dl_len, true, &gc);
+ memcpy(sdl, cp + offsetof(struct ifreq, i= fr_addr), sock_dl_len);
memcpy(rgi->= ;hwaddr, LLADDR(sdl), 6);
rgi->flag= s |=3D RGI_HWADDR_DEFINED;
}
@@ -3691,6 +3717,7 @@
cp +=3D len; }
}
+#endif /* if !defined(TARGET_SOLARIS) */

done:
if (sockfd >=3D 0)


To view, visit change 454. To unsubscribe, or f= or help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde= 5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--G6JadVbR+9A=--