diff mbox

Fix misaligned access accessing HEADER struct in res_query.c

Message ID 5E7CF115-219A-4260-BE53-19B5A8D1D7F3@bell.net
State New
Headers show

Commit Message

John David Anglin June 15, 2016, 11:42 a.m. UTC
The attached patch fixes BZ 20243.  The HEADER struct needs to be packed so that
byte accesses are used.  Tested using glibc 2.22-11on hppa.  Please install if okay.

Dave
--
John David Anglin	dave.anglin@bell.net
2016-06-16  John David Anglin  <danglin@gcc.gnu.org>

	[BZ 20243]
	* resolv/arpa/nameser_compat.h (HEADER): Use attribute packed.

Comments

Florian Weimer June 15, 2016, 11:56 a.m. UTC | #1
On 06/15/2016 01:42 PM, John David Anglin wrote:
> The attached patch fixes BZ 20243.  The HEADER struct needs to be packed so that
> byte accesses are used.  Tested using glibc 2.22-11on hppa.  Please install if okay.

> -} HEADER;
> +} __attribute__ ((packed)) HEADER;

Unfortunately, we cannot apply this simple fix because this is a public 
header, and the above changes alignment of the struct.  It would also 
have to be __attribute__ ((__packed__)).

Florian
Andreas Schwab June 15, 2016, 12:16 p.m. UTC | #2
Florian Weimer <fweimer@redhat.com> writes:

> On 06/15/2016 01:42 PM, John David Anglin wrote:
>> The attached patch fixes BZ 20243.  The HEADER struct needs to be packed so that
>> byte accesses are used.  Tested using glibc 2.22-11on hppa.  Please install if okay.
>
>> -} HEADER;
>> +} __attribute__ ((packed)) HEADER;
>
> Unfortunately, we cannot apply this simple fix because this is a public
> header, and the above changes alignment of the struct.  It would also have
> to be __attribute__ ((__packed__)).

It also needs to work with other compilers.

Andreas.
John David Anglin June 15, 2016, 2:40 p.m. UTC | #3
On 2016-06-15 8:16 AM, Andreas Schwab wrote:
> Florian Weimer<fweimer@redhat.com>  writes:
>
>> >On 06/15/2016 01:42 PM, John David Anglin wrote:
>>> >>The attached patch fixes BZ 20243.  The HEADER struct needs to be packed so that
>>> >>byte accesses are used.  Tested using glibc 2.22-11on hppa.  Please install if okay.
>> >
>>> >>-} HEADER;
>>> >>+} __attribute__ ((packed)) HEADER;
>> >
>> >Unfortunately, we cannot apply this simple fix because this is a public
>> >header, and the above changes alignment of the struct.  It would also have
>> >to be __attribute__ ((__packed__)).
> It also needs to work with other compilers.

The packed attribute is used in several places in glibc.  Maybe a packed 
derivative of HEADER
can be used in res_query.c to avoid the above issue?

I don't think the packed derivative affects the layout of HEADER given 
the way the fields are
arranged.

Dave
Florian Weimer June 15, 2016, 2:43 p.m. UTC | #4
On 06/15/2016 04:40 PM, John David Anglin wrote:
> On 2016-06-15 8:16 AM, Andreas Schwab wrote:
>> Florian Weimer<fweimer@redhat.com>  writes:
>>
>>> >On 06/15/2016 01:42 PM, John David Anglin wrote:
>>>> >>The attached patch fixes BZ 20243.  The HEADER struct needs to be
>>>> packed so that
>>>> >>byte accesses are used.  Tested using glibc 2.22-11on hppa.
>>>> Please install if okay.
>>> >
>>>> >>-} HEADER;
>>>> >>+} __attribute__ ((packed)) HEADER;


> I don't think the packed derivative affects the layout of HEADER given
> the way the fields are
> arranged.

I verified that it changes alignment, even on x86_64.  That's the 
intent, and it results in a layout change (if this struct is used as a 
part of other structs, for example).

Florian
John David Anglin June 15, 2016, 2:58 p.m. UTC | #5
On 2016-06-15 10:43 AM, Florian Weimer wrote:
>> I don't think the packed derivative affects the layout of HEADER given
>> the way the fields are
>> arranged.
>
> I verified that it changes alignment, even on x86_64.  That's the 
> intent, and it results in a layout change (if this struct is used as a 
> part of other structs, for example).

I'm suggesting this be done in res_query.c where we we need byte 
alignment.  Nothing else would
have access to this typedef, so it couldn't affect the layout of any 
other structs.

Dave
diff mbox

Patch

diff --git a/resolv/arpa/nameser_compat.h b/resolv/arpa/nameser_compat.h
index d59c9e4..6c0f69c 100644
--- a/resolv/arpa/nameser_compat.h
+++ b/resolv/arpa/nameser_compat.h
@@ -80,7 +80,7 @@  typedef struct {
 	unsigned	ancount :16;	/*%< number of answer entries */
 	unsigned	nscount :16;	/*%< number of authority entries */
 	unsigned	arcount :16;	/*%< number of resource entries */
-} HEADER;
+} __attribute__ ((packed)) HEADER;
 
 #define PACKETSZ	NS_PACKETSZ
 #define MAXDNAME	NS_MAXDNAME