public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-03-01 17:35 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-01 17:35 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=65fe20db90da9da7c9cc710467e7941ca1059bbe

commit 65fe20db90da9da7c9cc710467e7941ca1059bbe
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |   5 +
 sysdeps/unix/sysv/linux/Makefile                   |   2 +-
 sysdeps/unix/sysv/linux/Versions                   |   1 +
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |   7 ++
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 102 +++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               |  12 +++
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |   7 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |   7 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |   7 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  |  23 +++--
 sysdeps/unix/sysv/linux/setsockopt.c               |  16 +++-
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |   7 ++
 12 files changed, 188 insertions(+), 8 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 488114a41e..1f99f00a82 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index d4e9979605..d1054eda1f 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -53,3 +53,10 @@
 #define SO_SNDTIMEO_OLD 21
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c7fda26ee
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,102 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+# if !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
+#  include <bits/socket-constants.h>
+# endif
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case SO_TIMESTAMPNS_OLD:
+	  type = SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index a093e42f96..0cdf6cd011 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -72,6 +72,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index 2413529e71..aa09ab5958 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 16448
 #define SO_SNDTIMEO_NEW 16449
+
+#define SO_TIMESTAMP_OLD 0x4012
+#define SO_TIMESTAMPNS_OLD 0x4013
+#define SO_TIMESTAMPING_OLD 0x4020
+#define SO_TIMESTAMP_NEW 0x4038
+#define SO_TIMESTAMPNS_NEW 0x4039
+#define SO_TIMESTAMPING_NEW 0x403A
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index 2c124234fc..4a5d235aed 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index 682a3f4608..ff42df6f5f 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 19
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index a1f98a937d..59a53d2d2d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -19,7 +19,9 @@
 /* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
    however the new constants that describe 64-bit time support were added
    only on v5.1.  */
-#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD) \
+    || !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
 # include <bits/socket-constants.h>
 #endif
 #include <time.h>
@@ -74,6 +76,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index 3eae2176dd..bfd7eef233 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 16384
 #define SO_RCVTIMEO_NEW 68
 #define SO_SNDTIMEO_NEW 69
+
+#define SO_TIMESTAMP_OLD 0x001d
+#define SO_TIMESTAMPNS_OLD 0x0021
+#define SO_TIMESTAMPING_OLD 0x0023
+#define SO_TIMESTAMP_NEW 0x0046
+#define SO_TIMESTAMPNS_NEW 0x0042
+#define SO_TIMESTAMPING_NEW 0x0043


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-06-08 20:48 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-06-08 20:48 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=aee60ad0b1d50b6b88cd96364894c878996d975b

commit aee60ad0b1d50b6b88cd96364894c878996d975b
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).
    
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
    Tested-by: Carlos O'Donell <carlos@redhat.com>

Diff:
---
 include/sys/socket.h                               |  5 ++
 sysdeps/unix/sysv/linux/Makefile                   |  2 +-
 sysdeps/unix/sysv/linux/Versions                   |  1 +
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 96 ++++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               | 12 +++
 .../unix/sysv/linux/hppa/socket-constants-time64.h |  5 ++
 .../unix/sysv/linux/mips/socket-constants-time64.h |  5 ++
 .../sysv/linux/powerpc/socket-constants-time64.h   |  5 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  | 23 ++++--
 sysdeps/unix/sysv/linux/setsockopt.c               | 12 +++
 sysdeps/unix/sysv/linux/socket-constants-time64.h  |  5 ++
 .../sysv/linux/sparc/socket-constants-time64.h     |  5 ++
 12 files changed, 169 insertions(+), 7 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index bc14f20274..36d5ae020c 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c864ad38ca..4637fd651d 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -194,6 +194,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c123c28ce
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,96 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+# include <socket-constants-time64.h>
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returned control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In either case it is two uint64_t
+     members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case COMPAT_SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = COMPAT_SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case COMPAT_SO_TIMESTAMPNS_OLD:
+	  type = COMPAT_SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 688a7de087..f86b06dec6 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -70,6 +70,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	else
 	  memcpy (optval, &tv32, sizeof tv32);
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
index ea721e0fc2..f3b98012d5 100644
--- a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 16448
 #define COMPAT_SO_SNDTIMEO_NEW 16449
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x4012
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x4013
+#define COMPAT_SO_TIMESTAMP_NEW 0x4038
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x4039
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
index ab8bd62853..31fa69fa9f 100644
--- a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
index 1e48dcca8d..889251895b 100644
--- a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 6505202265..a4780a9d33 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -69,6 +69,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
index e5a3777f28..7f7ca05504 100644
--- a/sysdeps/unix/sysv/linux/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
index b137abdeea..56358923e1 100644
--- a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 68
 #define COMPAT_SO_SNDTIMEO_NEW 69
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x001d
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x0021
+#define COMPAT_SO_TIMESTAMP_NEW 0x0046
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x0042
+
 #endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-05-26 16:24 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-05-26 16:24 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=e468528be4560f0119fa183e329ad831d6e847cf

commit e468528be4560f0119fa183e329ad831d6e847cf
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |  5 ++
 sysdeps/unix/sysv/linux/Makefile                   |  2 +-
 sysdeps/unix/sysv/linux/Versions                   |  1 +
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 96 ++++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               | 12 +++
 .../unix/sysv/linux/hppa/socket-constants-time64.h |  5 ++
 .../unix/sysv/linux/mips/socket-constants-time64.h |  5 ++
 .../sysv/linux/powerpc/socket-constants-time64.h   |  5 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  | 23 ++++--
 sysdeps/unix/sysv/linux/setsockopt.c               | 12 +++
 sysdeps/unix/sysv/linux/socket-constants-time64.h  |  5 ++
 .../sysv/linux/sparc/socket-constants-time64.h     |  5 ++
 12 files changed, 169 insertions(+), 7 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index e9566e028a..920b627f8b 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index 220bb2dffe..148f04c50a 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c123c28ce
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,96 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+# include <socket-constants-time64.h>
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returned control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In either case it is two uint64_t
+     members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case COMPAT_SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = COMPAT_SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case COMPAT_SO_TIMESTAMPNS_OLD:
+	  type = COMPAT_SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index c8e502d976..14b782d0da 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -67,6 +67,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
index ea721e0fc2..f3b98012d5 100644
--- a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 16448
 #define COMPAT_SO_SNDTIMEO_NEW 16449
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x4012
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x4013
+#define COMPAT_SO_TIMESTAMP_NEW 0x4038
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x4039
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
index ab8bd62853..31fa69fa9f 100644
--- a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
index 1e48dcca8d..889251895b 100644
--- a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 6505202265..a4780a9d33 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -69,6 +69,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
index e5a3777f28..7f7ca05504 100644
--- a/sysdeps/unix/sysv/linux/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
index b137abdeea..56358923e1 100644
--- a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 68
 #define COMPAT_SO_SNDTIMEO_NEW 69
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x001d
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x0021
+#define COMPAT_SO_TIMESTAMP_NEW 0x0046
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x0042
+
 #endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-03-05 19:20 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-05 19:20 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=9143d1b7b524e0e91a3d9f260520d386762d2f0a

commit 9143d1b7b524e0e91a3d9f260520d386762d2f0a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |  5 ++
 sysdeps/unix/sysv/linux/Makefile                   |  2 +-
 sysdeps/unix/sysv/linux/Versions                   |  1 +
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 96 ++++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               | 12 +++
 .../unix/sysv/linux/hppa/socket-constants-time64.h |  5 ++
 .../unix/sysv/linux/mips/socket-constants-time64.h |  5 ++
 .../sysv/linux/powerpc/socket-constants-time64.h   |  5 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  | 23 ++++--
 sysdeps/unix/sysv/linux/setsockopt.c               | 12 +++
 sysdeps/unix/sysv/linux/socket-constants-time64.h  |  5 ++
 .../sysv/linux/sparc/socket-constants-time64.h     |  5 ++
 12 files changed, 169 insertions(+), 7 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 7b3bb30da2..6743936b5a 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c123c28ce
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,96 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+# include <socket-constants-time64.h>
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returned control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In either case it is two uint64_t
+     members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case COMPAT_SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = COMPAT_SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case COMPAT_SO_TIMESTAMPNS_OLD:
+	  type = COMPAT_SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index c8e502d976..14b782d0da 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -67,6 +67,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
index ea721e0fc2..f3b98012d5 100644
--- a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 16448
 #define COMPAT_SO_SNDTIMEO_NEW 16449
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x4012
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x4013
+#define COMPAT_SO_TIMESTAMP_NEW 0x4038
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x4039
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
index ab8bd62853..31fa69fa9f 100644
--- a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
index 1e48dcca8d..889251895b 100644
--- a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 6505202265..a4780a9d33 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -69,6 +69,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
index e5a3777f28..7f7ca05504 100644
--- a/sysdeps/unix/sysv/linux/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
index b137abdeea..56358923e1 100644
--- a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 68
 #define COMPAT_SO_SNDTIMEO_NEW 69
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x001d
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x0021
+#define COMPAT_SO_TIMESTAMP_NEW 0x0046
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x0042
+
 #endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-03-04 17:36 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-04 17:36 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=80fb49149543efd81bdf2e310dcf3d367a740724

commit 80fb49149543efd81bdf2e310dcf3d367a740724
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |   5 +
 sysdeps/unix/sysv/linux/Makefile                   |   2 +-
 sysdeps/unix/sysv/linux/Versions                   |   1 +
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 102 +++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               |  12 +++
 .../unix/sysv/linux/hppa/socket-constants-time64.h |   5 +
 .../unix/sysv/linux/mips/socket-constants-time64.h |   5 +
 .../sysv/linux/powerpc/socket-constants-time64.h   |   5 +
 sysdeps/unix/sysv/linux/recvmsg.c                  |  23 +++--
 sysdeps/unix/sysv/linux/setsockopt.c               |  12 +++
 sysdeps/unix/sysv/linux/socket-constants-time64.h  |   5 +
 .../sysv/linux/sparc/socket-constants-time64.h     |   5 +
 12 files changed, 175 insertions(+), 7 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 51e28b97ac..b5d785e083 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c7fda26ee
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,102 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+# if !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
+#  include <bits/socket-constants.h>
+# endif
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case SO_TIMESTAMPNS_OLD:
+	  type = SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index c8e502d976..14b782d0da 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -67,6 +67,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
index ea721e0fc2..f3b98012d5 100644
--- a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 16448
 #define COMPAT_SO_SNDTIMEO_NEW 16449
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x4012
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x4013
+#define COMPAT_SO_TIMESTAMP_NEW 0x4038
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x4039
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
index ab8bd62853..31fa69fa9f 100644
--- a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
index 1e48dcca8d..889251895b 100644
--- a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 6505202265..a4780a9d33 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -69,6 +69,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
index e5a3777f28..7f7ca05504 100644
--- a/sysdeps/unix/sysv/linux/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
index b137abdeea..56358923e1 100644
--- a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 68
 #define COMPAT_SO_SNDTIMEO_NEW 69
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x001d
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x0021
+#define COMPAT_SO_TIMESTAMP_NEW 0x0046
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x0042
+
 #endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-03-04 11:28 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-04 11:28 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=80fb49149543efd81bdf2e310dcf3d367a740724

commit 80fb49149543efd81bdf2e310dcf3d367a740724
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |   5 +
 sysdeps/unix/sysv/linux/Makefile                   |   2 +-
 sysdeps/unix/sysv/linux/Versions                   |   1 +
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 102 +++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               |  12 +++
 .../unix/sysv/linux/hppa/socket-constants-time64.h |   5 +
 .../unix/sysv/linux/mips/socket-constants-time64.h |   5 +
 .../sysv/linux/powerpc/socket-constants-time64.h   |   5 +
 sysdeps/unix/sysv/linux/recvmsg.c                  |  23 +++--
 sysdeps/unix/sysv/linux/setsockopt.c               |  12 +++
 sysdeps/unix/sysv/linux/socket-constants-time64.h  |   5 +
 .../sysv/linux/sparc/socket-constants-time64.h     |   5 +
 12 files changed, 175 insertions(+), 7 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 51e28b97ac..b5d785e083 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c7fda26ee
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,102 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+# if !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
+#  include <bits/socket-constants.h>
+# endif
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case SO_TIMESTAMPNS_OLD:
+	  type = SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index c8e502d976..14b782d0da 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -67,6 +67,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
index ea721e0fc2..f3b98012d5 100644
--- a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 16448
 #define COMPAT_SO_SNDTIMEO_NEW 16449
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x4012
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x4013
+#define COMPAT_SO_TIMESTAMP_NEW 0x4038
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x4039
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
index ab8bd62853..31fa69fa9f 100644
--- a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
index 1e48dcca8d..889251895b 100644
--- a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 6505202265..a4780a9d33 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -69,6 +69,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case COMPAT_SO_TIMESTAMP_NEW:
+    case COMPAT_SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == COMPAT_SO_TIMESTAMP_NEW)
+	  optname = COMPAT_SO_TIMESTAMP_OLD;
+	if (optname == COMPAT_SO_TIMESTAMPNS_NEW)
+	  optname = COMPAT_SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
index e5a3777f28..7f7ca05504 100644
--- a/sysdeps/unix/sysv/linux/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 66
 #define COMPAT_SO_SNDTIMEO_NEW 67
 
+#define COMPAT_SO_TIMESTAMP_OLD 29
+#define COMPAT_SO_TIMESTAMPNS_OLD 35
+#define COMPAT_SO_TIMESTAMP_NEW 63
+#define COMPAT_SO_TIMESTAMPNS_NEW 64
+
 #endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
index b137abdeea..56358923e1 100644
--- a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -27,4 +27,9 @@
 #define COMPAT_SO_RCVTIMEO_NEW 68
 #define COMPAT_SO_SNDTIMEO_NEW 69
 
+#define COMPAT_SO_TIMESTAMP_OLD 0x001d
+#define COMPAT_SO_TIMESTAMPNS_OLD 0x0021
+#define COMPAT_SO_TIMESTAMP_NEW 0x0046
+#define COMPAT_SO_TIMESTAMPNS_NEW 0x0042
+
 #endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-03-02 12:30 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-02 12:30 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=53a7eb66374a03d022f5d5fbfd13b335ba27efa2

commit 53a7eb66374a03d022f5d5fbfd13b335ba27efa2
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |   5 +
 sysdeps/unix/sysv/linux/Makefile                   |   2 +-
 sysdeps/unix/sysv/linux/Versions                   |   1 +
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |   7 ++
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 102 +++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               |  12 +++
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |   7 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |   7 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |   7 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  |  23 +++--
 sysdeps/unix/sysv/linux/setsockopt.c               |  16 +++-
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |   7 ++
 12 files changed, 188 insertions(+), 8 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 488114a41e..1f99f00a82 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index d4e9979605..d1054eda1f 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -53,3 +53,10 @@
 #define SO_SNDTIMEO_OLD 21
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c7fda26ee
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,102 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+# if !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
+#  include <bits/socket-constants.h>
+# endif
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case SO_TIMESTAMPNS_OLD:
+	  type = SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index a093e42f96..0cdf6cd011 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -72,6 +72,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index 2413529e71..aa09ab5958 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 16448
 #define SO_SNDTIMEO_NEW 16449
+
+#define SO_TIMESTAMP_OLD 0x4012
+#define SO_TIMESTAMPNS_OLD 0x4013
+#define SO_TIMESTAMPING_OLD 0x4020
+#define SO_TIMESTAMP_NEW 0x4038
+#define SO_TIMESTAMPNS_NEW 0x4039
+#define SO_TIMESTAMPING_NEW 0x403A
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index 2c124234fc..4a5d235aed 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index 682a3f4608..ff42df6f5f 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 19
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index a1f98a937d..59a53d2d2d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -19,7 +19,9 @@
 /* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
    however the new constants that describe 64-bit time support were added
    only on v5.1.  */
-#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD) \
+    || !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
 # include <bits/socket-constants.h>
 #endif
 #include <time.h>
@@ -74,6 +76,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index 3eae2176dd..bfd7eef233 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 16384
 #define SO_RCVTIMEO_NEW 68
 #define SO_SNDTIMEO_NEW 69
+
+#define SO_TIMESTAMP_OLD 0x001d
+#define SO_TIMESTAMPNS_OLD 0x0021
+#define SO_TIMESTAMPING_OLD 0x0023
+#define SO_TIMESTAMP_NEW 0x0046
+#define SO_TIMESTAMPNS_NEW 0x0042
+#define SO_TIMESTAMPING_NEW 0x0043


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-02-26 20:41 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-02-26 20:41 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=256b4aa78253c9664ba198f11961b86117490bad

commit 256b4aa78253c9664ba198f11961b86117490bad
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |   5 +
 sysdeps/unix/sysv/linux/Makefile                   |   2 +-
 sysdeps/unix/sysv/linux/Versions                   |   1 +
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |   7 ++
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 102 +++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               |  12 +++
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |   7 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |   7 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |   7 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  |  23 +++--
 sysdeps/unix/sysv/linux/setsockopt.c               |  16 +++-
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |   7 ++
 12 files changed, 188 insertions(+), 8 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 488114a41e..1f99f00a82 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index d4e9979605..d1054eda1f 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -53,3 +53,10 @@
 #define SO_SNDTIMEO_OLD 21
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c7fda26ee
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,102 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+# if !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
+#  include <bits/socket-constants.h>
+# endif
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case SO_TIMESTAMPNS_OLD:
+	  type = SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index a093e42f96..0cdf6cd011 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -72,6 +72,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index 2413529e71..aa09ab5958 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 16448
 #define SO_SNDTIMEO_NEW 16449
+
+#define SO_TIMESTAMP_OLD 0x4012
+#define SO_TIMESTAMPNS_OLD 0x4013
+#define SO_TIMESTAMPING_OLD 0x4020
+#define SO_TIMESTAMP_NEW 0x4038
+#define SO_TIMESTAMPNS_NEW 0x4039
+#define SO_TIMESTAMPING_NEW 0x403A
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index 2c124234fc..4a5d235aed 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index 682a3f4608..ff42df6f5f 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 19
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index a1f98a937d..59a53d2d2d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -19,7 +19,9 @@
 /* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
    however the new constants that describe 64-bit time support were added
    only on v5.1.  */
-#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD) \
+    || !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
 # include <bits/socket-constants.h>
 #endif
 #include <time.h>
@@ -74,6 +76,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index 3eae2176dd..bfd7eef233 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 16384
 #define SO_RCVTIMEO_NEW 68
 #define SO_SNDTIMEO_NEW 69
+
+#define SO_TIMESTAMP_OLD 0x001d
+#define SO_TIMESTAMPNS_OLD 0x0021
+#define SO_TIMESTAMPING_OLD 0x0023
+#define SO_TIMESTAMP_NEW 0x0046
+#define SO_TIMESTAMPNS_NEW 0x0042
+#define SO_TIMESTAMPING_NEW 0x0043


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-02-23 20:38 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-02-23 20:38 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=3ac3578dd21349778352e92a16cbf93b6d21645d

commit 3ac3578dd21349778352e92a16cbf93b6d21645d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                               |   5 +
 sysdeps/unix/sysv/linux/Makefile                   |   2 +-
 sysdeps/unix/sysv/linux/Versions                   |   1 +
 .../unix/sysv/linux/alpha/bits/socket-constants.h  |   7 ++
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |   7 ++
 sysdeps/unix/sysv/linux/convert_scm_timestamps.c   | 102 +++++++++++++++++++++
 sysdeps/unix/sysv/linux/getsockopt.c               |  12 +++
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |   7 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |   7 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |   7 ++
 sysdeps/unix/sysv/linux/recvmsg.c                  |  23 +++--
 sysdeps/unix/sysv/linux/setsockopt.c               |  16 +++-
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |   7 ++
 13 files changed, 195 insertions(+), 8 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..15d4a62b26 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len)
 
 libc_hidden_proto (__cmsg_nxthdr)
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ;
+libc_hidden_proto (__convert_scm_timestamps)
+#endif
+
 #endif
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 488114a41e..1f99f00a82 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   time64-support pselect32 \
 		   xstat fxstat lxstat xstat64 fxstat64 lxstat64 \
 		   fxstatat fxstatat64 \
-		   xmknod xmknodat
+		   xmknod xmknodat convert_scm_timestamps
 
 CFLAGS-gethostid.c = -fexceptions
 CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index c35f783e2a..257ccbd3b4 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -177,6 +177,7 @@ libc {
     __pread64_nocancel;
     __close_nocancel;
     __sigtimedwait;
+    __convert_scm_timestamps;
     # functions used by nscd
     __netlink_assert_response;
   }
diff --git a/sysdeps/unix/sysv/linux/alpha/bits/socket-constants.h b/sysdeps/unix/sysv/linux/alpha/bits/socket-constants.h
index 7d90c06a8b..85a0babb3a 100644
--- a/sysdeps/unix/sysv/linux/alpha/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/alpha/bits/socket-constants.h
@@ -36,3 +36,10 @@
 #define SO_SNDLOWAT 4113
 #define SO_SNDTIMEO 4115
 #define SO_TYPE 4104
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index d4e9979605..d1054eda1f 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -53,3 +53,10 @@
 #define SO_SNDTIMEO_OLD 21
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
new file mode 100644
index 0000000000..3c7fda26ee
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c
@@ -0,0 +1,102 @@
+/* Socket timestamp conversion routines.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <kernel-features.h>
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+# include <stdint.h>
+# include <string.h>
+# include <sys/socket.h>
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+# if !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
+#  include <bits/socket-constants.h>
+# endif
+
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+void
+__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SO_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SO_TIMESTAMP_NEW;
+	  goto common;
+
+	case SO_TIMESTAMPNS_OLD:
+	  type = SO_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+libc_hidden_def (__convert_scm_timestamps)
+#endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index a093e42f96..0cdf6cd011 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -72,6 +72,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index 2413529e71..aa09ab5958 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 16448
 #define SO_SNDTIMEO_NEW 16449
+
+#define SO_TIMESTAMP_OLD 0x4012
+#define SO_TIMESTAMPNS_OLD 0x4013
+#define SO_TIMESTAMPING_OLD 0x4020
+#define SO_TIMESTAMP_NEW 0x4038
+#define SO_TIMESTAMPNS_NEW 0x4039
+#define SO_TIMESTAMPING_NEW 0x403A
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index 2c124234fc..4a5d235aed 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 4101
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index 682a3f4608..ff42df6f5f 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 19
 #define SO_RCVTIMEO_NEW 66
 #define SO_SNDTIMEO_NEW 67
+
+#define SO_TIMESTAMP_OLD 29
+#define SO_TIMESTAMPNS_OLD 35
+#define SO_TIMESTAMPING_OLD 37
+#define SO_TIMESTAMP_NEW 63
+#define SO_TIMESTAMPNS_NEW 64
+#define SO_TIMESTAMPING_NEW 65
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..a2a600228b 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -19,16 +19,27 @@
 #include <sys/socket.h>
 #include <sysdep-cancel.h>
 #include <socketcall.h>
-#include <shlib-compat.h>
 
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    __convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index a1f98a937d..59a53d2d2d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -19,7 +19,9 @@
 /* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
    however the new constants that describe 64-bit time support were added
    only on v5.1.  */
-#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD) \
+    || !defined(SO_TIMESTAMP_OLD) || !defined(SO_TIMESTAMP_NEW) \
+    || !defined(SO_TIMESTAMPNS_OLD) || !defined(SO_TIMESTAMPNS_NEW)
 # include <bits/socket-constants.h>
 #endif
 #include <time.h>
@@ -74,6 +76,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index 3eae2176dd..bfd7eef233 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -41,3 +41,10 @@
 #define SO_SNDTIMEO_OLD 16384
 #define SO_RCVTIMEO_NEW 68
 #define SO_SNDTIMEO_NEW 69
+
+#define SO_TIMESTAMP_OLD 0x001d
+#define SO_TIMESTAMPNS_OLD 0x0021
+#define SO_TIMESTAMPING_OLD 0x0023
+#define SO_TIMESTAMP_NEW 0x0046
+#define SO_TIMESTAMPNS_NEW 0x0042
+#define SO_TIMESTAMPING_NEW 0x0043


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
@ 2021-02-23 12:36 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-02-23 12:36 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d3a8ef25d843cd848a00069d45f8ae307511ea08

commit d3a8ef25d843cd848a00069d45f8ae307511ea08
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 17:08:46 2020 -0300

    linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    The recvmsg handling is more complicated because it requires check the
    returned kernel control message and make some convertions.  For
    !__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
    or SO_TIMESTAMPNS and appends it to the control buffer if has extra
    space or returns MSG_CTRUNC otherwise.  The 32-bit time field is kept
    as-is.
    
    Calls with __TIMESIZE=32 will see the converted 64-bit time control
    messages as spurious control message of unknown type.  Calls with
    __TIMESIZE=64 running on pre-time64 kernels will see the original
    message as a spurious control ones of unknown typ while running on
    kernel with native 64-bit time support will only see the time64 version
    of the control message.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 include/sys/socket.h                 |  9 ++++
 sysdeps/unix/sysv/linux/getsockopt.c | 12 +++++
 sysdeps/unix/sysv/linux/recvmsg.c    | 93 ++++++++++++++++++++++++++++++++++--
 sysdeps/unix/sysv/linux/setsockopt.c | 12 +++++
 4 files changed, 121 insertions(+), 5 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 0e39dd2a3a..c551c8fa87 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -162,6 +162,15 @@ libc_hidden_proto (__libc_sa_len)
 # define SA_LEN(_x)      __libc_sa_len((_x)->sa_family)
 #endif
 
+/* Used on y2038 emulation on 64-bit time_t binaries running on older
+   kernel without 64-bit time_t support.  */
+#define SCM_TIMESTAMP_OLD     SO_TIMESTAMP_OLD
+#define SCM_TIMESTAMPNS_OLD   SO_TIMESTAMPNS_OLD
+#define SCM_TIMESTAMPING_OLD  SO_TIMESTAMPING_OLD
+#define SCM_TIMESTAMP_NEW     SO_TIMESTAMP_NEW
+#define SCM_TIMESTAMPNS_NEW   SO_TIMESTAMPNS_NEW
+#define SCM_TIMESTAMPING_NEW  SO_TIMESTAMPING_NEW
+
 libc_hidden_proto (__cmsg_nxthdr)
 
 #endif
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index a093e42f96..0cdf6cd011 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -72,6 +72,18 @@ getsockopt32 (int fd, int level, int optname, void *optval,
 	*tv64 = valid_timeval32_to_timeval64 (tv32);
 	*len = sizeof (*tv64);
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = getsockopt_syscall (fd, level, optname, optval, len);
+      }
+      break;
     }
 
   return r;
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
index b209b4ad99..c01a9009c7 100644
--- a/sysdeps/unix/sysv/linux/recvmsg.c
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
@@ -21,14 +21,97 @@
 #include <socketcall.h>
 #include <shlib-compat.h>
 
+#ifndef __ASSUME_TIME64_SYSCALLS
+/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
+   appends it to the control buffer.  The 32-bit time field is kept as-is.
+
+   Calls with __TIMESIZE=32 will see the converted 64-bit time control
+   messages as spurious control message of unknown type.
+
+   Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
+   original message as a spurious control ones of unknown typ while running
+   on kernel with native 64-bit time support will only see the time64 version
+   of the control message.  */
+static void
+convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
+{
+  if (msg->msg_control == NULL || msg->msg_controllen == 0)
+    return;
+
+  /* The returnted control message format for SO_TIMESTAMP_NEW is a
+     'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
+     'struct __kernel_timespec'.  In both case it is essentially two
+     uint64_t members.  */
+  uint64_t tvts[2];
+
+  struct cmsghdr *cmsg, *last = NULL;
+  int type = 0;
+
+  for (cmsg = CMSG_FIRSTHDR (msg);
+       cmsg != NULL;
+       cmsg = CMSG_NXTHDR (msg, cmsg))
+    {
+      if (cmsg->cmsg_level != SOL_SOCKET)
+	continue;
+
+      switch (cmsg->cmsg_type)
+	{
+	case SCM_TIMESTAMP_OLD:
+	  if (type != 0)
+	    break;
+	  type = SCM_TIMESTAMP_NEW;
+	  goto common;
+
+	case SCM_TIMESTAMPNS_OLD:
+	  type = SCM_TIMESTAMPNS_NEW;
+
+	/* fallthrough  */
+	common:
+	  memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts));
+	  break;
+	}
+
+      last = cmsg;
+    }
+
+  if (last == NULL || type == 0)
+    return;
+
+  if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
+    {
+      msg->msg_flags |= MSG_CTRUNC;
+      return;
+    }
+
+  msg->msg_controllen += CMSG_SPACE (sizeof tvts);
+  cmsg = CMSG_NXTHDR(msg, last);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type = type;
+  cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
+  memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
+}
+#endif
+
 ssize_t
 __libc_recvmsg (int fd, struct msghdr *msg, int flags)
 {
-# ifdef __ASSUME_RECVMSG_SYSCALL
-  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
-# else
-  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
-# endif
+  ssize_t r;
+#ifndef __ASSUME_TIME64_SYSCALLS
+  socklen_t orig_controllen = msg->msg_controllen;
+#endif
+
+#ifdef __ASSUME_RECVMSG_SYSCALL
+  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
+#else
+  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
+#endif
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r >= 0)
+    convert_scm_timestamps (msg, orig_controllen);
+#endif
+
+  return r;
 }
 weak_alias (__libc_recvmsg, recvmsg)
 weak_alias (__libc_recvmsg, __recvmsg)
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index a1f98a937d..355a6de208 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -74,6 +74,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval,
 
 	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
       }
+      break;
+
+    case SO_TIMESTAMP_NEW:
+    case SO_TIMESTAMPNS_NEW:
+      {
+	if (optname == SO_TIMESTAMP_NEW)
+	  optname = SO_TIMESTAMP_OLD;
+	if (optname == SO_TIMESTAMPNS_NEW)
+	  optname = SO_TIMESTAMPNS_OLD;
+	r = setsockopt_syscall (fd, level, optname, NULL, 0);
+      }
+      break;
     }
 
   return r;


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-06-08 20:48 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-01 17:35 [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS} Adhemerval Zanella
  -- strict thread matches above, loose matches on Subject: below --
2021-06-08 20:48 Adhemerval Zanella
2021-05-26 16:24 Adhemerval Zanella
2021-03-05 19:20 Adhemerval Zanella
2021-03-04 17:36 Adhemerval Zanella
2021-03-04 11:28 Adhemerval Zanella
2021-03-02 12:30 Adhemerval Zanella
2021-02-26 20:41 Adhemerval Zanella
2021-02-23 20:38 Adhemerval Zanella
2021-02-23 12:36 Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).