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