vmcore-dmesg stack smashing happend in extreme case
Subsystems (2)
PATCH 1/1 vmcore-dmesg stack smashing happend in extreme case
Description
in dump_dmesg_structured() the out_buf size is 4096, and if the length is less than 4080( 4096-16 ) it won't really write out. Normally, after writing one or four chars to the out_buf, it will check the length of out_buf. But in extreme cases, 19 chars was written to the out_buf before checking the length. This may cause the stack corruption. If the length was 4079 (won't realy write out), and then write 19 chars to it. the out_buf will overflow.
Solution
Change 16 to 32 and always check the lenth of out_buf before move to next record, thus can make sure that at the beginning of a message we always have 32 bytes of buffer.
--- vmcore-dmesg/vmcore-dmesg.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
vmcore-dmesg/vmcore-dmesg.c +6 -1
@@ -674,7 +674,7 @@ static void dump_dmesg_structured(int fd)else out_buf[len++] = c; -if (len >=OUT_BUF_SIZE -16 ) { +if (len >=OUT_BUF_SIZE -32 ) {write_to_stdout (out_buf, len); len =0 ; }
@@ -682,6 +682,11 @@ static void dump_dmesg_structured(int fd) out_buf[len++] ='\n' ; +if (len >=OUT_BUF_SIZE -32 ) { +write_to_stdout (out_buf, len); + len =0 ; + } +/* Move to next record */ current_idx =log_next (buf, current_idx); }
Signature · 7 lines
1.8.4.2 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Quoted 24 lines · arthur
Description
indump_dmesg_structured()theout_bufsize is 4096, and if the length is less than 4080( 4096-16 ) it won't really write out. Normally, after writing one or four chars to theout_buf, it will check the length ofout_buf. But in extreme cases, 19 chars was written to theout_bufbefore checking the length. This may cause the stack corruption. If the length was 4079 (won't realy write out), and then write 19 chars to it. theout_bufwill overflow.Solution
Change 16 to 32 and always check the lenth ofout_bufbefore move to next record, thus can make sure that at the beginning of a message we always have 32 bytes of buffer.Signed-off-by arthur <[email protected]>--- vmcore-dmesg/vmcore-dmesg.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)vmcore-dmesg/vmcore-dmesg.c +0
-0
vmcore-dmesg/vmcore-dmesg.c static void dump_dmesg_structured(int fd) · line 677
Quoted 5 lines
vmcore-dmesg/vmcore-dmesg.c +1
-1@@ -674,7 +674,7 @@ static void dump_dmesg_structured(int fd)else out_buf[len++] = c; -if (len >=OUT_BUF_SIZE -16 ) { +if (len >=OUT_BUF_SIZE -32 ) {
How do you know 32 is enough and will not overflow?
vmcore-dmesg/vmcore-dmesg.c static void dump_dmesg_structured(int fd) · line 688
Quoted 11 lines
vmcore-dmesg/vmcore-dmesg.c +4
-0write_to_stdout (out_buf, len); len =0 ; }@@ -682,6 +682,11 @@ static void dump_dmesg_structured(int fd) out_buf[len++] ='\n' ; +if (len >=OUT_BUF_SIZE -32 ) { +write_to_stdout (out_buf, len); + len =0 ; + }
Why do we need this? After last check only one extra character "\n" has been written?
I think that this overflow is coming from following sprintf().
len += sprintf(out_buf + len, "[%5llu.%06llu] ",
(long long unsigned int)imaxdiv_sec.quot,
(long long unsigned int)imaxdiv_usec.quot);So we are printing 2 long long unsigned int and 3 more chars "[] ". I
Max long long unsigned int is (2^64 -1) = 18446744073709551615
Above takes 20 bytes. So total max length can be 20 + 20 + 3 = 43.
I think just leave 64bytes of buffer before we enter the next message and that should be fine.
And we don't have to do this twice. Just do it once inside innermost for loop.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
----- Original Message -----
Quoted 54 lines · arthur
Quoted 24 lines
Description
indump_dmesg_structured()theout_bufsize is 4096, and if the length is less than 4080( 4096-16 ) it won't really write out. Normally, after writing one or four chars to theout_buf, it will check the length ofout_buf. But in extreme cases, 19 chars was written to theout_bufbefore checking the length. This may cause the stack corruption. If the length was 4079 (won't realy write out), and then write 19 chars to it. theout_bufwill overflow.Solution
Change 16 to 32 and always check the lenth ofout_bufbefore move to next record, thus can make sure that at the beginning of a message we always have 32 bytes of buffer.Signed-off-by arthur <[email protected]>--- vmcore-dmesg/vmcore-dmesg.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)vmcore-dmesg/vmcore-dmesg.c +0
-0vmcore-dmesg/vmcore-dmesg.c static void dump_dmesg_structured(int fd) · line 677
Quoted 5 lines
vmcore-dmesg/vmcore-dmesg.c +1
-1@@ -674,7 +674,7 @@ static void dump_dmesg_structured(int fd)else out_buf[len++] = c; -if (len >=OUT_BUF_SIZE -16 ) { +if (len >=OUT_BUF_SIZE -32 ) {How do you know 32 is enough and will not overflow?
vmcore-dmesg/vmcore-dmesg.c static void dump_dmesg_structured(int fd) · line 688
Quoted 11 lines
vmcore-dmesg/vmcore-dmesg.c +4
-0write_to_stdout (out_buf, len); len =0 ; }@@ -682,6 +682,11 @@ static void dump_dmesg_structured(int fd) out_buf[len++] ='\n' ; +if (len >=OUT_BUF_SIZE -32 ) { +write_to_stdout (out_buf, len); + len =0 ; + }Why do we need this? After last check only one extra character "\n" has been written?
I think that this overflow is coming from following
sprintf().len += sprintf(out_buf + len, "[%5llu.%06llu] ", (long long unsigned int)imaxdiv_sec.quot, (long long unsigned int)imaxdiv_usec.quot);
Quoted 8 lines
So we are printing 2 long long unsigned int and 3 more chars "[] ". I
Max long long unsigned int is (2^64 -1) = 18446744073709551615
Above takes 20 bytes. So total max length can be 20 + 20 + 3 = 43.
I think just leave 64bytes of buffer before we enter the next message and that should be fine.
Hi Vivek
You are right. we need leave 64bytes instead of 32bytes.
Quoted 3 lines
And we don't have to do this twice. Just do it once inside innermost for loop.
At the beginning, I think it is unnecessary to do this twice too. But if the text_len ==0 then the for loop won't be executed and check won't be executed either. May be this situation never come up, considering it's cost, I can accept it because most of time we just do the check instead of write. If it is sure that text_len == 0 never come up, then we should remove it.
Thanks
zzou
Quoted 9 lines
Thanks
Vivek_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
On Mon, Mar 10, 2014 at 09:42:18PM -0400, Zhi Zou wrote:
[..]
Quoted 10 lines
Quoted 3 lines
And we don't have to do this twice. Just do it once inside innermost for loop.
At the beginning, I think it is unnecessary to do this twice too. But if the
text_len==0 then the for loop won't be executed and check won't be executed either. May be this situation never come up, considering it's cost, I can accept it because most of time we just do the check instead of write. If it is sure thattext_len== 0 never come up, then we should remove it.
When do we get text_len=0? If text_len=0, then we are just printing time stamp? Does it really happen?
I think do a quick test and if we are not encountering such cases, don't worry about it right now. Making logic too complicated does not help.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec