Skip to content

Commit 8766b2e

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 4faf6e7 commit 8766b2e

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
@@ -297,6 +297,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
297297
return 0;
298298
}
299299

300+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
301+
300302
enum phantom_symlink_result {
301303
PHANTOM_SYMLINK_RETRY,
302304
PHANTOM_SYMLINK_DONE,
@@ -382,7 +384,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
382384
return PHANTOM_SYMLINK_DONE;
383385

384386
/* otherwise recreate the symlink with directory flag */
385-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
387+
if (DeleteFileW(wlink) &&
388+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
386389
return PHANTOM_SYMLINK_DIRECTORY;
387390

388391
errno = err_win_to_posix(GetLastError());
@@ -2730,7 +2733,7 @@ int symlink(const char *target, const char *link)
27302733
wtarget[len] = '\\';
27312734

27322735
/* create file symlink */
2733-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2736+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
27342737
errno = err_win_to_posix(GetLastError());
27352738
return -1;
27362739
}
@@ -3435,6 +3438,24 @@ static void maybe_redirect_std_handles(void)
34353438
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
34363439
}
34373440

3441+
static void adjust_symlink_flags(void)
3442+
{
3443+
/*
3444+
* Starting with Windows 10 Build 14972, symbolic links can be created
3445+
* using CreateSymbolicLink() without elevation by passing the flag
3446+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3447+
* parameter, provided the Developer Mode has been enabled. Some
3448+
* earlier Windows versions complain about this flag with an
3449+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3450+
* specifically.
3451+
*/
3452+
if (GetVersion() >= 14972 << 16) {
3453+
symlink_file_flags |= 2;
3454+
symlink_directory_flags |= 2;
3455+
}
3456+
3457+
}
3458+
34383459
#ifdef _MSC_VER
34393460
#ifdef _DEBUG
34403461
#include <crtdbg.h>
@@ -3469,6 +3490,7 @@ int wmain(int argc, const wchar_t **wargv)
34693490
#endif
34703491

34713492
maybe_redirect_std_handles();
3493+
adjust_symlink_flags();
34723494
fsync_object_files = 1;
34733495

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

0 commit comments

Comments
 (0)