From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from cmx-torrgo001.bell.net (mta-tor-005.bell.net [209.71.212.37]) by sourceware.org (Postfix) with ESMTP id 53E813858413 for ; Thu, 17 Mar 2022 23:04:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 53E813858413 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=bell.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=bell.net X-RG-CM-BuS: 0 X-RG-CM-SC: 0 X-RG-CM: Clean X-Originating-IP: [70.50.7.94] X-RG-Env-Sender: dave.anglin@bell.net X-RG-Rigid: 6228049100C54772 X-CM-Envelope: MS4xfLYTUMSFTj8+rLGSKo2shI14etnWp5WfjW2NltM405FdG7P+ya6FUmJSRG2HA0IJ8eieS+/+fBewcRm87e4Ey9p+ZG8ex5JgGE4CK3jQlMBA0dRPaZ+W ZCNjv6oBMmyfbTrlPKZoPxjvKiWjYruir3t9G+NH+kqWJYz36q5MBJnUa8L/xS5uwTwAMscPC6vJnS4p3sVdjQLs1V6WXvDqNMzE3CruCmwMS6uBroIQqZzh YV5wiN6klOQuNIRIlKm+C43cU0enrDMuNyACoa1lSPIrfhRsABHZdQzL2dKmLy8f3o7GXU+etThp/6QIy5QgEAlUPyBSRDsqvkjquE9GRMAcryW9qkYiGqme pv6oILmQtQKqIQFF1DytMaKihhYEMXBuG1+O9AcpbGvpNRGweHCr3syIb05FtRz70FisZEnd5jUF+Qc9WEy+hu1nkgDpXQSdikVsCwaIK8VrxzKmax7z11lm A88ItzcuAZay/YpLtD/QTVtpoyUu9dci/pQDgNyJO+X0kqaEp+5XTTLU4b4= X-CM-Analysis: v=2.4 cv=B8zabMhM c=1 sm=1 tr=0 ts=6233be7c a=9k1bCY7nR7m1ZFzoCuQ56g==:117 a=9k1bCY7nR7m1ZFzoCuQ56g==:17 a=o8Y5sQTvuykA:10 a=_0Jo8HVc6lrVxDrZ1fAA:9 a=CjuIK1q_8ugA:10 a=wo8HnzaHINky4B-pY5IA:9 a=FfaGCDsud1wA:10 Received: from mx3210.localdomain (70.50.7.94) by cmx-torrgo001.bell.net (5.8.807) (authenticated as dave.anglin@bell.net) id 6228049100C54772; Thu, 17 Mar 2022 19:04:28 -0400 Received: by mx3210.localdomain (Postfix, from userid 1000) id 972D2220115; Thu, 17 Mar 2022 23:04:27 +0000 (UTC) Date: Thu, 17 Mar 2022 23:04:27 +0000 From: John David Anglin To: libc-alpha@sourceware.org Subject: [PATCH v4] Fix misaligned accesses to fields in HEADER struct defined in Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="XYvPuXLZZSxKH2tj" Content-Disposition: inline X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Mar 2022 23:04:42 -0000 --XYvPuXLZZSxKH2tj Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable resolv: Fix unaligned accesses to fields in HEADER struct The structure HEADER is normally aligned to a word boundary but sometimes it needs to be accessed when aligned on a byte boundary. This change defines a new typedef, UHEADER, with alignment 1. It is used to ensure the fields are accessed with byte loads and stores when necessary. V4: Change to res_mkquery.c deleted. Small whitespace fix. Dave --- diff --git a/resolv/res_query.c b/resolv/res_query.c index 5d0a68dc81..4ac2c516a1 100644 --- a/resolv/res_query.c +++ b/resolv/res_query.c @@ -81,6 +81,14 @@ #include #include =20 +/* The structure HEADER is normally aligned to a word boundary and its + fields are accessed using word loads and stores. We need to access + this structure when it is aligned on a byte boundary. This can cause + problems on machines with strict alignment. So, we create a new + typedef to reduce its alignment to one. This ensures the fields are + accessed with byte loads and stores. */ +typedef HEADER __attribute__ ((__aligned__(1))) UHEADER; + #if PACKETSZ > 65536 #define MAXPACKET PACKETSZ #else @@ -112,8 +120,8 @@ __res_context_query (struct resolv_context *ctx, const = char *name, int *nanswerp2, int *resplen2, int *answerp2_malloced) { struct __res_state *statp =3D ctx->resp; - HEADER *hp =3D (HEADER *) answer; - HEADER *hp2; + UHEADER *hp =3D (UHEADER *) answer; + UHEADER *hp2; int n, use_malloc =3D 0; =20 size_t bufsize =3D (type =3D=3D T_QUERY_A_AND_AAAA ? 2 : 1) * QUERYSIZE; @@ -217,7 +225,7 @@ __res_context_query (struct resolv_context *ctx, const = char *name, =20 if (answerp !=3D NULL) /* __res_context_send might have reallocated the buffer. */ - hp =3D (HEADER *) *answerp; + hp =3D (UHEADER *) *answerp; =20 /* We simplify the following tests by assigning HP to HP2 or vice versa. It is easy to verify that this is the same as @@ -228,7 +236,7 @@ __res_context_query (struct resolv_context *ctx, const = char *name, } else { - hp2 =3D (HEADER *) *answerp2; + hp2 =3D (UHEADER *) *answerp2; if (n < (int) sizeof (HEADER)) { hp =3D hp2; @@ -338,7 +346,7 @@ __res_context_search (struct resolv_context *ctx, { struct __res_state *statp =3D ctx->resp; const char *cp; - HEADER *hp =3D (HEADER *) answer; + UHEADER *hp =3D (UHEADER *) answer; char tmp[NS_MAXDNAME]; u_int dots; int trailing_dot, ret, saved_herrno; diff --git a/resolv/res_send.c b/resolv/res_send.c index 5d6be4b82d..24e98f0bfe 100644 --- a/resolv/res_send.c +++ b/resolv/res_send.c @@ -111,6 +111,15 @@ #include #include =20 +/* The structure HEADER is normally aligned to a word boundary and its + fields are accessed using word loads and stores. We need to access + this structure when it is aligned on a byte boundary. This can cause + problems on machines with strict alignment. So, we create a new + typedef to reduce its alignment to one. This ensures the fields are + accessed with byte loads and stores. */ +typedef HEADER __attribute__ ((__aligned__(1))) UHEADER; +#define HEADER UHEADER + #if PACKETSZ > 65536 #define MAXPACKET PACKETSZ #else --XYvPuXLZZSxKH2tj Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEnRzl+6e9+DTrEhyEXb/Nrl8ZTfEFAmIzvnMACgkQXb/Nrl8Z TfEYlw//a4SdylS3GnfHvDZjj/teQgOQNvlbQlapBbQpzN025dBRZ5df2FYY3b8Y Y6Aq0ZnS3fatnixMIISDr4i/GmG1dhEORTMygSLmNZCZ0KkJNrppb7oNhCOgiKZg gA1QgxFI4jedXxmN4QvDAiWTyFHFvxtR+IlKOJvYeljHPWogc+NlxNiFNfXWE/gw s5tuKWijOtuA9ZUOgiBEcVuhPIDphb9kWo3W+Bfgzkt+n5NFGUf71/AiLWt5imEg ccQfOVw/GvUg81yGX+IfeH1sFmD6Kr8eIMhn1dGDvDEV47TeYR38aKeKJsYQ+9lA 0sKClrFNii8zus7msnUC5UIvQnz3WExB8oQlfpvx3zFKSdO0CTHm1avZZds8XfOh r+B/tG7pBLEZ1ym9JO1QAbQXE56ogu7FFrcEJYknUc9NWNbqjEdbN07SVGnpk6rR O3wzHYH8+qzBubMnfBRTE1UgemdRqU/WA7ZGTpvwqnD58gOkitwhbFrcPoP9ElF3 3hsnUSbDQb+t2QtY7UFSqJSnVoZ+sajRpllsxyZmUS3q935hXU0/wQuxkHO2r3ey 2whGQlueAfTYYdZe/NAwIBLEAwmR7146CbeY3LVXw+nZXpayo4wF4uSjcFq0LJEw Z0rGLIOHAZnaVkLVa34p+ge2vH081XCzqrUZ2+r6uoFPmItVJOI= =W1WT -----END PGP SIGNATURE----- --XYvPuXLZZSxKH2tj--