From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.17.24]) by sourceware.org (Postfix) with ESMTPS id C0C31386100E for ; Mon, 15 Feb 2021 13:20:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C0C31386100E Received: from calimero.vinschen.de ([24.134.7.25]) by mrelayeu.kundenserver.de (mreue109 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MEmAX-1l4iyb2AXb-00GHat for ; Mon, 15 Feb 2021 14:20:54 +0100 Received: by calimero.vinschen.de (Postfix, from userid 500) id A6CDAA80D37; Mon, 15 Feb 2021 14:20:53 +0100 (CET) Date: Mon, 15 Feb 2021 14:20:53 +0100 From: Corinna Vinschen To: cygwin-developers@cygwin.com Subject: Re: Root directory being a junction fools realpath() Message-ID: <20210215132053.GO4251@calimero.vinschen.de> Reply-To: cygwin-developers@cygwin.com Mail-Followup-To: cygwin-developers@cygwin.com References: <20210215122538.GN4251@calimero.vinschen.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210215122538.GN4251@calimero.vinschen.de> X-Provags-ID: V03:K1:mWulPsO/KF2ge1+QTPbRqoXuWpv2alCY4d5H4grW3ei+ynAOG8Y NmkFF8F0IV4TPNYcWhUxUlEFSIh00AtTOGJeId6ijlOwD52/ao3DEnrmze+xivEpfrexFPF 5it27/Tt5Bi/J1daFuGdLfjrndXjyADLle1IeZQz/J3cu5JmT8Zpc/w+kuJBP0RCIdxc+tY 5THSnqAvjrLJ7XmLmGp9w== X-UI-Out-Filterresults: notjunk:1;V03:K0:Uru0niVh6/M=:yYcKJnmlJtmC6aHiMVj2R5 cKTBGk87D22LAsLAIkNYoZmwnm/nMl8ifEoc0lPVuYoiTuxFaQ6L98mj77CaaZJ4FZF9wFMeg +h3eWDIictsnXHlo0qqQN0es5UR0GK30wDLu4EKQT3KrXEOg9hsmA9d48WlzNTOyumWnjIcp+ +lKi/H0tJsVaycj0iihKZhaHbYkR/Yuoc/6j+8cbe1+3rdkT2Cy9XotpxdudGDl18H8rGT0qB u4opsAxUNdVEsL3YBlOrTuZIweqKYxh7r1dLpmt0uXcJruhXSgyG2O0l7EIewpOHUrxrAlZGd d2nx9CMmeLrgT7RGms1aXdaD16Wo/OFU2zjJIluPDPSoNjHkSgD4q6EPKNdNeKOXioKl+4YBG wKzHVDz9zKatB1BJ13SRqxyj7JQzwfRhR5oiCHRedT3Hs9D17FB5CFrXRYKowq3Ab2MME2rOP ci4XwAeaBQ== X-Spam-Status: No, score=-107.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GOOD_FROM_CORINNA_CYGWIN, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: cygwin-developers@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component developers mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Feb 2021 13:20:57 -0000 On Feb 15 13:25, Corinna Vinschen via Cygwin-developers wrote: > On Feb 14 12:56, David Macek via Cygwin-developers wrote: > > Greetings. > > > > I think I found a way to fool Cygwin into misplacing its root > > directory. Given recent Cygwin with GCC and a simple test program: > > > > ``` > > #include > > #include > > #include > > int main() { > > char real[PATH_MAX]; > > realpath("/", real); > > printf("/ -> %s\n", real); > > return 0; > > } > > ``` > > > > ... compiled with: > > > > $ gcc -D_DEFAULT_SOURCE -o new.exe -Wall -Wextra -std=c11 new.c > > > > ... and my set-up with a junction (`mklink /j link target`) realpath() > > result is dependent on which path I use to invoke the Cygwin root > > process: > > > > > dir C:\ | findstr cygwin64 > > 10. 02. 2021 12:13 cygwin64 > > > > > dir D:\ | findstr cygwin64 > > 14. 02. 2021 12:19 cygwin64 [C:\cygwin64] > > > > > C:\cygwin64\bin\bash -lc /cygdrive/w/new > > / -> / > > > > > D:\cygwin64\bin\bash -lc /cygdrive/w/new > > / -> /cygdrive/c/cygwin64 > > This isn't realpath's fault. You're circumventing the mount point > handling which is automated in terms of /, depending on the path > returned from GetModuleFileNameW for the Cygwin DLL. Since you're > 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. Thus the result > you see above. A potential workaround is this: diff --git a/winsup/cygwin/cygheap.cc b/winsup/cygwin/cygheap.cc index 3a32c352d4e9..01b49468e804 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 */ { I just wonder if such a bordercase is worth the extra performance hit for each Cygwin invocation. Corinna