Repository navigation
[System.Drawing.Common] Work around libgdiplus use after free - #43074
Conversation
|
Tagging subscribers to this area: @roji, @ajcvickers |
08093c8 to
9a4e76c
Compare
On Windows, both of the following are legal
Metafile mf = ... ; // get a metafile instance
Graphics g = Graphics.FromImage(mf);
g.Dispose(); mf.Dispose();
and
Metafile mf = ... ; // get a metafile instance
Graphics g = Graphics.FromImage(mf);
mf.Dispose(); g.Dispose();
On Unix, libgdiplus has a use after free bug for the second form - the metafile
native image is disposed, but the graphics instance still has a pointer to the
memory that it will use during cleanup. If the memory is reused, the graphics
instance will see garbage values and crash.
The workaround is to add a MetadataHolder class and to transfer responsibility
for disposing of the native image instance to it if the Metafile is disposed
before the Graphics.
Note that the following is not allowed (throws OutOfMemoryException on GDI+ on
Windows), so there's only ever one instance of Graphics associated with a
Metafile at a time.
Graphics g = Graphics.FromImage(mf);
Graphics g2 = Graphics.FromImage(mf); // throws
Addresses dotnet#37838
9a4e76c to
48c2b9e
Compare
|
Tagging subscribers to this area: @safern, @tannergooding, @jeffhandley |
|
One thing I'm not sure about - is it worthwhile to make One reason not to do it might be because the rest of the SafeHandle methods aren't really meaningful to |
|
cc: @stephentoub |
Co-authored-by: Santiago Fernandez Madero <[email protected]>
| // Metafile mf = ...; // get a metafile instance | ||
| // Graphics g = Graphics.FromImage(mf); | ||
| // Graphics g2 = Graphics.FromImage(mf); // throws OutOfMemoryException on GDI+ on Win32 | ||
| internal sealed class MetafileHolder : IDisposable |
There was a problem hiding this comment.
Just for my own curiosity, any reason not to make it a struct?
There was a problem hiding this comment.
Looks like it has a finalizer. Structs can't have finalizers.
On Windows, both of the following are legal
and
On Unix, libgdiplus has a use after free bug for the second form - the metafile
native image is disposed, but the graphics instance still has a pointer to the
memory that it will use during cleanup. If the memory is reused, the graphics
instance will see garbage values and crash.
The workaround is to add a MetadataHolder class and to transfer responsibility
for disposing of the native image instance to it if the Metafile is disposed
before the Graphics.
Note that the following is not allowed (throws OutOfMemoryException on GDI+ on
Windows), so there's only ever one instance of Graphics associated with a
Metafile at a time.
Addresses #37838