Skip to content

Commit c96e816

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 102cf11 commit c96e816

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
@@ -365,6 +365,8 @@ static const wchar_t *make_relative_to(const wchar_t *path,
365365
return out;
366366
}
367367

368+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
369+
368370
enum phantom_symlink_result {
369371
PHANTOM_SYMLINK_RETRY,
370372
PHANTOM_SYMLINK_DONE,
@@ -415,7 +417,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
415417
return PHANTOM_SYMLINK_DONE;
416418

417419
/* otherwise recreate the symlink with directory flag */
418-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
420+
if (DeleteFileW(wlink) &&
421+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
419422
return PHANTOM_SYMLINK_DIRECTORY;
420423

421424
errno = err_win_to_posix(GetLastError());
@@ -2901,7 +2904,7 @@ int symlink(const char *target, const char *link)
29012904
wtarget[len] = '\\';
29022905

29032906
/* create file symlink */
2904-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2907+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
29052908
errno = err_win_to_posix(GetLastError());
29062909
return -1;
29072910
}
@@ -3837,6 +3840,24 @@ static void maybe_redirect_std_handles(void)
38373840
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
38383841
}
38393842

3843+
static void adjust_symlink_flags(void)
3844+
{
3845+
/*
3846+
* Starting with Windows 10 Build 14972, symbolic links can be created
3847+
* using CreateSymbolicLink() without elevation by passing the flag
3848+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3849+
* parameter, provided the Developer Mode has been enabled. Some
3850+
* earlier Windows versions complain about this flag with an
3851+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3852+
* specifically.
3853+
*/
3854+
if (GetVersion() >= 14972 << 16) {
3855+
symlink_file_flags |= 2;
3856+
symlink_directory_flags |= 2;
3857+
}
3858+
3859+
}
3860+
38403861
#ifdef _MSC_VER
38413862
#ifdef _DEBUG
38423863
#include <crtdbg.h>
@@ -3871,6 +3892,7 @@ int wmain(int argc, const wchar_t **wargv)
38713892
#endif
38723893

38733894
maybe_redirect_std_handles();
3895+
adjust_symlink_flags();
38743896
fsync_object_files = 1;
38753897

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

0 commit comments

Comments
 (0)