summaryrefslogtreecommitdiffstats
path: root/Modules/_winapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_winapi.c')
-rw-r--r--Modules/_winapi.c34
1 files changed, 25 insertions, 9 deletions
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index 55b3718..b44f44b 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -542,7 +542,12 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
{
/* Privilege adjustment */
HANDLE token = NULL;
- TOKEN_PRIVILEGES tp;
+ struct {
+ TOKEN_PRIVILEGES base;
+ /* overallocate by a few array elements */
+ LUID_AND_ATTRIBUTES privs[4];
+ } tp, previousTp;
+ int previousTpSize = 0;
/* Reparse data buffer */
const USHORT prefix_len = 4;
@@ -566,17 +571,21 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
/* Adjust privileges to allow rewriting directory entry as a
junction point. */
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
+ if (!OpenProcessToken(GetCurrentProcess(),
+ TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) {
goto cleanup;
+ }
- if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid))
+ if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.base.Privileges[0].Luid)) {
goto cleanup;
+ }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
- NULL, NULL))
+ tp.base.PrivilegeCount = 1;
+ tp.base.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+ if (!AdjustTokenPrivileges(token, FALSE, &tp.base, sizeof(previousTp),
+ &previousTp.base, &previousTpSize)) {
goto cleanup;
+ }
if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES)
goto cleanup;
@@ -657,8 +666,15 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
cleanup:
ret = GetLastError();
- CloseHandle(token);
- CloseHandle(junction);
+ if (previousTpSize) {
+ AdjustTokenPrivileges(token, FALSE, &previousTp.base, previousTpSize,
+ NULL, NULL);
+ }
+
+ if (token != NULL)
+ CloseHandle(token);
+ if (junction != NULL)
+ CloseHandle(junction);
PyMem_RawFree(rdb);
if (ret != 0)