linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
@ 2009-03-23 16:10 Daniel Mack
  2009-03-23 16:40 ` Frederic Weisbecker
  2009-03-23 16:42 ` [tip:tracing/ftrace] " Daniel Mack
  0 siblings, 2 replies; 6+ messages in thread
From: Daniel Mack @ 2009-03-23 16:10 UTC (permalink / raw)
  To: linux-kernel; +Cc: Daniel Mack

In kernel/trace/trace_functions_graph.c, print_graph_duration(), len can
be as low as 1 or 2, which could make snprintf() write beyond the
buffer bounds. (Found by cppcheck, no real-world bug occured)

Signed-off-by: Daniel Mack <daniel@caiaq.de>
---
 kernel/trace/trace_functions_graph.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c
index 930c08e..7533b25 100644
--- a/kernel/trace/trace_functions_graph.c
+++ b/kernel/trace/trace_functions_graph.c
@@ -281,7 +281,7 @@ print_graph_duration(unsigned long long duration, struct trace_seq *s)
 	unsigned long nsecs_rem = do_div(duration, 1000);
 	/* log10(ULONG_MAX) + '\0' */
 	char msecs_str[21];
-	char nsecs_str[5];
+	char nsecs_str[8];
 	int ret, len;
 	int i;
 
-- 
1.6.2


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

* Re: [PATCH] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
  2009-03-23 16:10 [PATCH] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size Daniel Mack
@ 2009-03-23 16:40 ` Frederic Weisbecker
  2009-03-23 16:57   ` Daniel Mack
  2009-03-23 16:42 ` [tip:tracing/ftrace] " Daniel Mack
  1 sibling, 1 reply; 6+ messages in thread
From: Frederic Weisbecker @ 2009-03-23 16:40 UTC (permalink / raw)
  To: Daniel Mack; +Cc: linux-kernel

On Mon, Mar 23, 2009 at 05:10:37PM +0100, Daniel Mack wrote:
> In kernel/trace/trace_functions_graph.c, print_graph_duration(), len can
> be as low as 1 or 2, which could make snprintf() write beyond the
> buffer bounds. (Found by cppcheck, no real-world bug occured)
> 
> Signed-off-by: Daniel Mack <daniel@caiaq.de>


Hi Daniel,


It can't really happen because nsecs_rem is a rest of a division
per 1000, so it will not exceed 3 digits (so it should be [4] and not
[5], btw).

I must confess I should have added a better comment on this
area.

We are printing a duration in usec with a part in nsec with the following
constraints:

_ never exceed 7 characters unless we are are upper 9999999 usecs
_ always keep the usecs consistants

Which means that while we have 4 or lesser digits for the usecs, we can
print the 3 digits of the nanosecs.

If we need 5 for usecs, drop the least significant nanosec digit.
If we need 6 for usecs, drop the two least significant nanosec digits

6754.543 is correct
67545.543 must become 67545.54
etc...

That's why we have:

len = strlen(msecs_str);

	/* Print nsecs (we don't want to exceed 7 numbers) */
	if (len < 7) {
		snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);

The 8 - len inside snprintf is not a security against overflow but a
precision set. We know that it will never go upper 3 digits, but if we
have 6 digits for usecs, we want only 8 - 6 = 2 nsecs

Hmm, may be it should be 7 -len. I don't remember. Anyway, this part
must be fixed to handle msecs and align the row to the left...


Frederic.


> ---
>  kernel/trace/trace_functions_graph.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c
> index 930c08e..7533b25 100644
> --- a/kernel/trace/trace_functions_graph.c
> +++ b/kernel/trace/trace_functions_graph.c
> @@ -281,7 +281,7 @@ print_graph_duration(unsigned long long duration, struct trace_seq *s)
>  	unsigned long nsecs_rem = do_div(duration, 1000);
>  	/* log10(ULONG_MAX) + '\0' */
>  	char msecs_str[21];
> -	char nsecs_str[5];
> +	char nsecs_str[8];
>  	int ret, len;
>  	int i;
>  
> -- 
> 1.6.2
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/


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

* [tip:tracing/ftrace] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
  2009-03-23 16:10 [PATCH] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size Daniel Mack
  2009-03-23 16:40 ` Frederic Weisbecker
@ 2009-03-23 16:42 ` Daniel Mack
  2009-03-23 16:47   ` Frederic Weisbecker
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel Mack @ 2009-03-23 16:42 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, hpa, mingo, fweisbec, rostedt, daniel, tglx, mingo

Commit-ID:  603b9b9081ae0a1af986b9059a0a5055876ddea9
Gitweb:     http://git.kernel.org/tip/603b9b9081ae0a1af986b9059a0a5055876ddea9
Author:     Daniel Mack <daniel@caiaq.de>
AuthorDate: Mon, 23 Mar 2009 17:10:37 +0100
Committer:  Ingo Molnar <mingo@elte.hu>
CommitDate: Mon, 23 Mar 2009 17:40:51 +0100

kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size

Impact: fix currently inactive buffer-overflow

In kernel/trace/trace_functions_graph.c, print_graph_duration(),
len can be as low as 1 or 2, which could make snprintf() write
beyond the buffer bounds. (Found by cppcheck, no real-world bug
occured)

Signed-off-by: Daniel Mack <daniel@caiaq.de>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
LKML-Reference: <1237824637-28190-1-git-send-email-daniel@caiaq.de>
Signed-off-by: Ingo Molnar <mingo@elte.hu>


---
 kernel/trace/trace_functions_graph.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c
index e876816..a305472 100644
--- a/kernel/trace/trace_functions_graph.c
+++ b/kernel/trace/trace_functions_graph.c
@@ -430,7 +430,7 @@ print_graph_duration(unsigned long long duration, struct trace_seq *s)
 	unsigned long nsecs_rem = do_div(duration, 1000);
 	/* log10(ULONG_MAX) + '\0' */
 	char msecs_str[21];
-	char nsecs_str[5];
+	char nsecs_str[8];
 	int ret, len;
 	int i;
 

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

* Re: [tip:tracing/ftrace] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
  2009-03-23 16:42 ` [tip:tracing/ftrace] " Daniel Mack
@ 2009-03-23 16:47   ` Frederic Weisbecker
  2009-03-23 17:15     ` Ingo Molnar
  0 siblings, 1 reply; 6+ messages in thread
From: Frederic Weisbecker @ 2009-03-23 16:47 UTC (permalink / raw)
  To: mingo, hpa, linux-kernel, rostedt, tglx, daniel, mingo; +Cc: linux-tip-commits

On Mon, Mar 23, 2009 at 04:42:27PM +0000, Daniel Mack wrote:
> Commit-ID:  603b9b9081ae0a1af986b9059a0a5055876ddea9
> Gitweb:     http://git.kernel.org/tip/603b9b9081ae0a1af986b9059a0a5055876ddea9
> Author:     Daniel Mack <daniel@caiaq.de>
> AuthorDate: Mon, 23 Mar 2009 17:10:37 +0100
> Committer:  Ingo Molnar <mingo@elte.hu>
> CommitDate: Mon, 23 Mar 2009 17:40:51 +0100
> 
> kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
> 
> Impact: fix currently inactive buffer-overflow
> 
> In kernel/trace/trace_functions_graph.c, print_graph_duration(),
> len can be as low as 1 or 2, which could make snprintf() write
> beyond the buffer bounds. (Found by cppcheck, no real-world bug
> occured)
> 
> Signed-off-by: Daniel Mack <daniel@caiaq.de>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Frederic Weisbecker <fweisbec@gmail.com>
> LKML-Reference: <1237824637-28190-1-git-send-email-daniel@caiaq.de>
> Signed-off-by: Ingo Molnar <mingo@elte.hu>
> 
> 
> ---
>  kernel/trace/trace_functions_graph.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)



Ingo, as I explained to Daniel before (I should have Cc you),
there is no overflow to protect here, so this patch will not
change anything.

This is my bad, I should better comment my code.

Frederic.


 
> diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c
> index e876816..a305472 100644
> --- a/kernel/trace/trace_functions_graph.c
> +++ b/kernel/trace/trace_functions_graph.c
> @@ -430,7 +430,7 @@ print_graph_duration(unsigned long long duration, struct trace_seq *s)
>  	unsigned long nsecs_rem = do_div(duration, 1000);
>  	/* log10(ULONG_MAX) + '\0' */
>  	char msecs_str[21];
> -	char nsecs_str[5];
> +	char nsecs_str[8];
>  	int ret, len;
>  	int i;
>  


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

* Re: [PATCH] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
  2009-03-23 16:40 ` Frederic Weisbecker
@ 2009-03-23 16:57   ` Daniel Mack
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Mack @ 2009-03-23 16:57 UTC (permalink / raw)
  To: Frederic Weisbecker; +Cc: linux-kernel

Hi Frederic,

On Mon, Mar 23, 2009 at 05:40:31PM +0100, Frederic Weisbecker wrote:
> It can't really happen because nsecs_rem is a rest of a division
> per 1000, so it will not exceed 3 digits (so it should be [4] and not
> [5], btw).
> 
> I must confess I should have added a better comment on this
> area.
> 
> We are printing a duration in usec with a part in nsec with the following
> constraints:
> 
> _ never exceed 7 characters unless we are are upper 9999999 usecs
> _ always keep the usecs consistants
> 
> Which means that while we have 4 or lesser digits for the usecs, we can
> print the 3 digits of the nanosecs.
> 
> If we need 5 for usecs, drop the least significant nanosec digit.
> If we need 6 for usecs, drop the two least significant nanosec digits

Ok, I see. I was just confused about the max len argument to snprintf()
(and so was cppcheck), but at a closer look, this can't become a
problem.

Nevermind, drop the patch - but good that we talked about it :)

Daniel


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

* Re: [tip:tracing/ftrace] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
  2009-03-23 16:47   ` Frederic Weisbecker
@ 2009-03-23 17:15     ` Ingo Molnar
  0 siblings, 0 replies; 6+ messages in thread
From: Ingo Molnar @ 2009-03-23 17:15 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: mingo, hpa, linux-kernel, rostedt, tglx, daniel, linux-tip-commits


* Frederic Weisbecker <fweisbec@gmail.com> wrote:

> On Mon, Mar 23, 2009 at 04:42:27PM +0000, Daniel Mack wrote:
> > Commit-ID:  603b9b9081ae0a1af986b9059a0a5055876ddea9
> > Gitweb:     http://git.kernel.org/tip/603b9b9081ae0a1af986b9059a0a5055876ddea9
> > Author:     Daniel Mack <daniel@caiaq.de>
> > AuthorDate: Mon, 23 Mar 2009 17:10:37 +0100
> > Committer:  Ingo Molnar <mingo@elte.hu>
> > CommitDate: Mon, 23 Mar 2009 17:40:51 +0100
> > 
> > kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size
> > 
> > Impact: fix currently inactive buffer-overflow
> > 
> > In kernel/trace/trace_functions_graph.c, print_graph_duration(),
> > len can be as low as 1 or 2, which could make snprintf() write
> > beyond the buffer bounds. (Found by cppcheck, no real-world bug
> > occured)
> > 
> > Signed-off-by: Daniel Mack <daniel@caiaq.de>
> > Cc: Steven Rostedt <rostedt@goodmis.org>
> > Cc: Frederic Weisbecker <fweisbec@gmail.com>
> > LKML-Reference: <1237824637-28190-1-git-send-email-daniel@caiaq.de>
> > Signed-off-by: Ingo Molnar <mingo@elte.hu>
> > 
> > 
> > ---
> >  kernel/trace/trace_functions_graph.c |    2 +-
> >  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> 
> 
> Ingo, as I explained to Daniel before (I should have Cc you),
> there is no overflow to protect here, so this patch will not
> change anything.
> 
> This is my bad, I should better comment my code.

Yeah - saw your reply, and zapped the commit already. Please add a 
comment in any case.

	Ingo

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

end of thread, other threads:[~2009-03-23 17:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-23 16:10 [PATCH] kernel/trace/trace_functions_graph.c: fix nsecs_str buffer size Daniel Mack
2009-03-23 16:40 ` Frederic Weisbecker
2009-03-23 16:57   ` Daniel Mack
2009-03-23 16:42 ` [tip:tracing/ftrace] " Daniel Mack
2009-03-23 16:47   ` Frederic Weisbecker
2009-03-23 17:15     ` Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).