The {%n
Specifier}
%n is the dangerous one: it does not print anything. It writes
the number of characters printed so far to the int* argument.
int count;
printf("hello%n", &count); /* count becomes 5 */
- Give
%nan attacker-controlled pointer ⇒ write to an arbitrary address. - Control the value by controlling how many bytes were printed before the
%n—- width specifiers do this cheaply. - Size variants avoid printing billions of bytes:
%nwrites 4 bytes (anint).%hnwrites 2 bytes (ashort).%hhnwrites 1 byte.