Skip to content

Commit b8daa01

Browse files
committed
Merge pull request #1188 from dscho/unprivileged-symlinks
Support creating symlinks outside elevated sessions
2 parents a278a25 + 7591ba5 commit b8daa01

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

compat/mingw.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ int mingw_core_config(const char *var, const char *value)
270270
return 0;
271271
}
272272

273+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
273274
DECLARE_PROC_ADDR(kernel32.dll, BOOLEAN, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD);
274275

275276
enum phantom_symlink_result {
@@ -314,7 +315,8 @@ static enum phantom_symlink_result process_phantom_symlink(
314315
return PHANTOM_SYMLINK_DONE;
315316

316317
/* otherwise recreate the symlink with directory flag */
317-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
318+
if (DeleteFileW(wlink) &&
319+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
318320
return PHANTOM_SYMLINK_DIRECTORY;
319321

320322
errno = err_win_to_posix(GetLastError());
@@ -2611,7 +2613,7 @@ int symlink(const char *target, const char *link)
26112613
wtarget[len] = '\\';
26122614

26132615
/* create file symlink */
2614-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2616+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
26152617
errno = err_win_to_posix(GetLastError());
26162618
return -1;
26172619
}
@@ -3133,6 +3135,24 @@ static void maybe_redirect_std_handles(void)
31333135
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
31343136
}
31353137

3138+
static void adjust_symlink_flags(void)
3139+
{
3140+
/*
3141+
* Starting with Windows 10 Build 14972, symbolic links can be created
3142+
* using CreateSymbolicLink() without elevation by passing the flag
3143+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3144+
* parameter, provided the Developer Mode has been enabled. Some
3145+
* earlier Windows versions complain about this flag with an
3146+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3147+
* specifically.
3148+
*/
3149+
if (GetVersion() >= 14972 << 16) {
3150+
symlink_file_flags |= 2;
3151+
symlink_directory_flags |= 2;
3152+
}
3153+
3154+
}
3155+
31363156
#if defined(_MSC_VER)
31373157

31383158
#ifdef _DEBUG
@@ -3172,6 +3192,7 @@ int msc_startup(int argc, wchar_t **w_argv, wchar_t **w_env)
31723192
#endif
31733193

31743194
maybe_redirect_std_handles();
3195+
adjust_symlink_flags();
31753196

31763197
/* determine size of argv conversion buffer */
31773198
maxlen = wcslen(_wpgmptr);
@@ -3238,6 +3259,7 @@ void mingw_startup(void)
32383259
_startupinfo si;
32393260

32403261
maybe_redirect_std_handles();
3262+
adjust_symlink_flags();
32413263

32423264
/* get wide char arguments and environment */
32433265
si.newmode = 0;

0 commit comments

Comments
 (0)