From: "cron2 (Code Review)" Date: Sun, 31 Dec 2023 18:12:55 +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="sHtipTTDkqE="; 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 Cc: flichtenheld , openvpn-devel --sHtipTTDkqE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/45= 4?usp=3Demail ) 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(-) 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: merged --sHtipTTDkqE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

Fix unaligned access in macOS, FreeBSD, Solari=
s hwaddr

The undefined behaviour USAN clang checker found this.
<= br>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 p= latform to iterate through a list
that does not contain the element we a= re looking for.

Add includes stddef.h for offsetof and integer.h for= max_int.

Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Si= gned-off-by: Arne Schwabe <arne@...1227...>
Acked-by: Gert Doering= <gert@...1296...>
Message-Id: <20231231173431.31356-1-gert@...493...= 1296...>
URL: https://www.mail-archive.com/openvpn-devel@...1659...= forge.net/msg27885.html
Signed-off-by: Gert Doering <gert@...1296...&= gt;
---
M src/openvpn/route.c
1 file changed, 46 insertions(+), 11= deletions(-)

diff --git a/src/openvpn/route.c b/src/openvpn/route.c
i= ndex 6cc112c..0e6667f 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/route.c
@@ -24,6 +24,7 @@

/*
* Support routines for adding/deleting n= etwork routes.
*/
+#include <stddef.h>

#ifdef HAVE_CONFIG_H
#include "config.h"

@@ -40,6 +41,7 @@
#include "win32.h"<= /span>
#include "options.h"
#include &= quot;networking.h"
+#include "integer.h"

#incl= ude "memdbg.h"

@@ -3639,11 +364= 1,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 add= ress from an API that does not provide that information */
+
/* try to read= MAC addr associated with interface that owns default gateway */
= if (rgi->flags & RGI_IFACE_DEFINED)
= {
struct ifconf ifc;
- struct ifreq *ifr;
= const int bufsize =3D 4096;
char *buffer;<= br>
@@ -3668,22 +3674,50 @@
=
for (cp =3D buffer; cp <=3D buffer + ifc.ifc_len - si= zeof(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
+ struct ifreq ifr =3D { 0 };
+ /* this is no= t 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, c= p, sizeof(struct ifreq));
+ const size_t len =3D sizeof(ifr.ifr_name) + max(sizeof(if= r.ifr_addr), ifr.ifr_addr.sa_len);

- if (!ifr->ifr_addr.sa_family= )
+ if (!i= fr.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)
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (ifr.ifr_add= r.sa_family =3D=3D AF_LINK)

{
- struct sockadd= r_dl *sdl =3D (struct sockaddr_dl *)&ifr->ifr_addr;

+ /* This is a con= fusing member access on multiple levels.
+ *
+ * struct sockaddr_dl is 20 byt= es (on macOS and NetBSD,
+ * larger on other BSDs) in size and has+ * 12 byt= es 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+ * locati= on of hwaddr extends beyond the struct.
+ *
+ * This struct is embedded into = ifreq that has
+ = * 16 bytes for a sockaddr and also expects this
<= span style=3D"color: hsl(120, 100%, 40%);">+ * struct t= o potentially extend beyond the bounds of

+ * the struct.
+ *
+ * We only copi= ed 32 bytes (size of ifr at least on macOS
+ * might differ on other platfor= ms again) from cp to ifr.
+ *
+ * But as hwaddr might extend but sdl might ex= tend beyond
+ = * ifr's. So we need recalculate how large the actual size<= /span>
+ = * of the embedded dl_sock actually is and then also need
+ * to copy it sin= ce it also most likely does not have the
+ * proper alignment required to ac= cess the struct.
+ = */
= + const size_t sock_dl_len =3D max_int((int) (sizeof(str= uct sockaddr_dl)),
+ = (int) (ifr.ifr_addr.s= a_len));
+
= + struct soc= kaddr_dl *sdl =3D gc_malloc(sock_dl_len, true, &gc);
+ memcpy(sdl, cp + o= ffsetof(struct ifreq, ifr_addr), sock_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_S= OLARIS) */

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: merged
--sHtipTTDkqE=--