From 9bd1c5364e2b9f05da27d228c23feb1cb3a1a424 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sun, 14 Apr 2024 22:48:02 -0700 Subject: [PATCH 1/2] Ignore strongname mismatch for assemblies returned by AssemblyResolve event This check was somehow missed during earlier cleanups that deleted strongname matching for assembly binding. Fixes #101029 --- src/coreclr/dlls/mscorrc/mscorrc.rc | 1 - src/coreclr/inc/corerror.xml | 6 --- src/coreclr/pal/prebuilt/corerror/mscorurt.rc | 1 - src/coreclr/pal/prebuilt/inc/corerror.h | 1 - src/coreclr/utilcode/ex.cpp | 1 - src/coreclr/vm/appdomain.cpp | 7 --- src/coreclr/vm/assemblyspec.cpp | 49 ------------------- src/coreclr/vm/assemblyspec.hpp | 2 - src/coreclr/vm/rexcep.h | 1 - .../System/AppDomainTests.cs | 19 +++++++ 10 files changed, 19 insertions(+), 69 deletions(-) diff --git a/src/coreclr/dlls/mscorrc/mscorrc.rc b/src/coreclr/dlls/mscorrc/mscorrc.rc index b95946881f10e..5b4faf05edb44 100644 --- a/src/coreclr/dlls/mscorrc/mscorrc.rc +++ b/src/coreclr/dlls/mscorrc/mscorrc.rc @@ -113,7 +113,6 @@ BEGIN COR_E_FILELOAD "Unable to load file '%1'." COR_E_ASSEMBLYEXPECTED "The module '%1' was expected to contain an assembly manifest." FUSION_E_REF_DEF_MISMATCH "The located assembly's manifest definition with name '%1' does not match the assembly reference." - FUSION_E_PRIVATE_ASM_DISALLOWED "Assembly '%1' is required to be strongly named." FUSION_E_INVALID_NAME "The given assembly name, '%1', was invalid." FUSION_E_APP_DOMAIN_LOCKED "The requested assembly version conflicts with what is already bound in the app domain or specified in the manifest." IDS_EE_HASH_VAL_FAILED "Hash validation failed for file or assembly '%1'." diff --git a/src/coreclr/inc/corerror.xml b/src/coreclr/inc/corerror.xml index 1d4129305fa68..68c9a1aa5dfb3 100644 --- a/src/coreclr/inc/corerror.xml +++ b/src/coreclr/inc/corerror.xml @@ -172,12 +172,6 @@ The located assembly's manifest definition does not match the assembly reference. - - FUSION_E_PRIVATE_ASM_DISALLOWED - "A strongly-named assembly is required." - A strongly-named assembly is required. - - FUSION_E_INVALID_NAME "The given assembly name was invalid." diff --git a/src/coreclr/pal/prebuilt/corerror/mscorurt.rc b/src/coreclr/pal/prebuilt/corerror/mscorurt.rc index 67c5f9d48fec1..df0e680abdd4f 100644 --- a/src/coreclr/pal/prebuilt/corerror/mscorurt.rc +++ b/src/coreclr/pal/prebuilt/corerror/mscorurt.rc @@ -14,7 +14,6 @@ BEGIN MSG_FOR_URT_HR(HOST_E_INVALIDOPERATION) "Invalid operation." MSG_FOR_URT_HR(HOST_E_CLRNOTAVAILABLE) "CLR has been disabled due to unrecoverable error." MSG_FOR_URT_HR(FUSION_E_REF_DEF_MISMATCH) "The located assembly's manifest definition does not match the assembly reference." - MSG_FOR_URT_HR(FUSION_E_PRIVATE_ASM_DISALLOWED) "A strongly-named assembly is required." MSG_FOR_URT_HR(FUSION_E_INVALID_NAME) "The given assembly name was invalid." MSG_FOR_URT_HR(FUSION_E_APP_DOMAIN_LOCKED) "The requested assembly version conflicts with what is already bound in the app domain or specified in the manifest." MSG_FOR_URT_HR(COR_E_LOADING_REFERENCE_ASSEMBLY) "Reference assemblies cannot be loaded for execution." diff --git a/src/coreclr/pal/prebuilt/inc/corerror.h b/src/coreclr/pal/prebuilt/inc/corerror.h index 12d3490aaf09d..0b475679050fd 100644 --- a/src/coreclr/pal/prebuilt/inc/corerror.h +++ b/src/coreclr/pal/prebuilt/inc/corerror.h @@ -41,7 +41,6 @@ #define HOST_E_INVALIDOPERATION EMAKEHR(0x1022) #define HOST_E_CLRNOTAVAILABLE EMAKEHR(0x1023) #define FUSION_E_REF_DEF_MISMATCH EMAKEHR(0x1040) -#define FUSION_E_PRIVATE_ASM_DISALLOWED EMAKEHR(0x1044) #define FUSION_E_INVALID_NAME EMAKEHR(0x1047) #define FUSION_E_APP_DOMAIN_LOCKED EMAKEHR(0x1053) #define COR_E_LOADING_REFERENCE_ASSEMBLY EMAKEHR(0x1058) diff --git a/src/coreclr/utilcode/ex.cpp b/src/coreclr/utilcode/ex.cpp index 7b65514b83d0b..adee9ba55618c 100644 --- a/src/coreclr/utilcode/ex.cpp +++ b/src/coreclr/utilcode/ex.cpp @@ -574,7 +574,6 @@ LPCSTR Exception::GetHRSymbolicName(HRESULT hr) CASE_HRESULT(COR_E_CANNOTUNLOADAPPDOMAIN) CASE_HRESULT(MSEE_E_ASSEMBLYLOADINPROGRESS) CASE_HRESULT(FUSION_E_REF_DEF_MISMATCH) - CASE_HRESULT(FUSION_E_PRIVATE_ASM_DISALLOWED) CASE_HRESULT(FUSION_E_INVALID_NAME) CASE_HRESULT(CLDB_E_FILE_BADREAD) CASE_HRESULT(CLDB_E_FILE_BADWRITE) diff --git a/src/coreclr/vm/appdomain.cpp b/src/coreclr/vm/appdomain.cpp index 07f24361dccca..59a4783648e2a 100644 --- a/src/coreclr/vm/appdomain.cpp +++ b/src/coreclr/vm/appdomain.cpp @@ -4528,13 +4528,6 @@ AppDomain::RaiseAssemblyResolveEvent( } GCPROTECT_END(); - if (pAssembly != NULL) - { - // Check that the public key token matches the one specified in the spec - // MatchPublicKeys throws as appropriate - pSpec->MatchPublicKeys(pAssembly); - } - RETURN pAssembly; } // AppDomain::RaiseAssemblyResolveEvent diff --git a/src/coreclr/vm/assemblyspec.cpp b/src/coreclr/vm/assemblyspec.cpp index 6144f000a8e05..a4872b732097b 100644 --- a/src/coreclr/vm/assemblyspec.cpp +++ b/src/coreclr/vm/assemblyspec.cpp @@ -295,55 +295,6 @@ void AssemblySpec::InitializeAssemblyNameRef(_In_ BINDER_SPACE::AssemblyName* as spec.AssemblyNameInit(assemblyNameRef); } - -// Check if the supplied assembly's public key matches up with the one in the Spec, if any -// Throws an appropriate exception in case of a mismatch -void AssemblySpec::MatchPublicKeys(Assembly *pAssembly) -{ - CONTRACTL - { - INSTANCE_CHECK; - THROWS; - GC_TRIGGERS; - MODE_ANY; - } - CONTRACTL_END; - - // Check that the public keys are the same as in the AR. - if (!IsStrongNamed()) - return; - - const void *pbPublicKey; - DWORD cbPublicKey; - pbPublicKey = pAssembly->GetPublicKey(&cbPublicKey); - if (cbPublicKey == 0) - ThrowHR(FUSION_E_PRIVATE_ASM_DISALLOWED); - - if (IsAfPublicKey(m_dwFlags)) - { - if ((m_cbPublicKeyOrToken != cbPublicKey) || - memcmp(m_pbPublicKeyOrToken, pbPublicKey, m_cbPublicKeyOrToken)) - { - ThrowHR(FUSION_E_REF_DEF_MISMATCH); - } - } - else - { - // Ref has a token - StrongNameToken strongNameToken; - - IfFailThrow(StrongNameTokenFromPublicKey((BYTE*)pbPublicKey, - cbPublicKey, - &strongNameToken)); - - if ((m_cbPublicKeyOrToken != StrongNameToken::SIZEOF_TOKEN) || - memcmp(m_pbPublicKeyOrToken, &strongNameToken, StrongNameToken::SIZEOF_TOKEN)) - { - ThrowHR(FUSION_E_REF_DEF_MISMATCH); - } - } -} - Assembly *AssemblySpec::LoadAssembly(FileLoadLevel targetLevel, BOOL fThrowOnFileNotFound) { CONTRACTL diff --git a/src/coreclr/vm/assemblyspec.hpp b/src/coreclr/vm/assemblyspec.hpp index a39b712359711..23a7c96e64756 100644 --- a/src/coreclr/vm/assemblyspec.hpp +++ b/src/coreclr/vm/assemblyspec.hpp @@ -193,8 +193,6 @@ class AssemblySpec : public BaseAssemblySpec static void InitializeAssemblyNameRef(_In_ BINDER_SPACE::AssemblyName* assemblyName, _Out_ ASSEMBLYNAMEREF* assemblyNameRef); public: - void MatchPublicKeys(Assembly *pAssembly); - AppDomain *GetAppDomain() { LIMITED_METHOD_CONTRACT; diff --git a/src/coreclr/vm/rexcep.h b/src/coreclr/vm/rexcep.h index 3f2c1cee117ae..648a08865d73b 100644 --- a/src/coreclr/vm/rexcep.h +++ b/src/coreclr/vm/rexcep.h @@ -144,7 +144,6 @@ DEFINE_EXCEPTION(g_SystemNS, FieldAccessException, false, C DEFINE_EXCEPTION(g_IONS, FileLoadException, true, COR_E_FILELOAD, FUSION_E_INVALID_NAME, - FUSION_E_PRIVATE_ASM_DISALLOWED, FUSION_E_REF_DEF_MISMATCH, HRESULT_FROM_WIN32(ERROR_TOO_MANY_OPEN_FILES), HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), HRESULT_FROM_WIN32(ERROR_LOCK_VIOLATION), diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/AppDomainTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/AppDomainTests.cs index 1ef1c6557a749..cb89e2343a4c3 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/AppDomainTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/AppDomainTests.cs @@ -7,6 +7,7 @@ using System.IO; using System.Net.Http; using System.Reflection; +using System.Reflection.Emit; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; @@ -654,6 +655,24 @@ public void AssemblyResolve_IsNotCalledForCoreLibResources() }).Dispose(); } + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + public void AssemblyResolve_IgnoresStrongNameMismatches() + { + RemoteExecutor.Invoke(() => + { + AppDomain.CurrentDomain.AssemblyResolve += + (sender, e) => + { + if (!e.Name.StartsWith("MyAssembly")) + return null; + + return AssemblyBuilder.DefineDynamicAssembly( + new AssemblyName("MyAssembly"), AssemblyBuilderAccess.Run); + }; + Assembly.Load("MyAssembly, PublicKeyToken=1234567890ABCDEF"); + }).Dispose(); + } + class CorrectlyPropagatesException : Exception { public CorrectlyPropagatesException(string message) : base(message) From f138b5b95852895e0edd7ea6adcc875d2e07151b Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 15 Apr 2024 13:12:21 -0700 Subject: [PATCH 2/2] Fix tests --- src/libraries/Common/src/System/HResults.cs | 1 - .../src/System/Runtime/InteropServices/Marshal.cs | 1 - .../tests/System.Runtime.Tests/System/IO/Exceptions.HResults.cs | 1 - .../System/IO/FileLoadException.InteropTests.cs | 1 - 4 files changed, 4 deletions(-) diff --git a/src/libraries/Common/src/System/HResults.cs b/src/libraries/Common/src/System/HResults.cs index f7ec9f94a628a..bbc434b87289c 100644 --- a/src/libraries/Common/src/System/HResults.cs +++ b/src/libraries/Common/src/System/HResults.cs @@ -127,7 +127,6 @@ internal static partial class HResults internal const int CTL_E_PATHNOTFOUND = unchecked((int)0x800A004C); internal const int CTL_E_FILENOTFOUND = unchecked((int)0x800A0035); internal const int FUSION_E_INVALID_NAME = unchecked((int)0x80131047); - internal const int FUSION_E_PRIVATE_ASM_DISALLOWED = unchecked((int)0x80131044); internal const int FUSION_E_REF_DEF_MISMATCH = unchecked((int)0x80131040); internal const int ERROR_TOO_MANY_OPEN_FILES = unchecked((int)0x80070004); internal const int ERROR_SHARING_VIOLATION = unchecked((int)0x80070020); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 74ef8257749aa..25bc1f195fa44 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -818,7 +818,6 @@ public static IntPtr GetHINSTANCE(Module m) }; } case HResults.FUSION_E_INVALID_NAME: - case HResults.FUSION_E_PRIVATE_ASM_DISALLOWED: case HResults.FUSION_E_REF_DEF_MISMATCH: case HResults.ERROR_TOO_MANY_OPEN_FILES: case HResults.ERROR_SHARING_VIOLATION: diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/Exceptions.HResults.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/Exceptions.HResults.cs index 777ffaebe963d..0bf947e802e79 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/Exceptions.HResults.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/Exceptions.HResults.cs @@ -18,7 +18,6 @@ public static class HResults public const int COR_E_FILELOAD = unchecked((int)0x80131621); public const int FUSION_E_INVALID_NAME = unchecked((int)0x80131047); - public const int FUSION_E_PRIVATE_ASM_DISALLOWED = unchecked((int)0x80131044); public const int FUSION_E_REF_DEF_MISMATCH = unchecked((int)0x80131040); public const int ERROR_TOO_MANY_OPEN_FILES = unchecked((int)0x80070004); public const int ERROR_SHARING_VIOLATION = unchecked((int)0x80070020); diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/FileLoadException.InteropTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/FileLoadException.InteropTests.cs index 9d449775aa692..53b2f704547f0 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/FileLoadException.InteropTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/FileLoadException.InteropTests.cs @@ -13,7 +13,6 @@ public static class FileLoadExceptionInteropTests [Theory] [InlineData(HResults.COR_E_FILELOAD)] [InlineData(HResults.FUSION_E_INVALID_NAME)] - [InlineData(HResults.FUSION_E_PRIVATE_ASM_DISALLOWED)] [InlineData(HResults.FUSION_E_REF_DEF_MISMATCH)] [InlineData(HResults.ERROR_TOO_MANY_OPEN_FILES)] [InlineData(HResults.ERROR_SHARING_VIOLATION)]