Re: [PATCH] staging: dgnc: fix potential format string flaw

From: Kees Cook
Date: Wed Sep 11 2013 - 15:25:16 EST


On Wed, Sep 11, 2013 at 12:09 PM, Joe Perches <joe@xxxxxxxxxxx> wrote:
> On Wed, 2013-09-11 at 11:19 -0700, Kees Cook wrote:
>> On Wed, Sep 11, 2013 at 2:31 AM, Dan Carpenter <dan.carpenter@xxxxxxxxxx> wrote:
>> > On Tue, Sep 10, 2013 at 10:19:17PM -0700, Kees Cook wrote:
>> >> In the former case, format characters will get processed by the
>> >> sprintf logic. In the latter, they are printed as-is. In this specific
>> >> case, if there was a way to inject strings like "ohai %n" into the
>> >> msgbuf string, the former would actually attempt to resolve the %n. In
>> >> the simple case, this could lead to Oopses, and in the unlucky case,
>> >> it could allow arbitrary memory writing and execution control.
>> >>
>> >> http://en.wikipedia.org/wiki/Uncontrolled_format_string
>> >
>> > The kernel ignores %n so hopefully it can't actually write to memory.
>>
>> I wish! This is not the case, though. See FORMAT_TYPE_NRCHARS in
>> lib/vsprintf.c's vsnprintf().
>>
>> $ git grep '%n' | wc -l
>> 111
>
> Umm.
>
> See: lib/vsprintf.c
>
> /**
> * vsnprintf - Format a string and place it in a buffer
> [...]
> * %n is ignored
>
> %n does work for vsscanf though.

The comment is a lie:

int len = 0;
printk("len:%d\n", len);
printk("%s%n\n", "Ohai!", &len);
printk("len:%d\n", len);

[ 0.025930] len:0
[ 0.026003] Ohai!
[ 0.026261] len:5

The functionality between scanf and printf was, I think, merged in
2009, if I'm reading the git blame correctly.

-Kees

--
Kees Cook
Chrome OS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/