public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Samuel Thibault <sthibaul@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] hurd: Make readlink* just reopen the file used for stat
Date: Thu, 15 Sep 2022 19:55:31 +0000 (GMT)	[thread overview]
Message-ID: <20220915195531.540743858C62@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=5652e12cce80825297c3e0666991deb10310343c

commit 5652e12cce80825297c3e0666991deb10310343c
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Thu Sep 15 21:53:57 2022 +0200

    hurd: Make readlink* just reopen the file used for stat
    
    9e5c991106cb ("hurd: Fix readlink() hanging on fifo") separated opening
    the file for the stat call from opening the file for the read call. That
    however opened a small window for the file to change. Better make this
    atomic by reopening the file with O_READ.

Diff:
---
 sysdeps/mach/hurd/readlink.c   | 32 ++++++++++++++++++++------------
 sysdeps/mach/hurd/readlinkat.c | 32 ++++++++++++++++++++------------
 2 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/sysdeps/mach/hurd/readlink.c b/sysdeps/mach/hurd/readlink.c
index 2d75ef7725..34f132cb92 100644
--- a/sysdeps/mach/hurd/readlink.c
+++ b/sysdeps/mach/hurd/readlink.c
@@ -28,33 +28,41 @@ ssize_t
 __readlink (const char *file_name, char *buf, size_t len)
 {
   error_t err;
-  file_t file;
+  file_t file_stat;
   struct stat64 st;
 
-  file = __file_name_lookup (file_name, O_NOLINK, 0);
-  if (file == MACH_PORT_NULL)
+  file_stat = __file_name_lookup (file_name, O_NOLINK, 0);
+  if (file_stat == MACH_PORT_NULL)
     return -1;
 
-  err = __io_stat (file, &st);
+  err = __io_stat (file_stat, &st);
   if (! err)
     if (S_ISLNK (st.st_mode))
       {
+	enum retry_type doretry;
+	char retryname[1024];
+	file_t file;
 	char *rbuf = buf;
 
-	__mach_port_deallocate (__mach_task_self (), file);
-	file = __file_name_lookup (file_name, O_READ | O_NOLINK, 0);
-
-	err = __io_read (file, &rbuf, &len, 0, len);
-	if (!err && rbuf != buf)
+	err = __dir_lookup (file_stat, "", O_READ | O_NOLINK, 0, &doretry, retryname, &file);
+	if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
+	  err = EGRATUITOUS;
+	if (! err)
 	  {
-	    memcpy (buf, rbuf, len);
-	    __vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
+	    err = __io_read (file, &rbuf, &len, 0, len);
+	    if (!err && rbuf != buf)
+	      {
+		memcpy (buf, rbuf, len);
+		__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
+	      }
+
+	    __mach_port_deallocate (__mach_task_self (), file);
 	  }
       }
     else
       err = EINVAL;
 
-  __mach_port_deallocate (__mach_task_self (), file);
+  __mach_port_deallocate (__mach_task_self (), file_stat);
 
   if (err)
     return __hurd_fail (err);
diff --git a/sysdeps/mach/hurd/readlinkat.c b/sysdeps/mach/hurd/readlinkat.c
index 113b92b732..278a0b5b00 100644
--- a/sysdeps/mach/hurd/readlinkat.c
+++ b/sysdeps/mach/hurd/readlinkat.c
@@ -29,33 +29,41 @@ ssize_t
 readlinkat (int fd, const char *file_name, char *buf, size_t len)
 {
   error_t err;
-  file_t file;
+  file_t file_stat;
   struct stat64 st;
 
-  file = __file_name_lookup_at (fd, 0, file_name, O_NOLINK, 0);
-  if (file == MACH_PORT_NULL)
+  file_stat = __file_name_lookup_at (fd, 0, file_name, O_NOLINK, 0);
+  if (file_stat == MACH_PORT_NULL)
     return -1;
 
-  err = __io_stat (file, &st);
+  err = __io_stat (file_stat, &st);
   if (! err)
     if (S_ISLNK (st.st_mode))
       {
+	enum retry_type doretry;
+	char retryname[1024];
+	file_t file;
 	char *rbuf = buf;
 
-	__mach_port_deallocate (__mach_task_self (), file);
-	file = __file_name_lookup_at (fd, 0, file_name, O_READ | O_NOLINK, 0);
-
-	err = __io_read (file, &rbuf, &len, 0, len);
-	if (!err && rbuf != buf)
+	err = __dir_lookup (file_stat, "", O_READ | O_NOLINK, 0, &doretry, retryname, &file);
+	if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
+	  err = EGRATUITOUS;
+	if (! err)
 	  {
-	    memcpy (buf, rbuf, len);
-	    __vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
+	    err = __io_read (file, &rbuf, &len, 0, len);
+	    if (!err && rbuf != buf)
+	      {
+		memcpy (buf, rbuf, len);
+		__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
+	      }
+
+	    __mach_port_deallocate (__mach_task_self (), file);
 	  }
       }
     else
       err = EINVAL;
 
-  __mach_port_deallocate (__mach_task_self (), file);
+  __mach_port_deallocate (__mach_task_self (), file_stat);
 
   return err ? __hurd_fail (err) : len;
 }

                 reply	other threads:[~2022-09-15 19:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220915195531.540743858C62@sourceware.org \
    --to=sthibaul@sourceware.org \
    --cc=glibc-cvs@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).