summaryrefslogtreecommitdiff
path: root/src/vm/securitydeclarativecache.cpp
blob: 202c0164594509da3efb5790bcdf93e900568295 (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
// 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 "common.h"
#include "appdomain.inl"
#include "security.h"
#include "field.h"
#include "comcallablewrapper.h"
#include "typeparse.h"


//
//----------------------------------------------------
//
//Brief design overview:
//
//Essentially we moved away from the old scheme of a per-process hash table for blob->index mapping, 
//and a growable per appdomain array containing the managed objects. The new scheme has a per 
//appdomain hash that does memory allocs from the appdomain heap. The hash table maps the metadata 
//blob to a data structure called PsetCacheEntry. PsetCacheEntry has the metadata blob and a handle 
//to the managed pset object. It is the central place where caching/creation of the managed pset 
//objects happen. Essentially whenever we see a new decl security blob, we insert it into the 
//appdomain hash (if it's not already there). The object is lazily created as needed (we let 
//threads race for object creation). 
//
//----------------------------------------------------
//

BOOL PsetCacheKey::IsEquiv(PsetCacheKey *pOther)
{
    WRAPPER_NO_CONTRACT;
    if (m_cbPset != pOther->m_cbPset || !m_pbPset || !pOther->m_pbPset)
        return FALSE;
    return memcmp(m_pbPset, pOther->m_pbPset, m_cbPset) == 0;
}

DWORD PsetCacheKey::Hash()
{
    LIMITED_METHOD_CONTRACT;
    DWORD dwHash = 0;
    for (DWORD i = 0; i < (m_cbPset / sizeof(DWORD)); i++)
        dwHash ^= GET_UNALIGNED_VAL32(&((DWORD*)m_pbPset)[i]);
    return dwHash;
}

void PsetCacheEntry::Init (PsetCacheKey *pKey, AppDomain *pDomain)
{
    CONTRACTL
    {
        GC_NOTRIGGER;
        THROWS;  // From CreateHandle()
        MODE_COOPERATIVE;
    } 
    CONTRACTL_END;
    
    m_pKey = pKey;
    m_eCanUnrestrictedOverride = CUO_DontKnow;
    m_fEmptyPermissionSet = false;
#ifndef CROSSGEN_COMPILE
    m_handle = pDomain->CreateHandle(NULL);
#endif // CROSSGEN_COMPILE
}

#ifndef CROSSGEN_COMPILE
OBJECTREF PsetCacheEntry::CreateManagedPsetObject(DWORD dwAction, bool createEmptySet /* = false */)
{
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    return NULL;
}
#endif // CROSSGEN_COMPILE

bool PsetCacheEntry::ContainsBuiltinCASPermsOnly (DWORD dwAction)
{
 
    if (m_eCanUnrestrictedOverride == CUO_Yes) {
        return true;
    }
    
    if (m_eCanUnrestrictedOverride == CUO_No) {
        return false;
    }
    
    bool bRet = ContainsBuiltinCASPermsOnlyInternal(dwAction);
    
    //
    // Cache the results.
    //

    if(bRet) {
        m_eCanUnrestrictedOverride = CUO_Yes;
    } else {
        m_eCanUnrestrictedOverride = CUO_No;
    }
    
    return bRet;
}

bool PsetCacheEntry::ContainsBuiltinCASPermsOnlyInternal(DWORD dwAction)
{
    //
    // Deserialize the CORSEC_ATTRSET
    //

    CORSEC_ATTRSET attrSet;
    HRESULT hr = BlobToAttributeSet(m_pKey->m_pbPset, m_pKey->m_cbPset, &attrSet, dwAction);
    
    if(FAILED(hr)) {
        COMPlusThrowHR(hr);
    }

    if (hr == S_FALSE) {
        //
        // BlobToAttributeSet didn't work as expected - bail out early
        //
        return FALSE; 
    }

    // Check the attributes
    return SecurityAttributes::ContainsBuiltinCASPermsOnly(&attrSet);
}

void SecurityDeclarativeCache::Init(LoaderHeap *pHeap) 
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    } CONTRACTL_END;

	_ASSERTE (pHeap);

    m_pHeap = pHeap;
	
    m_pCachedPsetsHash = new EEPsetHashTable;

    m_prCachedPsetsLock = new SimpleRWLock (COOPERATIVE_OR_PREEMPTIVE, 
                                            LOCK_TYPE_DEFAULT);

    if (!m_pCachedPsetsHash->Init(19, &g_lockTrustMeIAmThreadSafe, m_pHeap)) {
        ThrowOutOfMemory();
    }
}

PsetCacheEntry* SecurityDeclarativeCache::CreateAndCachePset(
    IN PBYTE pbAttrBlob,
    IN DWORD cbAttrBlob
    )
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    } CONTRACTL_END;

    PsetCacheEntry *pPCE;
    LoaderHeap *pHeap;
    SimpleWriteLockHolder writeLockHolder(m_prCachedPsetsLock);

    //
    // Check for Duplicates.
    //

    pPCE = GetCachedPsetWithoutLocks (pbAttrBlob, cbAttrBlob);
    if (pPCE) {
        return pPCE;
    }

    AppDomain *pDomain;
    PsetCacheKey *pKey;
    HashDatum datum;

    //
    // Buffer permission set blob (it might go away if the metadata scope it
    // came from is closed).
    //

    pDomain = GetAppDomain ();
    pHeap = pDomain->GetLowFrequencyHeap ();

    pKey = (PsetCacheKey*) ((void*) pHeap->AllocMem ((S_SIZE_T)sizeof(PsetCacheKey)));

    pKey->Init (pbAttrBlob, cbAttrBlob, TRUE, pHeap);


    
    pPCE = (PsetCacheEntry*) 
        ((void*) pHeap->AllocMem ((S_SIZE_T)sizeof(PsetCacheEntry)));

    pPCE->Init (pKey, pDomain);

    datum = reinterpret_cast<HashDatum>(pPCE);
    m_pCachedPsetsHash->InsertValue (pKey, datum);

    return pPCE;
}

PsetCacheEntry* SecurityDeclarativeCache::GetCachedPset(IN PBYTE pbAttrBlob,
                                                        IN DWORD cbAttrBlob
    )
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    } CONTRACTL_END;

    PsetCacheEntry *pPCE;
    SimpleReadLockHolder readLockHolder(m_prCachedPsetsLock);

    pPCE = GetCachedPsetWithoutLocks(pbAttrBlob, cbAttrBlob);
    return pPCE;
}

PsetCacheEntry* SecurityDeclarativeCache::GetCachedPsetWithoutLocks(
    IN PBYTE pbAttrBlob,
    IN DWORD cbAttrBlob
    )
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    } CONTRACTL_END;

    PsetCacheKey sKey;
    PsetCacheEntry *pPCE;
    BOOL found;
    HashDatum datum;

    sKey.Init (pbAttrBlob, cbAttrBlob, FALSE, NULL);

    found = m_pCachedPsetsHash->GetValue(&sKey, &datum);

    if (found) {
        pPCE = reinterpret_cast<PsetCacheEntry*>(datum);
        return pPCE;
    } else {
        return NULL;
    }
}

SecurityDeclarativeCache::~SecurityDeclarativeCache() 
{
    WRAPPER_NO_CONTRACT;

    // Destroy the hash table even if entries are allocated from 
    // appdomain heap: the hash table may have used non heap memory for internal data structures
    if (m_pCachedPsetsHash)
    {
        delete m_pCachedPsetsHash;
    }

    if (m_prCachedPsetsLock) 
    {
        delete m_prCachedPsetsLock;
    }
}