Skip to content

Commit bd7139c

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 9397462 commit bd7139c

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
@@ -358,6 +358,8 @@ static const wchar_t *make_relative_to(const wchar_t *path,
358358
return out;
359359
}
360360

361+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
362+
361363
enum phantom_symlink_result {
362364
PHANTOM_SYMLINK_RETRY,
363365
PHANTOM_SYMLINK_DONE,
@@ -408,7 +410,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
408410
return PHANTOM_SYMLINK_DONE;
409411

410412
/* otherwise recreate the symlink with directory flag */
411-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
413+
if (DeleteFileW(wlink) &&
414+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
412415
return PHANTOM_SYMLINK_DIRECTORY;
413416

414417
errno = err_win_to_posix(GetLastError());
@@ -3079,7 +3082,7 @@ int symlink(const char *target, const char *link)
30793082
wtarget[len] = '\\';
30803083

30813084
/* create file symlink */
3082-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
3085+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
30833086
errno = err_win_to_posix(GetLastError());
30843087
return -1;
30853088
}
@@ -4026,6 +4029,24 @@ static void maybe_redirect_std_handles(void)
40264029
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
40274030
}
40284031

4032+
static void adjust_symlink_flags(void)
4033+
{
4034+
/*
4035+
* Starting with Windows 10 Build 14972, symbolic links can be created
4036+
* using CreateSymbolicLink() without elevation by passing the flag
4037+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
4038+
* parameter, provided the Developer Mode has been enabled. Some
4039+
* earlier Windows versions complain about this flag with an
4040+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
4041+
* specifically.
4042+
*/
4043+
if (GetVersion() >= 14972 << 16) {
4044+
symlink_file_flags |= 2;
4045+
symlink_directory_flags |= 2;
4046+
}
4047+
4048+
}
4049+
40294050
#ifdef _MSC_VER
40304051
#ifdef _DEBUG
40314052
#include <crtdbg.h>
@@ -4061,6 +4082,7 @@ int wmain(int argc, const wchar_t **wargv)
40614082
#endif
40624083

40634084
maybe_redirect_std_handles();
4085+
adjust_symlink_flags();
40644086
fsync_object_files = 1;
40654087

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

0 commit comments

Comments
 (0)