Fix a race condition between certain Has properties and their collection property #843
Loading…
Reference in a new issue
No description provided.
Delete branch "master-fix-race-between-has-property"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
We found a rare race condition between
MethodDefinition.HasOverridesandMethodDefinition.Overrides.What can happen is
Thread 1 get's past the null check in
MethodDefinition.HasOverridesand then is suspended.Thread 2, calls
MethodDefinition.Overridesand executes at least as far as themetadata.RemoveOverrideMapping (method)call inAssemblyReader.ReadOverridesThread 1 resumes on
return HasImage && Module.Read (this, (method, reader) => reader.HasOverrides (method));It now proceeds to AssemblyReader.HasOverrides. No overrides are found and false is returned due to the overrides for that method having been removed fromMetadataSystemTo recap, the two notable behaviors are triggering this are
a) The following check in
MethodDefinition.HasOverrideshappens outside of the module lock.b) The call to
metadata.RemoveOverrideMappinginAssemblyReader.ReadOverridesmeans thatAssemblyReader.ReadOverridesandAssemblyReader.HasOverridescannot be called again after the first call toAssemblyReader.ReadOverridesI did not attempt to reproduce this vulnerability for every pair of properties that follows this pattern. However, I think it's safe to assume any pair of properties that follows this same pattern are vulnerable.
Using
ReadingMode.Deferredalso appears to be a required prerequisite to encounter this problem.We had two thoughts on how to fix this
Repeat the collection null check after obtaining the module lock in
Module.ReadduringMethodDefinition.HasOverridesRemove the behavior of
AssemblyReaderremoving data from theMetadataSystem.I decided to go with Fix 2 because it was easy to find all of problematic property pairings by searching
MetadataSystem.csforRemove. I also feel that this behavior of modifying the metadata system is asking for problems and probably not worth the freed memory is provides.If you'd prefer Fix 1 instead. Or both Fix 1 & Fix 2, or some other fix, let me know and I can change around the PR.
@jbevain What are your thoughts on this PR?
@jbevain Would you be willing to take this PR?
@mrvoorhe I would. Thank you!