From 7a928d80d2c6eb227b70144a0a8018f3ec67066b Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Fri, 22 Mar 2019 14:53:31 +0100 Subject: [PATCH] Disable loading IJW assemblies into collectible ALC --- src/dlls/mscorrc/mscorrc.rc | 1 + src/dlls/mscorrc/resource.h | 1 + src/vm/assemblynative.cpp | 16 +++++++++++++++- .../IJW/ManagedCallingNative/ManagedCallingNative.csproj | 2 ++ .../IJW/NativeCallingManaged/NativeCallingManaged.csproj | 2 ++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/dlls/mscorrc/mscorrc.rc b/src/dlls/mscorrc/mscorrc.rc index 8e272ad..4853f00 100644 --- a/src/dlls/mscorrc/mscorrc.rc +++ b/src/dlls/mscorrc/mscorrc.rc @@ -1079,6 +1079,7 @@ BEGIN BFA_UNEXPECTED_TOKEN_AFTER_GENINST "A valid typedef or typeref token is expected to follow a ELEMENT_TYPE_GENERICINST." BFA_TYPEDBYREFCANNOTHAVEBYREF "An ELEMENT_TYPE_TYPEDBYREF cannot have a ELEMENT_TYPE_BYREF modifier." BFA_REFERENCE_ASSEMBLY "Cannot load a reference assembly for execution." + BFA_IJW_IN_COLLECTIBLE_ALC "Cannot load a mixed assembly into a collectible AssemblyLoadContext." #ifdef FEATURE_COMINTEROP BFA_WINRT_INVALID_NAMESPACE_FOR_TYPE "Windows Runtime type '%1' has invalid namespace that does not begin with the file name in assembly '%2'." diff --git a/src/dlls/mscorrc/resource.h b/src/dlls/mscorrc/resource.h index a7d9487..609a31d 100644 --- a/src/dlls/mscorrc/resource.h +++ b/src/dlls/mscorrc/resource.h @@ -530,6 +530,7 @@ #define BFA_BAD_CLASS_INT_CA_FORMAT 0x2048 #define BFA_BAD_COMPLUS_SIG 0x2049 #define BFA_BAD_ELEM_IN_SIZEOF 0x204b +#define BFA_IJW_IN_COLLECTIBLE_ALC 0x204c #define IDS_CLASSLOAD_INTERFACE_NO_ACCESS 0x204f diff --git a/src/vm/assemblynative.cpp b/src/vm/assemblynative.cpp index ed2ce66..6cbb8af 100644 --- a/src/vm/assemblynative.cpp +++ b/src/vm/assemblynative.cpp @@ -248,6 +248,13 @@ void QCALLTYPE AssemblyNative::LoadFromPath(INT_PTR ptrNativeAssemblyLoadContext // Need to verify that this is a valid CLR assembly. if (!pILImage->CheckILFormat()) ThrowHR(COR_E_BADIMAGEFORMAT, BFA_BAD_IL); + + LoaderAllocator* pLoaderAllocator = NULL; + if (SUCCEEDED(pBinderContext->GetLoaderAllocator((LPVOID*)&pLoaderAllocator)) && pLoaderAllocator->IsCollectible() && !pILImage->IsILOnly()) + { + // Loading IJW assemblies into a collectible AssemblyLoadContext is not allowed + ThrowHR(COR_E_BADIMAGEFORMAT, BFA_IJW_IN_COLLECTIBLE_ALC); + } } // Form the PEImage for the NI assembly, if specified @@ -327,7 +334,14 @@ void QCALLTYPE AssemblyNative::LoadFromStream(INT_PTR ptrNativeAssemblyLoadConte // Get the binder context in which the assembly will be loaded ICLRPrivBinder *pBinderContext = reinterpret_cast(ptrNativeAssemblyLoadContext); - + + LoaderAllocator* pLoaderAllocator = NULL; + if (SUCCEEDED(pBinderContext->GetLoaderAllocator((LPVOID*)&pLoaderAllocator)) && pLoaderAllocator->IsCollectible() && !pILImage->IsILOnly()) + { + // Loading IJW assemblies into a collectible AssemblyLoadContext is not allowed + ThrowHR(COR_E_BADIMAGEFORMAT, BFA_IJW_IN_COLLECTIBLE_ALC); + } + // Pass the stream based assembly as IL and NI in an attempt to bind and load it Assembly* pLoadedAssembly = AssemblyNative::LoadFromPEImage(pBinderContext, pILImage, NULL); { diff --git a/tests/src/Interop/IJW/ManagedCallingNative/ManagedCallingNative.csproj b/tests/src/Interop/IJW/ManagedCallingNative/ManagedCallingNative.csproj index c8c1cca..c0dcb87 100644 --- a/tests/src/Interop/IJW/ManagedCallingNative/ManagedCallingNative.csproj +++ b/tests/src/Interop/IJW/ManagedCallingNative/ManagedCallingNative.csproj @@ -19,6 +19,8 @@ true + + true diff --git a/tests/src/Interop/IJW/NativeCallingManaged/NativeCallingManaged.csproj b/tests/src/Interop/IJW/NativeCallingManaged/NativeCallingManaged.csproj index d40837d..8b01960 100644 --- a/tests/src/Interop/IJW/NativeCallingManaged/NativeCallingManaged.csproj +++ b/tests/src/Interop/IJW/NativeCallingManaged/NativeCallingManaged.csproj @@ -19,6 +19,8 @@ true + + true -- 2.7.4