50f89d
commit 4b25485f03158959cff45379eecc1d73c7dcdd11
50f89d
Author: Florian Weimer <fweimer@redhat.com>
50f89d
Date:   Fri Aug 10 11:19:26 2018 +0200
50f89d
50f89d
    Linux: Rewrite __old_getdents64 [BZ #23497]
50f89d
    
50f89d
    Commit 298d0e3129c0b5137f4989275b13fe30d0733c4d ("Consolidate Linux
50f89d
    getdents{64} implementation") broke the implementation because it does
50f89d
    not take into account struct offset differences.
50f89d
    
50f89d
    The new implementation is close to the old one, before the
50f89d
    consolidation, but has been cleaned up slightly.
50f89d
    
50f89d
    (cherry picked from commit 690652882b499defb3d950dfeff8fe421d13cab5)
50f89d
50f89d
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
50f89d
index f71cc39c7e257a0a..773aaea0e980bdd6 100644
50f89d
--- a/sysdeps/unix/sysv/linux/Makefile
50f89d
+++ b/sysdeps/unix/sysv/linux/Makefile
50f89d
@@ -161,6 +161,7 @@ inhibit-glue = yes
50f89d
 
50f89d
 ifeq ($(subdir),dirent)
50f89d
 sysdep_routines += getdirentries getdirentries64
50f89d
+tests-internal += tst-readdir64-compat
50f89d
 endif
50f89d
 
50f89d
 ifeq ($(subdir),nis)
50f89d
diff --git a/sysdeps/unix/sysv/linux/getdents64.c b/sysdeps/unix/sysv/linux/getdents64.c
50f89d
index 3bde0cf4f0226f95..bc140b5a7fac3040 100644
50f89d
--- a/sysdeps/unix/sysv/linux/getdents64.c
50f89d
+++ b/sysdeps/unix/sysv/linux/getdents64.c
50f89d
@@ -33,41 +33,80 @@ strong_alias (__getdents64, __getdents)
50f89d
 # include <shlib-compat.h>
50f89d
 
50f89d
 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
50f89d
-# include <olddirent.h>
50f89d
+#  include <olddirent.h>
50f89d
+#  include <unistd.h>
50f89d
 
50f89d
-/* kernel definition of as of 3.2.  */
50f89d
-struct compat_linux_dirent
50f89d
+static ssize_t
50f89d
+handle_overflow (int fd, __off64_t offset, ssize_t count)
50f89d
 {
50f89d
-  /* Both d_ino and d_off are compat_ulong_t which are defined in all
50f89d
-     architectures as 'u32'.  */
50f89d
-  uint32_t        d_ino;
50f89d
-  uint32_t        d_off;
50f89d
-  unsigned short  d_reclen;
50f89d
-  char            d_name[1];
50f89d
-};
50f89d
+  /* If this is the first entry in the buffer, we can report the
50f89d
+     error.  */
50f89d
+  if (count == 0)
50f89d
+    {
50f89d
+      __set_errno (EOVERFLOW);
50f89d
+      return -1;
50f89d
+    }
50f89d
+
50f89d
+  /* Otherwise, seek to the overflowing entry, so that the next call
50f89d
+     will report the error, and return the data read so far..  */
50f89d
+  if (__lseek64 (fd, offset, SEEK_SET) != 0)
50f89d
+    return -1;
50f89d
+  return count;
50f89d
+}
50f89d
 
50f89d
 ssize_t
50f89d
 __old_getdents64 (int fd, char *buf, size_t nbytes)
50f89d
 {
50f89d
-  ssize_t retval = INLINE_SYSCALL_CALL (getdents, fd, buf, nbytes);
50f89d
+  /* We do not move the individual directory entries.  This is only
50f89d
+     possible if the target type (struct __old_dirent64) is smaller
50f89d
+     than the source type.  */
50f89d
+  _Static_assert (offsetof (struct __old_dirent64, d_name)
50f89d
+		  <= offsetof (struct dirent64, d_name),
50f89d
+		  "__old_dirent64 is larger than dirent64");
50f89d
+  _Static_assert (__alignof__ (struct __old_dirent64)
50f89d
+		  <= __alignof__ (struct dirent64),
50f89d
+		  "alignment of __old_dirent64 is larger than dirent64");
50f89d
 
50f89d
-  /* The kernel added the d_type value after the name.  Change this now.  */
50f89d
-  if (retval != -1)
50f89d
+  ssize_t retval = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes);
50f89d
+  if (retval > 0)
50f89d
     {
50f89d
-      union
50f89d
-      {
50f89d
-	struct compat_linux_dirent k;
50f89d
-	struct dirent u;
50f89d
-      } *kbuf = (void *) buf;
50f89d
-
50f89d
-      while ((char *) kbuf < buf + retval)
50f89d
+      char *p = buf;
50f89d
+      char *end = buf + retval;
50f89d
+      while (p < end)
50f89d
 	{
50f89d
-	  char d_type = *((char *) kbuf + kbuf->k.d_reclen - 1);
50f89d
-	  memmove (kbuf->u.d_name, kbuf->k.d_name,
50f89d
-		   strlen (kbuf->k.d_name) + 1);
50f89d
-	  kbuf->u.d_type = d_type;
50f89d
+	  struct dirent64 *source = (struct dirent64 *) p;
50f89d
+
50f89d
+	  /* Copy out the fixed-size data.  */
50f89d
+	  __ino_t ino = source->d_ino;
50f89d
+	  __off64_t offset = source->d_off;
50f89d
+	  unsigned int reclen = source->d_reclen;
50f89d
+	  unsigned char type = source->d_type;
50f89d
+
50f89d
+	  /* Check for ino_t overflow.  */
50f89d
+	  if (__glibc_unlikely (ino != source->d_ino))
50f89d
+	    return handle_overflow (fd, offset, p - buf);
50f89d
+
50f89d
+	  /* Convert to the target layout.  Use a separate struct and
50f89d
+	     memcpy to side-step aliasing issues.  */
50f89d
+	  struct __old_dirent64 result;
50f89d
+	  result.d_ino = ino;
50f89d
+	  result.d_off = offset;
50f89d
+	  result.d_reclen = reclen;
50f89d
+	  result.d_type = type;
50f89d
+
50f89d
+	  /* Write the fixed-sized part of the result to the
50f89d
+	     buffer.  */
50f89d
+	  size_t result_name_offset = offsetof (struct __old_dirent64, d_name);
50f89d
+	  memcpy (p, &result, result_name_offset);
50f89d
+
50f89d
+	  /* Adjust the position of the name if necessary.  Copy
50f89d
+	     everything until the end of the record, including the
50f89d
+	     terminating NUL byte.  */
50f89d
+	  if (result_name_offset != offsetof (struct dirent64, d_name))
50f89d
+	    memmove (p + result_name_offset, source->d_name,
50f89d
+		     reclen - offsetof (struct dirent64, d_name));
50f89d
 
50f89d
-	  kbuf = (void *) ((char *) kbuf + kbuf->k.d_reclen);
50f89d
+	  p += reclen;
50f89d
 	}
50f89d
      }
50f89d
   return retval;
50f89d
diff --git a/sysdeps/unix/sysv/linux/tst-readdir64-compat.c b/sysdeps/unix/sysv/linux/tst-readdir64-compat.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..43c4a8477c7403c5
50f89d
--- /dev/null
50f89d
+++ b/sysdeps/unix/sysv/linux/tst-readdir64-compat.c
50f89d
@@ -0,0 +1,111 @@
50f89d
+/* Test readdir64 compatibility symbol.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <dirent.h>
50f89d
+#include <dlfcn.h>
50f89d
+#include <errno.h>
50f89d
+#include <shlib-compat.h>
50f89d
+#include <stdbool.h>
50f89d
+#include <stdio.h>
50f89d
+#include <string.h>
50f89d
+#include <support/check.h>
50f89d
+
50f89d
+/* Copied from <olddirent.h>.  */
50f89d
+struct __old_dirent64
50f89d
+  {
50f89d
+    __ino_t d_ino;
50f89d
+    __off64_t d_off;
50f89d
+    unsigned short int d_reclen;
50f89d
+    unsigned char d_type;
50f89d
+    char d_name[256];
50f89d
+  };
50f89d
+
50f89d
+typedef struct __old_dirent64 *(*compat_readdir64_type) (DIR *);
50f89d
+
50f89d
+#if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
50f89d
+struct __old_dirent64 *compat_readdir64 (DIR *);
50f89d
+compat_symbol_reference (libc, compat_readdir64, readdir64, GLIBC_2_1);
50f89d
+#endif
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+#if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
50f89d
+
50f89d
+  /* Directory stream using the non-compat readdir64 symbol.  The test
50f89d
+     checks against this.  */
50f89d
+  DIR *dir_reference = opendir (".");
50f89d
+  TEST_VERIFY_EXIT (dir_reference != NULL);
50f89d
+  DIR *dir_test = opendir (".");
50f89d
+  TEST_VERIFY_EXIT (dir_test != NULL);
50f89d
+
50f89d
+  /* This loop assumes that the enumeration order is consistent for
50f89d
+     two different handles.  Nothing should write to the current
50f89d
+     directory (in the source tree) while this test runs, so there
50f89d
+     should not be any difference due to races.  */
50f89d
+  size_t count = 0;
50f89d
+  while (true)
50f89d
+    {
50f89d
+      errno = 0;
50f89d
+      struct dirent64 *entry_reference = readdir64 (dir_reference);
50f89d
+      if (entry_reference == NULL && errno != 0)
50f89d
+        FAIL_EXIT1 ("readdir64 entry %zu: %m\n", count);
50f89d
+      struct __old_dirent64 *entry_test = compat_readdir64 (dir_test);
50f89d
+      if (entry_reference == NULL)
50f89d
+        {
50f89d
+          if (errno == EOVERFLOW)
50f89d
+            {
50f89d
+              TEST_VERIFY (entry_reference->d_ino
50f89d
+                           != (__ino_t) entry_reference->d_ino);
50f89d
+              printf ("info: inode number overflow at entry %zu\n", count);
50f89d
+              break;
50f89d
+            }
50f89d
+          if (errno != 0)
50f89d
+            FAIL_EXIT1 ("compat readdir64 entry %zu: %m\n", count);
50f89d
+        }
50f89d
+
50f89d
+      /* Check that both streams end at the same time.  */
50f89d
+      if (entry_reference == NULL)
50f89d
+        {
50f89d
+          TEST_VERIFY (entry_test == NULL);
50f89d
+          break;
50f89d
+        }
50f89d
+      else
50f89d
+        TEST_VERIFY_EXIT (entry_test != NULL);
50f89d
+
50f89d
+      /* Check that the entries are the same.  */
50f89d
+      TEST_COMPARE_BLOB (entry_reference->d_name,
50f89d
+                         strlen (entry_reference->d_name),
50f89d
+                         entry_test->d_name, strlen (entry_test->d_name));
50f89d
+      TEST_COMPARE (entry_reference->d_ino, entry_test->d_ino);
50f89d
+      TEST_COMPARE (entry_reference->d_off, entry_test->d_off);
50f89d
+      TEST_COMPARE (entry_reference->d_type, entry_test->d_type);
50f89d
+      TEST_COMPARE (entry_reference->d_reclen, entry_test->d_reclen);
50f89d
+
50f89d
+      ++count;
50f89d
+    }
50f89d
+  printf ("info: %zu directory entries found\n", count);
50f89d
+  TEST_VERIFY (count >= 3);     /* ".", "..", and some source files.  */
50f89d
+
50f89d
+  TEST_COMPARE (closedir (dir_test), 0);
50f89d
+  TEST_COMPARE (closedir (dir_reference), 0);
50f89d
+#endif
50f89d
+  return 0;
50f89d
+}
50f89d
+
50f89d
+#include <support/test-driver.c>