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