public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: John David Anglin <dave.anglin@bell.net>, libc-alpha@sourceware.org
Subject: Re: [PATCH v4] Fix misaligned accesses to fields in HEADER struct defined in <arpa/nameser_compat.h>
Date: Fri, 18 Mar 2022 14:37:33 -0300	[thread overview]
Message-ID: <c8006da1-a98b-1cbf-2a08-eab33c9b7b37@linaro.org> (raw)
In-Reply-To: <YjO+e+33ZoHL8EbV@mx3210.localdomain>



On 17/03/2022 20:04, 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.
> 
> 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 <string.h>
>  #include <shlib-compat.h>
>  
> +/* 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;
> +

Instead of replicate it on both files, I think a better strategy would to
move it to resolv/resolv-internal.h.

>  #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 = 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 +225,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 +236,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 +346,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..24e98f0bfe 100644
> --- a/resolv/res_send.c
> +++ b/resolv/res_send.c
> @@ -111,6 +111,15 @@
>  #include <libc-diag.h>
>  #include <random-bits.h>
>  
> +/* 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
> +

I think it would be better to just do what you did on res_query.c and
replace all required HEADER usage with UHEADER.

>  #if PACKETSZ > 65536
>  #define MAXPACKET       PACKETSZ
>  #else

      reply	other threads:[~2022-03-18 17:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 23:04 John David Anglin
2022-03-18 17:37 ` Adhemerval Zanella [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c8006da1-a98b-1cbf-2a08-eab33c9b7b37@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=dave.anglin@bell.net \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).