Skip to content

Commit 1bf3aea

Browse files
dschovdye
authored andcommitted
mingw: try to create symlinks without elevated permissions
With Windows 10 Build 14972 in Developer Mode, a new flag is supported by CreateSymbolicLink() to create symbolic links even when running outside of an elevated session (which was previously required). This new flag is called SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE and has the numeric value 0x02. Previous Windows 10 versions will not understand that flag and return an ERROR_INVALID_PARAMETER, therefore we have to be careful to try passing that flag only when the build number indicates that it is supported. For more information about the new flag, see this blog post: https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/ This patch is loosely based on the patch submitted by Samuel D. Leslie as #1184. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent df258c9 commit 1bf3aea

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

compat/mingw.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
300300
return 0;
301301
}
302302

303+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
304+
303305
enum phantom_symlink_result {
304306
PHANTOM_SYMLINK_RETRY,
305307
PHANTOM_SYMLINK_DONE,
@@ -385,7 +387,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
385387
return PHANTOM_SYMLINK_DONE;
386388

387389
/* otherwise recreate the symlink with directory flag */
388-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
390+
if (DeleteFileW(wlink) &&
391+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
389392
return PHANTOM_SYMLINK_DIRECTORY;
390393

391394
errno = err_win_to_posix(GetLastError());
@@ -2816,7 +2819,7 @@ int symlink(const char *target, const char *link)
28162819
wtarget[len] = '\\';
28172820

28182821
/* create file symlink */
2819-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2822+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
28202823
errno = err_win_to_posix(GetLastError());
28212824
return -1;
28222825
}
@@ -3638,6 +3641,24 @@ static void maybe_redirect_std_handles(void)
36383641
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
36393642
}
36403643

3644+
static void adjust_symlink_flags(void)
3645+
{
3646+
/*
3647+
* Starting with Windows 10 Build 14972, symbolic links can be created
3648+
* using CreateSymbolicLink() without elevation by passing the flag
3649+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3650+
* parameter, provided the Developer Mode has been enabled. Some
3651+
* earlier Windows versions complain about this flag with an
3652+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3653+
* specifically.
3654+
*/
3655+
if (GetVersion() >= 14972 << 16) {
3656+
symlink_file_flags |= 2;
3657+
symlink_directory_flags |= 2;
3658+
}
3659+
3660+
}
3661+
36413662
#ifdef _MSC_VER
36423663
#ifdef _DEBUG
36433664
#include <crtdbg.h>
@@ -3672,6 +3693,7 @@ int wmain(int argc, const wchar_t **wargv)
36723693
#endif
36733694

36743695
maybe_redirect_std_handles();
3696+
adjust_symlink_flags();
36753697
fsync_object_files = 1;
36763698

36773699
/* determine size of argv and environ conversion buffer */

0 commit comments

Comments
 (0)