Skip to content

Commit e34bdc0

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 d527197 commit e34bdc0

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

370+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
371+
370372
enum phantom_symlink_result {
371373
PHANTOM_SYMLINK_RETRY,
372374
PHANTOM_SYMLINK_DONE,
@@ -417,7 +419,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
417419
return PHANTOM_SYMLINK_DONE;
418420

419421
/* otherwise recreate the symlink with directory flag */
420-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
422+
if (DeleteFileW(wlink) &&
423+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
421424
return PHANTOM_SYMLINK_DIRECTORY;
422425

423426
errno = err_win_to_posix(GetLastError());
@@ -3049,7 +3052,7 @@ int symlink(const char *target, const char *link)
30493052
wtarget[len] = '\\';
30503053

30513054
/* create file symlink */
3052-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
3055+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
30533056
errno = err_win_to_posix(GetLastError());
30543057
return -1;
30553058
}
@@ -3985,6 +3988,24 @@ static void maybe_redirect_std_handles(void)
39853988
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
39863989
}
39873990

3991+
static void adjust_symlink_flags(void)
3992+
{
3993+
/*
3994+
* Starting with Windows 10 Build 14972, symbolic links can be created
3995+
* using CreateSymbolicLink() without elevation by passing the flag
3996+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3997+
* parameter, provided the Developer Mode has been enabled. Some
3998+
* earlier Windows versions complain about this flag with an
3999+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
4000+
* specifically.
4001+
*/
4002+
if (GetVersion() >= 14972 << 16) {
4003+
symlink_file_flags |= 2;
4004+
symlink_directory_flags |= 2;
4005+
}
4006+
4007+
}
4008+
39884009
#ifdef _MSC_VER
39894010
#ifdef _DEBUG
39904011
#include <crtdbg.h>
@@ -4020,6 +4041,7 @@ int wmain(int argc, const wchar_t **wargv)
40204041
#endif
40214042

40224043
maybe_redirect_std_handles();
4044+
adjust_symlink_flags();
40234045
fsync_object_files = 1;
40244046

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

0 commit comments

Comments
 (0)