Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlocalserver_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qlocalserver.h"
7#include "qlocalsocket.h"
8#include <QtCore/private/qsystemerror_p.h>
9
10#include <qdebug.h>
11
12#include <aclapi.h>
13#include <accctrl.h>
14#include <sddl.h>
15
16#include <memory>
17
18// The buffer size need to be 0 otherwise data could be
19// lost if the socket that has written data closes the connection
20// before it is read. Pipewriter is used for write buffering.
21#define BUFSIZE 0
22
24
25using namespace Qt::StringLiterals;
26
27bool QLocalServerPrivate::addListener()
28{
29 // The object must not change its address once the
30 // contained OVERLAPPED struct is passed to Windows.
31 listeners.push_back(std::make_unique<Listener>());
32 auto &listener = listeners.back();
33
34 SECURITY_ATTRIBUTES sa;
35 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
36 sa.bInheritHandle = FALSE; //non inheritable handle, same as default
37 sa.lpSecurityDescriptor = 0; //default security descriptor
38
39 std::unique_ptr<SECURITY_DESCRIPTOR> pSD;
40 PSID worldSID = 0;
41 QByteArray aclBuffer;
42 QByteArray tokenUserBuffer;
43 QByteArray tokenGroupBuffer;
44
45 // create security descriptor if access options were specified
46 if ((socketOptions.value() & QLocalServer::WorldAccessOption)) {
47 pSD.reset(new SECURITY_DESCRIPTOR);
48 if (!InitializeSecurityDescriptor(pSD.get(), SECURITY_DESCRIPTOR_REVISION)) {
49 setError("QLocalServerPrivate::addListener"_L1);
50 return false;
51 }
52 HANDLE hToken = NULL;
53 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
54 return false;
55 DWORD dwBufferSize = 0;
56 GetTokenInformation(hToken, TokenUser, 0, 0, &dwBufferSize);
57 tokenUserBuffer.fill(0, dwBufferSize);
58 auto pTokenUser = reinterpret_cast<PTOKEN_USER>(tokenUserBuffer.data());
59 if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dwBufferSize, &dwBufferSize)) {
60 setError("QLocalServerPrivate::addListener"_L1);
61 CloseHandle(hToken);
62 return false;
63 }
64
65 dwBufferSize = 0;
66 GetTokenInformation(hToken, TokenPrimaryGroup, 0, 0, &dwBufferSize);
67 tokenGroupBuffer.fill(0, dwBufferSize);
68 auto pTokenGroup = reinterpret_cast<PTOKEN_PRIMARY_GROUP>(tokenGroupBuffer.data());
69 if (!GetTokenInformation(hToken, TokenPrimaryGroup, pTokenGroup, dwBufferSize, &dwBufferSize)) {
70 setError("QLocalServerPrivate::addListener"_L1);
71 CloseHandle(hToken);
72 return false;
73 }
74 CloseHandle(hToken);
75
76#ifdef QLOCALSERVER_DEBUG
77 DWORD groupNameSize;
78 DWORD domainNameSize;
79 SID_NAME_USE groupNameUse;
80 LPWSTR groupNameSid;
81 LookupAccountSid(0, pTokenGroup->PrimaryGroup, 0, &groupNameSize, 0, &domainNameSize, &groupNameUse);
82 auto groupName = std::unique_ptr<wchar_t[]>(new wchar_t[groupNameSize]);
83 auto domainName = std::unique_ptr<wchar_t[]>(new wchar_t[domainNameSize]);
84 const bool lookup = LookupAccountSid(0, pTokenGroup->PrimaryGroup, groupName.get(),
85 &groupNameSize, domainName.get(), &domainNameSize,
86 &groupNameUse);
87 if (lookup) {
88 qDebug() << "primary group" << QString::fromWCharArray(domainName.get()) << "\\"
89 << QString::fromWCharArray(groupName.get()) << "type=" << groupNameUse;
90 }
91 if (ConvertSidToStringSid(pTokenGroup->PrimaryGroup, &groupNameSid)) {
92 qDebug() << "primary group SID" << QString::fromWCharArray(groupNameSid) << "valid" << IsValidSid(pTokenGroup->PrimaryGroup);
93 LocalFree(groupNameSid);
94 }
95#endif
96
97 SID_IDENTIFIER_AUTHORITY WorldAuth = { SECURITY_WORLD_SID_AUTHORITY };
98 if (!AllocateAndInitializeSid(&WorldAuth, 1, SECURITY_WORLD_RID,
99 0, 0, 0, 0, 0, 0, 0,
100 &worldSID)) {
101 setError("QLocalServerPrivate::addListener"_L1);
102 return false;
103 }
104
105 //calculate size of ACL buffer
106 DWORD aclSize = sizeof(ACL) + ((sizeof(ACCESS_ALLOWED_ACE)) * 3);
107 aclSize += GetLengthSid(pTokenUser->User.Sid) - sizeof(DWORD);
108 aclSize += GetLengthSid(pTokenGroup->PrimaryGroup) - sizeof(DWORD);
109 aclSize += GetLengthSid(worldSID) - sizeof(DWORD);
110 aclSize = (aclSize + (sizeof(DWORD) - 1)) & 0xfffffffc;
111
112 aclBuffer.fill(0, aclSize);
113 auto acl = reinterpret_cast<PACL>(aclBuffer.data());
114 InitializeAcl(acl, aclSize, ACL_REVISION_DS);
115
116 if (socketOptions.value() & QLocalServer::UserAccessOption) {
117 if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_ALL_ACCESS, pTokenUser->User.Sid)) {
118 setError("QLocalServerPrivate::addListener"_L1);
119 FreeSid(worldSID);
120 return false;
121 }
122 }
123 if (socketOptions.value() & QLocalServer::GroupAccessOption) {
124 if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_ALL_ACCESS, pTokenGroup->PrimaryGroup)) {
125 setError("QLocalServerPrivate::addListener"_L1);
126 FreeSid(worldSID);
127 return false;
128 }
129 }
130 if (socketOptions.value() & QLocalServer::OtherAccessOption) {
131 if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_ALL_ACCESS, worldSID)) {
132 setError("QLocalServerPrivate::addListener"_L1);
133 FreeSid(worldSID);
134 return false;
135 }
136 }
137 SetSecurityDescriptorOwner(pSD.get(), pTokenUser->User.Sid, FALSE);
138 SetSecurityDescriptorGroup(pSD.get(), pTokenGroup->PrimaryGroup, FALSE);
139 if (!SetSecurityDescriptorDacl(pSD.get(), TRUE, acl, FALSE)) {
140 setError("QLocalServerPrivate::addListener"_L1);
141 FreeSid(worldSID);
142 return false;
143 }
144
145 sa.lpSecurityDescriptor = pSD.get();
146 }
147
148 listener->handle = CreateNamedPipe(
149 reinterpret_cast<const wchar_t *>(fullServerName.utf16()), // pipe name
150 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, // read/write access
151 PIPE_TYPE_BYTE | // byte type pipe
152 PIPE_READMODE_BYTE | // byte-read mode
153 PIPE_WAIT, // blocking mode
154 PIPE_UNLIMITED_INSTANCES, // max. instances
155 BUFSIZE, // output buffer size
156 BUFSIZE, // input buffer size
157 3000, // client time-out
158 &sa);
159
160 if (listener->handle == INVALID_HANDLE_VALUE) {
161 setError("QLocalServerPrivate::addListener"_L1);
162 listeners.pop_back();
163 return false;
164 }
165
166 if (worldSID)
167 FreeSid(worldSID);
168
169 memset(&listener->overlapped, 0, sizeof(OVERLAPPED));
170 listener->overlapped.hEvent = eventHandle;
171
172 // Beware! ConnectNamedPipe will reset the eventHandle to non-signaled.
173 // Callers of addListener must check all listeners for connections.
174 if (!ConnectNamedPipe(listener->handle, &listener->overlapped)) {
175 switch (GetLastError()) {
176 case ERROR_IO_PENDING:
177 listener->connected = false;
178 break;
179 case ERROR_PIPE_CONNECTED:
180 listener->connected = true;
181 break;
182 default:
183 CloseHandle(listener->handle);
184 setError("QLocalServerPrivate::addListener"_L1);
185 listeners.pop_back();
186 return false;
187 }
188 } else {
189 Q_ASSERT_X(false, "QLocalServerPrivate::addListener", "The impossible happened");
190 SetEvent(eventHandle);
191 }
192 return true;
193}
194
195void QLocalServerPrivate::setError(const QString &function)
196{
197 int windowsError = GetLastError();
198 errorString = QString::fromLatin1("%1: %2").arg(function, qt_error_string(windowsError));
199 error = QAbstractSocket::UnknownSocketError;
200}
201
202void QLocalServerPrivate::init()
203{
204}
205
206bool QLocalServerPrivate::removeServer(const QString &name)
207{
208 Q_UNUSED(name);
209 return true;
210}
211
212bool QLocalServerPrivate::listen(const QString &name)
213{
214 Q_Q(QLocalServer);
215
216 const auto pipePath = "\\\\.\\pipe\\"_L1;
217 if (name.startsWith(pipePath))
218 fullServerName = name;
219 else
220 fullServerName = pipePath + name;
221
222 // Use only one event for all listeners of one socket.
223 // The idea is that listener events are rare, so polling all listeners once in a while is
224 // cheap compared to waiting for N additional events in each iteration of the main loop.
225 eventHandle = CreateEvent(NULL, TRUE, FALSE, NULL); // If the function fails, the return value is NULL
226 connectionEventNotifier = new QWinEventNotifier(eventHandle , q);
227 q->connect(connectionEventNotifier, SIGNAL(activated(HANDLE)), q, SLOT(_q_onNewConnection()));
228
229 for (int i = 0; i < listenBacklog; ++i)
230 if (!addListener())
231 return false;
232
233 _q_onNewConnection();
234 return true;
235}
236
237bool QLocalServerPrivate::listen(qintptr)
238{
239 qWarning("QLocalServer::listen(qintptr) is not supported on Windows QTBUG-24230");
240 return false;
241}
242
243void QLocalServerPrivate::_q_onNewConnection()
244{
245 Q_Q(QLocalServer);
246 DWORD dummy;
247 bool tryAgain;
248 do {
249 tryAgain = false;
250
251 // Reset first, otherwise we could reset an event which was asserted
252 // immediately after we checked the conn status.
253 ResetEvent(eventHandle);
254
255 // Testing shows that there is indeed absolutely no guarantee which listener gets
256 // a client connection first, so there is no way around polling all of them.
257 for (size_t i = 0; i < listeners.size(); ) {
258 HANDLE handle = listeners[i]->handle;
259 if (listeners[i]->connected
260 || GetOverlappedResult(handle, &listeners[i]->overlapped, &dummy, FALSE))
261 {
262 listeners.erase(listeners.begin() + i);
263
264 addListener();
265
266 if (pendingConnections.size() > maxPendingConnections)
267 connectionEventNotifier->setEnabled(false);
268 else
269 tryAgain = true;
270
271 // Make this the last thing so connected slots can wreak the least havoc
272 q->incomingConnection(reinterpret_cast<quintptr>(handle));
273 } else {
274 if (GetLastError() != ERROR_IO_INCOMPLETE) {
275 q->close();
276 setError("QLocalServerPrivate::_q_onNewConnection"_L1);
277 return;
278 }
279
280 ++i;
281 }
282 }
283 } while (tryAgain);
284}
285
286void QLocalServerPrivate::closeServer()
287{
288 connectionEventNotifier->setEnabled(false); // Otherwise, closed handle is checked before deleter runs
289 connectionEventNotifier->deleteLater();
290 connectionEventNotifier = 0;
291 CloseHandle(eventHandle);
292 eventHandle = nullptr;
293 for (size_t i = 0; i < listeners.size(); ++i)
294 CloseHandle(listeners[i]->handle);
295 listeners.clear();
296}
297
298void QLocalServerPrivate::waitForNewConnection(int msecs, bool *timedOut)
299{
300 Q_Q(QLocalServer);
301 if (!pendingConnections.isEmpty() || !q->isListening())
302 return;
303
304 DWORD result = WaitForSingleObject(eventHandle, (msecs == -1) ? INFINITE : msecs);
305 if (result == WAIT_TIMEOUT) {
306 if (timedOut)
307 *timedOut = true;
308 } else {
309 _q_onNewConnection();
310 }
311}
312
313QT_END_NAMESPACE
#define BUFSIZE