From: "plaisthos (Code Review)" Date: Tue, 21 Nov 2023 16:06:46 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <83c5e06d672c765f7d6bd8646088869e8a4e04da-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="hqqjuEOhvQU="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Fix unaligned access in macOS/Solaris hwaddr List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --hqqjuEOhvQU= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=3Demail to review the following change. Change subject: Fix unaligned access in macOS/Solaris hwaddr ...................................................................... Fix unaligned access in macOS/Solaris hwaddr The undefined behaviour USAN clang checker found this. This fix is a bit messy but so are the original structures. Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8 --- M src/openvpn/route.c 1 file changed, 33 insertions(+), 8 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/1 diff --git a/src/openvpn/route.c b/src/openvpn/route.c index ff64938..ab5bff7 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -3643,7 +3643,6 @@ if (rgi->flags & RGI_IFACE_DEFINED) { struct ifconf ifc; - struct ifreq *ifr; const int bufsize =3D 4096; char *buffer; @@ -3668,22 +3667,48 @@ for (cp =3D buffer; cp <=3D buffer + ifc.ifc_len - sizeof(struct i= freq); ) { - ifr =3D (struct ifreq *)cp; + struct ifreq ifr =3D { 0 }; + /* this is not always using an 8 byte alignment that struct ifr + * requires */ + memcpy(&ifr, cp, sizeof(struct ifreq)); #if defined(TARGET_SOLARIS) - const size_t len =3D sizeof(ifr->ifr_name) + sizeof(ifr->ifr_a= ddr); + const size_t len =3D sizeof(ifr.ifr_name) + sizeof(ifr.ifr_add= r); #else - const size_t len =3D sizeof(ifr->ifr_name) + max(sizeof(ifr->i= fr_addr), ifr->ifr_addr.sa_len); + const size_t len =3D sizeof(ifr.ifr_name) + max(sizeof(ifr.ifr= _addr), ifr.ifr_addr.sa_len); #endif - 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; } -- 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: 1 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --hqqjuEOhvQU= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos would like flichtenheld to review this change.

Vi= ew Change

Fix unaligned access in macOS/Solaris hwaddr

The undef= ined behaviour USAN clang checker found this.

This fix is a bit mess= y but so are the original structures.

Change-Id: Ia797c8801fa9a9bc10= b6674efde5fdbd7132e4a8
---
M src/openvpn/route.c
1 file changed, 3= 3 insertions(+), 8 deletions(-)

git pull ssh://gerrit.openvpn.net=
:29418/openvpn refs/changes/54/454/1
diff --git a/src/openvpn/route=
.c b/src/openvpn/route.c
index ff64938..ab5bff7 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/rou= te.c
@@ -3643,7 +3643,6 @@
if (rgi->= ;flags & RGI_IFACE_DEFINED)
{
= struct ifconf ifc;
- struct ifreq *ifr;
const int bufsize =3D= 4096;
char *buffer;

<= span>@@ -3668,22 +3667,48 @@


for= (cp =3D buffer; cp <=3D buffer + ifc.ifc_len - sizeof(struct ifreq); )<= /span>
{
- ifr =3D (struct ifreq *)cp;
+ struct ifreq ifr =3D { 0 };
= + /* this is not alw= ays using an 8 byte alignment that struct ifr
+ * requires */
+ memcpy(&ifr, cp, sizeof(s= truct ifreq));
#if defined(TARGET_SOLARIS)
- const size_t len =3D size= of(ifr->ifr_name) + sizeof(ifr->ifr_addr);

+ const size_t len =3D sizeof(ifr.if= r_name) + sizeof(ifr.ifr_addr);
#else
- const size_t len =3D sizeof(if= r->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
+ const siz= e_t len =3D sizeof(ifr.ifr_name) + max(sizeof(ifr.ifr_addr), ifr.ifr_addr.s= a_len);
#endif

- if (!ifr->ifr_addr.sa_family)
+ if (!ifr.i= fr_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 confusi= ng member access on multiple levels.
+ *
+ * struct sockaddr_dl is 20 bytes i= n size and has
+ = * 12 bytes space for the hw address (6 bytes)
+ * and Ethern= et interface name (max 16 bytes)
+ *
+ * So if the interface name is more tha= n 6 byte, it
+ = * extends beyond the struct.
+ *
+ * This struct is embedded in= to ifreq that has
+ = * 16 bytes for a sockaddr and also expects this+ * struc= t 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 by= tes
+ = * behind the struct
+ */
+ const size_t sock_dl_len =3D max_int((int) (si= zeof(struct sockaddr_dl)),
+ (int) (ifr.if= r_addr.sa_len));
+

+ st= ruct sockaddr_dl *sdl =3D gc_malloc(sock_dl_len, true, &gc);
= + memcpy(sdl= , cp + offsetof(struct ifreq, ifr_addr), sock_dl_len);
= memcpy(rgi->hwaddr, LLADDR(sdl), 6);
= rgi->flags |=3D RGI_HWADDR_DEFINED;
= }

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: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--hqqjuEOhvQU=--