mirror of https://github.com/python/cpython
gh-114096: Restore privileges in _winapi.CreateJunction after creating the junction (GH-114089)
This avoids impact on later parts of the application which may be able to do things they otherwise shouldn't.
This commit is contained in:
parent
31a2543c80
commit
de4ced54eb
|
@ -0,0 +1,3 @@
|
||||||
|
Process privileges that are activated for creating directory junctions are
|
||||||
|
now restored afterwards, avoiding behaviour changes in other parts of the
|
||||||
|
program.
|
|
@ -532,7 +532,12 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
|
||||||
{
|
{
|
||||||
/* Privilege adjustment */
|
/* Privilege adjustment */
|
||||||
HANDLE token = NULL;
|
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 */
|
/* Reparse data buffer */
|
||||||
const USHORT prefix_len = 4;
|
const USHORT prefix_len = 4;
|
||||||
|
@ -556,17 +561,21 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
|
||||||
|
|
||||||
/* Adjust privileges to allow rewriting directory entry as a
|
/* Adjust privileges to allow rewriting directory entry as a
|
||||||
junction point. */
|
junction point. */
|
||||||
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
|
if (!OpenProcessToken(GetCurrentProcess(),
|
||||||
|
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) {
|
||||||
goto cleanup;
|
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;
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
tp.PrivilegeCount = 1;
|
tp.base.PrivilegeCount = 1;
|
||||||
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
tp.base.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
||||||
if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
|
if (!AdjustTokenPrivileges(token, FALSE, &tp.base, sizeof(previousTp),
|
||||||
NULL, NULL))
|
&previousTp.base, &previousTpSize)) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES)
|
if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
@ -647,6 +656,11 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
|
||||||
cleanup:
|
cleanup:
|
||||||
ret = GetLastError();
|
ret = GetLastError();
|
||||||
|
|
||||||
|
if (previousTpSize) {
|
||||||
|
AdjustTokenPrivileges(token, FALSE, &previousTp.base, previousTpSize,
|
||||||
|
NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
if (token != NULL)
|
if (token != NULL)
|
||||||
CloseHandle(token);
|
CloseHandle(token);
|
||||||
if (junction != NULL)
|
if (junction != NULL)
|
||||||
|
|
Loading…
Reference in New Issue