From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from cmx-mtlrgo002.bell.net (mta-mtl-002.bell.net [209.71.208.12]) by sourceware.org (Postfix) with ESMTP id 158EB3858C83 for ; Sun, 20 Mar 2022 21:46:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 158EB3858C83 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: 621D9CE90236F8C6 X-CM-Envelope: MS4xfDmBQ9ZMWzLyooj9QIp67jV2xhCmTXtg5XxuxDN0xYe02bpjf7G0+pqnqm5kd8LKbbQ87xBSlmPRYVR0H9UASPFMjoga4Z+hEKhOQpRHsy8Y/HTtJIW4 BqBz8g/Xh38ZDmTROmgR8F7AU7gP/Ct0THCHa4sZISXSjDk/GiAkuQ4UHm9AMjxWDstdakCLG4KovcYcgZrNWODA4G+EQGFe232U22zkk3hEfm4jGIk6evFF KOss6U+6YA6rEpimXMdh95kW2b36RUc91lLWIizUuNlxxrNtPQfU9FsQCDm7aJH5rXIpv8JV+0PpnQYsTgai/VxuPoNV+9P4o0FWSwB9bWPS8JhnM5PMMuhd QDOWbJFVvdPPs1KZ6qkzNwsIC9P9rpIvkPXL/002LXmtjkS1jAngbqa4blNpanTe4LGylIfb/eDL7jx3b5R8WIFU7MvAclCeZRd+BgOKP6mwdXD40teV0MjO 8A+pe2w/M1hUGCqbfU5q+/mEm6N++FDVKOkCRc31iIoq83DQ1hbZdFFAIz0= X-CM-Analysis: v=2.4 cv=aKWTFZxm c=1 sm=1 tr=0 ts=6237a0b4 a=9k1bCY7nR7m1ZFzoCuQ56g==:117 a=9k1bCY7nR7m1ZFzoCuQ56g==:17 a=o8Y5sQTvuykA:10 a=Fk7lMGSkRLm671ICTooA:9 a=CjuIK1q_8ugA:10 a=ssDyWtRf1zN5D6Sr_fEA:9 a=FfaGCDsud1wA:10 Received: from mx3210.localdomain (70.50.7.94) by cmx-mtlrgo002.bell.net (5.8.807) (authenticated as dave.anglin@bell.net) id 621D9CE90236F8C6; Sun, 20 Mar 2022 17:46:28 -0400 Received: by mx3210.localdomain (Postfix, from userid 1000) id D21B3220116; Sun, 20 Mar 2022 21:46:27 +0000 (UTC) Date: Sun, 20 Mar 2022 21:46:27 +0000 From: John David Anglin To: libc-alpha@sourceware.org Subject: [PATCH v5] 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="rry4xi7DZxcZj4/R" Content-Disposition: inline X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, 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: Sun, 20 Mar 2022 21:46:40 -0000 --rry4xi7DZxcZj4/R 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. V5: Move UHEADER typedef to resolv/resolv-internal.h. Replace all HEADER usage with UHEADER in resolv/res_send.c. Dave --- diff --git a/resolv/res_query.c b/resolv/res_query.c index 5d0a68dc81..3b5c604261 100644 --- a/resolv/res_query.c +++ b/resolv/res_query.c @@ -112,8 +112,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 +217,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 +228,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 +338,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..d6c85fd7a2 100644 --- a/resolv/res_send.c +++ b/resolv/res_send.c @@ -245,7 +245,7 @@ static void mask_ad_bit (struct resolv_context *ctx, void *buf) { if (!(ctx->resp->options & RES_TRUSTAD)) - ((HEADER *) buf)->ad =3D 0; + ((UHEADER *) buf)->ad =3D 0; } =20 int @@ -560,9 +560,9 @@ send_vc(res_state statp, int *terrno, int ns, u_char **anscp, u_char **ansp2, int *anssizp2, int *resplen2, int *ansp2_malloced) { - const HEADER *hp =3D (HEADER *) buf; - const HEADER *hp2 =3D (HEADER *) buf2; - HEADER *anhp =3D (HEADER *) *ansp; + const UHEADER *hp =3D (UHEADER *) buf; + const UHEADER *hp2 =3D (UHEADER *) buf2; + UHEADER *anhp =3D (UHEADER *) *ansp; struct sockaddr *nsap =3D __res_get_nsaddr (statp, ns); int truncating, connreset, n; /* On some architectures compiler might emit a warning indicating @@ -697,7 +697,7 @@ send_vc(res_state statp, thisansp =3D ansp2; thisresplenp =3D resplen2; } - anhp =3D (HEADER *) *thisansp; + anhp =3D (UHEADER *) *thisansp; =20 *thisresplenp =3D rlen; /* Is the answer buffer too small? */ @@ -718,7 +718,7 @@ send_vc(res_state statp, *thisansp =3D newp; if (thisansp =3D=3D ansp2) *ansp2_malloced =3D 1; - anhp =3D (HEADER *) newp; + anhp =3D (UHEADER *) newp; /* A uint16_t can't be larger than MAXPACKET thus it's safe to allocate MAXPACKET but read RLEN bytes instead. */ @@ -925,8 +925,8 @@ send_dg(res_state statp, int *terrno, int ns, int *v_circuit, int *gotsomewhere, u_char **anscp, u_char **ansp2, int *anssizp2, int *resplen2, int *ansp2_malloced) { - const HEADER *hp =3D (HEADER *) buf; - const HEADER *hp2 =3D (HEADER *) buf2; + const UHEADER *hp =3D (UHEADER *) buf; + const UHEADER *hp2 =3D (UHEADER *) buf2; struct timespec now, timeout, finish; struct pollfd pfd[1]; int ptimeout; @@ -1168,7 +1168,7 @@ send_dg(res_state statp, MSG_TRUNC which is only available on Linux. We can abstract out the Linux-specific feature in the future to detect truncation. */ - HEADER *anhp =3D (HEADER *) *thisansp; + UHEADER *anhp =3D (UHEADER *) *thisansp; socklen_t fromlen =3D sizeof(struct sockaddr_in6); assert (sizeof(from) <=3D fromlen); *thisresplenp =3D __recvfrom (pfd[0].fd, (char *) *thisansp, diff --git a/resolv/resolv-internal.h b/resolv/resolv-internal.h index 2fa57d394f..9d2e832d68 100644 --- a/resolv/resolv-internal.h +++ b/resolv/resolv-internal.h @@ -27,6 +27,13 @@ #define RES_F_CONN 0x00000002 /* Socket is connected. */ #define RES_F_EDNS0ERR 0x00000004 /* EDNS0 caused errors. */ =20 +/* The structure HEADER is normally aligned on a word boundary. In + some code, we need to access this structure when it may be aligned + on a byte boundary. To avoid unaligned accesses, we need a typedef + with alignment one. This ensures the fields are accessed with byte + loads and stores. */ +typedef HEADER __attribute__ ((__aligned__(1))) UHEADER; + /* Legacy function. This needs to be removed once all NSS modules have been adjusted. */ static inline bool --rry4xi7DZxcZj4/R Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEnRzl+6e9+DTrEhyEXb/Nrl8ZTfEFAmI3oK0ACgkQXb/Nrl8Z TfH3qA//Qdxj0PngnG+cbyBq75p3QExaigmouDvx5iknK6X4NCVwSQQH8oJa6/LB ZRrH7jaV3ZYeg+Wg6Daupc+tkNKTknEoP0SHedEe0xsFQRt7EIfZVT/i5iJF5Ajs qFkunfCpXl92zR1jaFh0Rw13qJxSK5Li8vhRO07jMYgZoyGZVmu+ptf3QXA8xxJq ih/vmNREc/hVH2/AUa7E/PPLKi9pcn689RgPoowg43hFWzSNbI3dZpiU0vxoagDw 35ZZj2rwRuaurZFq31NCqrqtdrLaaNi1q+kjIw78kR+DfMfi/0U4yKtxyNpafQXK 3Bcq5KoK9SatX0maBkGfyDDSTtEkrhVj+WEBnklkxN4GU169cppD+j99NC+Wcg3f xYjcS7eYJaJiLKoxbYLPjfhjr7bFovkZPCIeJvIE4LdHuknZ2KVUTxsLiCWlVFjT zIxXzAdBS2TnHSr//eJQsllGhsVGEn6hogoVkMathx4IZACOXqHYy+pwjLFRxq7x rQt82CmniOxKyRak3Z6Ka/87UTsTGkwI9onbOyrexpoa0wBIRr6XIQ5/k4GkyIeB KPPQh6cnJeeBvz6ST+e+0u524j20Lubx5mMCZr0IYIc300AmbOXSLQCxvp5/Fr25 1CyLw9UGkQG3HHcZsxxAYWq93pwL840hmHa+AxG5zWgq5H0OLP8= =6sJH -----END PGP SIGNATURE----- --rry4xi7DZxcZj4/R--