From: "cron2 (Code Review)" Date: Sun, 31 Dec 2023 18:12:53 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="Eo918YXZ+As="; charset=UTF-8 Subject: [Openvpn-devel] [M] 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: plaisthos , flichtenheld Cc: openvpn-devel --Eo918YXZ+As= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#4) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 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. Add includes stddef.h for offsetof and integer.h for max_int. Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8 Signed-off-by: Arne Schwabe Acked-by: Gert Doering Message-Id: <20231231173431.31356-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg27= 885.html Signed-off-by: Gert Doering --- M src/openvpn/route.c 1 file changed, 46 insertions(+), 11 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/4 diff --git a/src/openvpn/route.c b/src/openvpn/route.c index 6cc112c..0e6667f 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -24,6 +24,7 @@ /* * Support routines for adding/deleting network routes. */ +#include #ifdef HAVE_CONFIG_H #include "config.h" @@ -40,6 +41,7 @@ #include "win32.h" #include "options.h" #include "networking.h" +#include "integer.h" #include "memdbg.h" @@ -3639,11 +3641,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 +3674,50 @@ 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. Need to memcpy() to a strict ifr to force 8-byte + * alignment required for member access */ + 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 (on macOS and NetBSD, + * larger on other BSDs) in size and has + * 12 bytes space for the Ethernet interface name + * (max 16 bytes) and hw address (6 bytes) + * + * So if the interface name is more than 6 byte, the + * location of hwaddr 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. + * + * We only copied 32 bytes (size of ifr at least on ma= cOS + * might differ on other platforms again) from cp to i= fr. + * + * But as hwaddr might extend but sdl might extend bey= ond + * ifr's. So we need recalculate how large the actual = size + * of the embedded dl_sock actually is and then also n= eed + * to copy it since it also most likely does not have = the + * proper alignment required to access 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 +3725,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: 4 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --Eo918YXZ+As= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #4 to the change originally cr= eated by plaisthos.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by cron2

Fix unaligned access in macOS, FreeBSD, Solaris hwad=
dr

The undefined behaviour USAN clang checker found this.

Thi= s fix is a bit messy but so are the original structures.

Since the A= PI 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 platfor= m to iterate through a list
that does not contain the element we are loo= king for.

Add includes stddef.h for offsetof and integer.h for max_i= nt.

Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Signed-o= ff-by: Arne Schwabe <arne@...1227...>
Acked-by: Gert Doering <g= ert@...1296...>
Message-Id: <20231231173431.31356-1-gert@...2132...= .>
URL: https://www.mail-archive.com/openvpn-devel@...290...= net/msg27885.html
Signed-off-by: Gert Doering <gert@...1296...>---
M src/openvpn/route.c
1 file changed, 46 insertions(+), 11 delet= ions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/chan=
ges/54/454/4
diff --git a/src/openvpn/route.c b/src/openvpn/route.c=

index 6cc112c..0e6667f 100644
--- a/src/op= envpn/route.c
+++ b/src/openvpn/route.c
@@ = -24,6 +24,7 @@
/*
* Support routines for= adding/deleting network routes.
*/
+#include <stddef.h>

#ifdef HAVE_CONFIG_H
#include "co= nfig.h"
@@ -40,6 +41,7 @@
#include &q= uot;win32.h"
#include "options.h" #include "networking.h"
+#include "integer.h"

#include "memdbg.h"

@@ -3639,11 +3641,15 @@

rgi->flags |=3D RGI_N= ETMASK_DEFINED;
}

+#if !defined(TARGET_SOLARIS)+ /* Illumos/Solaris does n= ot 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 gat= eway */
if (rgi->flags & RGI_IFACE_DEFINED)
{
struct ifconf ifc;
<= span style=3D"color: hsl(0, 100%, 40%);">- struct ifreq *ifr;
=
const int bufsize =3D 4096;
cha= r *buffer;

@@ -3668,22 +3674,50 @@=

for (cp =3D buffer; cp <=3D buffer = + ifc.ifc_len - sizeof(struct ifreq); )
{- ifr =3D (struct if= req *)cp;
-#if defined(= TARGET_SOLARIS)
- = const size_t len =3D sizeof(ifr->ifr_name) + sizeof(ifr->ifr_ad= dr);
-#else
<= span style=3D"color: hsl(0, 100%, 40%);">- const size_t len =3D = sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.s= a_len);
-#endif<= br>+ struct ifreq if= r =3D { 0 };
+ = /* this is not always using an 8 byte alignment that struct ifr=
+ * requires. N= eed to memcpy() to a strict ifr to force 8-byte
+ * alignment required for member ac= cess */
+ = memcpy(&ifr, cp, sizeof(struct ifreq));
+ const size_t len =3D sizeof(ifr.ifr_nam= e) + max(sizeof(ifr.ifr_addr), ifr.ifr_addr.sa_len);

- if (!ifr->i= fr_addr.sa_family)
+ = if (!ifr.ifr_addr.sa_family)
{
break;
}- if (!strncmp(ifr-&= gt;ifr_name, rgi->iface, IFNAMSIZ))
+ if (!strncmp(ifr.ifr_name, rgi->iface, IF= NAMSIZ))
{
- 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 levels.
+ *
+ * struct sock= addr_dl is 20 bytes (on macOS and NetBSD,

+ * larger on other BSDs) in size = and has
+ = * 12 bytes space for the Ethernet interface name
+ * (max 16 bytes)= and hw address (6 bytes)
+ *
+ * So if the interface name is more than 6 by= te, the
+ = * location of hwaddr 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.
+ = *
+ = * We only copied 32 bytes (size of ifr at least on macOS
+ * might differ= on other platforms again) from cp to ifr.

+ *
+ * But as hwaddr might extend= but sdl might extend beyond
+ * ifr's. So we need recalculate how large= the actual size
+ = * of the embedded dl_sock actually is and then also need<= /span>
+ = * to copy it since it also most likely does not have the
+ * proper alignme= nt required to access the struct.
+ */
+ const 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_malloc(sock_dl_len, true, &gc);
+ m= emcpy(sdl, cp + offsetof(struct ifreq, ifr_addr), sock_dl_len);
<= span> memcpy(rgi->hwaddr, LLADDR(sdl), 6);
rgi->flags |=3D RGI_HWADDR_DEFINED; }
@@ -3691,6 +3725,7 @@
= cp +=3D len;
}
= }
+#endif /* if = !defined(TARGET_SOLARIS) */

done:=
if (sockfd >=3D 0)

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde= 5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 4
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-MessageType: newpatchset
--Eo918YXZ+As=--