Welcome to MSDN Blogs Sign in | Join | Help

Michael Howard's Web Log

A Simple Software Security Guy at Microsoft!
Whatever Happened to sprintf(..., “%n”,...)?

You may have noticed that if your code calls functions in the sprintf family and the format template string uses the %n parameter, then it fails to run correctly after it is compiled with Visual Studio 2005. Why? Well, it's pretty simple, by default we disabled support for %n because it can be a security issue if arguments used by sprintf come from an attacker and are not validated correctly. It also turns out very few developers use %n.

%n means "write this number of characters written so far to the buffer at an address in the function argument list". If the attacker has a good deal of freedom when calling this function, then the attacker can essentially spray any value in memory.

If you must enable %n in your code, you need to call _set_printf_count_output.

Posted: Thursday, September 28, 2006 10:56 AM by michael_HOWARD
Filed under:

Comments

Peter Ritchie's MVP Blog said:

Whatever Happened to sprintf(..., "%n", ...)? Strsafe.h: Safer String Handling in C Security
# September 28, 2006 2:30 PM

Steven Alexander Jr. said:

A couple of years ago, I replaced libc's %n handling with an abort() on one of my FreeBSD machines and I never had anything crash because of it (the machine is retired now). %n seems rare indeed.
# October 2, 2006 12:43 PM

Steve Pitts said:

Does the same apply to the scanf family too??

I really don't see that %n is any great security exposure, over and above the inherent security flaws in sprintf itself. Can you think of an example where an attacker could take advantage of %n to do 'useful' damage to a system, because I'm not sure I can.

# October 24, 2006 10:59 AM

DaveZero said:

'useful' damage to a system?  lol.  like in the case of DoS?

# October 25, 2006 1:58 AM

michael_HOWARD said:

>>scanf

nope - sprintf only right now.

# October 25, 2006 2:01 PM
New Comments to this post are disabled
Page view tracker