All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts/kallsyms.c: fix potential segfault
@ 2011-05-01  3:41 Xiaochen Wang
  2011-05-03 11:56 ` Paulo Marques
  2011-05-12 15:24 ` Michal Marek
  0 siblings, 2 replies; 3+ messages in thread
From: Xiaochen Wang @ 2011-05-01  3:41 UTC (permalink / raw)
  To: Jean Sacren, Michal Marek, linux-kernel

Description:
This bug hardly appears during real kernel compiling,
 because the vmlinux symbols table is huge.

But we can still catch it under strict condition , as follows.
   $ echo "c101b97b T do_fork" | ./scripts/kallsyms --all-symbols
   #include <asm/types.h>
   ......
   ......
   .globl kallsyms_token_table
           ALGN
   kallsyms_token_table:
   Segmentation fault (core dumped)
   $

If symbols table is small, all entries in token_profit[0x10000] may
decrease to 0 after several calls of compress_symbols() in optimize_result().
In that case, find_best_token() always return 0 and
best_table[i] is set to "\0\0" and best_table_len[i] is set to 2.

As a result, expand_symbol(best_table[0]="\0\0", best_table_len[0]=2, buf)
in write_src() will run in infinite recursion until stack overflows,
causing segfault.

This patch checks the find_best_token() return value. If all entries in
token_profit[0x10000] become 0 according to return value, it breaks the loop
in optimize_result().
And expand_symbol() works well when best_table_len[i] is 0.


Signed-off-by: Xiaochen Wang <wangxiaochen0@gmail.com>
---
 scripts/kallsyms.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 60dd3eb..487ac6f 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -500,6 +500,8 @@ static void optimize_result(void)
 
 			/* find the token with the breates profit value */
 			best = find_best_token();
+			if (token_profit[best] == 0)
+				break;
 
 			/* place it in the "best" table */
 			best_table_len[i] = 2;
-- 
1.7.2.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] scripts/kallsyms.c: fix potential segfault
  2011-05-01  3:41 [PATCH] scripts/kallsyms.c: fix potential segfault Xiaochen Wang
@ 2011-05-03 11:56 ` Paulo Marques
  2011-05-12 15:24 ` Michal Marek
  1 sibling, 0 replies; 3+ messages in thread
From: Paulo Marques @ 2011-05-03 11:56 UTC (permalink / raw)
  To: Xiaochen Wang; +Cc: Jean Sacren, Michal Marek, linux-kernel

Xiaochen Wang wrote:
> Description:
> This bug hardly appears during real kernel compiling,
>  because the vmlinux symbols table is huge.
> 
> But we can still catch it under strict condition , as follows.
>    $ echo "c101b97b T do_fork" | ./scripts/kallsyms --all-symbols
>    #include <asm/types.h>
>    ......
>    ......
>    .globl kallsyms_token_table
>            ALGN
>    kallsyms_token_table:
>    Segmentation fault (core dumped)
>    $
> 
> If symbols table is small, all entries in token_profit[0x10000] may
> decrease to 0 after several calls of compress_symbols() in optimize_result().
> In that case, find_best_token() always return 0 and
> best_table[i] is set to "\0\0" and best_table_len[i] is set to 2.
> 
> As a result, expand_symbol(best_table[0]="\0\0", best_table_len[0]=2, buf)
> in write_src() will run in infinite recursion until stack overflows,
> causing segfault.
> 
> This patch checks the find_best_token() return value. If all entries in
> token_profit[0x10000] become 0 according to return value, it breaks the loop
> in optimize_result().
> And expand_symbol() works well when best_table_len[i] is 0.

For this to happen on a real kernel, the list of symbols needs to have
less than 256 single characters + two letter sequences, which is highly
unlikely. From a quick test, a symbol table of a mere 30 symbols was
able to overcome those constraints.

Having said that, the fix is very small, it is not on a hot-path and it
is the right thing to do from a correctness stand point, so you have my ack.

Acked-by: Paulo Marques <pmarques@grupopie.com>

-- 
Paulo Marques - www.grupopie.com

"Conservation of angular momentum makes the world go around" - John Clark

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scripts/kallsyms.c: fix potential segfault
  2011-05-01  3:41 [PATCH] scripts/kallsyms.c: fix potential segfault Xiaochen Wang
  2011-05-03 11:56 ` Paulo Marques
@ 2011-05-12 15:24 ` Michal Marek
  1 sibling, 0 replies; 3+ messages in thread
From: Michal Marek @ 2011-05-12 15:24 UTC (permalink / raw)
  To: Xiaochen Wang; +Cc: Jean Sacren, linux-kernel

On Sun, May 01, 2011 at 11:41:41AM +0800, Xiaochen Wang wrote:
> Description:
> This bug hardly appears during real kernel compiling,
>  because the vmlinux symbols table is huge.
> 
> But we can still catch it under strict condition , as follows.
>    $ echo "c101b97b T do_fork" | ./scripts/kallsyms --all-symbols
>    #include <asm/types.h>
>    ......
>    ......
>    .globl kallsyms_token_table
>            ALGN
>    kallsyms_token_table:
>    Segmentation fault (core dumped)
>    $
> 
> If symbols table is small, all entries in token_profit[0x10000] may
> decrease to 0 after several calls of compress_symbols() in optimize_result().
> In that case, find_best_token() always return 0 and
> best_table[i] is set to "\0\0" and best_table_len[i] is set to 2.
> 
> As a result, expand_symbol(best_table[0]="\0\0", best_table_len[0]=2, buf)
> in write_src() will run in infinite recursion until stack overflows,
> causing segfault.
> 
> This patch checks the find_best_token() return value. If all entries in
> token_profit[0x10000] become 0 according to return value, it breaks the loop
> in optimize_result().
> And expand_symbol() works well when best_table_len[i] is 0.
> 
> 
> Signed-off-by: Xiaochen Wang <wangxiaochen0@gmail.com>
> ---
>  scripts/kallsyms.c |    2 ++
>  1 files changed, 2 insertions(+), 0 deletions(-)

Pushed to kbuild-2.6.git#kbuild, thanks.

Michal

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-05-12 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-01  3:41 [PATCH] scripts/kallsyms.c: fix potential segfault Xiaochen Wang
2011-05-03 11:56 ` Paulo Marques
2011-05-12 15:24 ` Michal Marek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.