076f82
commit 88a8637cb4658cd91a002659db05867716b88b36
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Wed Apr 27 13:40:30 2022 -0300
076f82
076f82
    linux: Fix fchmodat with AT_SYMLINK_NOFOLLOW for 64 bit time_t (BZ#29097)
076f82
    
076f82
    The AT_SYMLINK_NOFOLLOW emulation ues the default 32 bit stat internal
076f82
    calls, which fails with EOVERFLOW if the file constains timestamps
076f82
    beyond 2038.
076f82
    
076f82
    Checked on i686-linux-gnu.
076f82
    
076f82
    (cherry picked from commit 118a2aee07f64d605b6668cbe195c1f44eac6be6)
076f82
076f82
diff --git a/io/Makefile b/io/Makefile
076f82
index 9871ecbc74020a6d..01968b81042e01e4 100644
076f82
--- a/io/Makefile
076f82
+++ b/io/Makefile
076f82
@@ -81,16 +81,17 @@ tests		:= test-utime test-stat test-stat2 test-lfs tst-getcwd \
076f82
 		   tst-closefrom \
076f82
 
076f82
 tests-time64 := \
076f82
+  tst-fcntl-time64 \
076f82
+  tst-fts-time64 \
076f82
   tst-futimens-time64 \
076f82
   tst-futimes-time64\
076f82
-  tst-fts-time64 \
076f82
+  tst-futimesat-time64 \
076f82
+  tst-lchmod-time64 \
076f82
   tst-lutimes-time64 \
076f82
   tst-stat-time64 \
076f82
-  tst-futimesat-time64 \
076f82
   tst-utime-time64 \
076f82
   tst-utimensat-time64 \
076f82
   tst-utimes-time64 \
076f82
-  tst-fcntl-time64 \
076f82
   # tests-time64
076f82
 
076f82
 # Likewise for statx, but we do not need static linking here.
076f82
@@ -134,6 +135,7 @@ CFLAGS-close.c += -fexceptions -fasynchronous-unwind-tables
076f82
 
076f82
 CFLAGS-test-stat.c += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE
076f82
 CFLAGS-test-lfs.c += -D_LARGEFILE64_SOURCE
076f82
+CFLAGS-tst-lchmod.c += -D_FILE_OFFSET_BITS=64
076f82
 
076f82
 test-stat2-ARGS = Makefile . $(objpfx)test-stat2
076f82
 
076f82
diff --git a/io/tst-lchmod-time64.c b/io/tst-lchmod-time64.c
076f82
new file mode 100644
076f82
index 0000000000000000..f2b7cc9d358f2a77
076f82
--- /dev/null
076f82
+++ b/io/tst-lchmod-time64.c
076f82
@@ -0,0 +1,2 @@
076f82
+#define CHECK_TIME64
076f82
+#include "tst-lchmod.c"
076f82
diff --git a/io/tst-lchmod.c b/io/tst-lchmod.c
076f82
index 0fe98e01b74b713d..472766b186975922 100644
076f82
--- a/io/tst-lchmod.c
076f82
+++ b/io/tst-lchmod.c
076f82
@@ -66,10 +66,27 @@ select_path (bool do_relative_path, const char *full_path, const char *relative_
076f82
     return full_path;
076f82
 }
076f82
 
076f82
+static void
076f82
+update_file_time_to_y2038 (const char *fname, int flags)
076f82
+{
076f82
+#ifdef CHECK_TIME64
076f82
+  /* Y2038 threshold plus 1 second.  */
076f82
+  const struct timespec ts[] = { { 0x80000001LL, 0}, { 0x80000001LL } };
076f82
+  TEST_VERIFY_EXIT (utimensat (AT_FDCWD, fname, ts, flags) == 0);
076f82
+#endif
076f82
+}
076f82
+
076f82
 static void
076f82
 test_1 (bool do_relative_path, int (*chmod_func) (int fd, const char *, mode_t, int))
076f82
 {
076f82
   char *tempdir = support_create_temp_directory ("tst-lchmod-");
076f82
+#ifdef CHECK_TIME64
076f82
+  if (!support_path_support_time64 (tempdir))
076f82
+    {
076f82
+      puts ("info: test skipped, filesystem does not support 64 bit time_t");
076f82
+      return;
076f82
+    }
076f82
+#endif
076f82
 
076f82
   char *path_dangling = xasprintf ("%s/dangling", tempdir);
076f82
   char *path_file = xasprintf ("%s/file", tempdir);
076f82
@@ -93,9 +110,12 @@ test_1 (bool do_relative_path, int (*chmod_func) (int fd, const char *, mode_t,
076f82
   xsymlink ("loop", path_loop);
076f82
   xsymlink ("target-does-not-exist", path_dangling);
076f82
 
076f82
+  update_file_time_to_y2038 (path_file, 0);
076f82
+  update_file_time_to_y2038 (path_to_file, AT_SYMLINK_NOFOLLOW);
076f82
+
076f82
   /* Check that the modes do not collide with what we will use in the
076f82
      test.  */
076f82
-  struct stat64 st;
076f82
+  struct stat st;
076f82
   xstat (path_file, &st);
076f82
   TEST_VERIFY ((st.st_mode & 0777) != 1);
076f82
   xlstat (path_to_file, &st);
076f82
diff --git a/sysdeps/unix/sysv/linux/fchmodat.c b/sysdeps/unix/sysv/linux/fchmodat.c
076f82
index 5bd1eb96a5d78130..b0cf61949a9302d9 100644
076f82
--- a/sysdeps/unix/sysv/linux/fchmodat.c
076f82
+++ b/sysdeps/unix/sysv/linux/fchmodat.c
076f82
@@ -48,8 +48,8 @@ fchmodat (int fd, const char *file, mode_t mode, int flag)
076f82
 
076f82
       /* Use fstatat because fstat does not work on O_PATH descriptors
076f82
 	 before Linux 3.6.  */
076f82
-      struct stat64 st;
076f82
-      if (__fstatat64 (pathfd, "", &st, AT_EMPTY_PATH) != 0)
076f82
+      struct __stat64_t64 st;
076f82
+      if (__fstatat64_time64 (pathfd, "", &st, AT_EMPTY_PATH) != 0)
076f82
 	{
076f82
 	  __close_nocancel (pathfd);
076f82
 	  return -1;