Skip to content

Commit f7de75f

Browse files
committed
Frontported fix for bug #9094 : FB4 XNET/local attach stack-overflows in fbclient (gds__log recursion) when the calling thread impersonates another user's token
Also, correct checks for mutex existence.
1 parent 3217f44 commit f7de75f

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

src/common/isc.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,30 @@ class SecurityAttributes
6767
explicit SecurityAttributes(MemoryPool& pool)
6868
: m_pool(pool)
6969
{
70+
attributes.nLength = sizeof(attributes);
71+
attributes.lpSecurityDescriptor = NULL;
72+
attributes.bInheritHandle = TRUE;
73+
74+
static bool initializing = false;
75+
76+
// initializing == true means recursive call of SecurityAttributes constructor.
77+
// It happens if first instance throws an exception and handling code accesses
78+
// SecurityAttributes. Avoid recursion and leave null at attributes.lpSecurityDescriptor
79+
// in this case.
80+
81+
if (initializing)
82+
return;
83+
84+
initializing = true;
85+
7086
// Ensure that our process has the SYNCHRONIZE privilege granted to everyone
7187
PSECURITY_DESCRIPTOR pOldSD = NULL;
7288
PACL pOldACL = NULL;
7389

7490
// Pseudo-handles do not work on WinNT. Need real process handle.
75-
HANDLE hCurrentProcess = OpenProcess(READ_CONTROL | WRITE_DAC, FALSE, GetCurrentProcessId());
76-
if (hCurrentProcess == NULL) {
77-
Firebird::system_call_failed::raise("OpenProcess");
78-
}
91+
// hvlad: it have PROCESS_ALL_ACCESS access right since Vista, also it is not
92+
// affected by impersonation
93+
HANDLE hCurrentProcess = GetCurrentProcess();
7994

8095
DWORD result = GetSecurityInfo(hCurrentProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
8196
NULL, NULL, &pOldACL, NULL, &pOldSD);
@@ -88,10 +103,7 @@ class SecurityAttributes
88103
}
89104

90105
if (result != ERROR_SUCCESS)
91-
{
92-
CloseHandle(hCurrentProcess);
93106
Firebird::system_call_failed::raise("GetSecurityInfo", result);
94-
}
95107

96108
// NULL pOldACL means all privileges. If we assign pNewACL in this case
97109
// we'll lost all privileges except assigned SYNCHRONIZE
@@ -117,19 +129,15 @@ class SecurityAttributes
117129
SetSecurityInfo(hCurrentProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
118130
NULL, NULL, pNewACL, NULL);
119131

120-
if (pSID) {
132+
if (pSID)
121133
FreeSid(pSID);
122-
}
123-
if (pNewACL) {
134+
135+
if (pNewACL)
124136
LocalFree(pNewACL);
125-
}
126137
}
127138

128-
CloseHandle(hCurrentProcess);
129-
130-
if (pOldSD) {
139+
if (pOldSD)
131140
LocalFree(pOldSD);
132-
}
133141

134142
// Create and initialize the default security descriptor
135143
// to be assigned to various IPC objects.
@@ -140,16 +148,16 @@ class SecurityAttributes
140148
PSECURITY_DESCRIPTOR p_security_desc = static_cast<PSECURITY_DESCRIPTOR>(
141149
FB_NEW_POOL(m_pool) char[SECURITY_DESCRIPTOR_MIN_LENGTH]);
142150

143-
attributes.nLength = sizeof(attributes);
144-
attributes.lpSecurityDescriptor = p_security_desc;
145-
attributes.bInheritHandle = TRUE;
146-
147151
if (!InitializeSecurityDescriptor(p_security_desc, SECURITY_DESCRIPTOR_REVISION) ||
148152
!SetSecurityDescriptorDacl(p_security_desc, TRUE, NULL, FALSE))
149153
{
150154
delete p_security_desc;
151-
attributes.lpSecurityDescriptor = NULL;
152155
}
156+
else
157+
{
158+
attributes.lpSecurityDescriptor = p_security_desc;
159+
}
160+
initializing = false;
153161
}
154162

155163
~SecurityAttributes()

src/yvalve/gds.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,10 @@ class LogFileHandles
11201120

11211121
~LogFileHandles()
11221122
{
1123-
if (mutex_handle != INVALID_HANDLE_VALUE)
1123+
if (mutex_handle != NULL)
11241124
CloseHandle(mutex_handle);
11251125

1126-
mutex_handle = INVALID_HANDLE_VALUE;
1126+
mutex_handle = NULL;
11271127

11281128
if (file_handle != INVALID_HANDLE_VALUE)
11291129
CloseHandle(file_handle);
@@ -1182,7 +1182,7 @@ void LogFileHandles::trace_raw(const char* text, unsigned int length)
11821182

11831183
Firebird::InitInstance<LogFileHandles> logFileHandles;
11841184

1185-
HANDLE LogFileHandles::mutex_handle = INVALID_HANDLE_VALUE;
1185+
HANDLE LogFileHandles::mutex_handle = NULL;
11861186
HANDLE LogFileHandles::file_handle = INVALID_HANDLE_VALUE;
11871187

11881188

0 commit comments

Comments
 (0)