Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assemblies returned from AppDomain.AssemblyResolve are checked for matching strong name #101029

Closed
reflectronic opened this issue Apr 14, 2024 · 2 comments · Fixed by #101039
Closed

Comments

@reflectronic
Copy link
Contributor

Reproduction

Make a dummy project without strong-name signing:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <AssemblyName>Newtonsoft.Json</AssemblyName>
        <AssemblyVersion>15.0.0.0</AssemblyVersion>
    </PropertyGroup>

</Project>
namespace Newtonsoft.Json.Linq
{
    public class JArray
    {
        public JArray(params object[] values) { }

        public override string ToString() => "Surprise!";
    }
}

Then, the project to demonstrate the error:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <OutputType>Exe</OutputType>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" ExcludeAssets="runtime" />
        <!-- Change path to wherever you decide to put it... -->
        <None Include="../Newtonsoft.Json/bin/Debug/net8.0/Newtonsoft.Json.dll" CopyToOutputDirectory="PreserveNewest" />
    </ItemGroup>

</Project>
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json.Linq;

// #1
// Assembly.LoadFrom("Newtonsoft.Json.dll");

// #2
// AppDomain.CurrentDomain.AssemblyResolve += (_, _) =>
// {
//     return Assembly.LoadFrom("Newtonsoft.Json.dll");
// };

Run();

[MethodImpl(MethodImplOptions.NoInlining)]
void Run()
{
    Console.WriteLine(new JArray(1, 2, 3).ToString());
}

Run the project. It should fail with:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The system cannot find the file specified.'

This is expected.

Uncomment the code labeled '#1'. The code should run with output:

Surprise!

This is also expected, since .NET Core should ignore strong names for the purpose of assembly binding.

Re-comment that code, and now uncomment the code labeled '#2'. The code will fail with:

System.IO.FileLoadException: 'Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. A strongly-named assembly is required. (0x80131044)'

This is not expected—the code should run successfully as it did in the previous example.

Commentary

This code in AppDomain::RaiseAssemblyResolveEvent seems suspicious:

// Check that the public key token matches the one specified in the spec
// MatchPublicKeys throws as appropriate
pSpec->MatchPublicKeys(pAssembly);

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 14, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 14, 2024
@jkotas jkotas added area-AssemblyLoader-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 14, 2024
Copy link
Contributor

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

jkotas added a commit to jkotas/runtime that referenced this issue Apr 15, 2024
… event

This check was somehow missed during earlier cleanups that deleted strongname matching for assembly binding.

Fixes dotnet#101029
jkotas added a commit that referenced this issue Apr 16, 2024
… event (#101039)

This check was somehow missed during earlier cleanups that deleted strongname matching for assembly binding.

Fixes #101029
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 16, 2024
@jkotas
Copy link
Member

jkotas commented Apr 16, 2024

Thank you for reporting this issue!

matouskozak pushed a commit to matouskozak/runtime that referenced this issue Apr 30, 2024
… event (dotnet#101039)

This check was somehow missed during earlier cleanups that deleted strongname matching for assembly binding.

Fixes dotnet#101029
@github-actions github-actions bot locked and limited conversation to collaborators May 16, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this issue May 30, 2024
… event (dotnet#101039)

This check was somehow missed during earlier cleanups that deleted strongname matching for assembly binding.

Fixes dotnet#101029
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants