2013-05-29 09:44:09

by Chen Gang

[permalink] [raw]
Subject: [PATCH] arch: blackfin: kernel: sprintf(), need avoid NUL for '%s'


When it is kernel symbol, the 'modname' will be NUL, and the 'symname'
contents the valid name.

So for sprintf(), need avoid NUL for '%s'.


Signed-off-by: Chen Gang <[email protected]>
---
arch/blackfin/kernel/trace.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/arch/blackfin/kernel/trace.c b/arch/blackfin/kernel/trace.c
index c36efa0..11f98bb 100644
--- a/arch/blackfin/kernel/trace.c
+++ b/arch/blackfin/kernel/trace.c
@@ -51,7 +51,7 @@ void decode_address(char *buf, unsigned long address)
if (!modname)
modname = delim = "";
sprintf(buf, "{ %s%s%s%s + 0x%lx }",
- delim, modname, delim, symname,
+ delim, modname ? : "kernel", delim, symname,
(unsigned long)offset);
return;
}
--
1.7.7.6


2013-05-29 10:07:55

by Chen Gang

[permalink] [raw]
Subject: [PATCH] arch: blackfin: kernel: memory overflow, 'namebuf' length need be more than 256


The 'name' length in decode_address() may be 255, after call d_path()
successfully.

So for decode_address(), the input 'buf' need be more than 256, or may
memory overflow.

For simply thinking of, use 'namebuf[512]' instead of 'namebuf[150]'
which will pass to decode_address() as input 'buf'.

Also better use macro instead of hard code number when processing
symbols work.


Signed-off-by: Chen Gang <[email protected]>
---
arch/blackfin/kernel/trace.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/blackfin/kernel/trace.c b/arch/blackfin/kernel/trace.c
index c36efa0..4fecd3e 100644
--- a/arch/blackfin/kernel/trace.c
+++ b/arch/blackfin/kernel/trace.c
@@ -37,7 +37,7 @@ void decode_address(char *buf, unsigned long address)
const char *symname;
char *modname;
char *delim = ":";
- char namebuf[128];
+ char namebuf[KSYM_NAME_LEN];
#endif

buf += sprintf(buf, "<0x%08lx> ", address);
@@ -845,7 +845,7 @@ void dump_bfin_mem(struct pt_regs *fp)

void show_regs(struct pt_regs *fp)
{
- char buf[150];
+ char buf[512];
struct irqaction *action;
unsigned int i;
unsigned long flags = 0;
--
1.7.7.6

2013-05-29 11:30:08

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH] arch: blackfin: kernel: memory overflow, 'namebuf' length need be more than 256

On Wed, May 29, 2013 at 12:07 PM, Chen Gang <[email protected]> wrote:
> The 'name' length in decode_address() may be 255, after call d_path()
> successfully.
>
> So for decode_address(), the input 'buf' need be more than 256, or may
> memory overflow.
>
> For simply thinking of, use 'namebuf[512]' instead of 'namebuf[150]'
> which will pass to decode_address() as input 'buf'.
>
> Also better use macro instead of hard code number when processing
> symbols work.
>
>
> Signed-off-by: Chen Gang <[email protected]>
> ---
> arch/blackfin/kernel/trace.c | 4 ++--
> 1 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/blackfin/kernel/trace.c b/arch/blackfin/kernel/trace.c
> index c36efa0..4fecd3e 100644
> --- a/arch/blackfin/kernel/trace.c
> +++ b/arch/blackfin/kernel/trace.c
> @@ -37,7 +37,7 @@ void decode_address(char *buf, unsigned long address)
> const char *symname;
> char *modname;
> char *delim = ":";
> - char namebuf[128];
> + char namebuf[KSYM_NAME_LEN];
> #endif
>
> buf += sprintf(buf, "<0x%08lx> ", address);
> @@ -845,7 +845,7 @@ void dump_bfin_mem(struct pt_regs *fp)
>
> void show_regs(struct pt_regs *fp)
> {
> - char buf[150];
> + char buf[512];

This will increase stack usage a lot. And this function calls decode_address(),
which allocates another buffer on the stack.

However, as this is in debug code which is (never?) called concurrently, both
buffers can be made static?

> struct irqaction *action;
> unsigned int i;
> unsigned long flags = 0;

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

2013-05-29 12:07:46

by Chen Gang

[permalink] [raw]
Subject: Re: [PATCH] arch: blackfin: kernel: memory overflow, 'namebuf' length need be more than 256

On 05/29/2013 07:30 PM, Geert Uytterhoeven wrote:
>> void show_regs(struct pt_regs *fp)
>> > {
>> > - char buf[150];
>> > + char buf[512];
> This will increase stack usage a lot. And this function calls decode_address(),
> which allocates another buffer on the stack.
>

Can it have a risk to cause the related stack used up ? (excuse me, I
don't know the system or thread stack size of blackfin).

If so, we really need save some byes (e.g. buf[300] ...), at least. But
I'm not sure whether it still has the risk too.


> However, as this is in debug code which is (never?) called concurrently, both
> buffers can be made static?
>

trap_c() may call show_regs(), and can multiple traps occur at the same
time ?

It seems it can (but I am not quite sure): for trap_c() only use stack
variables, and "cpu = raw_smp_processor_id()".



Thanks.
--
Chen Gang

Asianux Corporation

2013-06-16 03:02:52

by Mike Frysinger

[permalink] [raw]
Subject: Re: [PATCH] arch: blackfin: kernel: sprintf(), need avoid NUL for '%s'

i think you mean NULL instead of NUL

that said, the kernel is smart enough to replace NULL with "(null)",
so i don't see much point in this patch
-mike

On Wed, May 29, 2013 at 5:43 AM, Chen Gang <[email protected]> wrote:
>
> When it is kernel symbol, the 'modname' will be NUL, and the 'symname'
> contents the valid name.
>
> So for sprintf(), need avoid NUL for '%s'.
>
>
> Signed-off-by: Chen Gang <[email protected]>
> ---
> arch/blackfin/kernel/trace.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/arch/blackfin/kernel/trace.c b/arch/blackfin/kernel/trace.c
> index c36efa0..11f98bb 100644
> --- a/arch/blackfin/kernel/trace.c
> +++ b/arch/blackfin/kernel/trace.c
> @@ -51,7 +51,7 @@ void decode_address(char *buf, unsigned long address)
> if (!modname)
> modname = delim = "";
> sprintf(buf, "{ %s%s%s%s + 0x%lx }",
> - delim, modname, delim, symname,
> + delim, modname ? : "kernel", delim, symname,
> (unsigned long)offset);
> return;
> }
> --
> 1.7.7.6
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/

2013-06-17 01:14:18

by Chen Gang

[permalink] [raw]
Subject: Re: [PATCH] arch: blackfin: kernel: sprintf(), need avoid NUL for '%s'

On 06/16/2013 11:02 AM, Mike Frysinger wrote:
> i think you mean NULL instead of NUL
>
> that said, the kernel is smart enough to replace NULL with "(null)",
> so i don't see much point in this patch

Oh, really it is, it is my fault (originally, I did not know about it).

Thanks.
--
Chen Gang

Asianux Corporation