Skip to content

Commit 6c085c3

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 514775f commit 6c085c3

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

371+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
372+
371373
enum phantom_symlink_result {
372374
PHANTOM_SYMLINK_RETRY,
373375
PHANTOM_SYMLINK_DONE,
@@ -418,7 +420,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
418420
return PHANTOM_SYMLINK_DONE;
419421

420422
/* otherwise recreate the symlink with directory flag */
421-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
423+
if (DeleteFileW(wlink) &&
424+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
422425
return PHANTOM_SYMLINK_DIRECTORY;
423426

424427
errno = err_win_to_posix(GetLastError());
@@ -3137,7 +3140,7 @@ int symlink(const char *target, const char *link)
31373140
wtarget[len] = '\\';
31383141

31393142
/* create file symlink */
3140-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
3143+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
31413144
errno = err_win_to_posix(GetLastError());
31423145
return -1;
31433146
}
@@ -4073,6 +4076,24 @@ static void maybe_redirect_std_handles(void)
40734076
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
40744077
}
40754078

4079+
static void adjust_symlink_flags(void)
4080+
{
4081+
/*
4082+
* Starting with Windows 10 Build 14972, symbolic links can be created
4083+
* using CreateSymbolicLink() without elevation by passing the flag
4084+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
4085+
* parameter, provided the Developer Mode has been enabled. Some
4086+
* earlier Windows versions complain about this flag with an
4087+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
4088+
* specifically.
4089+
*/
4090+
if (GetVersion() >= 14972 << 16) {
4091+
symlink_file_flags |= 2;
4092+
symlink_directory_flags |= 2;
4093+
}
4094+
4095+
}
4096+
40764097
#ifdef _MSC_VER
40774098
#ifdef _DEBUG
40784099
#include <crtdbg.h>
@@ -4108,6 +4129,7 @@ int wmain(int argc, const wchar_t **wargv)
41084129
#endif
41094130

41104131
maybe_redirect_std_handles();
4132+
adjust_symlink_flags();
41114133
fsync_object_files = 1;
41124134

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

0 commit comments

Comments
 (0)