This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/4381] New: unaligned memory access in gethostbyname_r()


The third argument of gethostbyname_r() is supposed to be a buffer of type char 
*buf, with no particular memory alignment.

This buffer is then casted into a (struct host_data *). This causes a SIGBUS on 
architecture that does not support unaligned memory access.

As it is not possible to change the API, please find a small patch below, which 
align the buffer before using it. The drawback of this method is that the 
buffer may be a bit smaller.


--- resolv/nss_dns/dns-host.c.orig      2007-04-16 00:13:12.000000000 +0200
+++ resolv/nss_dns/dns-host.c   2007-04-16 00:34:41.000000000 +0200
@@ -78,6 +78,7 @@
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
+#include <obstack.h>
 #include <sys/syslog.h>

 #include "nsswitch.h"
@@ -465,8 +466,8 @@
     char *aliases[MAX_NR_ALIASES];
     unsigned char host_addr[16];       /* IPv4 or IPv6 */
     char *h_addr_ptrs[0];
-  } *host_data = (struct host_data *) buffer;
-  int linebuflen = buflen - sizeof (struct host_data);
+  } *host_data;
+  int linebuflen;
   register const HEADER *hp;
   const u_char *end_of_message, *cp;
   int n, ancount, qdcount;
@@ -479,6 +480,10 @@
   int have_to_map = 0;
   int32_t ttl = 0;

+  /* Align the buffer. */
+  host_data = (struct host_data *) __PTR_ALIGN(0, buffer, sizeof(char*) - 1);
+  linebuflen = buflen - sizeof (struct host_data) - ((char *)host_data - 
buffer);
+
   if (__builtin_expect (linebuflen, 0) < 0)
     {
       /* The buffer is too small.  */

-- 
           Summary: unaligned memory access in gethostbyname_r()
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: aurelien at aurel32 dot net
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: sparc-unknown-linux-gnu
  GCC host triplet: sparc-unknown-linux-gnu
GCC target triplet: sparc-unknown-linux-gnu


http://sourceware.org/bugzilla/show_bug.cgi?id=4381

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]