public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org,
	"Andreas K . Huettel" <dilfridge@gentoo.org>,
	Paul Eggert <eggert@cs.ucla.edu>,
	Florian Weimer <fweimer@redhat.com>
Subject: [PATCH v6 1/3] linux: Use getdents64 on non-LFS readdir
Date: Thu,  2 Mar 2023 11:57:30 -0300	[thread overview]
Message-ID: <20230302145732.2293756-2-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20230302145732.2293756-1-adhemerval.zanella@linaro.org>

It is similar to what non-LFS getdents do (including overflow check).

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 sysdeps/unix/sysv/linux/readdir.c | 97 +++++++++++++++++++++++--------
 1 file changed, 73 insertions(+), 24 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c
index 4a4c00ea07..72ba895afe 100644
--- a/sysdeps/unix/sysv/linux/readdir.c
+++ b/sysdeps/unix/sysv/linux/readdir.c
@@ -20,43 +20,92 @@
 
 #if !_DIRENT_MATCHES_DIRENT64
 #include <dirstream.h>
+#include <unistd.h>
+
+# ifndef DIRENT_SET_DP_INO
+#  define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value)
+# endif
 
 /* Read a directory entry from DIRP.  */
 struct dirent *
 __readdir_unlocked (DIR *dirp)
 {
-  struct dirent *dp;
   int saved_errno = errno;
 
-  if (dirp->offset >= dirp->size)
+  while (1)
     {
-      /* We've emptied out our buffer.  Refill it.  */
-
-      size_t maxread = dirp->allocation;
-      ssize_t bytes;
-
-      bytes = __getdents (dirp->fd, dirp->data, maxread);
-      if (bytes <= 0)
+      if (dirp->offset >= dirp->size)
 	{
-	  /* Linux may fail with ENOENT on some file systems if the
-	     directory inode is marked as dead (deleted).  POSIX
-	     treats this as a regular end-of-directory condition, so
-	     do not set errno in that case, to indicate success.  */
-	  if (bytes == 0 || errno == ENOENT)
-	    __set_errno (saved_errno);
+	  ssize_t bytes = __getdents64 (dirp->fd, dirp->data,
+					dirp->allocation);
+	  if (bytes <= 0)
+	    {
+	      /* Linux may fail with ENOENT on some file systems if the
+		 directory inode is marked as dead (deleted).  POSIX
+		 treats this as a regular end-of-directory condition, so
+		 do not set errno in that case, to indicate success.  */
+	      if (bytes < 0 && errno == ENOENT)
+		__set_errno (saved_errno);
+	      return NULL;
+	    }
+	  dirp->size = bytes;
+
+ 	  /* Reset the offset into the buffer.  */
+	  dirp->offset = 0;
+ 	}
+
+    /* These two pointers might alias the same memory buffer.  Standard C
+       requires that we always use the same type for them, so we must use the
+       union type.  */
+      union
+      {
+	struct dirent64 dp64;
+	struct dirent dp;
+	char *b;
+      } *inp, *outp;
+      inp = (void*) &dirp->data[dirp->offset];
+      outp = (void*) &dirp->data[dirp->offset];
+
+      const size_t size_diff = offsetof (struct dirent64, d_name)
+	- offsetof (struct dirent, d_name);
+
+      /* Since inp->dp64.d_reclen is already aligned for the kernel structure
+	 this may compute a value that is bigger than necessary.  */
+      size_t old_reclen = inp->dp64.d_reclen;
+      size_t new_reclen = ALIGN_UP (old_reclen - size_diff,
+				    _Alignof (struct dirent));
+
+      if (!in_ino_t_range (inp->dp64.d_ino)
+	  || !in_off_t_range (inp->dp64.d_off))
+	{
+	  /* Overflow.  If there was at least one entry before this one,
+	     return them without error, otherwise signal overflow.  */
+	  if (dirp->offset != 0)
+	    {
+	      __lseek64 (dirp->fd, dirp->offset, SEEK_SET);
+	      outp = (void*)(outp->b - dirp->data);
+	      return &outp->dp;
+	    }
+	  __set_errno (EOVERFLOW);
 	  return NULL;
 	}
-      dirp->size = (size_t) bytes;
 
-      /* Reset the offset into the buffer.  */
-      dirp->offset = 0;
+      /* Copy the data from INP and access only OUTP.  */
+      const uint64_t d_ino = inp->dp64.d_ino;
+      const int64_t d_off = inp->dp64.d_off;
+      const uint8_t d_type = inp->dp64.d_type;
+      outp->dp.d_ino = d_ino;
+      outp->dp.d_off = d_off;
+      outp->dp.d_reclen = new_reclen;
+      outp->dp.d_type = d_type;
+      memmove (outp->dp.d_name, inp->dp64.d_name,
+	       old_reclen - offsetof (struct dirent64, d_name));
+
+      dirp->filepos = d_off;
+      dirp->offset += old_reclen;
+
+      return &outp->dp;
     }
-
-  dp = (struct dirent *) &dirp->data[dirp->offset];
-  dirp->offset += dp->d_reclen;
-  dirp->filepos = dp->d_off;
-
-  return dp;
 }
 
 struct dirent *
-- 
2.34.1


  reply	other threads:[~2023-03-02 14:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-02 14:57 [PATCH v6 0/3] Fix opendir regression on some FS Adhemerval Zanella
2023-03-02 14:57 ` Adhemerval Zanella [this message]
2023-03-02 14:57 ` [PATCH v6 2/3] support: Add xreallocarray Adhemerval Zanella
2023-03-10 16:49   ` Florian Weimer
2023-03-10 18:44     ` Adhemerval Zanella Netto
2023-03-10 21:21       ` Paul Eggert
2023-03-02 14:57 ` [PATCH v6 3/3] linux: Set internal DIR filepos as off64_t (BZ #23960, BZ #24050) Adhemerval Zanella
2023-03-10 21:41   ` Paul Eggert
2023-03-13 12:40     ` Adhemerval Zanella Netto
2023-03-13 12:45       ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230302145732.2293756-2-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=dilfridge@gentoo.org \
    --cc=eggert@cs.ucla.edu \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).