summaryrefslogtreecommitdiff
path: root/src/vm/appdomainstack.cpp
blob: 0dabefa1becca43221ab84ce7c3307441bbbeb58 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// 


// 


#include "common.h"

#include "appdomainstack.h"
#include "appdomainstack.inl"
#include "security.h"
#include "securitypolicy.h"
#include "appdomain.inl"
#ifdef FEATURE_REMOTING
#include "crossdomaincalls.h"
#else
#include "callhelpers.h"
#endif

#ifdef _DEBUG
void AppDomainStack::CheckOverridesAssertCounts()
{
    LIMITED_METHOD_CONTRACT;
    DWORD   dwAppDomainIndex = 0;
    DWORD dwOverrides = 0;
    DWORD dwAsserts = 0;
    AppDomainStackEntry *pEntry = NULL;
    for(dwAppDomainIndex=0;dwAppDomainIndex<m_numEntries;dwAppDomainIndex++)
    {
        pEntry = __GetEntryPtr(dwAppDomainIndex);
        dwOverrides += pEntry->m_dwOverridesCount;
        dwAsserts += pEntry->m_dwAsserts;
    }
    _ASSERTE(dwOverrides == m_dwOverridesCount);
    _ASSERTE(dwAsserts == m_dwAsserts);    
}
#endif

BOOL AppDomainStackEntry::IsFullyTrustedWithNoStackModifiers(void)
{
    LIMITED_METHOD_CONTRACT;   
    if (m_domainID.m_dwId == INVALID_APPDOMAIN_ID || m_dwOverridesCount != 0 || m_dwAsserts != 0)
        return FALSE;

    AppDomainFromIDHolder pDomain(m_domainID, FALSE);
    if (pDomain.IsUnloaded())
        return FALSE;
    IApplicationSecurityDescriptor *currAppSecDesc = pDomain->GetSecurityDescriptor();
    if (currAppSecDesc == NULL)
        return FALSE;
    return Security::CheckDomainWideSpecialFlag(currAppSecDesc, 1 << SECURITY_FULL_TRUST);
}
BOOL AppDomainStackEntry::IsHomogeneousWithNoStackModifiers(void)
{
    LIMITED_METHOD_CONTRACT;
    if (m_domainID.m_dwId == INVALID_APPDOMAIN_ID || m_dwOverridesCount != 0 || m_dwAsserts != 0)
        return FALSE;

    AppDomainFromIDHolder pDomain(m_domainID, FALSE);
    if (pDomain.IsUnloaded())
        return FALSE;
    IApplicationSecurityDescriptor *currAppSecDesc = pDomain->GetSecurityDescriptor();
    if (currAppSecDesc == NULL)
        return FALSE;
    return (currAppSecDesc->IsHomogeneous() && !currAppSecDesc->ContainsAnyRefusedPermissions());
}

BOOL AppDomainStackEntry::HasFlagsOrFullyTrustedWithNoStackModifiers(DWORD flags)
{
    LIMITED_METHOD_CONTRACT;
    if (m_domainID.m_dwId == INVALID_APPDOMAIN_ID || m_dwOverridesCount != 0 || m_dwAsserts != 0)
        return FALSE;

    AppDomainFromIDHolder pDomain(m_domainID, FALSE);
    if (pDomain.IsUnloaded())
        return FALSE;
    IApplicationSecurityDescriptor *currAppSecDesc = pDomain->GetSecurityDescriptor();
    if (currAppSecDesc == NULL)
        return FALSE;
    
    // either the desired flag (often 0) or fully trusted will do
    flags |= (1<<SECURITY_FULL_TRUST);
    return Security::CheckDomainWideSpecialFlag(currAppSecDesc, flags);
}


void AppDomainStackEntry::UpdateHomogeneousPLS(OBJECTREF* homogeneousPLS)
{
    
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;
    
    AppDomainFromIDHolder domain(m_domainID, TRUE);
    if (domain.IsUnloaded())
        return;

    IApplicationSecurityDescriptor *thisAppSecDesc = domain->GetSecurityDescriptor();

    if (thisAppSecDesc->IsHomogeneous())
    {
        // update the intersection with the current grant set
        
        NewArrayHolder<BYTE> pbtmpSerializedObject(NULL);
        
        struct gc
        {
            OBJECTREF refGrantSet;
        } gc;
        ZeroMemory( &gc, sizeof( gc ) );
        AppDomain* pCurrentDomain;
        pCurrentDomain = GetAppDomain();
            
        GCPROTECT_BEGIN( gc );
#ifdef FEATURE_REMOTING // should not be possible without remoting
        DWORD cbtmpSerializedObject = 0;
        if (pCurrentDomain->GetId() != m_domainID)
        {
            // Unlikely scenario where we have another homogeneous AD on the callstack that's different from
            // the current one. If there's another AD on the callstack, it's likely to be FT.
            ENTER_DOMAIN_ID(m_domainID)
            {
                // Release the holder to allow GCs.  This is safe because we've entered the AD, so it won't go away.
                domain.Release();

                gc.refGrantSet = thisAppSecDesc->GetGrantedPermissionSet(NULL); 
                AppDomainHelper::MarshalObject(GetAppDomain(), &gc.refGrantSet, &pbtmpSerializedObject, &cbtmpSerializedObject);
                if (pbtmpSerializedObject == NULL)
            	{
                    // this is an error: possibly an OOM prevented the blob from getting created.
                    // We could return null and let the managed code use a fully restricted object or throw here.
                    // Let's throw here...
                    COMPlusThrow(kSecurityException);
                }
                gc.refGrantSet = NULL;

            }
            END_DOMAIN_TRANSITION
            AppDomainHelper::UnmarshalObject(pCurrentDomain,pbtmpSerializedObject, cbtmpSerializedObject, &gc.refGrantSet);
        }
        else
#else
        _ASSERTE(pCurrentDomain->GetId() == m_domainID);
#endif //!FEATURE_CORECLR 
        {
            // Release the holder to allow GCs.  This is safe because we're running in this AD, so it won't go away.
            domain.Release();
            gc.refGrantSet = thisAppSecDesc->GetGrantedPermissionSet(NULL); 
        }

        // At this point gc.refGrantSet has the grantSet of pDomain (thisAppSecDesc) in the current domain.
        // We don't care about refused perms since we established there were 
        // none earlier for this call stack.
        // Let's intersect with what we've already got.

        PREPARE_NONVIRTUAL_CALLSITE(METHOD__PERMISSION_LIST_SET__UPDATE);
        DECLARE_ARGHOLDER_ARRAY(args, 2);
        args[ARGNUM_0]  = OBJECTREF_TO_ARGHOLDER(*homogeneousPLS);    // arg 0
        args[ARGNUM_1]  = OBJECTREF_TO_ARGHOLDER(gc.refGrantSet);       // arg 1 
        CALL_MANAGED_METHOD_NORET(args);

        GCPROTECT_END();
    }
}


BOOL AppDomainStack::AllDomainsHomogeneousWithNoStackModifiers()
{
    WRAPPER_NO_CONTRACT;

    // Used primarily by CompressedStack code to decide if a CS has to be constructed 

    DWORD   dwAppDomainIndex = 0;


    InitDomainIteration(&dwAppDomainIndex);
    while (dwAppDomainIndex != 0)
    {
        AppDomainStackEntry* pEntry = GetNextDomainEntryOnStack(&dwAppDomainIndex);
        _ASSERTE(pEntry != NULL);
        
        if (!pEntry->IsHomogeneousWithNoStackModifiers() && !pEntry->IsFullyTrustedWithNoStackModifiers())
            return FALSE;
    }

    return TRUE;
}