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