1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
static const DWORD PIPE_64KB = 64 * 1024;
static const DWORD PIPE_WAIT_FOR_CONNECTION = 100; // wait a 10th of a second,
static const DWORD PIPE_RETRY_FOR_CONNECTION = 1800; // for up to 3 minutes.
static const LPCWSTR PIPE_NAME_FORMAT_STRING = L"\\\\.\\pipe\\%ls";
static const LPCWSTR CACHE_PIPE_NAME_FORMAT_STRING = L"\\\\.\\pipe\\%ls.Cache";
static const LPCWSTR LOGGING_PIPE_NAME_FORMAT_STRING = L"\\\\.\\pipe\\%ls.Log";
static HRESULT AllocatePipeMessage(
__in DWORD dwMessage,
__in_bcount_opt(cbData) LPVOID pvData,
__in SIZE_T cbData,
__out_bcount(cb) LPVOID* ppvMessage,
__out SIZE_T* pcbMessage
);
static void FreePipeMessage(
__in BURN_PIPE_MESSAGE *pMsg
);
static HRESULT WritePipeMessage(
__in HANDLE hPipe,
__in DWORD dwMessage,
__in_bcount_opt(cbData) LPVOID pvData,
__in SIZE_T cbData
);
static HRESULT GetPipeMessage(
__in HANDLE hPipe,
__in BURN_PIPE_MESSAGE* pMsg
);
static HRESULT ChildPipeConnected(
__in HANDLE hPipe,
__in_z LPCWSTR wzSecret,
__inout DWORD* pdwProcessId
);
/*******************************************************************
PipeConnectionInitialize - initialize pipe connection data.
*******************************************************************/
void PipeConnectionInitialize(
__in BURN_PIPE_CONNECTION* pConnection
)
{
memset(pConnection, 0, sizeof(BURN_PIPE_CONNECTION));
pConnection->hPipe = INVALID_HANDLE_VALUE;
pConnection->hCachePipe = INVALID_HANDLE_VALUE;
pConnection->hLoggingPipe = INVALID_HANDLE_VALUE;
}
/*******************************************************************
PipeConnectionUninitialize - free data in a pipe connection.
*******************************************************************/
void PipeConnectionUninitialize(
__in BURN_PIPE_CONNECTION* pConnection
)
{
ReleaseFileHandle(pConnection->hLoggingPipe);
ReleaseFileHandle(pConnection->hCachePipe);
ReleaseFileHandle(pConnection->hPipe);
ReleaseHandle(pConnection->hProcess);
ReleaseStr(pConnection->sczSecret);
ReleaseStr(pConnection->sczName);
PipeConnectionInitialize(pConnection);
}
/*******************************************************************
PipeSendMessage -
*******************************************************************/
extern "C" HRESULT PipeSendMessage(
__in HANDLE hPipe,
__in DWORD dwMessage,
__in_bcount_opt(cbData) LPVOID pvData,
__in SIZE_T cbData,
__in_opt PFN_PIPE_MESSAGE_CALLBACK pfnCallback,
__in_opt LPVOID pvContext,
__out DWORD* pdwResult
)
{
HRESULT hr = S_OK;
BURN_PIPE_RESULT result = { };
hr = WritePipeMessage(hPipe, dwMessage, pvData, cbData);
ExitOnFailure(hr, "Failed to write send message to pipe.");
hr = PipePumpMessages(hPipe, pfnCallback, pvContext, &result);
ExitOnFailure(hr, "Failed to pump messages during send message to pipe.");
*pdwResult = result.dwResult;
LExit:
return hr;
}
/*******************************************************************
PipePumpMessages -
*******************************************************************/
extern "C" HRESULT PipePumpMessages(
__in HANDLE hPipe,
__in_opt PFN_PIPE_MESSAGE_CALLBACK pfnCallback,
__in_opt LPVOID pvContext,
__in BURN_PIPE_RESULT* pResult
)
{
HRESULT hr = S_OK;
BURN_PIPE_MESSAGE msg = { };
SIZE_T iData = 0;
LPSTR sczMessage = NULL;
DWORD dwResult = 0;
// Pump messages from child process.
while (S_OK == (hr = GetPipeMessage(hPipe, &msg)))
{
switch (msg.dwMessage)
{
case BURN_PIPE_MESSAGE_TYPE_LOG:
iData = 0;
hr = BuffReadStringAnsi((BYTE*)msg.pvData, msg.cbData, &iData, &sczMessage);
ExitOnFailure(hr, "Failed to read log message.");
hr = LogStringWorkRaw(sczMessage);
ExitOnFailure(hr, "Failed to write log message:'%hs'.", sczMessage);
dwResult = static_cast<DWORD>(hr);
break;
case BURN_PIPE_MESSAGE_TYPE_COMPLETE:
if (!msg.pvData || sizeof(DWORD) != msg.cbData)
{
hr = E_INVALIDARG;
ExitOnRootFailure(hr, "No status returned to PipePumpMessages()");
}
pResult->dwResult = *static_cast<DWORD*>(msg.pvData);
ExitFunction1(hr = S_OK); // exit loop.
case BURN_PIPE_MESSAGE_TYPE_TERMINATE:
iData = 0;
hr = BuffReadNumber(static_cast<BYTE*>(msg.pvData), msg.cbData, &iData, &pResult->dwResult);
ExitOnFailure(hr, "Failed to read returned result to PipePumpMessages()");
if (sizeof(DWORD) * 2 == msg.cbData)
{
hr = BuffReadNumber(static_cast<BYTE*>(msg.pvData), msg.cbData, &iData, (DWORD*)&pResult->fRestart);
ExitOnFailure(hr, "Failed to read returned restart to PipePumpMessages()");
}
ExitFunction1(hr = S_OK); // exit loop.
default:
if (pfnCallback)
{
hr = pfnCallback(&msg, pvContext, &dwResult);
}
else
{
hr = E_INVALIDARG;
}
ExitOnFailure(hr, "Failed to process message: %u", msg.dwMessage);
break;
}
// post result
hr = WritePipeMessage(hPipe, static_cast<DWORD>(BURN_PIPE_MESSAGE_TYPE_COMPLETE), &dwResult, sizeof(dwResult));
ExitOnFailure(hr, "Failed to post result to child process.");
FreePipeMessage(&msg);
}
ExitOnFailure(hr, "Failed to get message over pipe");
if (S_FALSE == hr)
{
hr = S_OK;
}
LExit:
ReleaseStr(sczMessage);
FreePipeMessage(&msg);
return hr;
}
/*******************************************************************
PipeCreateNameAndSecret -
*******************************************************************/
extern "C" HRESULT PipeCreateNameAndSecret(
__out_z LPWSTR *psczConnectionName,
__out_z LPWSTR *psczSecret
)
{
HRESULT hr = S_OK;
WCHAR wzGuid[GUID_STRING_LENGTH];
LPWSTR sczConnectionName = NULL;
LPWSTR sczSecret = NULL;
// Create the unique pipe name.
hr = GuidFixedCreate(wzGuid);
ExitOnRootFailure(hr, "Failed to create pipe guid.");
hr = StrAllocFormatted(&sczConnectionName, L"BurnPipe.%s", wzGuid);
ExitOnFailure(hr, "Failed to allocate pipe name.");
// Create the unique client secret.
hr = GuidFixedCreate(wzGuid);
ExitOnRootFailure(hr, "Failed to create pipe secret.");
hr = StrAllocString(&sczSecret, wzGuid, 0);
ExitOnFailure(hr, "Failed to allocate pipe secret.");
*psczConnectionName = sczConnectionName;
sczConnectionName = NULL;
*psczSecret = sczSecret;
sczSecret = NULL;
LExit:
ReleaseStr(sczSecret);
ReleaseStr(sczConnectionName);
return hr;
}
/*******************************************************************
PipeCreatePipes - create the pipes and event to signal child process.
*******************************************************************/
extern "C" HRESULT PipeCreatePipes(
__in BURN_PIPE_CONNECTION* pConnection,
__in BOOL fCompanion
)
{
Assert(pConnection->sczName);
Assert(INVALID_HANDLE_VALUE == pConnection->hPipe);
Assert(INVALID_HANDLE_VALUE == pConnection->hCachePipe);
Assert(INVALID_HANDLE_VALUE == pConnection->hLoggingPipe);
HRESULT hr = S_OK;
PSECURITY_DESCRIPTOR psd = NULL;
SECURITY_ATTRIBUTES sa = { };
LPWSTR sczFullPipeName = NULL;
HANDLE hPipe = INVALID_HANDLE_VALUE;
HANDLE hCachePipe = INVALID_HANDLE_VALUE;
HANDLE hLoggingPipe = INVALID_HANDLE_VALUE;
// Only grant special rights when the pipe is being used for "embedded" scenarios.
if (!fCompanion)
{
// Create the security descriptor that grants read/write/sync access to Everyone.
// TODO: consider locking down "WD" to LogonIds (logon session)
LPCWSTR wzSddl = L"D:(A;;GA;;;SY)(A;;GA;;;BA)(A;;GRGW0x00100000;;;WD)";
if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW(wzSddl, SDDL_REVISION_1, &psd, NULL))
{
ExitWithLastError(hr, "Failed to create the security descriptor for the connection event and pipe.");
}
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = psd;
sa.bInheritHandle = FALSE;
}
// Create the pipe.
hr = StrAllocFormatted(&sczFullPipeName, PIPE_NAME_FORMAT_STRING, pConnection->sczName);
ExitOnFailure(hr, "Failed to allocate full name of pipe: %ls", pConnection->sczName);
// TODO: consider using overlapped IO to do waits on the pipe and still be able to cancel and such.
hPipe = ::CreateNamedPipeW(sczFullPipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, PIPE_64KB, PIPE_64KB, 1, psd ? &sa : NULL);
if (INVALID_HANDLE_VALUE == hPipe)
{
ExitWithLastError(hr, "Failed to create pipe: %ls", sczFullPipeName);
}
if (fCompanion)
{
// Create the cache pipe.
hr = StrAllocFormatted(&sczFullPipeName, CACHE_PIPE_NAME_FORMAT_STRING, pConnection->sczName);
ExitOnFailure(hr, "Failed to allocate full name of cache pipe: %ls", pConnection->sczName);
hCachePipe = ::CreateNamedPipeW(sczFullPipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, PIPE_64KB, PIPE_64KB, 1, NULL);
if (INVALID_HANDLE_VALUE == hCachePipe)
{
ExitWithLastError(hr, "Failed to create cache pipe: %ls", sczFullPipeName);
}
// Create the logging pipe.
hr = StrAllocFormatted(&sczFullPipeName, LOGGING_PIPE_NAME_FORMAT_STRING, pConnection->sczName);
ExitOnFailure(hr, "Failed to allocate full name of logging pipe: %ls", pConnection->sczName);
hLoggingPipe = ::CreateNamedPipeW(sczFullPipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, PIPE_64KB, PIPE_64KB, 1, NULL);
if (INVALID_HANDLE_VALUE == hLoggingPipe)
{
ExitWithLastError(hr, "Failed to create logging pipe: %ls", sczFullPipeName);
}
}
pConnection->hLoggingPipe = hLoggingPipe;
hLoggingPipe = INVALID_HANDLE_VALUE;
pConnection->hCachePipe = hCachePipe;
hCachePipe = INVALID_HANDLE_VALUE;
pConnection->hPipe = hPipe;
hPipe = INVALID_HANDLE_VALUE;
LExit:
ReleaseFileHandle(hLoggingPipe);
ReleaseFileHandle(hCachePipe);
ReleaseFileHandle(hPipe);
ReleaseStr(sczFullPipeName);
if (psd)
{
::LocalFree(psd);
}
return hr;
}
/*******************************************************************
PipeWaitForChildConnect -
*******************************************************************/
extern "C" HRESULT PipeWaitForChildConnect(
__in BURN_PIPE_CONNECTION* pConnection
)
{
HRESULT hr = S_OK;
HANDLE hPipes[3] = { pConnection->hPipe, pConnection->hCachePipe, pConnection->hLoggingPipe};
LPCWSTR wzSecret = pConnection->sczSecret;
DWORD cbSecret = lstrlenW(wzSecret) * sizeof(WCHAR);
DWORD dwCurrentProcessId = ::GetCurrentProcessId();
DWORD dwAck = 0;
for (DWORD i = 0; i < countof(hPipes) && INVALID_HANDLE_VALUE != hPipes[i]; ++i)
{
HANDLE hPipe = hPipes[i];
DWORD dwPipeState = PIPE_READMODE_BYTE | PIPE_NOWAIT;
// Temporarily make the pipe non-blocking so we will not get stuck in ::ConnectNamedPipe() forever
// if the child decides not to show up.
if (!::SetNamedPipeHandleState(hPipe, &dwPipeState, NULL, NULL))
{
ExitWithLastError(hr, "Failed to set pipe to non-blocking.");
}
// Loop for a while waiting for a connection from child process.
DWORD cRetry = 0;
do
{
if (!::ConnectNamedPipe(hPipe, NULL))
{
DWORD er = ::GetLastError();
if (ERROR_PIPE_CONNECTED == er)
{
hr = S_OK;
break;
}
else if (ERROR_PIPE_LISTENING == er)
{
if (cRetry < PIPE_RETRY_FOR_CONNECTION)
{
hr = HRESULT_FROM_WIN32(er);
}
else
{
hr = HRESULT_FROM_WIN32(ERROR_TIMEOUT);
break;
}
++cRetry;
::Sleep(PIPE_WAIT_FOR_CONNECTION);
}
else
{
hr = HRESULT_FROM_WIN32(er);
break;
}
}
} while (HRESULT_FROM_WIN32(ERROR_PIPE_LISTENING) == hr);
ExitOnRootFailure(hr, "Failed to wait for child to connect to pipe.");
// Put the pipe back in blocking mode.
dwPipeState = PIPE_READMODE_BYTE | PIPE_WAIT;
if (!::SetNamedPipeHandleState(hPipe, &dwPipeState, NULL, NULL))
{
ExitWithLastError(hr, "Failed to reset pipe to blocking.");
}
// Prove we are the one that created the elevated process by passing the secret.
hr = FileWriteHandle(hPipe, reinterpret_cast<LPCBYTE>(&cbSecret), sizeof(cbSecret));
ExitOnFailure(hr, "Failed to write secret length to pipe.");
hr = FileWriteHandle(hPipe, reinterpret_cast<LPCBYTE>(wzSecret), cbSecret);
ExitOnFailure(hr, "Failed to write secret to pipe.");
hr = FileWriteHandle(hPipe, reinterpret_cast<LPCBYTE>(&dwCurrentProcessId), sizeof(dwCurrentProcessId));
ExitOnFailure(hr, "Failed to write our process id to pipe.");
// Wait until the elevated process responds that it is ready to go.
hr = FileReadHandle(hPipe, reinterpret_cast<LPBYTE>(&dwAck), sizeof(dwAck));
ExitOnFailure(hr, "Failed to read ACK from pipe.");
// The ACK should match out expected child process id.
//if (pConnection->dwProcessId != dwAck)
//{
// hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
// ExitOnRootFailure(hr, "Incorrect ACK from elevated pipe: %u", dwAck);
//}
}
LExit:
return hr;
}
/*******************************************************************
PipeTerminateLoggingPipe -
*******************************************************************/
extern "C" HRESULT PipeTerminateLoggingPipe(
__in HANDLE hLoggingPipe,
__in DWORD dwParentExitCode
)
{
HRESULT hr = S_OK;
BYTE* pbData = NULL;
SIZE_T cbData = 0;
// Prepare the exit message.
hr = BuffWriteNumber(&pbData, &cbData, dwParentExitCode);
ExitOnFailure(hr, "Failed to write exit code to message buffer.");
hr = WritePipeMessage(hLoggingPipe, static_cast<DWORD>(BURN_PIPE_MESSAGE_TYPE_COMPLETE), pbData, cbData);
ExitOnFailure(hr, "Failed to post complete message to logging pipe.");
LExit:
ReleaseBuffer(pbData);
return hr;
}
/*******************************************************************
PipeTerminateChildProcess -
*******************************************************************/
extern "C" HRESULT PipeTerminateChildProcess(
__in BURN_PIPE_CONNECTION* pConnection,
__in DWORD dwParentExitCode,
__in BOOL fRestart
)
{
HRESULT hr = S_OK;
BYTE* pbData = NULL;
SIZE_T cbData = 0;
BOOL fTimedOut = FALSE;
// Prepare the exit message.
hr = BuffWriteNumber(&pbData, &cbData, dwParentExitCode);
ExitOnFailure(hr, "Failed to write exit code to message buffer.");
hr = BuffWriteNumber(&pbData, &cbData, fRestart);
ExitOnFailure(hr, "Failed to write restart to message buffer.");
// Send the messages.
if (INVALID_HANDLE_VALUE != pConnection->hCachePipe)
{
hr = WritePipeMessage(pConnection->hCachePipe, static_cast<DWORD>(BURN_PIPE_MESSAGE_TYPE_TERMINATE), pbData, cbData);
ExitOnFailure(hr, "Failed to post terminate message to child process cache thread.");
}
hr = WritePipeMessage(pConnection->hPipe, static_cast<DWORD>(BURN_PIPE_MESSAGE_TYPE_TERMINATE), pbData, cbData);
ExitOnFailure(hr, "Failed to post terminate message to child process.");
// If we were able to get a handle to the other process, wait for it to exit.
if (pConnection->hProcess)
{
hr = AppWaitForSingleObject(pConnection->hProcess, PIPE_WAIT_FOR_CONNECTION * PIPE_RETRY_FOR_CONNECTION);
ExitOnWaitObjectFailure(hr, fTimedOut, "Failed to wait for child process exit.");
AssertSz(!fTimedOut, "Timed out while waiting for child process to exit.");
}
#ifdef DEBUG
if (pConnection->hProcess && !fTimedOut)
{
DWORD dwChildExitCode = 0;
HRESULT hrDebug = S_OK;
hrDebug = CoreWaitForProcCompletion(pConnection->hProcess, 0, &dwChildExitCode);
if (E_ACCESSDENIED != hrDebug && FAILED(hrDebug)) // if the other process is elevated and we are not, then we'll get ERROR_ACCESS_DENIED.
{
TraceError(hrDebug, "Failed to wait for child process completion.");
}
AssertSz(E_ACCESSDENIED == hrDebug || dwChildExitCode == dwParentExitCode,
"Child elevated process did not return matching exit code to parent process.");
}
#endif
LExit:
ReleaseBuffer(pbData);
return hr;
}
/*******************************************************************
PipeChildConnect - Called from the child process to connect back
to the pipe provided by the parent process.
*******************************************************************/
extern "C" HRESULT PipeChildConnect(
__in BURN_PIPE_CONNECTION* pConnection,
__in BOOL fCompanion
)
{
Assert(pConnection->sczName);
Assert(pConnection->sczSecret);
Assert(!pConnection->hProcess);
Assert(INVALID_HANDLE_VALUE == pConnection->hPipe);
Assert(INVALID_HANDLE_VALUE == pConnection->hCachePipe);
Assert(INVALID_HANDLE_VALUE == pConnection->hLoggingPipe);
HRESULT hr = S_OK;
LPWSTR sczPipeName = NULL;
// Try to connect to the parent.
hr = StrAllocFormatted(&sczPipeName, PIPE_NAME_FORMAT_STRING, pConnection->sczName);
ExitOnFailure(hr, "Failed to allocate name of parent pipe.");
hr = E_UNEXPECTED;
for (DWORD cRetry = 0; FAILED(hr) && cRetry < PIPE_RETRY_FOR_CONNECTION; ++cRetry)
{
pConnection->hPipe = ::CreateFileW(sczPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == pConnection->hPipe)
{
hr = HRESULT_FROM_WIN32(::GetLastError());
if (E_FILENOTFOUND == hr) // if the pipe isn't created, call it a timeout waiting on the parent.
{
hr = HRESULT_FROM_WIN32(ERROR_TIMEOUT);
}
::Sleep(PIPE_WAIT_FOR_CONNECTION);
}
else // we have a connection, go with it.
{
hr = S_OK;
}
}
ExitOnRootFailure(hr, "Failed to open parent pipe: %ls", sczPipeName)
// Verify the parent and notify it that the child connected.
hr = ChildPipeConnected(pConnection->hPipe, pConnection->sczSecret, &pConnection->dwProcessId);
ExitOnFailure(hr, "Failed to verify parent pipe: %ls", sczPipeName);
if (fCompanion)
{
// Connect to the parent for the cache pipe.
hr = StrAllocFormatted(&sczPipeName, CACHE_PIPE_NAME_FORMAT_STRING, pConnection->sczName);
ExitOnFailure(hr, "Failed to allocate name of parent cache pipe.");
pConnection->hCachePipe = ::CreateFileW(sczPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == pConnection->hCachePipe)
{
ExitWithLastError(hr, "Failed to open parent cache pipe: %ls", sczPipeName)
}
// Verify the parent and notify it that the child connected.
hr = ChildPipeConnected(pConnection->hCachePipe, pConnection->sczSecret, &pConnection->dwProcessId);
ExitOnFailure(hr, "Failed to verify parent cache pipe: %ls", sczPipeName);
// Connect to the parent for the logging pipe.
hr = StrAllocFormatted(&sczPipeName, LOGGING_PIPE_NAME_FORMAT_STRING, pConnection->sczName);
ExitOnFailure(hr, "Failed to allocate name of parent logging pipe.");
pConnection->hLoggingPipe = ::CreateFileW(sczPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == pConnection->hLoggingPipe)
{
ExitWithLastError(hr, "Failed to open parent logging pipe: %ls", sczPipeName)
}
// Verify the parent and notify it that the child connected.
hr = ChildPipeConnected(pConnection->hLoggingPipe, pConnection->sczSecret, &pConnection->dwProcessId);
ExitOnFailure(hr, "Failed to verify parent logging pipe: %ls", sczPipeName);
}
pConnection->hProcess = ::OpenProcess(SYNCHRONIZE, FALSE, pConnection->dwProcessId);
ExitOnNullWithLastError(pConnection->hProcess, hr, "Failed to open companion process with PID: %u", pConnection->dwProcessId);
LExit:
ReleaseStr(sczPipeName);
return hr;
}
static HRESULT AllocatePipeMessage(
__in DWORD dwMessage,
__in_bcount_opt(cbData) LPVOID pvData,
__in SIZE_T cbData,
__out_bcount(cb) LPVOID* ppvMessage,
__out SIZE_T* pcbMessage
)
{
HRESULT hr = S_OK;
LPVOID pv = NULL;
size_t cb = 0;
DWORD dwcbData = 0;
// If no data was provided, ensure the count of bytes is zero.
if (!pvData)
{
cbData = 0;
}
else if (MAXDWORD < cbData)
{
ExitWithRootFailure(hr, E_INVALIDDATA, "Pipe message is too large.");
}
hr = ::SizeTAdd(sizeof(dwMessage) + sizeof(dwcbData), cbData, &cb);
ExitOnRootFailure(hr, "Failed to calculate total pipe message size");
dwcbData = (DWORD)cbData;
// Allocate the message.
pv = MemAlloc(cb, FALSE);
ExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to allocate memory for message.");
memcpy_s(pv, cb, &dwMessage, sizeof(dwMessage));
memcpy_s(static_cast<BYTE*>(pv) + sizeof(dwMessage), cb - sizeof(dwMessage), &dwcbData, sizeof(dwcbData));
if (dwcbData)
{
memcpy_s(static_cast<BYTE*>(pv) + sizeof(dwMessage) + sizeof(dwcbData), cb - sizeof(dwMessage) - sizeof(dwcbData), pvData, dwcbData);
}
*pcbMessage = cb;
*ppvMessage = pv;
pv = NULL;
LExit:
ReleaseMem(pv);
return hr;
}
static void FreePipeMessage(
__in BURN_PIPE_MESSAGE *pMsg
)
{
if (pMsg->fAllocatedData)
{
ReleaseNullMem(pMsg->pvData);
pMsg->fAllocatedData = FALSE;
}
}
static HRESULT WritePipeMessage(
__in HANDLE hPipe,
__in DWORD dwMessage,
__in_bcount_opt(cbData) LPVOID pvData,
__in SIZE_T cbData
)
{
HRESULT hr = S_OK;
LPVOID pv = NULL;
SIZE_T cb = 0;
hr = AllocatePipeMessage(dwMessage, pvData, cbData, &pv, &cb);
ExitOnFailure(hr, "Failed to allocate message to write.");
// Write the message.
hr = FileWriteHandle(hPipe, reinterpret_cast<LPCBYTE>(pv), cb);
ExitOnFailure(hr, "Failed to write message type to pipe.");
LExit:
ReleaseMem(pv);
return hr;
}
static HRESULT GetPipeMessage(
__in HANDLE hPipe,
__in BURN_PIPE_MESSAGE* pMsg
)
{
HRESULT hr = S_OK;
BYTE pbMessageAndByteCount[sizeof(DWORD) + sizeof(DWORD)] = { };
hr = FileReadHandle(hPipe, pbMessageAndByteCount, sizeof(pbMessageAndByteCount));
if (HRESULT_FROM_WIN32(ERROR_BROKEN_PIPE) == hr)
{
memset(pbMessageAndByteCount, 0, sizeof(pbMessageAndByteCount));
hr = S_FALSE;
}
ExitOnFailure(hr, "Failed to read message from pipe.");
pMsg->dwMessage = *(DWORD*)(pbMessageAndByteCount);
pMsg->cbData = *(DWORD*)(pbMessageAndByteCount + sizeof(DWORD));
if (pMsg->cbData)
{
pMsg->pvData = MemAlloc(pMsg->cbData, FALSE);
ExitOnNull(pMsg->pvData, hr, E_OUTOFMEMORY, "Failed to allocate data for message.");
hr = FileReadHandle(hPipe, reinterpret_cast<LPBYTE>(pMsg->pvData), pMsg->cbData);
ExitOnFailure(hr, "Failed to read data for message.");
pMsg->fAllocatedData = TRUE;
}
LExit:
if (!pMsg->fAllocatedData && pMsg->pvData)
{
MemFree(pMsg->pvData);
}
return hr;
}
static HRESULT ChildPipeConnected(
__in HANDLE hPipe,
__in_z LPCWSTR wzSecret,
__inout DWORD* pdwProcessId
)
{
HRESULT hr = S_OK;
LPWSTR sczVerificationSecret = NULL;
DWORD cbVerificationSecret = 0;
DWORD dwVerificationProcessId = 0;
DWORD dwAck = ::GetCurrentProcessId(); // send our process id as the ACK.
// Read the verification secret.
hr = FileReadHandle(hPipe, reinterpret_cast<LPBYTE>(&cbVerificationSecret), sizeof(cbVerificationSecret));
ExitOnFailure(hr, "Failed to read size of verification secret from parent pipe.");
if (255 < cbVerificationSecret / sizeof(WCHAR))
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
ExitOnRootFailure(hr, "Verification secret from parent is too big.");
}
hr = StrAlloc(&sczVerificationSecret, cbVerificationSecret / sizeof(WCHAR) + 1);
ExitOnFailure(hr, "Failed to allocate buffer for verification secret.");
FileReadHandle(hPipe, reinterpret_cast<LPBYTE>(sczVerificationSecret), cbVerificationSecret);
ExitOnFailure(hr, "Failed to read verification secret from parent pipe.");
// Verify the secrets match.
if (CSTR_EQUAL != ::CompareStringW(LOCALE_NEUTRAL, 0, sczVerificationSecret, -1, wzSecret, -1))
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
ExitOnRootFailure(hr, "Verification secret from parent does not match.");
}
// Read the verification process id.
hr = FileReadHandle(hPipe, reinterpret_cast<LPBYTE>(&dwVerificationProcessId), sizeof(dwVerificationProcessId));
ExitOnFailure(hr, "Failed to read verification process id from parent pipe.");
// If a process id was not provided, we'll trust the process id from the parent.
if (*pdwProcessId == 0)
{
*pdwProcessId = dwVerificationProcessId;
}
else if (*pdwProcessId != dwVerificationProcessId) // verify the ids match.
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
ExitOnRootFailure(hr, "Verification process id from parent does not match.");
}
// All is well, tell the parent process.
// TODO: consider sending BURN_PROTOCOL_VERSION as a way to verify compatibility.
hr = FileWriteHandle(hPipe, reinterpret_cast<LPCBYTE>(&dwAck), sizeof(dwAck));
ExitOnFailure(hr, "Failed to inform parent process that child is running.");
LExit:
ReleaseStr(sczVerificationSecret);
return hr;
}
|