BinaryFormatter can hang within modified stream?

This is definitely weird.  I could have sworn the binary formatter in V1.0 would throw an error whenever the stream was invalid (can happen if the transport protocol burps or a file gets messed up on your disk), but with V1.1 installed, all the formatter does is hang.

Without going in to what I was testing, all I did was to go into the file and change a typename.  I have two identical classes in my assembly, one named MyFish and the other MyDolphin.  By changing MyFish in the binary file to MyDolphin the formatter goes to work and never comes back.  I'm assuming this is due to some offsets in the file that are looking for specific field values (most likely a string value).

The fact that it never comes back really annoys me, because it creates a simple DOS attack against any applications that use the BinaryFormatter (by never I've only tested for about 5 minutes.  I'm going to leave it running for a while longer though).

Here is the skinny as I see it:
1.  Changing the length of the type name will result in an out of memory exception.
2.  If you have a custom binder it will happen in the BindToType call.
3.  Without a custom binder it will happen within the binary formatter itself.
4.  I'm not sure if this is a string interning problem of some sort or what, but the system memory is completely consumed.
5.  The attack stops when the application is terminated (can be hard since my entire machine was frozen most of the time) or an OutOfMemoryException is thrown which cancels the application out.

 

Published Monday, February 02, 2004 9:55 PM by Justin Rogers
Filed under: ,

Comments

Tuesday, February 17, 2004 6:49 AM by Darren Neimke

# re: BinaryFormatter can hang within modified stream?

Justin... did you find a suitable hack, err, *workaround* :-) for this?
Wednesday, February 18, 2004 3:48 AM by Justin Rogers

# re: BinaryFormatter can hang within modified stream?

I never did work through to the root cause, reason being, I don't have access to the code for the BinaryFormatter itself to figure out why changing the length of a type name in the stream would cause such a hang.

However, examining the types involved, it would take a good deal of inspection through IL in order to get to the root of the problem. The BinaryFormatter appears to be one of the more complex pieces of code present in the framework.

Maybe I'll head on over to the Rotor source in the next week and use that to discover the root issue.
Thursday, February 19, 2004 1:10 AM by Darren Neimke

# re: BinaryFormatter can hang within modified stream?

Cool, thanks Justin. I'll check back here from time-to-time as I remember to but, if you do find out anything interesting inside the next 6 months (say), and you remember, can you give me a ping?

Cheers :-)
Thursday, February 19, 2004 5:26 AM by Darren Neimke

# re: BinaryFormatter can hang within modified stream?

got it! thanks mate :-)
Thursday, February 19, 2004 8:12 AM by TrackBack

# Follow up to corrupted binary stream in BinaryFormatter causing OOM exception...

Thursday, February 19, 2004 8:31 AM by TrackBack

# Follow up to corrupted binary stream in BinaryFormatter causing OOM exception...

Thursday, February 19, 2004 9:46 AM by TrackBack

# Final complaints about BinaryFormatter and the persistence format...

Leave a Comment

(required) 
(required) 
(optional)
(required)