summaryrefslogtreecommitdiff
path: root/src/ipcman/ipcsharedsrc.cpp
blob: ed1e7dc1ba854476c7ddd95a1d0d9a04bbdd0606 (plain)
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// ==++==
//
 
//
// ==--==
//*****************************************************************************
// File: IPCSharedSrc.cpp
//
// Shared source for COM+ IPC Reader & Writer classes
//
//*****************************************************************************

#include "stdafx.h"
#include "ipcshared.h"
#include "ipcmanagerinterface.h"

#ifndef FEATURE_CORECLR
#include "AppXUtil.h"
#endif

#if defined(FEATURE_IPCMAN)

//-----------------------------------------------------------------------------
// Close a handle and pointer to any memory mapped file
//-----------------------------------------------------------------------------
void IPCShared::CloseMemoryMappedFile(HANDLE & hMemFile, void * & pBlock)
{
    WRAPPER_NO_CONTRACT;

    LOG((LF_CORDB, LL_INFO10, "IPCS::CloseMemoryMappedFile: closing 0x%08x\n", hMemFile));

    if (pBlock != NULL) {
        if (!UnmapViewOfFile(pBlock))
            _ASSERTE(!"UnmapViewOfFile failed");
        pBlock = NULL;
    }

    if (hMemFile != NULL) {
        CloseHandle(hMemFile);
        hMemFile = NULL;
    }
}

//-----------------------------------------------------------------------------
// Based on the pid, write a unique name for a memory mapped file
//-----------------------------------------------------------------------------
void IPCShared::GenerateName(DWORD pid, SString & sName)
{
    WRAPPER_NO_CONTRACT;

    const WCHAR * szFormat = CorLegacyPrivateIPCBlock;
    szFormat = L"Global\\" CorLegacyPrivateIPCBlock;

    sName.Printf(szFormat, pid);
}

//-----------------------------------------------------------------------------
// Based on the pid, write a unique name for a memory mapped file
//-----------------------------------------------------------------------------
void IPCShared::GenerateNameLegacyTempV4(DWORD pid, SString & sName)
{
    WRAPPER_NO_CONTRACT;

    const WCHAR * szFormat = CorLegacyPrivateIPCBlockTempV4;
    szFormat = L"Global\\" CorLegacyPrivateIPCBlockTempV4;

    sName.Printf(szFormat, pid);
}

//-----------------------------------------------------------------------------
// Based on the pid, write a unique name for a memory mapped file
//-----------------------------------------------------------------------------
void IPCShared::GenerateLegacyPublicName(DWORD pid, SString & sName)
{
    WRAPPER_NO_CONTRACT;

    const WCHAR * szFormat = CorLegacyPublicIPCBlock;
    szFormat = L"Global\\" CorLegacyPublicIPCBlock;

    sName.Printf(szFormat, pid);
}

#ifndef DACCESS_COMPILE

//-----------------------------------------------------------------------------
// Based on the pid, write a unique name for the IPCBlockTable on Vista and Higher
//-----------------------------------------------------------------------------
HRESULT IPCShared::GenerateBlockTableName(DWORD pid, SString & sName, HANDLE & pBoundaryDesc, HANDLE & pPrivateNamespace, PSID* pSID, BOOL bCreate)
{
    WRAPPER_NO_CONTRACT;
    HRESULT hr = E_FAIL;

#define SIZE 100
    const WCHAR * szFormat = CorSxSPublicIPCBlock;
    static HMODULE hKernel32 = NULL;
    if(hKernel32 == NULL)
        hKernel32 = WszGetModuleHandle(L"kernel32.dll");
    if(hKernel32 == NULL)
    {
        hr = HRESULT_FROM_GetLastError();
        return hr;
    }
    //We are using static function pointers so that we dont call GetProcAddress every time
    //We know that the Writer will call this function only once and the reader (perfmon) is a single
    //threaded App. Therefore its safe to assign static local variables in this case. 
    typedef WINBASEAPI BOOL (WINAPI ADD_SID_TO_BOUNDARY_DESCRIPTOR)(HANDLE*, PSID);
    static ADD_SID_TO_BOUNDARY_DESCRIPTOR * pAddSIDToBoundaryDescriptor = NULL;

    typedef WINBASEAPI HANDLE (WINAPI CREATE_BOUNDARY_DESCRIPTOR)(LPCWSTR,ULONG);
    static CREATE_BOUNDARY_DESCRIPTOR * pCreateBoundaryDescriptor = NULL;
    
    typedef WINBASEAPI HANDLE (WINAPI CREATE_PRIVATE_NAMESPACE )(LPSECURITY_ATTRIBUTES, LPVOID, LPCWSTR);
    static CREATE_PRIVATE_NAMESPACE * pCreatePrivateNamespace = NULL;

    typedef WINBASEAPI HANDLE (WINAPI OPEN_PRIVATE_NAMESPACE)(LPVOID,LPCWSTR);
    static OPEN_PRIVATE_NAMESPACE * pOpenPrivateNamespace = NULL;

    if(pAddSIDToBoundaryDescriptor == NULL)
        pAddSIDToBoundaryDescriptor = (ADD_SID_TO_BOUNDARY_DESCRIPTOR *)GetProcAddress(hKernel32, "AddSIDToBoundaryDescriptor"); 
    if(pCreateBoundaryDescriptor == NULL)
        pCreateBoundaryDescriptor = (CREATE_BOUNDARY_DESCRIPTOR *)GetProcAddress(hKernel32, "CreateBoundaryDescriptorW"); 
    if(pCreatePrivateNamespace == NULL)
        pCreatePrivateNamespace = (CREATE_PRIVATE_NAMESPACE *)GetProcAddress(hKernel32, "CreatePrivateNamespaceW"); 
    if(pOpenPrivateNamespace==NULL)
        pOpenPrivateNamespace = (OPEN_PRIVATE_NAMESPACE *)GetProcAddress(hKernel32, "OpenPrivateNamespaceW");
    _ASSERTE((pAddSIDToBoundaryDescriptor != NULL) && 
            (pCreateBoundaryDescriptor != NULL) && 
            (pCreatePrivateNamespace != NULL) && 
            (pOpenPrivateNamespace != NULL));

    if ((pAddSIDToBoundaryDescriptor == NULL) || 
            (pCreateBoundaryDescriptor == NULL) || 
            (pCreatePrivateNamespace == NULL) || 
            (pOpenPrivateNamespace == NULL))
    {
        return ERROR_PROC_NOT_FOUND;
    }

    WCHAR wsz[SIZE];
    swprintf_s(wsz,SIZE, CorSxSBoundaryDescriptor, pid);

    ULONG flags = 0;
    if (RunningOnWin8())
    {
        // on win8 we specify this flag regardless if the process is inside an appcontainer, the kernel will do the right thing.
        // note that for appcontainers this flag is necessary regardless of producer or consumer, ie you can't create a boundary
        // descriptor in an appcontainer process without adding the appcontainer SID (the API call will fail).
        flags |= CREATE_BOUNDARY_DESCRIPTOR_ADD_APPCONTAINER_SID;
    }

    pBoundaryDesc = (*pCreateBoundaryDescriptor)((LPCWSTR)&wsz, flags);
    if(!pBoundaryDesc)
    {
        hr = HRESULT_FROM_GetLastError();
        return hr;
    }        
    SID_IDENTIFIER_AUTHORITY SIDWorldAuth = SECURITY_WORLD_SID_AUTHORITY;
    if(!AllocateAndInitializeSid( &SIDWorldAuth, 1,SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, pSID)) 
    {
         hr = HRESULT_FROM_GetLastError();
         return hr;
    }
    if(!(*pAddSIDToBoundaryDescriptor) (&pBoundaryDesc,*pSID))
    {
        hr = HRESULT_FROM_GetLastError();
        return hr;
    }

#ifndef FEATURE_CORECLR
    // when pid != GetCurrentProcessId() it means we're the consumer opening other process perf counter data
    if (pid != GetCurrentProcessId())
    {
        // if the target process is inside an appcontainer we need to add the appcontainer SID to the boundary descriptor.
        NewArrayHolder<BYTE> pbTokenMem;
        hr = AppX::GetAppContainerTokenInfoForProcess(pid, pbTokenMem);

        if (FAILED(hr))
        {
            // failed to open the target's process, continue on
            // assuming that the process isn't in an AppContainer.
            _ASSERTE(pbTokenMem == NULL);
        }
        else
        {
            if (hr == S_FALSE)
            {
                // not an appcontainer
                _ASSERTE(pbTokenMem == NULL);
            }
            else
            {
                // process is an appcontainer so add the SID
                PTOKEN_APPCONTAINER_INFORMATION pAppContainerTokenInfo =
                    reinterpret_cast<PTOKEN_APPCONTAINER_INFORMATION>(pbTokenMem.GetValue());
                _ASSERTE(pAppContainerTokenInfo);
                _ASSERTE(pAppContainerTokenInfo->TokenAppContainer);

                if (!(*pAddSIDToBoundaryDescriptor)(&pBoundaryDesc, pAppContainerTokenInfo->TokenAppContainer))
                    return HRESULT_FROM_WIN32(GetLastError());
            }
        }
    }
#endif // FEATURE_CORECLR
    
    if(bCreate)
    {
        SECURITY_ATTRIBUTES *pSA = NULL;
        IPCShared::CreateWinNTDescriptor(pid, FALSE, &pSA, PrivateNamespace, eDescriptor_Public);
        pPrivateNamespace = (*pCreatePrivateNamespace)(pSA, (VOID *)(pBoundaryDesc), 
                                                        (LPCWSTR)CorSxSWriterPrivateNamespacePrefix);
        if(!pPrivateNamespace)
        { 
            hr = HRESULT_FROM_GetLastError();
        }
        IPCShared::DestroySecurityAttributes(pSA);

        if(!pPrivateNamespace)
        { 
            //if already created by a different version of the runtime we return OK.
            if(hr ==HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
            {
                hr = S_OK;
            }
            else
            {
                return hr;
            }
        }
     }
     else
     {
        pPrivateNamespace = (*pOpenPrivateNamespace)((VOID *)(pBoundaryDesc), (LPCWSTR)CorSxSReaderPrivateNamespacePrefix);
        if(!pPrivateNamespace)
        { 
            hr = HRESULT_FROM_GetLastError();
            return hr;
        }
     }
    szFormat = (bCreate ? CorSxSWriterPrivateNamespacePrefix L"\\"  CorSxSVistaPublicIPCBlock : CorSxSReaderPrivateNamespacePrefix L"\\"  CorSxSVistaPublicIPCBlock);
    sName.Printf(szFormat);
    hr=S_OK;

    return hr;
}

#endif

//-----------------------------------------------------------------------------
// Free's the handle to a boundary descriptor and a SID
//-----------------------------------------------------------------------------
HRESULT IPCShared::FreeHandles(HANDLE & hBoundaryDescriptor, PSID & pSID)
{
    WRAPPER_NO_CONTRACT;
    HRESULT hr = S_OK;
    if(hBoundaryDescriptor != NULL) 
    {
        static HMODULE hKernel32 = NULL;
        if(hKernel32 == NULL)
            hKernel32 = WszGetModuleHandle(L"kernel32.dll");
        if(hKernel32 == NULL)
        {
            hr = HRESULT_FROM_GetLastError();
            return hr;
        }
        typedef WINBASEAPI VOID (WINAPI DELETE_BOUNDARY_DESCRIPTOR)(HANDLE);
        static DELETE_BOUNDARY_DESCRIPTOR * pDeleteBoundaryDescriptor = NULL;
        if(pDeleteBoundaryDescriptor == NULL) 
            pDeleteBoundaryDescriptor = (DELETE_BOUNDARY_DESCRIPTOR *)GetProcAddress(hKernel32, "DeleteBoundaryDescriptor");
        _ASSERTE(pDeleteBoundaryDescriptor != NULL);
        if (pDeleteBoundaryDescriptor == NULL)
        {
            hr = ERROR_PROC_NOT_FOUND;
        }
        else 
        {
            (*pDeleteBoundaryDescriptor)(hBoundaryDescriptor);
            hBoundaryDescriptor = NULL;
    
        }
    }
    if(pSID != NULL)
    {
        FreeSid(pSID);
        pSID = NULL;
    }

    return hr;
}

//--------------------------------------------------------------------------------------
// Free's the handle to a boundary descriptor, a SID and a handle to a privatenamespace
//--------------------------------------------------------------------------------------
HRESULT IPCShared::FreeHandles(HANDLE & hBoundaryDescriptor, PSID & pSID, HANDLE & hPrivateNamespace)
{
    WRAPPER_NO_CONTRACT;
    HRESULT hr = S_OK;

    hr = IPCShared::FreeHandles(hBoundaryDescriptor,pSID);
    if(!SUCCEEDED(hr))
        return hr;
    if(hPrivateNamespace != NULL) 
    {
        static HMODULE hKernel32 = NULL;
        if(hKernel32 == NULL)
            hKernel32 = WszGetModuleHandle(L"kernel32.dll");
        if(hKernel32 == NULL)
        {
            hr = HRESULT_FROM_GetLastError();
            return hr;
        }
        typedef WINBASEAPI BOOL (WINAPI CLOSE_PRIVATE_NAMESPACE)(HANDLE, ULONG);
        static CLOSE_PRIVATE_NAMESPACE * pClosePrivateNamespace;
        if(pClosePrivateNamespace == NULL)
            pClosePrivateNamespace = (CLOSE_PRIVATE_NAMESPACE *)GetProcAddress(hKernel32, "ClosePrivateNamespace");
        _ASSERTE(pClosePrivateNamespace != NULL);
        if (pClosePrivateNamespace == NULL)
        {
            hr = ERROR_PROC_NOT_FOUND;
        }
        else
        {
            BOOL isClosed = (*pClosePrivateNamespace)(hPrivateNamespace,0);
            hPrivateNamespace = NULL;
            if(!isClosed)
            {
                hr = HRESULT_FROM_GetLastError();
            }
            
        }
    }

    return hr;
}

HRESULT IPCShared::CreateWinNTDescriptor(DWORD pid, BOOL bRestrictiveACL, SECURITY_ATTRIBUTES **ppSA, KernelObject whatObject)
{
    WRAPPER_NO_CONTRACT;

    return IPCShared::CreateWinNTDescriptor(pid, bRestrictiveACL, ppSA, whatObject, eDescriptor_Private);
}

//-----------------------------------------------------------------------------
// Setup a security descriptor for the named kernel objects if we're on NT.
//-----------------------------------------------------------------------------

HRESULT IPCShared::CreateWinNTDescriptor(DWORD pid, BOOL bRestrictiveACL, SECURITY_ATTRIBUTES **ppSA, KernelObject whatObject, EDescriptorType descType)
{
    WRAPPER_NO_CONTRACT;

    HRESULT hr = NO_ERROR;

    // Gotta have a place to stick the new SA...
    if (ppSA == NULL)
    {
        _ASSERTE(!"Caller must supply ppSA");
        return E_INVALIDARG;
    }

    *ppSA = NULL;

    ACL *pACL = NULL;
    SECURITY_DESCRIPTOR *pSD = NULL;
    SECURITY_ATTRIBUTES *pSA = NULL;

    // Allocate a SD.
    _ASSERTE (SECURITY_DESCRIPTOR_MIN_LENGTH == sizeof(SECURITY_DESCRIPTOR));
    pSD = new (nothrow) SECURITY_DESCRIPTOR;

    if (pSD == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto errExit;
    }

    // Do basic SD initialization
    if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
    {
        hr = HRESULT_FROM_GetLastError();
        goto errExit;
    }

    // Grab the ACL for the IPC block for the given process
    if (!InitializeGenericIPCAcl(pid, bRestrictiveACL, &pACL, whatObject, descType))
    {
        hr = E_FAIL;
        goto errExit;
    }

    // Add the ACL as the DACL for the SD.
    if (!SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
    {
        hr = HRESULT_FROM_GetLastError();
        goto errExit;
    }

    // Allocate a SA.
    pSA = new (nothrow) SECURITY_ATTRIBUTES;

    if (pSA == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto errExit;
    }

    // Pass out the new SA.
    *ppSA = pSA;

    pSA->nLength = sizeof(SECURITY_ATTRIBUTES);
    pSA->lpSecurityDescriptor = pSD;
    pSA->bInheritHandle = FALSE;

    // uncomment this line if you want to see the DACL being generated.
    //DumpSD(pSD);

errExit:
    if (FAILED(hr))
    {
        if (pACL != NULL)
        {
            for(int i = 0; i < pACL->AceCount; i++)
                DeleteAce(pACL, i);

            delete [] pACL;
        }

        if (pSD != NULL)
            delete pSD;
    }

    return hr;
}

//-----------------------------------------------------------------------------
// Helper to destroy the security attributes for the shared memory for a given
// process.
//-----------------------------------------------------------------------------
void IPCShared::DestroySecurityAttributes(SECURITY_ATTRIBUTES *pSA)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    // We'll take a NULL param just to be nice.
    if (pSA == NULL)
        return;

    // Cleanup the DACL in the SD.
    SECURITY_DESCRIPTOR *pSD = (SECURITY_DESCRIPTOR*) pSA->lpSecurityDescriptor;

    if (pSD != NULL)
    {
        // Grab the DACL
        BOOL isDACLPresent = FALSE;
        BOOL isDefaultDACL = FALSE;
        ACL *pACL = NULL;

        BOOL res = GetSecurityDescriptorDacl(pSD, &isDACLPresent, &pACL, &isDefaultDACL);

        // If we got the DACL, then free the stuff inside of it.
        if (res && isDACLPresent && (pACL != NULL) && !isDefaultDACL)
        {
            for(int i = 0; i < pACL->AceCount; i++)
                DeleteAce(pACL, i);

            delete [] pACL;
        }

        // Free the SD from within the SA.
        delete pSD;
    }

    // Finally, free the SA.
    delete pSA;
}

//-----------------------------------------------------------------------------
// Given a PID, grab the SID for the owner of the process.
//
// NOTE:: Caller has to free *ppBufferToFreeByCaller.
// This buffer is allocated to hold the PSID return by GetPrcoessTokenInformation.
// The tkOwner field may contain a poniter into this allocated buffer. So we cannot free
// the buffer in GetSidForProcess.
//
//-----------------------------------------------------------------------------
HRESULT IPCShared::GetSidForProcess(HINSTANCE hDll,
                                    DWORD pid,
                                    PSID *ppSID,
                                    __deref_out_opt char **ppBufferToFreeByCaller)
{
    WRAPPER_NO_CONTRACT;

    HRESULT hr = S_OK;
    HANDLE hProc = NULL;
    HANDLE hToken = NULL;
    PSID_IDENTIFIER_AUTHORITY pSID = NULL;
    TOKEN_OWNER *ptkOwner = NULL;
    DWORD dwRetLength;

    LOG((LF_CORDB, LL_INFO10, "IPCWI::GSFP: GetSidForProcess 0x%x (%d)", pid, pid));

    // Grab a handle to the target process.
    hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);

    *ppBufferToFreeByCaller = NULL;

    if (hProc == NULL)
    {
        hr = HRESULT_FROM_GetLastError();

        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::GSFP: Unable to get SID for process. "
             "OpenProcess(%d) failed: 0x%08x\n", pid, hr));

        goto ErrorExit;
    }

    // Get the pointer to the requested function
    FARPROC pProcAddr = GetProcAddress(hDll, "OpenProcessToken");

    // If the proc address was not found, return error
    if (pProcAddr == NULL)
    {
        hr = HRESULT_FROM_GetLastError();

        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::GSFP: Unable to get SID for process. "
             "GetProcAddr (OpenProcessToken) failed: 0x%08x\n", hr));

        goto ErrorExit;
    }

    typedef BOOL WINAPI OPENPROCESSTOKEN(HANDLE, DWORD, PHANDLE);

    // Retrieve a handle of the access token
    if (!((OPENPROCESSTOKEN *)pProcAddr)(hProc, TOKEN_QUERY, &hToken))
    {
        hr = HRESULT_FROM_GetLastError();

        LOG((LF_CORDB, LL_INFO100,
             "IPCWI::GSFP: OpenProcessToken() failed: 0x%08x\n", hr));

        goto ErrorExit;
    }

    // Get the pointer to the requested function
    pProcAddr = GetProcAddress(hDll, "GetTokenInformation");

    // If the proc address was not found, return error
    if (pProcAddr == NULL)
    {
        hr = HRESULT_FROM_GetLastError();

        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::GSFP: Unable to get SID for process. "
             "GetProcAddr (GetTokenInformation) failed: 0x%08x\n", hr));

        goto ErrorExit;
    }

    typedef BOOL GETTOKENINFORMATION(HANDLE, TOKEN_INFORMATION_CLASS, LPVOID,
                                     DWORD, PDWORD);

    // get the required size of buffer
    ((GETTOKENINFORMATION *)pProcAddr) (hToken, TokenOwner, NULL,
                                        0, &dwRetLength);
    _ASSERTE (dwRetLength);

    *ppBufferToFreeByCaller = new (nothrow) char [dwRetLength];
    if ((ptkOwner = (TOKEN_OWNER *) *ppBufferToFreeByCaller) == NULL)
    {
        hr = E_OUTOFMEMORY;

        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::GSFP: OutOfMemory... "
             "GetTokenInformation() failed.\n"));

        goto ErrorExit;
    }

    if (!((GETTOKENINFORMATION *)pProcAddr) (hToken, TokenOwner, (LPVOID)ptkOwner,
                                            dwRetLength, &dwRetLength))
    {
        hr = HRESULT_FROM_GetLastError();

        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::GSFP: Unable to get SID for process. "
             "GetTokenInformation() failed: 0x%08x\n", hr));

        goto ErrorExit;
    }

    *ppSID = ptkOwner->Owner;

ErrorExit:
    if (hProc != NULL)
        CloseHandle(hProc);

    if (hToken != NULL)
        CloseHandle(hToken);

    return hr;
}

/* static  */
DWORD IPCShared::GetAccessFlagsForObject(KernelObject whatObject, BOOL bFullControlACL)
{
    _ASSERTE(whatObject >= 0 && whatObject < TotalKernelObjects);
    
    DWORD dwAccessFlags = 0;
    
    if (!bFullControlACL)
    {
        if (whatObject == Section)
            dwAccessFlags = (STANDARD_RIGHTS_ALL | SECTION_MAP_READ) & ~WRITE_DAC & ~WRITE_OWNER & ~DELETE;
        else if (whatObject == Event)
            dwAccessFlags = (EVENT_ALL_ACCESS) & ~WRITE_DAC & ~WRITE_OWNER & ~DELETE;
        else if (whatObject == PrivateNamespace)
            dwAccessFlags = FILE_MAP_READ;
    }
    else
    {
        _ASSERTE(whatObject != PrivateNamespace);
        if (whatObject == Section)
            dwAccessFlags = CLR_IPC_GENERIC_RIGHT;
        else if (whatObject == Event)
            dwAccessFlags = EVENT_ALL_ACCESS;
    }

    _ASSERTE(dwAccessFlags != 0);
    return dwAccessFlags;
}


//-----------------------------------------------------------------------------
// This function will initialize the Access Control List with three
// Access Control Entries:
// The first ACE entry grants all permissions to "Administrators".
// The second ACE grants all permissions to the monitoring users (for perfcounters).
// The third ACE grants all permissions to "Owner" of the target process.
//-----------------------------------------------------------------------------
BOOL IPCShared::InitializeGenericIPCAcl(DWORD pid, BOOL bRestrictiveACL, PACL *ppACL, KernelObject whatObject, EDescriptorType descType)
{
    WRAPPER_NO_CONTRACT;

    struct PermissionStruct
    {
        PSID    rgPSID;
        DWORD   rgAccessFlags;
    } PermStruct[MaxNumberACEs];

    SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
    HRESULT hr = S_OK;
    DWORD dwAclSize;
    BOOL returnCode = false;
    *ppACL = NULL;
    DWORD i;
    DWORD cActualACECount = 0;
    char *pBufferToFreeByCaller = NULL;
    int iSIDforAdmin = -1;
    int iSIDforUsers = -1;
    int iSIDforLoggingUsers = -1;
#if !defined (DACCESS_COMPILE) && !defined(FEATURE_CORECLR)
    NewArrayHolder<BYTE> pbTokenMem;
    PTOKEN_APPCONTAINER_INFORMATION pAppContainerTokenInfo = NULL;
#endif

    PermStruct[0].rgPSID = NULL;

    HINSTANCE hDll = WszGetModuleHandle(L"advapi32");

    if (hDll == NULL)
    {
        LOG((LF_CORDB, LL_INFO10, "IPCWI::IGIPCA: Unable to generate ACL for IPC. LoadLibrary (advapi32) failed.\n"));
        return false;
    }
    _ASSERTE(hDll != NULL);

    // Get the pointer to the requested function
    FARPROC pProcAddr = GetProcAddress(hDll, "AllocateAndInitializeSid");

    // If the proc address was not found, return error
    if (pProcAddr == NULL)
    {
        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::IGIPCA: Unable to generate ACL for IPC. "
             "GetProcAddr (AllocateAndInitializeSid) failed.\n"));
        goto ErrorExit;
    }

    typedef BOOL ALLOCATEANDINITIALIZESID(PSID_IDENTIFIER_AUTHORITY,
                            BYTE, DWORD, DWORD, DWORD, DWORD,
                            DWORD, DWORD, DWORD, DWORD, PSID *);


    BOOL bGrantAllAccess = ((descType == eDescriptor_Private) ? TRUE : FALSE);
    // Create a SID for the BUILTIN\Administrators group.
    // SECURITY_BUILTIN_DOMAIN_RID + DOMAIN_ALIAS_RID_ADMINS = all Administrators. This translates to (A;;GA;;;BA).
    if (!((ALLOCATEANDINITIALIZESID *) pProcAddr)(&SIDAuthNT,
                                                  2,
                                                  SECURITY_BUILTIN_DOMAIN_RID,
                                                  DOMAIN_ALIAS_RID_ADMINS,
                                                  0, 0, 0, 0, 0, 0,
                                                  &PermStruct[0].rgPSID))
    {
        hr = HRESULT_FROM_GetLastError();
        _ASSERTE(SUCCEEDED(hr));

        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::IGIPCA: failed to allocate AdminSid: 0x%08x\n", hr));

        goto ErrorExit;
    }
    // GENERIC_ALL access for Administrators
    PermStruct[cActualACECount].rgAccessFlags = GetAccessFlagsForObject(whatObject, bGrantAllAccess);

    iSIDforAdmin = cActualACECount;
    cActualACECount++;

    // Next, we get the SID for the owner of the current process.
    hr = GetSidForProcess(hDll, GetCurrentProcessId(), &(PermStruct[cActualACECount].rgPSID), &pBufferToFreeByCaller);
    DWORD accessFlags = 0;
    if (whatObject == Section) {
        //special case, grant SECTION_MAP_WRITE for current owner just to support inProc SxS.
        accessFlags = GetAccessFlagsForObject(whatObject, bGrantAllAccess) | SECTION_MAP_WRITE;
    }
    else {
        accessFlags = GetAccessFlagsForObject(whatObject, bGrantAllAccess);
    }
    PermStruct[cActualACECount].rgAccessFlags = accessFlags;

    // Don't fail out if we cannot get the SID for the owner of the current process. In this case, the
    // share memory block will be created with only Admin (and optionall "Users") permissions.
    // Currently we discovered the anonymous user doesn't have privilege to call OpenProcess. Without OpenProcess,
    // we cannot get the SID...
    //
    if (SUCCEEDED(hr))
    {
        cActualACECount++;
    }
#if _DEBUG
    else
        LOG((LF_CORDB, LL_INFO100, "IPCWI::IGIPCA: GetSidForProcess() failed: 0x%08x\n", hr));
#endif // _DEBUG


    if (descType == eDescriptor_Public)
    {
        DWORD dwRet = ((ALLOCATEANDINITIALIZESID *) pProcAddr)(&SIDAuthNT,
                                                    2,
                                                    SECURITY_BUILTIN_DOMAIN_RID,
                                                    DOMAIN_ALIAS_RID_MONITORING_USERS,
                                                    0, 0, 0, 0, 0, 0,
                                                    &PermStruct[cActualACECount].rgPSID);

        if (dwRet)
        {
            // "Users" shouldn't be able to write to block, delete object, change DACLs, or change ownership
            PermStruct[cActualACECount].rgAccessFlags = GetAccessFlagsForObject(whatObject, FALSE);

            iSIDforUsers = cActualACECount;
            cActualACECount++;
        }
        else
        {
            hr = HRESULT_FROM_GetLastError();
            _ASSERTE(SUCCEEDED(hr));

            LOG((LF_CORDB, LL_INFO10,
                 "IPCWI::IGIPCA: failed to allocate Users Sid: 0x%08x\n", hr));

            // non-fatal error, so don't goto errorexit
        }
        
        dwRet = ((ALLOCATEANDINITIALIZESID *) pProcAddr)(&SIDAuthNT,
                                                    2,
                                                    SECURITY_BUILTIN_DOMAIN_RID,
                                                    DOMAIN_ALIAS_RID_LOGGING_USERS,
                                                    0, 0, 0, 0, 0, 0,
                                                    &PermStruct[cActualACECount].rgPSID);
        if (dwRet)
        {
            PermStruct[cActualACECount].rgAccessFlags = GetAccessFlagsForObject(whatObject, FALSE);

            iSIDforLoggingUsers = cActualACECount;
            cActualACECount++;
        }
        else
        {
            hr = HRESULT_FROM_GetLastError();
            _ASSERTE(SUCCEEDED(hr));

            LOG((LF_CORDB, LL_INFO10,
                    "IPCWI::IGIPCA: failed to allocate Domain Logging Users Sid: 0x%08x\n", hr));

            // non-fatal error, so don't goto errorexit
        }

#if !defined(DACCESS_COMPILE) && !defined(FEATURE_CORECLR)
        // when running on win8 if the process is an appcontainer then add the appcontainer SID to the ACL
        // going down this code path means we're creating the descriptor for our current PID.
        _ASSERTE(pid == GetCurrentProcessId());
        hr = AppX::GetAppContainerTokenInfoForProcess(pid, pbTokenMem);

        if (FAILED(hr))
        {
            // failed to open the target's process, continue on
            // assuming that the process isn't in an AppContainer.
            _ASSERTE(pbTokenMem == NULL);
        }
        else
        {
            if (hr == S_FALSE)
            {
                // not an appcontainer
                _ASSERTE(pbTokenMem == NULL);
            }
            else
            {
                // process is an appcontainer so add the SID
                pAppContainerTokenInfo =
                    reinterpret_cast<PTOKEN_APPCONTAINER_INFORMATION>(pbTokenMem.GetValue());
                _ASSERTE(pAppContainerTokenInfo);
                _ASSERTE(pAppContainerTokenInfo->TokenAppContainer);

                PermStruct[cActualACECount].rgPSID = pAppContainerTokenInfo->TokenAppContainer;
                PermStruct[cActualACECount].rgAccessFlags = GetAccessFlagsForObject(whatObject, FALSE);
                ++cActualACECount;
            }
        }
#endif // !defined(DACCESS_COMPILE) && !defined(FEATURE_CORECLR)        
    }

    _ASSERTE(cActualACECount <= MaxNumberACEs);

    // Now, create an Initialize an ACL and add the ACE entries to it.  NOTE: We're not using "SetEntriesInAcl" because
    // it loads a bunch of other dlls which can be avoided by using this roundabout way!!

    // Get the pointer to the requested function
    pProcAddr = GetProcAddress(hDll, "InitializeAcl");

    // If the proc address was not found, return error
    if (pProcAddr == NULL)
    {
        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::IGIPCA: Unable to generate ACL for IPC. "
             "GetProcAddr (InitializeAcl) failed.\n"));
        goto ErrorExit;
    }

    // Also calculate the memory required for ACE entries in the ACL using the
    // following method:
    // "sizeof (ACCESS_ALLOWED_ACE) - sizeof (ACCESS_ALLOWED_ACE.SidStart) + GetLengthSid (pAceSid);"

    dwAclSize = sizeof (ACL) + (sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD)) * cActualACECount;

    for (i = 0; i < cActualACECount; i++)
    {
        dwAclSize += GetLengthSid(PermStruct[i].rgPSID);
    }

    // now allocate memory
    if ((*ppACL = (PACL) new (nothrow) char[dwAclSize]) == NULL)
    {
        LOG((LF_CORDB, LL_INFO10, "IPCWI::IGIPCA: OutOfMemory... 'new Acl' failed.\n"));

        goto ErrorExit;
    }

    typedef BOOL INITIALIZEACL(PACL, DWORD, DWORD);

    if (!((INITIALIZEACL *)pProcAddr)(*ppACL, dwAclSize, ACL_REVISION))
    {
        hr = HRESULT_FROM_GetLastError();

        LOG((LF_CORDB, LL_INFO100,
             "IPCWI::IGIPCA: InitializeACL() failed: 0x%08x\n", hr));

        goto ErrorExit;
    }

    // Get the pointer to the requested function
    pProcAddr = GetProcAddress(hDll, "AddAccessAllowedAce");

    // If the proc address was not found, return error
    if (pProcAddr == NULL)
    {
        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::IGIPCA: Unable to generate ACL for IPC. "
             "GetProcAddr (AddAccessAllowedAce) failed.\n"));
        goto ErrorExit;
    }

    typedef BOOL ADDACCESSALLOWEDACE(PACL, DWORD, DWORD, PSID);

    for (i=0; i < cActualACECount; i++)
    {
        if (!((ADDACCESSALLOWEDACE *)pProcAddr)(*ppACL,
                                                ACL_REVISION,
                                                PermStruct[i].rgAccessFlags,
                                                PermStruct[i].rgPSID))

        {
            hr = HRESULT_FROM_GetLastError();

            LOG((LF_CORDB, LL_INFO100,
                 "IPCWI::IGIPCA: AddAccessAllowedAce() failed: 0x%08x\n", hr));
            goto ErrorExit;
        }
    }

    returnCode = true;
    goto NormalExit;


ErrorExit:
    returnCode = FALSE;

    if (*ppACL)
    {
        delete [] (*ppACL);
        *ppACL = NULL;
    }

NormalExit:

    if (pBufferToFreeByCaller != NULL)
        delete [] pBufferToFreeByCaller;

    // Get the pointer to the requested function
    pProcAddr = GetProcAddress(hDll, "FreeSid");

    // If the proc address was not found, return error
    if (pProcAddr == NULL)
    {
        LOG((LF_CORDB, LL_INFO10,
             "IPCWI::IGIPCA: Unable to generate ACL for IPC. "
             "GetProcAddr (FreeSid) failed.\n"));
        return false;
    }

    typedef BOOL FREESID(PSID);

    // Free the SID created earlier. Function does not return a value.
    if( iSIDforAdmin != -1 )
        ((FREESID *) pProcAddr)(PermStruct[iSIDforAdmin].rgPSID);

    // free the SID for "Users"
    if (iSIDforUsers != -1)
        ((FREESID *) pProcAddr)(PermStruct[iSIDforUsers].rgPSID);

    // free the SID for "Performance Logging Users"
    if (iSIDforLoggingUsers != -1)
        ((FREESID *) pProcAddr)(PermStruct[iSIDforLoggingUsers].rgPSID);

    return returnCode;
}

#endif // FEATURE_IPCMAN