ce426f
commit 3375cfafa7961c6ae0e509c31c3b3cef9ad1f03d
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Mon May 23 19:43:09 2016 +0200
ce426f
ce426f
    Make padding in struct sockaddr_storage explicit [BZ #20111]
ce426f
    
ce426f
    This avoids aliasing issues with GCC 6 in -fno-strict-aliasing
ce426f
    mode.  (With implicit padding, not all data is copied.)
ce426f
    
ce426f
    This change makes it explicit that struct sockaddr_storage is
ce426f
    only 126 bytes large on m68k (unlike elsewhere, where we end up
ce426f
    with the requested 128 bytes).  The new test case makes sure that
ce426f
    this does not happen on other architectures.
ce426f
ce426f
[modified by DJ Delorie <dj@redhat.com> for RHEL]
ce426f
ce426f
diff -rupN a/bits/sockaddr.h b/bits/sockaddr.h
ce426f
--- a/bits/sockaddr.h	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ b/bits/sockaddr.h	2017-03-01 16:54:46.606261055 -0500
ce426f
@@ -1,4 +1,4 @@
ce426f
-/* Definition of `struct sockaddr_*' common members.  Generic/4.2 BSD version.
ce426f
+/* Definition of struct sockaddr_* common members and sizes, generic version.
ce426f
    Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -36,4 +36,7 @@ typedef unsigned short int sa_family_t;
ce426f
 
ce426f
 #define __SOCKADDR_COMMON_SIZE	(sizeof (unsigned short int))
ce426f
 
ce426f
+/* Size of struct sockaddr_storage.  */
ce426f
+#define _SS_SIZE 128
ce426f
+
ce426f
 #endif	/* bits/sockaddr.h */
ce426f
diff -rupN a/bits/socket.h b/bits/socket.h
ce426f
--- a/bits/socket.h	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ b/bits/socket.h	2017-03-01 16:38:24.861208175 -0500
ce426f
@@ -133,20 +133,20 @@ struct sockaddr
ce426f
 
ce426f
 
ce426f
 /* Structure large enough to hold any socket address (with the historical
ce426f
-   exception of AF_UNIX).  We reserve 128 bytes.  */
ce426f
+   exception of AF_UNIX).  */
ce426f
 #if ULONG_MAX > 0xffffffff
ce426f
 # define __ss_aligntype	__uint64_t
ce426f
 #else
ce426f
 # define __ss_aligntype	__uint32_t
ce426f
 #endif
ce426f
-#define _SS_SIZE	128
ce426f
-#define _SS_PADSIZE	(_SS_SIZE - (2 * sizeof (__ss_aligntype)))
ce426f
+#define _SS_PADSIZE \
ce426f
+  (_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))
ce426f
 
ce426f
 struct sockaddr_storage
ce426f
   {
ce426f
     __SOCKADDR_COMMON (ss_);	/* Address family, etc.  */
ce426f
-    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
     char __ss_padding[_SS_PADSIZE];
ce426f
+    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
   };
ce426f
 
ce426f
 
ce426f
diff -rupN a/inet/Makefile b/inet/Makefile
ce426f
--- a/inet/Makefile	2017-03-01 16:06:12.000000000 -0500
ce426f
+++ b/inet/Makefile	2017-03-01 16:55:21.919485376 -0500
ce426f
@@ -51,7 +51,7 @@ aux := check_pf check_native ifreq
ce426f
 
ce426f
 tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
ce426f
 	 tst-gethnm test-ifaddrs bug-if1 test-inet6_opt tst-ether_line \
ce426f
-	 tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-deadline
ce426f
+	 tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-deadline tst-sockaddr
ce426f
 
ce426f
 # tst-deadline must be linked statically so that we can access
ce426f
 # internal functions.
ce426f
@@ -89,6 +89,8 @@ CFLAGS-either_hton.c = -fexceptions
ce426f
 CFLAGS-getnetgrent.c = -fexceptions
ce426f
 CFLAGS-getnetgrent_r.c = -fexceptions
ce426f
 
ce426f
+CFLAGS-tst-sockaddr.c = -fno-strict-aliasing
ce426f
+
ce426f
 endif
ce426f
 
ce426f
 ifeq ($(build-static-nss),yes)
ce426f
diff -rupN a/inet/tst-sockaddr.c b/inet/tst-sockaddr.c
ce426f
--- a/inet/tst-sockaddr.c	1969-12-31 19:00:00.000000000 -0500
ce426f
+++ b/inet/tst-sockaddr.c	2017-03-01 16:38:24.869208278 -0500
ce426f
@@ -0,0 +1,125 @@
ce426f
+/* Tests for socket address type definitions.
ce426f
+   Copyright (C) 2016 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public License as
ce426f
+   published by the Free Software Foundation; either version 2.1 of the
ce426f
+   License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; see the file COPYING.LIB.  If
ce426f
+   not, see <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <netinet/in.h>
ce426f
+#include <stdbool.h>
ce426f
+#include <stddef.h>
ce426f
+#include <stdio.h>
ce426f
+#include <stdlib.h>
ce426f
+#include <string.h>
ce426f
+#include <sys/socket.h>
ce426f
+#include <sys/un.h>
ce426f
+
ce426f
+/* This is a copy of the previous definition of struct
ce426f
+   sockaddr_storage.  It is not equal to the old value of _SS_SIZE
ce426f
+   (128) on all architectures.  We must stay compatible with the old
ce426f
+   definition.  */
ce426f
+
ce426f
+#define OLD_REFERENCE_SIZE 128
ce426f
+#define OLD_PADSIZE (OLD_REFERENCE_SIZE - (2 * sizeof (__ss_aligntype)))
ce426f
+struct sockaddr_storage_old
ce426f
+  {
ce426f
+    __SOCKADDR_COMMON (old_);
ce426f
+    __ss_aligntype old_align;
ce426f
+    char old_padding[OLD_PADSIZE];
ce426f
+  };
ce426f
+
ce426f
+static bool errors;
ce426f
+
ce426f
+static void
ce426f
+check (bool ok, const char *message)
ce426f
+{
ce426f
+  if (!ok)
ce426f
+    {
ce426f
+      printf ("error: failed check: %s\n", message);
ce426f
+      errors = true;
ce426f
+    }
ce426f
+}
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  check (OLD_REFERENCE_SIZE >= _SS_SIZE,
ce426f
+         "old target size is not smaller than actual size");
ce426f
+  check (sizeof (struct sockaddr_storage_old)
ce426f
+         == sizeof (struct sockaddr_storage),
ce426f
+         "old and new sizes match");
ce426f
+  check (__alignof (struct sockaddr_storage_old)
ce426f
+         == __alignof (struct sockaddr_storage),
ce426f
+         "old and new alignment matches");
ce426f
+  check (offsetof (struct sockaddr_storage_old, old_family)
ce426f
+         == offsetof (struct sockaddr_storage, ss_family),
ce426f
+         "old and new family offsets match");
ce426f
+  check (sizeof (struct sockaddr_storage) == _SS_SIZE,
ce426f
+         "struct sockaddr_storage size");
ce426f
+
ce426f
+  /* Check for lack of holes in the struct definition.   */
ce426f
+  check (offsetof (struct sockaddr_storage, __ss_padding)
ce426f
+         == __SOCKADDR_COMMON_SIZE,
ce426f
+         "implicit padding before explicit padding");
ce426f
+  check (offsetof (struct sockaddr_storage, __ss_align)
ce426f
+         == __SOCKADDR_COMMON_SIZE
ce426f
+           + sizeof (((struct sockaddr_storage) {}).__ss_padding),
ce426f
+         "implicit padding before explicit padding");
ce426f
+
ce426f
+  /* Check for POSIX compatibility requirements between struct
ce426f
+     sockaddr_storage and struct sockaddr_un.  */
ce426f
+  check (sizeof (struct sockaddr_storage) >= sizeof (struct sockaddr_un),
ce426f
+         "sockaddr_storage is at least as large as sockaddr_un");
ce426f
+  check (__alignof (struct sockaddr_storage)
ce426f
+         >= __alignof (struct sockaddr_un),
ce426f
+         "sockaddr_storage is at least as aligned as sockaddr_un");
ce426f
+  check (offsetof (struct sockaddr_storage, ss_family)
ce426f
+         == offsetof (struct sockaddr_un, sun_family),
ce426f
+         "family offsets match");
ce426f
+
ce426f
+  /* Check that the compiler preserves bit patterns in aggregate
ce426f
+     copies.  Based on <https://gcc.gnu.org/PR71120>.  */
ce426f
+  check (sizeof (struct sockaddr_storage) >= sizeof (struct sockaddr_in),
ce426f
+         "sockaddr_storage is at least as large as sockaddr_in");
ce426f
+  {
ce426f
+    struct sockaddr_storage addr;
ce426f
+    memset (&addr, 0, sizeof (addr));
ce426f
+    {
ce426f
+      struct sockaddr_in *sinp = (struct sockaddr_in *)&addr;
ce426f
+      sinp->sin_family = AF_INET;
ce426f
+      sinp->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
ce426f
+      sinp->sin_port = htons (80);
ce426f
+    }
ce426f
+    struct sockaddr_storage copy;
ce426f
+    copy = addr;
ce426f
+
ce426f
+    struct sockaddr_storage *p = malloc (sizeof (*p));
ce426f
+    if (p == NULL)
ce426f
+      {
ce426f
+        printf ("error: malloc: %m\n");
ce426f
+        return 1;
ce426f
+      }
ce426f
+    *p = copy;
ce426f
+    const struct sockaddr_in *sinp = (const struct sockaddr_in *)p;
ce426f
+    check (sinp->sin_family == AF_INET, "sin_family");
ce426f
+    check (sinp->sin_addr.s_addr == htonl (INADDR_LOOPBACK), "sin_addr");
ce426f
+    check (sinp->sin_port == htons (80), "sin_port");
ce426f
+    free (p);
ce426f
+  }
ce426f
+
ce426f
+  return errors;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"
ce426f
diff -rupN a/sysdeps/mach/hurd/bits/socket.h b/sysdeps/mach/hurd/bits/socket.h
ce426f
--- a/sysdeps/mach/hurd/bits/socket.h	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ b/sysdeps/mach/hurd/bits/socket.h	2017-03-01 16:38:24.873208329 -0500
ce426f
@@ -156,20 +156,20 @@ struct sockaddr
ce426f
 
ce426f
 
ce426f
 /* Structure large enough to hold any socket address (with the historical
ce426f
-   exception of AF_UNIX).  We reserve 128 bytes.  */
ce426f
+   exception of AF_UNIX).  */
ce426f
 #if ULONG_MAX > 0xffffffff
ce426f
 # define __ss_aligntype	__uint64_t
ce426f
 #else
ce426f
 # define __ss_aligntype	__uint32_t
ce426f
 #endif
ce426f
-#define _SS_SIZE	128
ce426f
-#define _SS_PADSIZE	(_SS_SIZE - (2 * sizeof (__ss_aligntype)))
ce426f
+#define _SS_PADSIZE \
ce426f
+  (_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))
ce426f
 
ce426f
 struct sockaddr_storage
ce426f
   {
ce426f
     __SOCKADDR_COMMON (ss_);	/* Address family, etc.  */
ce426f
-    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
     char __ss_padding[_SS_PADSIZE];
ce426f
+    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
   };
ce426f
 
ce426f
 
ce426f
diff -rupN a/sysdeps/unix/bsd/bsd4.4/bits/sockaddr.h b/sysdeps/unix/bsd/bsd4.4/bits/sockaddr.h
ce426f
--- a/sysdeps/unix/bsd/bsd4.4/bits/sockaddr.h	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ b/sysdeps/unix/bsd/bsd4.4/bits/sockaddr.h	2017-03-01 17:30:34.962261748 -0500
ce426f
@@ -37,6 +37,9 @@ typedef unsigned char sa_family_t;
ce426f
 
ce426f
 #define __SOCKADDR_COMMON_SIZE	(2 * sizeof (unsigned char))
ce426f
 
ce426f
+/* Size of struct sockaddr_storage.  */
ce426f
+#define _SS_SIZE	128
ce426f
+
ce426f
 #define _HAVE_SA_LEN	1	/* We have the sa_len field.  */
ce426f
 
ce426f
 #endif	/* bits/sockaddr.h */
ce426f
diff -rupN a/sysdeps/unix/bsd/bsd4.4/bits/socket.h b/sysdeps/unix/bsd/bsd4.4/bits/socket.h
ce426f
--- a/sysdeps/unix/bsd/bsd4.4/bits/socket.h	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ b/sysdeps/unix/bsd/bsd4.4/bits/socket.h	2017-03-01 17:31:23.790246360 -0500
ce426f
@@ -142,14 +142,13 @@ struct sockaddr
ce426f
 #else
ce426f
 # define __ss_aligntype	__uint32_t
ce426f
 #endif
ce426f
-#define _SS_SIZE	128
ce426f
-#define _SS_PADSIZE	(_SS_SIZE - (2 * sizeof (__ss_aligntype)))
ce426f
+#define _SS_PADSIZE	(_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))
ce426f
 
ce426f
 struct sockaddr_storage
ce426f
   {
ce426f
     __SOCKADDR_COMMON (ss_);	/* Address family, etc.  */
ce426f
-    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
     char __ss_padding[_SS_PADSIZE];
ce426f
+    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
   };
ce426f
 
ce426f
 
ce426f
diff -rupN a/sysdeps/unix/sysv/linux/bits/socket.h b/sysdeps/unix/sysv/linux/bits/socket.h
ce426f
--- a/sysdeps/unix/sysv/linux/bits/socket.h	2017-03-01 16:06:12.000000000 -0500
ce426f
+++ b/sysdeps/unix/sysv/linux/bits/socket.h	2017-03-01 16:38:26.993235460 -0500
ce426f
@@ -155,16 +155,16 @@ struct sockaddr
ce426f
 
ce426f
 
ce426f
 /* Structure large enough to hold any socket address (with the historical
ce426f
-   exception of AF_UNIX).  We reserve 128 bytes.  */
ce426f
+   exception of AF_UNIX).  */
ce426f
 #define __ss_aligntype	unsigned long int
ce426f
-#define _SS_SIZE	128
ce426f
-#define _SS_PADSIZE	(_SS_SIZE - (2 * sizeof (__ss_aligntype)))
ce426f
+#define _SS_PADSIZE \
ce426f
+  (_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))
ce426f
 
ce426f
 struct sockaddr_storage
ce426f
   {
ce426f
     __SOCKADDR_COMMON (ss_);	/* Address family, etc.  */
ce426f
-    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
     char __ss_padding[_SS_PADSIZE];
ce426f
+    __ss_aligntype __ss_align;	/* Force desired alignment.  */
ce426f
   };
ce426f
 
ce426f