From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 6935D385842E for ; Tue, 22 Mar 2022 12:05:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6935D385842E Received: by mail-ot1-x331.google.com with SMTP id y3-20020a056830070300b005cd9c4d03feso1516270ots.3 for ; Tue, 22 Mar 2022 05:05:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=edFgeDNSyYbfdKA18DD+sQZ+BMeHwuxIE2/2ZpartjE=; b=lt7rMmY9++x/zuLOXmca8cd9S8MCE+t7UxWq2yDGrhmwozdP5HlDw+45KqJQuCTV5D jTiS/eGiA2G7br72O6YjBZn9zPWwkQ+46q6DSw8jbRe152FzXjPvHWfcVlZzU+qDQOR6 WmfpDmg5OhYpNimZnUmnQ3l2P1I1Vu0V9JyjEWaKo2QAHV9dzYqyYFqrr/MxQP+WvIes uK0ZjzOQr8f/SnI2j7wqblSreBBnBQfKLK85Zb4DIGX70aaEpFyQqRsCHMYyhDes18b3 LCqmidEgUvP8+KP1n6YDA17NxSrqvFTfWhK90O4IOXSIPwnCCumfDu7jx8gr9a8ETuAH gnTg== X-Gm-Message-State: AOAM532IF+p4HDsj1amrk6VN3aCJLTGeDfyMgwnHzyiF/P1JfluQSyz2 +IVS+0D3yF+cQPKNx+5ynAIkeQ== X-Google-Smtp-Source: ABdhPJwNOgaUcfJyqc3NKu+X67fwa7ImeuNXOLOLFC+r4Gf9grUrtbRwhrdYoHvTATgcgLjkxknQ0g== X-Received: by 2002:a9d:454c:0:b0:5b2:3757:9fc6 with SMTP id p12-20020a9d454c000000b005b237579fc6mr9631390oti.314.1647950726632; Tue, 22 Mar 2022 05:05:26 -0700 (PDT) Received: from ?IPV6:2804:431:c7ca:2d55:3e27:6bd1:b1d4:af84? ([2804:431:c7ca:2d55:3e27:6bd1:b1d4:af84]) by smtp.gmail.com with ESMTPSA id y143-20020aca4b95000000b002d9b530a96esm8738575oia.44.2022.03.22.05.05.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 22 Mar 2022 05:05:25 -0700 (PDT) Message-ID: <91065257-88f0-ec87-bee6-d3f87b451dff@linaro.org> Date: Tue, 22 Mar 2022 09:05:22 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v5] Fix misaligned accesses to fields in HEADER struct defined in Content-Language: en-US To: John David Anglin , libc-alpha@sourceware.org References: From: Adhemerval Zanella In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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: Tue, 22 Mar 2022 12:05:29 -0000 On 20/03/2022 18:46, John David Anglin wrote: > 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 LGTM, thanks. Reviewed-by: Adhemerval Zanella > --- > > 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 = ctx->resp; > - HEADER *hp = (HEADER *) answer; > - HEADER *hp2; > + UHEADER *hp = (UHEADER *) answer; > + UHEADER *hp2; > int n, use_malloc = 0; > > size_t bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * QUERYSIZE; > @@ -217,7 +217,7 @@ __res_context_query (struct resolv_context *ctx, const char *name, > > if (answerp != NULL) > /* __res_context_send might have reallocated the buffer. */ > - hp = (HEADER *) *answerp; > + hp = (UHEADER *) *answerp; > > /* 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 = (HEADER *) *answerp2; > + hp2 = (UHEADER *) *answerp2; > if (n < (int) sizeof (HEADER)) > { > hp = hp2; > @@ -338,7 +338,7 @@ __res_context_search (struct resolv_context *ctx, > { > struct __res_state *statp = ctx->resp; > const char *cp; > - HEADER *hp = (HEADER *) answer; > + UHEADER *hp = (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 = 0; > + ((UHEADER *) buf)->ad = 0; > } > > 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 = (HEADER *) buf; > - const HEADER *hp2 = (HEADER *) buf2; > - HEADER *anhp = (HEADER *) *ansp; > + const UHEADER *hp = (UHEADER *) buf; > + const UHEADER *hp2 = (UHEADER *) buf2; > + UHEADER *anhp = (UHEADER *) *ansp; > struct sockaddr *nsap = __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 = ansp2; > thisresplenp = resplen2; > } > - anhp = (HEADER *) *thisansp; > + anhp = (UHEADER *) *thisansp; > > *thisresplenp = rlen; > /* Is the answer buffer too small? */ > @@ -718,7 +718,7 @@ send_vc(res_state statp, > *thisansp = newp; > if (thisansp == ansp2) > *ansp2_malloced = 1; > - anhp = (HEADER *) newp; > + anhp = (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 = (HEADER *) buf; > - const HEADER *hp2 = (HEADER *) buf2; > + const UHEADER *hp = (UHEADER *) buf; > + const UHEADER *hp2 = (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 = (HEADER *) *thisansp; > + UHEADER *anhp = (UHEADER *) *thisansp; > socklen_t fromlen = sizeof(struct sockaddr_in6); > assert (sizeof(from) <= fromlen); > *thisresplenp = __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. */ > > +/* 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