Skip to content

Commit 15b5228

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 17d6544 commit 15b5228

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
@@ -285,6 +285,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
285285
return 0;
286286
}
287287

288+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
289+
288290
enum phantom_symlink_result {
289291
PHANTOM_SYMLINK_RETRY,
290292
PHANTOM_SYMLINK_DONE,
@@ -370,7 +372,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
370372
return PHANTOM_SYMLINK_DONE;
371373

372374
/* otherwise recreate the symlink with directory flag */
373-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
375+
if (DeleteFileW(wlink) &&
376+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
374377
return PHANTOM_SYMLINK_DIRECTORY;
375378

376379
errno = err_win_to_posix(GetLastError());
@@ -2367,7 +2370,7 @@ int symlink(const char *target, const char *link)
23672370
wtarget[len] = '\\';
23682371

23692372
/* create file symlink */
2370-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2373+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
23712374
errno = err_win_to_posix(GetLastError());
23722375
return -1;
23732376
}
@@ -2855,6 +2858,24 @@ static void maybe_redirect_std_handles(void)
28552858
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
28562859
}
28572860

2861+
static void adjust_symlink_flags(void)
2862+
{
2863+
/*
2864+
* Starting with Windows 10 Build 14972, symbolic links can be created
2865+
* using CreateSymbolicLink() without elevation by passing the flag
2866+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
2867+
* parameter, provided the Developer Mode has been enabled. Some
2868+
* earlier Windows versions complain about this flag with an
2869+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
2870+
* specifically.
2871+
*/
2872+
if (GetVersion() >= 14972 << 16) {
2873+
symlink_file_flags |= 2;
2874+
symlink_directory_flags |= 2;
2875+
}
2876+
2877+
}
2878+
28582879
#ifdef _MSC_VER
28592880
#ifdef _DEBUG
28602881
#include <crtdbg.h>
@@ -2889,6 +2910,7 @@ int wmain(int argc, const wchar_t **wargv)
28892910
#endif
28902911

28912912
maybe_redirect_std_handles();
2913+
adjust_symlink_flags();
28922914
fsync_object_files = 1;
28932915

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

0 commit comments

Comments
 (0)