Skip to content

Commit 1ba130e

Browse files
committed
Revert "try fix start ti process on Windows11 22623.1250"
This reverts commit 306f50f.
1 parent 306f50f commit 1ba130e

File tree

3 files changed

+27
-47
lines changed

3 files changed

+27
-47
lines changed

lib/exec/elevator.cc

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ inline auto IsSystemProcessName(std::wstring_view name) {
100100
}
101101

102102
constexpr DWORD INVALID_PROCESS_ID = 0xFFFFFFFF;
103-
bool SearchSystemProcess(std::vector<DWORD> &pss) {
103+
DWORD LookupSystemProcess() {
104104
PWTS_PROCESS_INFOW pi{nullptr};
105105
DWORD count{0};
106106
if (::WTSEnumerateProcessesW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pi, &count) != TRUE) {
107-
return false;
107+
return INVALID_PROCESS_ID;
108108
}
109109
auto closer = bela::finally([&] {
110110
if (pi != nullptr) {
@@ -115,10 +115,10 @@ bool SearchSystemProcess(std::vector<DWORD> &pss) {
115115
for (auto it = pi; it != end; it++) {
116116
if (it->SessionId == 0 && IsSystemProcessName(it->pProcessName) &&
117117
IsWellKnownSid(it->pUserSid, WinLocalSystemSid) == TRUE) {
118-
pss.emplace_back(it->ProcessId);
118+
return it->ProcessId;
119119
}
120120
}
121-
return !pss.empty();
121+
return INVALID_PROCESS_ID;
122122
}
123123

124124
// Get Current Process SessionID and Enable SeDebugPrivilege
@@ -214,24 +214,24 @@ bool privileges_view_enabled(HANDLE hToken, const privilege_entries *pv) {
214214
return true;
215215
}
216216

217-
bool Elavator::impersonation_system_token(DWORD systemProcessId, bela::error_code &ec) {
217+
bool Elavator::impersonation_system_token(bela::error_code &ec) {
218218
HANDLE hExistingToken = INVALID_HANDLE_VALUE;
219219
auto hProcess = ::OpenProcess(MAXIMUM_ALLOWED, FALSE, systemProcessId);
220220
if (hProcess == INVALID_HANDLE_VALUE) {
221221
ec = bela::make_system_error_code(
222-
bela::StringCat(L"impersonation_system_token<OpenProcess> ", systemProcessId, L" "));
222+
bela::StringCat(L"Elavator::impersonation_system_token<OpenProcess> ", systemProcessId, L" "));
223223
return false;
224224
}
225225
auto hpdeleter = bela::finally([&] { CloseHandle(hProcess); });
226226
if (OpenProcessToken(hProcess, MAXIMUM_ALLOWED, &hExistingToken) != TRUE) {
227227
ec = bela::make_system_error_code(
228-
bela::StringCat(L"impersonation_system_token<OpenProcessToken> ", systemProcessId, L" "));
228+
bela::StringCat(L"Elavator::impersonation_system_token<OpenProcessToken> ", systemProcessId, L" "));
229229
return false;
230230
}
231231
auto htdeleter = bela::finally([&] { CloseHandle(hExistingToken); });
232232
if (DuplicateTokenEx(hExistingToken, MAXIMUM_ALLOWED, nullptr, SecurityImpersonation, TokenImpersonation, &hToken) !=
233233
TRUE) {
234-
ec = bela::make_system_error_code(L"impersonation_system_token<DuplicateTokenEx> ");
234+
ec = bela::make_system_error_code(L"Elavator::impersonation_system_token<DuplicateTokenEx> ");
235235
return false;
236236
}
237237
return true;
@@ -242,31 +242,20 @@ bool Elavator::ImpersonationSystemPrivilege(const privilege_entries *pv, bela::e
242242
if (!EnableSeDebugPrivilege(currentSessionId, ec)) {
243243
return false;
244244
}
245-
if (!SearchSystemProcess(systemProcesses)) {
246-
ec = bela::make_error_code(1, L"ImpersonationSystemPrivilege search system process error");
245+
if (systemProcessId = LookupSystemProcess(); systemProcessId == INVALID_PROCESS_ID) {
246+
ec = bela::make_error_code(1, L"Elevator::ImpersonationSystemPrivilege unable lookup system process pid");
247247
return false;
248248
}
249-
250-
auto impersonation_system_token_all = [&]() -> bool {
251-
for (auto pid : systemProcesses) {
252-
if (impersonation_system_token(pid, ec)) {
253-
return true;
254-
}
255-
}
256-
return false;
257-
};
258-
259-
if (!impersonation_system_token_all()) {
249+
if (!impersonation_system_token(ec)) {
260250
return false;
261251
}
262-
263252
if (!privileges_view_enabled(hToken, pv)) {
264-
ec = bela::make_error_code(1, L"ImpersonationSystemPrivilegeunable enable privileges: ",
253+
ec = bela::make_error_code(1, L"Elevator::ImpersonationSystemPrivilege unable enable privileges: ",
265254
pv == nullptr ? L"all" : pv->format());
266255
return false;
267256
}
268257
if (SetThreadToken(nullptr, hToken) != TRUE) {
269-
ec = bela::make_error_code(1, L"ImpersonationSystemPrivilege<SetThreadToken> ");
258+
ec = bela::make_error_code(1, L"Elevator::ImpersonationSystemPrivilege<SetThreadToken> ");
270259
return false;
271260
}
272261
return true;

lib/exec/execinternal.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ class Elavator {
4444
Elavator(const Elavator &) = delete;
4545
Elavator &operator=(const Elavator &) = delete;
4646
~Elavator() { FreeToken(hToken); }
47+
// System Process PID
48+
DWORD SystemPID() const { return systemProcessId; }
4749
// Session ID
4850
DWORD SessionID() const { return currentSessionId; }
49-
const auto &SystemProcesses() const { return systemProcesses; }
5051
bool ImpersonationSystemPrivilege(const privilege_entries *pv, bela::error_code &ec);
5152

5253
private:
53-
bool impersonation_system_token(DWORD systemProcessId, bela::error_code &ec);
54+
bool impersonation_system_token(bela::error_code &ec);
5455
HANDLE hToken{nullptr};
5556
DWORD currentSessionId{0};
56-
// system process list
57-
std::vector<DWORD> systemProcesses;
57+
// system process id
58+
DWORD systemProcessId{0};
5859
};
5960
bool execute_basic(command &cmd, bela::error_code &ec);
6061
bool execute_with_ti(command &cmd, bela::error_code &ec);

lib/exec/system.cc

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,16 @@ bool execute_with_system(command &cmd, bela::error_code &ec) {
122122
FreeToken(hToken);
123123
FreeToken(hPrimary);
124124
});
125-
126-
auto impersonation_system_token_all = [&]() -> bool {
127-
for (auto pid : eo.SystemProcesses()) {
128-
if ((hProcess = OpenProcess(MAXIMUM_ALLOWED, FALSE, pid)) == nullptr) {
129-
ec = bela::make_system_error_code(L"execute_with_system<OpenProcess> ");
130-
continue;
131-
}
132-
if (OpenProcessToken(hProcess, MAXIMUM_ALLOWED, &hToken) != TRUE) {
133-
ec = bela::make_system_error_code(L"execute_with_system<OpenProcessToken> ");
134-
continue;
135-
}
136-
if (DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenPrimary, &hPrimary) != TRUE) {
137-
ec = bela::make_system_error_code(L"execute_with_system<DuplicateTokenEx> ");
138-
continue;
139-
}
140-
return true;
141-
}
125+
if ((hProcess = OpenProcess(MAXIMUM_ALLOWED, FALSE, eo.SystemPID())) == nullptr) {
126+
ec = bela::make_system_error_code(L"execute_with_system<OpenProcess> ");
127+
return false;
128+
}
129+
if (OpenProcessToken(hProcess, MAXIMUM_ALLOWED, &hToken) != TRUE) {
130+
ec = bela::make_system_error_code(L"execute_with_system<OpenProcessToken> ");
142131
return false;
143-
};
144-
if (!impersonation_system_token_all()) {
132+
}
133+
if (DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenPrimary, &hPrimary) != TRUE) {
134+
ec = bela::make_system_error_code(L"execute_with_system<DuplicateTokenEx> ");
145135
return false;
146136
}
147137
auto session = eo.SessionID();

0 commit comments

Comments
 (0)