Skip to content

Commit d3b89c2

Browse files
committed
mingw: try to create symlinks without elevated permissions
As of 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/ Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 7b6dbc7 commit d3b89c2

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
@@ -331,6 +331,8 @@ static const wchar_t *make_relative_to(const wchar_t *path,
331331
return out;
332332
}
333333

334+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
335+
334336
enum phantom_symlink_result {
335337
PHANTOM_SYMLINK_RETRY,
336338
PHANTOM_SYMLINK_DONE,
@@ -381,7 +383,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
381383
return PHANTOM_SYMLINK_DONE;
382384

383385
/* otherwise recreate the symlink with directory flag */
384-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
386+
if (DeleteFileW(wlink) &&
387+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
385388
return PHANTOM_SYMLINK_DIRECTORY;
386389

387390
errno = err_win_to_posix(GetLastError());
@@ -2846,7 +2849,7 @@ int symlink(const char *target, const char *link)
28462849
wtarget[len] = '\\';
28472850

28482851
/* create file symlink */
2849-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2852+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
28502853
errno = err_win_to_posix(GetLastError());
28512854
return -1;
28522855
}
@@ -3523,6 +3526,24 @@ static void maybe_redirect_std_handles(void)
35233526
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
35243527
}
35253528

3529+
static void adjust_symlink_flags(void)
3530+
{
3531+
/*
3532+
* Starting with Windows 10 Build 14972, symbolic links can be created
3533+
* using CreateSymbolicLink() without elevation by passing the flag
3534+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3535+
* parameter, provided the Developer Mode has been enabled. Some
3536+
* earlier Windows versions complain about this flag with an
3537+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3538+
* specifically.
3539+
*/
3540+
if (GetVersion() >= 14972 << 16) {
3541+
symlink_file_flags |= 2;
3542+
symlink_directory_flags |= 2;
3543+
}
3544+
3545+
}
3546+
35263547
#ifdef _MSC_VER
35273548
#ifdef _DEBUG
35283549
#include <crtdbg.h>
@@ -3558,6 +3579,7 @@ int wmain(int argc, const wchar_t **wargv)
35583579
#endif
35593580

35603581
maybe_redirect_std_handles();
3582+
adjust_symlink_flags();
35613583

35623584
/* determine size of argv and environ conversion buffer */
35633585
maxlen = wcslen(wargv[0]);

0 commit comments

Comments
 (0)