@@ -532,7 +532,12 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
532
532
{
533
533
/* Privilege adjustment */
534
534
HANDLE token = NULL ;
535
- TOKEN_PRIVILEGES tp ;
535
+ struct {
536
+ TOKEN_PRIVILEGES base ;
537
+ /* overallocate by a few array elements */
538
+ LUID_AND_ATTRIBUTES privs [4 ];
539
+ } tp , previousTp ;
540
+ int previousTpSize = 0 ;
536
541
537
542
/* Reparse data buffer */
538
543
const USHORT prefix_len = 4 ;
@@ -556,17 +561,21 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
556
561
557
562
/* Adjust privileges to allow rewriting directory entry as a
558
563
junction point. */
559
- if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES , & token ))
564
+ if (!OpenProcessToken (GetCurrentProcess (),
565
+ TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY , & token )) {
560
566
goto cleanup ;
567
+ }
561
568
562
- if (!LookupPrivilegeValue (NULL , SE_RESTORE_NAME , & tp .Privileges [0 ].Luid ))
569
+ if (!LookupPrivilegeValue (NULL , SE_RESTORE_NAME , & tp .base . Privileges [0 ].Luid )) {
563
570
goto cleanup ;
571
+ }
564
572
565
- tp .PrivilegeCount = 1 ;
566
- tp .Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED ;
567
- if (!AdjustTokenPrivileges (token , FALSE, & tp , sizeof (TOKEN_PRIVILEGES ),
568
- NULL , NULL ))
573
+ tp .base . PrivilegeCount = 1 ;
574
+ tp .base . Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED ;
575
+ if (!AdjustTokenPrivileges (token , FALSE, & tp . base , sizeof (previousTp ),
576
+ & previousTp . base , & previousTpSize )) {
569
577
goto cleanup ;
578
+ }
570
579
571
580
if (GetFileAttributesW (src_path ) == INVALID_FILE_ATTRIBUTES )
572
581
goto cleanup ;
@@ -647,6 +656,11 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
647
656
cleanup :
648
657
ret = GetLastError ();
649
658
659
+ if (previousTpSize ) {
660
+ AdjustTokenPrivileges (token , FALSE, & previousTp .base , previousTpSize ,
661
+ NULL , NULL );
662
+ }
663
+
650
664
if (token != NULL )
651
665
CloseHandle (token );
652
666
if (junction != NULL )
0 commit comments