Skip to content

Commit cbaedc8

Browse files
committed
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 a439a9c commit cbaedc8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

compat/mingw.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
319319
return 0;
320320
}
321321

322+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
323+
322324
enum phantom_symlink_result {
323325
PHANTOM_SYMLINK_RETRY,
324326
PHANTOM_SYMLINK_DONE,
@@ -404,7 +406,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
404406
return PHANTOM_SYMLINK_DONE;
405407

406408
/* otherwise recreate the symlink with directory flag */
407-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
409+
if (DeleteFileW(wlink) &&
410+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
408411
return PHANTOM_SYMLINK_DIRECTORY;
409412

410413
errno = err_win_to_posix(GetLastError());
@@ -2860,7 +2863,7 @@ int symlink(const char *target, const char *link)
28602863
wtarget[len] = '\\';
28612864

28622865
/* create file symlink */
2863-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2866+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
28642867
errno = err_win_to_posix(GetLastError());
28652868
return -1;
28662869
}
@@ -3747,6 +3750,24 @@ static void maybe_redirect_std_handles(void)
37473750
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
37483751
}
37493752

3753+
static void adjust_symlink_flags(void)
3754+
{
3755+
/*
3756+
* Starting with Windows 10 Build 14972, symbolic links can be created
3757+
* using CreateSymbolicLink() without elevation by passing the flag
3758+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3759+
* parameter, provided the Developer Mode has been enabled. Some
3760+
* earlier Windows versions complain about this flag with an
3761+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3762+
* specifically.
3763+
*/
3764+
if (GetVersion() >= 14972 << 16) {
3765+
symlink_file_flags |= 2;
3766+
symlink_directory_flags |= 2;
3767+
}
3768+
3769+
}
3770+
37503771
#ifdef _MSC_VER
37513772
#ifdef _DEBUG
37523773
#include <crtdbg.h>
@@ -3781,6 +3802,7 @@ int wmain(int argc, const wchar_t **wargv)
37813802
#endif
37823803

37833804
maybe_redirect_std_handles();
3805+
adjust_symlink_flags();
37843806
fsync_object_files = 1;
37853807

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

0 commit comments

Comments
 (0)