public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Jeremy Drake <cygwin@jdrake.com>
To: cygwin-patches@cygwin.com
Subject: [PATCH v4] Cygwin: respect PC_SYM_FOLLOW and PC_SYM_NOFOLLOW_REP with inner links
Date: Tue, 6 Jul 2021 10:40:19 -0700 (PDT)	[thread overview]
Message-ID: <alpine.BSO.2.21.2107061038250.56404@resin.csoft.net> (raw)
In-Reply-To: <YORvS4cn1fQX3O70@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 3929 bytes --]

The new GetFinalPathNameW handling for native symlinks in inner path
components is disabled if caller doesn't want to follow symlinks, or
doesn't want to follow reparse points.
---
 winsup/cygwin/path.cc | 88 ++++++++++++++++++++++---------------------
 1 file changed, 45 insertions(+), 43 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index e62f8fe2b..1869fb8c8 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -722,9 +722,10 @@ path_conv::check (const char *src, unsigned opt,
 	  int symlen = 0;

 	  /* Make sure to check certain flags on last component only. */
-	  for (unsigned pc_flags = opt & (PC_NO_ACCESS_CHECK | PC_KEEP_HANDLE);
+	  for (unsigned pc_flags = opt & (PC_NO_ACCESS_CHECK | PC_KEEP_HANDLE
+					 | PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP);
 	       ;
-	       pc_flags = 0)
+	       pc_flags = opt & (PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP))
 	    {
 	      const suffix_info *suff;
 	      char *full_path;
@@ -3480,48 +3481,49 @@ restart:
 	    goto file_not_symlink;
 	}
 #endif /* __i386__ */
-      {
-	PWCHAR fpbuf = tp.w_get ();
-	DWORD ret;
-
-	ret = GetFinalPathNameByHandleW (h, fpbuf, NT_MAX_PATH, 0);
-	if (ret)
-	  {
-	    UNICODE_STRING fpath;
+      if ((pc_flags & (PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP)) == PC_SYM_FOLLOW)
+	{
+	  PWCHAR fpbuf = tp.w_get ();
+	  DWORD ret;

-	    RtlInitCountedUnicodeString (&fpath, fpbuf, ret * sizeof (WCHAR));
-	    fpbuf[1] = L'?';	/* \\?\ --> \??\ */
-	    if (!RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
-	      {
-		issymlink = true;
-		/* upath.Buffer is big enough and unused from this point on.
-		   Reuse it here, avoiding yet another buffer allocation. */
-		char *nfpath = (char *) upath.Buffer;
-		sys_wcstombs (nfpath, NT_MAX_PATH, fpbuf);
-		res = posixify (nfpath);
-
-		/* If the incoming path consisted of a drive prefix only,
-		   we just handle a virtual drive, created with, e.g.
-
-		     subst X: C:\foo\bar
-
-		   Treat it like a symlink.  This is required to tell an
-		   lstat caller that the "drive" is actually pointing
-		   somewhere else, thus, it's a symlink in POSIX speak. */
-		if (upath.Length == 14)	/* \??\X:\ */
-		  {
-		    fileattr &= ~FILE_ATTRIBUTE_DIRECTORY;
-		    path_flags |= PATH_SYMLINK;
-		  }
-		/* For final paths differing in inner path components return
-		   length as negative value.  This informs path_conv::check
-		   to skip realpath handling on the last path component. */
-		else
-		  res = -res;
-		break;
-	      }
-	  }
-      }
+	  ret = GetFinalPathNameByHandleW (h, fpbuf, NT_MAX_PATH, 0);
+	  if (ret)
+	    {
+	      UNICODE_STRING fpath;
+
+	      RtlInitCountedUnicodeString (&fpath, fpbuf, ret * sizeof (WCHAR));
+	      fpbuf[1] = L'?';	/* \\?\ --> \??\ */
+	      if (!RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
+	        {
+		  issymlink = true;
+		  /* upath.Buffer is big enough and unused from this point on.
+		     Reuse it here, avoiding yet another buffer allocation. */
+		  char *nfpath = (char *) upath.Buffer;
+		  sys_wcstombs (nfpath, NT_MAX_PATH, fpbuf);
+		  res = posixify (nfpath);
+
+		  /* If the incoming path consisted of a drive prefix only,
+		     we just handle a virtual drive, created with, e.g.
+
+		       subst X: C:\foo\bar
+
+		     Treat it like a symlink.  This is required to tell an
+		     lstat caller that the "drive" is actually pointing
+		     somewhere else, thus, it's a symlink in POSIX speak. */
+		  if (upath.Length == 14)	/* \??\X:\ */
+		    {
+		      fileattr &= ~FILE_ATTRIBUTE_DIRECTORY;
+		      path_flags |= PATH_SYMLINK;
+		    }
+		  /* For final paths differing in inner path components return
+		     length as negative value.  This informs path_conv::check
+		     to skip realpath handling on the last path component. */
+		  else
+		    res = -res;
+		  break;
+	        }
+	    }
+	}

     /* Normal file. */
     file_not_symlink:
-- 
2.32.0.windows.1

[-- Attachment #2: Type: text/plain, Size: 4296 bytes --]

From 67a276c35a3b48697c6b61caaf4ffea5a174c75b Mon Sep 17 00:00:00 2001
From: Jeremy Drake <cygwin@jdrake.com>
Date: Sat, 29 May 2021 11:48:11 -0700
Subject: [PATCH] Cygwin: respect PC_SYM_FOLLOW and PC_SYM_NOFOLLOW_REP with
 inner links.

The new GetFinalPathNameW handling for native symlinks in inner path
components is disabled if caller doesn't want to follow symlinks, or
doesn't want to follow reparse points.
---
 winsup/cygwin/path.cc | 88 ++++++++++++++++++++++---------------------
 1 file changed, 45 insertions(+), 43 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index e62f8fe2b..1869fb8c8 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -722,9 +722,10 @@ path_conv::check (const char *src, unsigned opt,
 	  int symlen = 0;
 
 	  /* Make sure to check certain flags on last component only. */
-	  for (unsigned pc_flags = opt & (PC_NO_ACCESS_CHECK | PC_KEEP_HANDLE);
+	  for (unsigned pc_flags = opt & (PC_NO_ACCESS_CHECK | PC_KEEP_HANDLE
+					 | PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP);
 	       ;
-	       pc_flags = 0)
+	       pc_flags = opt & (PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP))
 	    {
 	      const suffix_info *suff;
 	      char *full_path;
@@ -3480,48 +3481,49 @@ restart:
 	    goto file_not_symlink;
 	}
 #endif /* __i386__ */
-      {
-	PWCHAR fpbuf = tp.w_get ();
-	DWORD ret;
-
-	ret = GetFinalPathNameByHandleW (h, fpbuf, NT_MAX_PATH, 0);
-	if (ret)
-	  {
-	    UNICODE_STRING fpath;
+      if ((pc_flags & (PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP)) == PC_SYM_FOLLOW)
+	{
+	  PWCHAR fpbuf = tp.w_get ();
+	  DWORD ret;
 
-	    RtlInitCountedUnicodeString (&fpath, fpbuf, ret * sizeof (WCHAR));
-	    fpbuf[1] = L'?';	/* \\?\ --> \??\ */
-	    if (!RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
-	      {
-		issymlink = true;
-		/* upath.Buffer is big enough and unused from this point on.
-		   Reuse it here, avoiding yet another buffer allocation. */
-		char *nfpath = (char *) upath.Buffer;
-		sys_wcstombs (nfpath, NT_MAX_PATH, fpbuf);
-		res = posixify (nfpath);
-
-		/* If the incoming path consisted of a drive prefix only,
-		   we just handle a virtual drive, created with, e.g.
-
-		     subst X: C:\foo\bar
-
-		   Treat it like a symlink.  This is required to tell an
-		   lstat caller that the "drive" is actually pointing
-		   somewhere else, thus, it's a symlink in POSIX speak. */
-		if (upath.Length == 14)	/* \??\X:\ */
-		  {
-		    fileattr &= ~FILE_ATTRIBUTE_DIRECTORY;
-		    path_flags |= PATH_SYMLINK;
-		  }
-		/* For final paths differing in inner path components return
-		   length as negative value.  This informs path_conv::check
-		   to skip realpath handling on the last path component. */
-		else
-		  res = -res;
-		break;
-	      }
-	  }
-      }
+	  ret = GetFinalPathNameByHandleW (h, fpbuf, NT_MAX_PATH, 0);
+	  if (ret)
+	    {
+	      UNICODE_STRING fpath;
+
+	      RtlInitCountedUnicodeString (&fpath, fpbuf, ret * sizeof (WCHAR));
+	      fpbuf[1] = L'?';	/* \\?\ --> \??\ */
+	      if (!RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
+	        {
+		  issymlink = true;
+		  /* upath.Buffer is big enough and unused from this point on.
+		     Reuse it here, avoiding yet another buffer allocation. */
+		  char *nfpath = (char *) upath.Buffer;
+		  sys_wcstombs (nfpath, NT_MAX_PATH, fpbuf);
+		  res = posixify (nfpath);
+
+		  /* If the incoming path consisted of a drive prefix only,
+		     we just handle a virtual drive, created with, e.g.
+
+		       subst X: C:\foo\bar
+
+		     Treat it like a symlink.  This is required to tell an
+		     lstat caller that the "drive" is actually pointing
+		     somewhere else, thus, it's a symlink in POSIX speak. */
+		  if (upath.Length == 14)	/* \??\X:\ */
+		    {
+		      fileattr &= ~FILE_ATTRIBUTE_DIRECTORY;
+		      path_flags |= PATH_SYMLINK;
+		    }
+		  /* For final paths differing in inner path components return
+		     length as negative value.  This informs path_conv::check
+		     to skip realpath handling on the last path component. */
+		  else
+		    res = -res;
+		  break;
+	        }
+	    }
+	}
 
     /* Normal file. */
     file_not_symlink:
-- 
2.32.0.windows.1


  parent reply	other threads:[~2021-07-06 17:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-29 20:34 [PATCH] Cygwin: tweak handling of native symlinks from chdir Jeremy Drake
2021-05-29 23:15 ` Jeremy Drake
2021-05-30  6:05   ` Jeremy Drake
2021-05-30 19:58     ` [PATCH v2] Cygwin: respect PC_SYM_FOLLOW and PC_SYM_NOFOLLOW_REP with inner links Jeremy Drake
2021-05-31  8:02       ` Corinna Vinschen
2021-05-31  8:17         ` Corinna Vinschen
2021-05-31 17:55           ` Jeremy Drake
2021-07-03 21:01           ` Jeremy Drake
2021-07-07 18:52           ` Jeremy Drake
2021-07-08 14:48             ` Corinna Vinschen
2021-06-03 20:29         ` [PATCH v3] " Jeremy Drake
2021-06-03 20:57           ` Jeremy Drake
2021-07-06 14:57             ` Corinna Vinschen
2021-07-06 17:38               ` Jeremy Drake
2021-07-06 17:40               ` Jeremy Drake [this message]
2021-07-07  8:47                 ` [PATCH v4] " Corinna Vinschen
2021-07-07  6:50               ` [PATCH v3] " Jeremy Drake

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=alpine.BSO.2.21.2107061038250.56404@resin.csoft.net \
    --to=cygwin@jdrake.com \
    --cc=cygwin-patches@cygwin.com \
    /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).