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