Bound every .resources length against the remaining stream before allocating - #4047
Merged
Merged
Conversation
…ocating A .resources file's resource count, type count, name lengths, binary resource lengths and serialized-object lengths all come from the file and were only checked for being non-negative before sizing an allocation. A crafted file can therefore request multi-gigabyte arrays from a few-hundred-byte payload (CWE-789), turning a click on a resource node into an out-of-memory condition. The serialization-format kind was additionally an assert-only check that vanishes in Release builds. Each element of these counts occupies at least one byte in the stream, so a value needing more bytes than remain after the current position cannot be honest. Reject it with the same BadImageFormatException the callers already handle, and promote the format-kind assert into a real check. Assisted-by: Claude:claude-fable-5:Claude Code
siegfriedpammer
requested changes
Aug 25, 2026
| /// position cannot describe real data; rejecting it up front keeps a crafted header from | ||
| /// forcing a multi-gigabyte allocation. | ||
| /// </summary> | ||
| void CheckLength(int count, int bytesPerElement) |
Member
There was a problem hiding this comment.
Call it CheckLengthOrThrow?
Reviewed with Stampeded!
Assisted-by: Claude:claude-fable-5:Claude Code
siegfriedpammer
approved these changes
Aug 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validates every count and length that a
.resourcesfile supplies against the bytes that actually remain in the stream before that value is used to size an allocation. A crafted resource can no longer turn a few hundred bytes into a multi-gigabyte allocation request, and the serialization-format kind check is now a real validation instead of aDebug.Assert.The vulnerability (CWE-789, allocation with excessive size from untrusted input)
ICSharpCode.Decompiler/Util/ResourcesFile.csis a hardened port of the .NETResourceReader. The residual exposure is allocation-size denial of service: several values are read from the file, checked only for being non-negative, and then immediately drive an allocation.numTypes(int32)new string[numTypes]- up to 16 GB of referencesnumResources(int32)new int[numResources]- up to 2 GB (after the existingchecked(4 * numResources)seek caps it atint.MaxValue / 4)GetResourceNamebyteLen(7-bit int)new byte[byteLen]- up to 2 GBLoadObjectV2ByteArray/Streamlen(int32)ReadBytes(len)- up to 2 GBGetBytesForSerializedObjectlen(7-bit int) when the reader type isDeserializingResourceReaderReadBytes(len)- up to 2 GBThe last row also relied on
Debug.Assert(Enum.IsDefined(typeof(SerializationFormat), kind)), which is compiled out of Release builds, so an unknown kind was silently accepted.Values above the CLR's maximum array length fail immediately with
OutOfMemoryException("Array dimensions exceeded supported range"); values just below it commit real memory before the subsequent read fails. Either way the failure is an OOM, not theBadImageFormatExceptionthe callers (ResourcesFileTreeNode,WholeProjectDecompiler,ilspycmd) are written to catch.Reachability
Expanding a
.resourcesnode in the resource tree (ResourcesFileTreeNode.LoadChildren->new ResourcesFile(stream)), exporting to ResX, whole-project decompilation, andilspycmdresource extraction all parse the file this way. No action beyond opening the assembly and clicking the resource is needed.What was re-verified and found not to be a problem
numResources * 2inGetStartPositionscannot overflow. The constructor already performsreader.Seek(checked(4 * numResources), ...)and converts theOverflowExceptioninto aBadImageFormatException, so anyResourcesFileinstance that exists hasnumResources <= int.MaxValue / 4, andnumResources * 2 <= int.MaxValue / 2. The new bound (numResources * 8 <= remaining bytes) tightens this further; no change toGetStartPositionswas needed.BinaryReader.ReadString(type names, string resources) is already safe. It reads in 128-byte chunks into aStringBuilderrather than pre-allocating the declared length, so a huge declared length just hits end-of-stream.The fix
One private helper, applied at every site in the table:
Every element of these counts occupies at least
bytesPerElementbytes in the stream (8 per resource: a 4-byte name hash plus a 4-byte name position; 1 per type name, since each is a length-prefixed string; 1 per byte of any byte length). A value that would need more bytes than remain cannot describe real data, so it is rejected before any allocation happens. The bound is relative to the input, so it never rejects a well-formed file of any size and introduces no arbitrary constant.ResourcesFiledocuments that its stream must be seekable, soLength/Positionare always available.The
Debug.Asserton the serialization-format kind is promoted to aBadImageFormatException, since only the four kinds defined bySystem.Resources.Extensionsare valid.Tests
ICSharpCode.Decompiler.Tests/Util/ResourcesFileTests.cs(new), with a small in-test.resourceswriter:DeserializingResourceReaderserialized object round-trips (guards against the bounds rejecting legitimate data).numTypes = int.MaxValue->BadImageFormatException(wasOutOfMemoryException).numResources = 100000on a header-only stream ->BadImageFormatExceptionbefore positions are read (wasEndOfStreamExceptionafter allocating).int.MaxValue->BadImageFormatException(wasOutOfMemoryException).ByteArrayandStreamlengthint.MaxValue->BadImageFormatException(wasOutOfMemoryException).int.MaxValue->BadImageFormatException(wasOutOfMemoryException).BadImageFormatException(was an assertion failure in Debug, silently accepted in Release).All seven failure cases were confirmed red before the change and green after; the existing
AvaloniaResourcesFileTestsstill pass.Generated with Claude Code