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 v2] Cygwin: respect PC_SYM_FOLLOW and PC_SYM_NOFOLLOW_REP with inner links
Date: Sun, 30 May 2021 12:58:30 -0700 (PDT)	[thread overview]
Message-ID: <alpine.BSO.2.21.2105301213380.30039@resin.csoft.net> (raw)
In-Reply-To: <alpine.BSO.2.21.2105292259570.30039@resin.csoft.net>

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

First, revert the handling of virtual drives as non-symlinks.  This is no
longer necessary.

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.  Set flag to not follow reparse
points in chdir, allowing native processes to see their cwd potentially
including native symlinks, rather than dereferencing them.
---

For v2, I realized the PC_SYM_NOFOLLOW_REP flag was supposed to do this,
and that lack of PC_SYM_FOLLOW was not being respected either.  With this,
and patching `pwd -P` to `pwd` in makepkg, the long-named package builds
successfully.  I did not re-indent the code for the addition of the if due
to having learned from my patch to rebase, but it looks kind of ugly.

 winsup/cygwin/path.cc | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index e62f8fe2b..2ce5aef81 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -722,9 +722,9 @@ 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;
@@ -3452,6 +3452,8 @@ restart:
 	    break;
 	}

+      if ((pc_flags & (PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP)) == PC_SYM_FOLLOW)
+      {
       /* Check if the inner path components contain native symlinks or
 	 junctions, or if the drive is a virtual drive.  Compare incoming
 	 path with path returned by GetFinalPathNameByHandleA.  If they
@@ -3522,6 +3524,7 @@ restart:
 	      }
 	  }
       }
+      }

     /* Normal file. */
     file_not_symlink:
@@ -3704,7 +3707,7 @@ chdir (const char *in_dir)

       /* Convert path.  PC_NONULLEMPTY ensures that we don't check for
 	 NULL/empty/invalid again. */
-      path_conv path (in_dir, PC_SYM_FOLLOW | PC_POSIX | PC_NONULLEMPTY);
+      path_conv path (in_dir, PC_SYM_FOLLOW | PC_POSIX | PC_NONULLEMPTY | PC_SYM_NOFOLLOW_REP);
       if (path.error)
 	{
 	  set_errno (path.error);
-- 
2.31.1.windows.1

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

From 4bb959b57606465d5a7abe7d3ae168db66f5f6fa Mon Sep 17 00:00:00 2001
From: Jeremy Drake <cygwin@jdrake.com>
Date: Sat, 29 May 2021 13:17:08 -0700
Subject: [PATCH 1/2] Revert "Cygwin: Handle virtual drives as non-symlinks"

This reverts commit c8949d04001e3dbc03651475b6cd1c5623400835.
---
 winsup/cygwin/path.cc | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index 6a07f0d06..e62f8fe2b 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -3505,9 +3505,14 @@ restart:
 
 		     subst X: C:\foo\bar
 
-		   Treat it as a normal file. */
+		   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:\ */
-		  goto file_not_symlink;
+		  {
+		    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. */
-- 
2.31.1.windows.1


[-- Attachment #3: Type: text/plain, Size: 2255 bytes --]

From 9a1d868c3e027416876d9bd110161562f8b77b0a Mon Sep 17 00:00:00 2001
From: Jeremy Drake <cygwin@jdrake.com>
Date: Sat, 29 May 2021 11:48:11 -0700
Subject: [PATCH 2/2] 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.  Set flag to not follow reparse
points in chdir, allowing native processes to see their cwd potentially
including native symlinks, rather than dereferencing them.
---
 winsup/cygwin/path.cc | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index e62f8fe2b..2ce5aef81 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -722,9 +722,9 @@ 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;
@@ -3452,6 +3452,8 @@ restart:
 	    break;
 	}
 
+      if ((pc_flags & (PC_SYM_FOLLOW | PC_SYM_NOFOLLOW_REP)) == PC_SYM_FOLLOW)
+      {
       /* Check if the inner path components contain native symlinks or
 	 junctions, or if the drive is a virtual drive.  Compare incoming
 	 path with path returned by GetFinalPathNameByHandleA.  If they
@@ -3522,6 +3524,7 @@ restart:
 	      }
 	  }
       }
+      }
 
     /* Normal file. */
     file_not_symlink:
@@ -3704,7 +3707,7 @@ chdir (const char *in_dir)
 
       /* Convert path.  PC_NONULLEMPTY ensures that we don't check for
 	 NULL/empty/invalid again. */
-      path_conv path (in_dir, PC_SYM_FOLLOW | PC_POSIX | PC_NONULLEMPTY);
+      path_conv path (in_dir, PC_SYM_FOLLOW | PC_POSIX | PC_NONULLEMPTY | PC_SYM_NOFOLLOW_REP);
       if (path.error)
 	{
 	  set_errno (path.error);
-- 
2.31.1.windows.1


  reply	other threads:[~2021-05-30 19:58 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     ` Jeremy Drake [this message]
2021-05-31  8:02       ` [PATCH v2] Cygwin: respect PC_SYM_FOLLOW and PC_SYM_NOFOLLOW_REP with inner links 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               ` [PATCH v4] " Jeremy Drake
2021-07-07  8:47                 ` 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.2105301213380.30039@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).