public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: realpath: fix cygwin installation dir being access via junction
Date: Fri, 19 Feb 2021 18:21:33 +0000 (GMT)	[thread overview]
Message-ID: <20210219182133.06E253857C48@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=6d898f43fc87a7bf3ab100d75538e381bd86f657

commit 6d898f43fc87a7bf3ab100d75538e381bd86f657
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Fri Feb 19 18:15:58 2021 +0100

    Cygwin: realpath: fix cygwin installation dir being access via junction
    
    Consider this case:
    
    - Cygwin installed in C:\cygwin64
    
    - mklink /j D:\cygwin64 C:\cygwin64
    
    - create testcase calling
    
        realpath("/", result);
        printf ("%s\n", result);
    
    - start cmd
    
        >C:\cygwin64\bin\bash -lc <path-to-testcase>
        /
        >D\cygwin64\bin\bash -lc <path-to-testcase>
        /cygdrive/c/cygwin64
    
    This scenario circumventing the mount point handling which is automated
    in terms of /, depending on the path returned from GetModuleFileNameW
    for the Cygwin DLL.  When calling D:\cygwin64\bin\bash the dir returned
    from GetModuleFileNameW is D:\cygwin64\bin, thus root is D:\cygwin64.
    
    However, junctions are treated as symlinks in Cygwin which explains why
    the path gets converted to a cygdrive path.
    
    Fix this by calling GetFinalPathNameByHandleW on the result from
    GetModuleFileNameW to get the correct root path, even if accessed via
    a junction point.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/cygheap.cc | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/winsup/cygwin/cygheap.cc b/winsup/cygwin/cygheap.cc
index 3a32c352d..01b49468e 100644
--- a/winsup/cygwin/cygheap.cc
+++ b/winsup/cygwin/cygheap.cc
@@ -144,6 +144,19 @@ init_cygheap::init_installation_root ()
     api_fatal ("Can't initialize Cygwin installation root dir.\n"
 	       "GetModuleFileNameW(%p, %p, %u), %E",
 	       cygwin_hmodule, installation_root_buf, PATH_MAX);
+
+  /* We don't care if fetching the final pathname fails, it's non-fatal and
+     the path returned by GetModuleFileNameW is still valid. */
+  HANDLE h;
+  h = CreateFileW (installation_root_buf, GENERIC_READ, FILE_SHARE_VALID_FLAGS,
+		   &sec_none, OPEN_EXISTING, 0, 0);
+  if (h != INVALID_HANDLE_VALUE)
+    {
+      GetFinalPathNameByHandleW (h, installation_root_buf, PATH_MAX,
+				 FILE_NAME_NORMALIZED);
+      CloseHandle (h);
+    }
+
   PWCHAR p = installation_root_buf;
   if (wcsncasecmp (p, L"\\\\", 2))	/* Normal drive letter path */
     {


                 reply	other threads:[~2021-02-19 18:21 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=20210219182133.06E253857C48@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-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).