[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <tip-b832796caa1fda8516464a003c8c7cc547bc20c2@git.kernel.org>
Date: Wed, 14 Mar 2012 12:59:19 -0700
From: tip-bot for Anton Blanchard <anton@...ba.org>
To: linux-tip-commits@...r.kernel.org
Cc: acme@...hat.com, linux-kernel@...r.kernel.org, paulus@...ba.org,
anton@...ba.org, hpa@...or.com, mingo@...hat.com,
yanmin_zhang@...ux.intel.com, peterz@...radead.org,
emunson@...bm.net, fweisbec@...il.com, dsahern@...il.com,
tglx@...utronix.de, mingo@...e.hu
Subject: [tip:perf/urgent] perf tools:
Incorrect use of snprintf results in SEGV
Commit-ID: b832796caa1fda8516464a003c8c7cc547bc20c2
Gitweb: http://git.kernel.org/tip/b832796caa1fda8516464a003c8c7cc547bc20c2
Author: Anton Blanchard <anton@...ba.org>
AuthorDate: Wed, 7 Mar 2012 11:42:49 +1100
Committer: Arnaldo Carvalho de Melo <acme@...hat.com>
CommitDate: Wed, 14 Mar 2012 12:36:19 -0300
perf tools: Incorrect use of snprintf results in SEGV
I have a workload where perf top scribbles over the stack and we SEGV.
What makes it interesting is that an snprintf is causing this.
The workload is a c++ gem that has method names over 3000 characters
long, but snprintf is designed to avoid overrunning buffers. So what
went wrong?
The problem is we assume snprintf returns the number of characters
written:
ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", self->level);
...
ret += repsep_snprintf(bf + ret, size - ret, "%s", self->ms.sym->name);
Unfortunately this is not how snprintf works. snprintf returns the
number of characters that would have been written if there was enough
space. In the above case, if the first snprintf returns a value larger
than size, we pass a negative size into the second snprintf and happily
scribble over the stack. If you have 3000 character c++ methods thats a
lot of stack to trample.
This patch fixes repsep_snprintf by clamping the value at size - 1 which
is the maximum snprintf can write before adding the NULL terminator.
I get the sinking feeling that there are a lot of other uses of snprintf
that have this same bug, we should audit them all.
Cc: David Ahern <dsahern@...il.com>
Cc: Eric B Munson <emunson@...bm.net>
Cc: Frederic Weisbecker <fweisbec@...il.com>
Cc: Ingo Molnar <mingo@...e.hu>
Cc: Paul Mackerras <paulus@...ba.org>
Cc: Peter Zijlstra <peterz@...radead.org>
Cc: Yanmin Zhang <yanmin_zhang@...ux.intel.com>
Cc: stable@...nel.org
Link: http://lkml.kernel.org/r/20120307114249.44275ca3@kryten
Signed-off-by: Anton Blanchard <anton@...ba.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@...hat.com>
---
tools/perf/util/sort.c | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 16da30d..076c9d4 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -33,6 +33,9 @@ static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
}
}
va_end(ap);
+
+ if (n >= (int)size)
+ return size - 1;
return n;
}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists