Skip to content

Commit bed39ff

Browse files
dschoGit for Windows Build Agent
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 81a791f commit bed39ff

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
@@ -288,6 +288,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
288288
return 0;
289289
}
290290

291+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
292+
291293
enum phantom_symlink_result {
292294
PHANTOM_SYMLINK_RETRY,
293295
PHANTOM_SYMLINK_DONE,
@@ -373,7 +375,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
373375
return PHANTOM_SYMLINK_DONE;
374376

375377
/* otherwise recreate the symlink with directory flag */
376-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
378+
if (DeleteFileW(wlink) &&
379+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
377380
return PHANTOM_SYMLINK_DIRECTORY;
378381

379382
errno = err_win_to_posix(GetLastError());
@@ -2386,7 +2389,7 @@ int symlink(const char *target, const char *link)
23862389
wtarget[len] = '\\';
23872390

23882391
/* create file symlink */
2389-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2392+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
23902393
errno = err_win_to_posix(GetLastError());
23912394
return -1;
23922395
}
@@ -2898,6 +2901,24 @@ static void maybe_redirect_std_handles(void)
28982901
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
28992902
}
29002903

2904+
static void adjust_symlink_flags(void)
2905+
{
2906+
/*
2907+
* Starting with Windows 10 Build 14972, symbolic links can be created
2908+
* using CreateSymbolicLink() without elevation by passing the flag
2909+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
2910+
* parameter, provided the Developer Mode has been enabled. Some
2911+
* earlier Windows versions complain about this flag with an
2912+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
2913+
* specifically.
2914+
*/
2915+
if (GetVersion() >= 14972 << 16) {
2916+
symlink_file_flags |= 2;
2917+
symlink_directory_flags |= 2;
2918+
}
2919+
2920+
}
2921+
29012922
#ifdef _MSC_VER
29022923
#ifdef _DEBUG
29032924
#include <crtdbg.h>
@@ -2932,6 +2953,7 @@ int wmain(int argc, const wchar_t **wargv)
29322953
#endif
29332954

29342955
maybe_redirect_std_handles();
2956+
adjust_symlink_flags();
29352957
fsync_object_files = 1;
29362958

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

0 commit comments

Comments
 (0)