summaryrefslogtreecommitdiff
path: root/src/utilcode/securityutil.cpp
blob: b5032aa9b415cdd35053a878d9ba3757a8a8ef39 (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
// 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.


#include "stdafx.h"

#include "securityutil.h"
#include "ex.h"

#include "securitywrapper.h"

// This is defined in ipcmanagerinterface.h, but including that is problematic at the moment...
// These are the right that we will give to the global section and global events used
// in communicating between debugger and debugee
//
// SECTION_ALL_ACCESS is needed for the IPC block. Unfortunately, we DACL our events and
// IPC block identically. Or this particular right does not need to bleed into here. 
//
#ifndef CLR_IPC_GENERIC_RIGHT
#define CLR_IPC_GENERIC_RIGHT (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | STANDARD_RIGHTS_ALL | SECTION_ALL_ACCESS)
#endif


//*****************************************************************
// static helper function
//
// helper to form ACL that contains AllowedACE of users of current 
// process and target process
//
// [IN] pid - target process id
// [OUT] ppACL - ACL for the process
//
// Clean up - 
// Caller remember to call FreeACL() on *ppACL
//*****************************************************************
HRESULT SecurityUtil::GetACLOfPid(DWORD pid, PACL *ppACL)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    HRESULT         hr = S_OK;
    _ASSERTE(ppACL);
    *ppACL = NULL;

    PSID    pCurrentProcessSid = NULL;
    PSID    pTargetProcessSid = NULL;
    PSID    pTargetProcessAppContainerSid = NULL;
    DWORD   cSid = 0;
    DWORD   dwAclSize = 0;

    LOG((LF_CORDB, LL_INFO10000,
         "SecurityUtil::GetACLOfPid on pid : 0x%08x\n",
         pid));


    SidBuffer sidCurrentProcess;
    SidBuffer sidTargetProcess;
    SidBuffer sidTargetProcessAppContainer;

    // Get sid for current process.            
    EX_TRY
    {
        sidCurrentProcess.InitFromProcess(GetCurrentProcessId()); // throw on error.
        pCurrentProcessSid = sidCurrentProcess.GetSid().RawSid();
        cSid++;
    }
    EX_CATCH
    {
    }
    EX_END_CATCH(RethrowTerminalExceptions);

    // Get sid for target process.
    EX_TRY
    {
        sidTargetProcess.InitFromProcess(pid); // throws on error.
        pTargetProcessSid = sidTargetProcess.GetSid().RawSid();
        cSid++;
    }
    EX_CATCH
    {
    }
    EX_END_CATCH(RethrowTerminalExceptions);
    
    //FISHY: what is the scenario where only one of the above calls succeeds?
    if (cSid == 0)
    {
        // failed to get any useful sid. Just return.
        // need a better error.
        //
        hr = E_FAIL;
        goto exit;
    }

    hr = sidTargetProcessAppContainer.InitFromProcessAppContainerSidNoThrow(pid);
    if (FAILED(hr))
    {
        goto exit;
    }
    else if (hr == S_OK)
    {
        pTargetProcessAppContainerSid = sidTargetProcessAppContainer.GetSid().RawSid();
        cSid++;
    }
    else if(hr == S_FALSE) //not an app container, no sid to add
    {
        hr = S_OK; // don't leak S_FALSE
    }

    LOG((LF_CORDB, LL_INFO10000,
         "SecurityUtil::GetACLOfPid number of sid : 0x%08x\n",
         cSid));

    // Now allocate space for ACL. First calculate the space is need to hold ACL
    dwAclSize = sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) * cSid;
    if (pCurrentProcessSid)
    {
        dwAclSize += GetLengthSid(pCurrentProcessSid);
    }
    if (pTargetProcessSid)
    {
        dwAclSize += GetLengthSid(pTargetProcessSid);
    }
    if (pTargetProcessAppContainerSid)
    {
        dwAclSize += GetLengthSid(pTargetProcessAppContainerSid);
    }

    *ppACL = (PACL) new (nothrow) char[dwAclSize];
    if (*ppACL == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto exit;
    }

    // Initialize ACL
    // add each sid to the allowed ace list
    if (!InitializeAcl(*ppACL, dwAclSize, ACL_REVISION))
    {
        hr = HRESULT_FROM_GetLastError();
        goto exit;
    }

    if (pCurrentProcessSid)
    {
        // add the current process's sid into ACL if we have it
        if (!AddAccessAllowedAce(*ppACL, ACL_REVISION, CLR_IPC_GENERIC_RIGHT, pCurrentProcessSid))
        {
            hr = HRESULT_FROM_GetLastError();
            goto exit;
        }
    }

    if (pTargetProcessSid)
    {
        // add the target process's sid into ACL if we have it
        if (!AddAccessAllowedAce(*ppACL, ACL_REVISION, CLR_IPC_GENERIC_RIGHT, pTargetProcessSid))
        {
            hr = HRESULT_FROM_GetLastError();
            goto exit;
        }
    }

    if (pTargetProcessAppContainerSid)
    {
        // add the target process's AppContainer's sid into ACL if we have it
        if (!AddAccessAllowedAce(*ppACL, ACL_REVISION, CLR_IPC_GENERIC_RIGHT, pTargetProcessAppContainerSid))
        {
            hr = HRESULT_FROM_GetLastError();
            goto exit;
        }
    }

    // we better to form a valid ACL to return
    _ASSERTE(IsValidAcl(*ppACL));
exit:
    if (FAILED(hr) && *ppACL)
    {
        delete [] (reinterpret_cast<char*>(ppACL));
    }
    return hr;
}   // SecurityUtil::GetACLOfPid


//*****************************************************************
// static helper function
//
// free the ACL allocated by SecurityUtil::GetACLOfPid
//
// [IN] pACL - ACL to be freed
//
//*****************************************************************
void SecurityUtil::FreeACL(PACL pACL)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;
    if (pACL)
    {
        delete [] (reinterpret_cast<char*>(pACL));
    }
}   // SecurityUtil::FreeACL


//*****************************************************************
// constructor
//
// [IN] pACL - ACL that this instance of SecurityUtil will held on to
//
//*****************************************************************
SecurityUtil::SecurityUtil(PACL pACL)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;
    m_pACL = pACL;
    m_pSacl = NULL;
    m_fInitialized = false;
}

//*****************************************************************
// destructor
//
// free the ACL that this instance of SecurityUtil helds on to
//
//*****************************************************************
SecurityUtil::~SecurityUtil()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;
    FreeACL(m_pACL);
    FreeACL(m_pSacl);
}

//*****************************************************************
// Initialization function
//
// form the SecurityDescriptor that will represent the m_pACL
//
//*****************************************************************
HRESULT SecurityUtil::Init()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    HRESULT    hr = S_OK;

    if (m_pACL)
    {
        if (!InitializeSecurityDescriptor(&m_SD, SECURITY_DESCRIPTOR_REVISION))
        {
            hr = HRESULT_FROM_GetLastError();
            return hr;
        }
        if (!SetSecurityDescriptorDacl(&m_SD, TRUE, m_pACL, FALSE))
        {
            hr = HRESULT_FROM_GetLastError();
            return hr;
        }

        m_SA.nLength = sizeof(SECURITY_ATTRIBUTES);
        m_SA.lpSecurityDescriptor = &m_SD;
        m_SA.bInheritHandle = FALSE;
        m_fInitialized = true;
    }
    return S_OK;
}

// ***************************************************************************
// Initialization functions which will call the normal Init and add a 
// mandatory label entry to the sacl
//
// Expects hProcess to be a valid handle to the process which has the desired
// mandatory label
// ***************************************************************************
HRESULT SecurityUtil::Init(HANDLE hProcess)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    HRESULT hr = Init();
    if (FAILED(hr))
    {
        return hr;
    }
    
    NewArrayHolder<BYTE> pLabel;

    hr = GetMandatoryLabelFromProcess(hProcess, &pLabel);
    if (FAILED(hr))
    { 
        return hr;
    }

    TOKEN_MANDATORY_LABEL * ptml = (TOKEN_MANDATORY_LABEL *) pLabel.GetValue();

    hr = SetSecurityDescriptorMandatoryLabel(ptml->Label.Sid);

    return hr;
}


// ***************************************************************************
// Given a process, this will put the mandatory label into a buffer and point
// ppbLabel at the buffer.
// 
// Caller must free ppbLabel via the array "delete []" operator
// ***************************************************************************
HRESULT SecurityUtil::GetMandatoryLabelFromProcess(HANDLE hProcess, LPBYTE * ppbLabel)
{
    *ppbLabel = NULL;
    
    DWORD dwSize = 0;
    HandleHolder hToken;
    DWORD err = 0;

    if(!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
    {
        return HRESULT_FROM_GetLastError();
    }

    if(!GetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS)TokenIntegrityLevel, NULL, 0, &dwSize))
    {
        err = GetLastError();
    }
    
    // We need to make sure that GetTokenInformation failed in a predictable manner so we know that
    // dwSize has the correct buffer size in it.
    if (err != ERROR_INSUFFICIENT_BUFFER || dwSize == 0)
    {
        return HRESULT_FROM_WIN32(err);
    }

    NewArrayHolder<BYTE> pLabel = new (nothrow) BYTE[dwSize];
    if (pLabel == NULL)
    {
        return E_OUTOFMEMORY;
    }

    if(!GetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS)TokenIntegrityLevel, pLabel, dwSize, &dwSize))
    {
        return HRESULT_FROM_GetLastError();
    }

    // Our caller will be freeing the memory so use Extract
    *ppbLabel = pLabel.Extract();

    return S_OK;
}

//---------------------------------------------------------------------------------------
//
// Returns pointer inside the specified mandatory SID to the DWORD representing the
// integrity level of the process.  This DWORD will be one of the
// SECURITY_MANDATORY_*_RID constants.
//
// Arguments:
//      psidIntegrityLevelLabel - [in] PSID in which to find the integrity level.
//
// Return Value:
//      Pointer to the RID stored in the specified SID.  This RID represents the
//      integrity level of the process
//

// static
DWORD * SecurityUtil::GetIntegrityLevelFromMandatorySID(PSID psidIntegrityLevelLabel)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    return GetSidSubAuthority(psidIntegrityLevelLabel, (*GetSidSubAuthorityCount(psidIntegrityLevelLabel) - 1));
}

// Creates a mandatory label ace and sets it to be the entry in the 
// security descriptor's sacl. This assumes there are no other entries 
// in the sacl
HRESULT SecurityUtil::SetSecurityDescriptorMandatoryLabel(PSID psidIntegrityLevelLabel)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    HRESULT hr;

    DWORD cbSid = GetLengthSid(psidIntegrityLevelLabel);
    DWORD cbAceStart = offsetof(SYSTEM_MANDATORY_LABEL_ACE, SidStart);
    // We are about allocate memory for a ACL and an ACE so we need space for:
    // 1) the ACL: sizeof(ACL)
    // 2) the entry: the sid is of variable size, so the SYSTEM_MANDATORY_LABEL_ACE
    //    structure has only the first DWORD of the sid in its definition, to get the
    //    appropriate size we get size without SidStart and add on the actual size of the sid
    DWORD cbSacl = sizeof(ACL) + cbAceStart + cbSid;

    NewArrayHolder<BYTE> sacl = new (nothrow) BYTE[cbSacl];
    
    m_pSacl = NULL;

    if (sacl == NULL)
    {
        return E_OUTOFMEMORY;
    }
    ZeroMemory(sacl.GetValue(), cbSacl);
    PACL pSacl = reinterpret_cast<ACL *>(sacl.GetValue());
    SYSTEM_MANDATORY_LABEL_ACE * pLabelAce = reinterpret_cast<SYSTEM_MANDATORY_LABEL_ACE *>(sacl.GetValue() + sizeof(ACL));
    PSID psid = reinterpret_cast<SID *>(&pLabelAce->SidStart);

    // Our buffer looks like this now: (not drawn to scale)
    // sacl  pSacl pLabelAce psid
    //  -      -
    //  |      |
    //  |      -       -
    //  |              |       
    //  |              |       -
    //  |              -       |
    //  -                      -

    DWORD dwIntegrityLevel = *(GetIntegrityLevelFromMandatorySID(psidIntegrityLevelLabel));

    if (dwIntegrityLevel >= SECURITY_MANDATORY_MEDIUM_RID)
    {
        // No need to set the integrity level unless it's lower than medium
        return S_OK;
    }

    if(!InitializeAcl(pSacl, cbSacl, ACL_REVISION))
    { 
        return HRESULT_FROM_GetLastError();
    }

    pSacl->AceCount = 1;

    pLabelAce->Header.AceType = SYSTEM_MANDATORY_LABEL_ACE_TYPE;
    pLabelAce->Header.AceSize = WORD(cbAceStart + cbSid);
    pLabelAce->Mask = SYSTEM_MANDATORY_LABEL_NO_WRITE_UP;

    memcpy(psid, psidIntegrityLevelLabel, cbSid);

    if(!SetSecurityDescriptorSacl(m_SA.lpSecurityDescriptor, TRUE, pSacl, FALSE))
    {
        return HRESULT_FROM_GetLastError();
    }

    // No need to delete the sacl buffer, it will be deleted in the
    // destructor of this class
    m_pSacl = (PACL)sacl.Extract();
    return S_OK;
}

//*****************************************************************
// Return SECURITY_ATTRIBUTES that we form in the Init function
//
// No clean up is needed after calling this function. The destructor of the 
// instance will do the right thing. Note that this is designed such that
// we minimize memory allocation, ie the SECURITY_DESCRIPTOR and
// SECURITY_ATTRIBUTES are embedded in the SecurityUtil instance. 
//
// Caller should not modify the returned SECURITY_ATTRIBUTES!!!
//*****************************************************************
HRESULT SecurityUtil::GetSA(SECURITY_ATTRIBUTES **ppSA)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    _ASSERTE(ppSA);

    if (m_fInitialized == false)
    {
        _ASSERTE(!"Bad code path!");
        *ppSA = NULL;
        return E_FAIL;
    }

    *ppSA = &m_SA;
    return S_OK;
}