Skip to content

Commit 8a8209a

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 c2d7566 commit 8a8209a

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
@@ -271,6 +271,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
271271
return 0;
272272
}
273273

274+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
275+
274276
enum phantom_symlink_result {
275277
PHANTOM_SYMLINK_RETRY,
276278
PHANTOM_SYMLINK_DONE,
@@ -356,7 +358,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
356358
return PHANTOM_SYMLINK_DONE;
357359

358360
/* otherwise recreate the symlink with directory flag */
359-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
361+
if (DeleteFileW(wlink) &&
362+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
360363
return PHANTOM_SYMLINK_DIRECTORY;
361364

362365
errno = err_win_to_posix(GetLastError());
@@ -2403,7 +2406,7 @@ int symlink(const char *target, const char *link)
24032406
wtarget[len] = '\\';
24042407

24052408
/* create file symlink */
2406-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2409+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
24072410
errno = err_win_to_posix(GetLastError());
24082411
return -1;
24092412
}
@@ -2920,6 +2923,24 @@ static void maybe_redirect_std_handles(void)
29202923
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
29212924
}
29222925

2926+
static void adjust_symlink_flags(void)
2927+
{
2928+
/*
2929+
* Starting with Windows 10 Build 14972, symbolic links can be created
2930+
* using CreateSymbolicLink() without elevation by passing the flag
2931+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
2932+
* parameter, provided the Developer Mode has been enabled. Some
2933+
* earlier Windows versions complain about this flag with an
2934+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
2935+
* specifically.
2936+
*/
2937+
if (GetVersion() >= 14972 << 16) {
2938+
symlink_file_flags |= 2;
2939+
symlink_directory_flags |= 2;
2940+
}
2941+
2942+
}
2943+
29232944
void mingw_startup(void)
29242945
{
29252946
int i, maxlen, argc;
@@ -2928,6 +2949,7 @@ void mingw_startup(void)
29282949
_startupinfo si;
29292950

29302951
maybe_redirect_std_handles();
2952+
adjust_symlink_flags();
29312953

29322954
/* get wide char arguments and environment */
29332955
si.newmode = 0;

0 commit comments

Comments
 (0)