* [PATCH] trace-cmd: append to CFLAGS instead of being overriden @ 2010-06-13 17:11 Chase Douglas 2010-06-13 17:11 ` [PATCH] trace-cmd: prevent print_graph_duration buffer overflow Chase Douglas 0 siblings, 1 reply; 10+ messages in thread From: Chase Douglas @ 2010-06-13 17:11 UTC (permalink / raw) To: Steven Rostedt; +Cc: linux-kernel Most package builders apply their own CFLAGS, often set during the make invocation. The trace-cmd internal CFLAGS is overriden in this case. Make sure the important flags are appended. Signed-off-by: Chase Douglas <chase.douglas@canonical.com> --- Makefile | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-) diff --git a/Makefile b/Makefile index a278510..08fc4ca 100644 --- a/Makefile +++ b/Makefile @@ -183,7 +183,11 @@ KERNELSHARK_VERSION = $(KS_VERSION).$(KS_PATCHLEVEL).$(KS_EXTRAVERSION) INCLUDES = -I. -I/usr/local/include $(CONFIG_INCLUDES) -CFLAGS = -g -Wall $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) +# Set compile option CFLAGS if not set elsewhere +CFLAGS ?= -g -Wall + +# Append required CFLAGS +override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) ifeq ($(VERBOSE),1) Q = -- 1.7.0.4 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-13 17:11 [PATCH] trace-cmd: append to CFLAGS instead of being overriden Chase Douglas @ 2010-06-13 17:11 ` Chase Douglas 2010-06-13 20:52 ` Valdis.Kletnieks 0 siblings, 1 reply; 10+ messages in thread From: Chase Douglas @ 2010-06-13 17:11 UTC (permalink / raw) To: Steven Rostedt; +Cc: linux-kernel Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow condition. We know the exact size of nsecs_str, so use it instead of math that may overflow. Signed-off-by: Chase Douglas <chase.douglas@canonical.com> --- trace-ftrace.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/trace-ftrace.c b/trace-ftrace.c index af9ac8d..ee7c6dc 100644 --- a/trace-ftrace.c +++ b/trace-ftrace.c @@ -148,7 +148,7 @@ static void print_graph_duration(struct trace_seq *s, unsigned long long duratio /* Print nsecs (we don't want to exceed 7 numbers) */ if ((s->len - len) < 7) { - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); trace_seq_printf(s, ".%s", nsecs_str); } -- 1.7.0.4 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-13 17:11 ` [PATCH] trace-cmd: prevent print_graph_duration buffer overflow Chase Douglas @ 2010-06-13 20:52 ` Valdis.Kletnieks 2010-06-13 21:01 ` Chase Douglas 0 siblings, 1 reply; 10+ messages in thread From: Valdis.Kletnieks @ 2010-06-13 20:52 UTC (permalink / raw) To: Chase Douglas; +Cc: Steven Rostedt, linux-kernel [-- Attachment #1: Type: text/plain, Size: 735 bytes --] On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > condition. We know the exact size of nsecs_str, so use it instead of > math that may overflow. > /* Print nsecs (we don't want to exceed 7 numbers) */ > if ((s->len - len) < 7) { > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); We only get into this code after we've checked that the length is under 7 characters. How much overflow can happen as long as the sizeof(nsecs_str) is a sane size (like at least 8 chars)? Probably a better bet would be doing the right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? [-- Attachment #2: Type: application/pgp-signature, Size: 227 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-13 20:52 ` Valdis.Kletnieks @ 2010-06-13 21:01 ` Chase Douglas 2010-06-14 21:40 ` Valdis.Kletnieks 0 siblings, 1 reply; 10+ messages in thread From: Chase Douglas @ 2010-06-13 21:01 UTC (permalink / raw) To: Valdis.Kletnieks; +Cc: Steven Rostedt, linux-kernel On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > condition. We know the exact size of nsecs_str, so use it instead of > > math that may overflow. > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > if ((s->len - len) < 7) { > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > We only get into this code after we've checked that the length is under 7 > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > sane size (like at least 8 chars)? Probably a better bet would be doing the > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? nsecs_str is a local variable defined just above this block of code as: char nsecs_str[5]; I was hitting cases where s->len == 64 and len == 63, leading to the size argument of snprintf being 7 on a 5 byte string. I didn't delve too much into the reasoning for the if statement, but I think it's math is not actually related to the size of nsecs_rem but to some other string length. -- Chase ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-13 21:01 ` Chase Douglas @ 2010-06-14 21:40 ` Valdis.Kletnieks 2010-06-15 0:16 ` Steven Rostedt 0 siblings, 1 reply; 10+ messages in thread From: Valdis.Kletnieks @ 2010-06-14 21:40 UTC (permalink / raw) To: Chase Douglas; +Cc: Steven Rostedt, linux-kernel [-- Attachment #1: Type: text/plain, Size: 1760 bytes --] On Sun, 13 Jun 2010 17:01:34 EDT, Chase Douglas said: > On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > > condition. We know the exact size of nsecs_str, so use it instead of > > > math that may overflow. > > > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > > if ((s->len - len) < 7) { > > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > > > We only get into this code after we've checked that the length is under 7 > > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > > sane size (like at least 8 chars)? Probably a better bet would be doing the > > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? > > nsecs_str is a local variable defined just above this block of code as: > > char nsecs_str[5]; > > I was hitting cases where s->len == 64 and len == 63, leading to the > size argument of snprintf being 7 on a 5 byte string. I didn't delve too > much into the reasoning for the if statement, but I think it's math is > not actually related to the size of nsecs_rem but to some other string > length. This is starting to smell like that patch is just papering over a bug... I saw that '8 -' and made the rash assumption that was the size of the array. Is 5 in fact big enough and the 's->len - len' calculation is broken, or should it be bigger? As you noted, that length calculation is looking a tad sketchy. (And if we're stuck with '5' because it's a magic number for somebody's formatting purposes, maybe it needs to be a #define?) [-- Attachment #2: Type: application/pgp-signature, Size: 227 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-14 21:40 ` Valdis.Kletnieks @ 2010-06-15 0:16 ` Steven Rostedt 2010-06-15 12:49 ` Frederic Weisbecker 0 siblings, 1 reply; 10+ messages in thread From: Steven Rostedt @ 2010-06-15 0:16 UTC (permalink / raw) To: Valdis.Kletnieks; +Cc: Chase Douglas, linux-kernel, Frederic Weisbecker On Mon, 2010-06-14 at 17:40 -0400, Valdis.Kletnieks@vt.edu wrote: > On Sun, 13 Jun 2010 17:01:34 EDT, Chase Douglas said: > > On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > > > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > > > condition. We know the exact size of nsecs_str, so use it instead of > > > > math that may overflow. > > > > > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > > > if ((s->len - len) < 7) { > > > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > > > > > We only get into this code after we've checked that the length is under 7 > > > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > > > sane size (like at least 8 chars)? Probably a better bet would be doing the > > > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? > > > > nsecs_str is a local variable defined just above this block of code as: > > > > char nsecs_str[5]; > > > > I was hitting cases where s->len == 64 and len == 63, leading to the > > size argument of snprintf being 7 on a 5 byte string. I didn't delve too > > much into the reasoning for the if statement, but I think it's math is > > not actually related to the size of nsecs_rem but to some other string > > length. > > This is starting to smell like that patch is just papering over a bug... > > I saw that '8 -' and made the rash assumption that was the size of the array. > Is 5 in fact big enough and the 's->len - len' calculation is broken, or > should it be bigger? As you noted, that length calculation is looking a tad > sketchy. (And if we're stuck with '5' because it's a magic number for > somebody's formatting purposes, maybe it needs to be a #define?) > Ouch, this is worse than that. this code was cut & pasted almost directly from the Linux kernel (kernel/trace/trace_function_graph.c). And it looks like any bug here is also a bug there. The difference is that if we trigger the bug there we crash the kernel :-p -- Steve ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-15 0:16 ` Steven Rostedt @ 2010-06-15 12:49 ` Frederic Weisbecker 2010-06-15 13:04 ` Chase Douglas 0 siblings, 1 reply; 10+ messages in thread From: Frederic Weisbecker @ 2010-06-15 12:49 UTC (permalink / raw) To: Steven Rostedt; +Cc: Valdis.Kletnieks, Chase Douglas, linux-kernel On Mon, Jun 14, 2010 at 08:16:03PM -0400, Steven Rostedt wrote: > On Mon, 2010-06-14 at 17:40 -0400, Valdis.Kletnieks@vt.edu wrote: > > On Sun, 13 Jun 2010 17:01:34 EDT, Chase Douglas said: > > > On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > > > > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > > > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > > > > condition. We know the exact size of nsecs_str, so use it instead of > > > > > math that may overflow. > > > > > > > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > > > > if ((s->len - len) < 7) { > > > > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > > > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > > > > > > > We only get into this code after we've checked that the length is under 7 > > > > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > > > > sane size (like at least 8 chars)? Probably a better bet would be doing the > > > > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? > > > > > > nsecs_str is a local variable defined just above this block of code as: > > > > > > char nsecs_str[5]; > > > > > > I was hitting cases where s->len == 64 and len == 63, leading to the > > > size argument of snprintf being 7 on a 5 byte string. I didn't delve too > > > much into the reasoning for the if statement, but I think it's math is > > > not actually related to the size of nsecs_rem but to some other string > > > length. > > > > This is starting to smell like that patch is just papering over a bug... > > > > I saw that '8 -' and made the rash assumption that was the size of the array. > > Is 5 in fact big enough and the 's->len - len' calculation is broken, or > > should it be bigger? As you noted, that length calculation is looking a tad > > sketchy. (And if we're stuck with '5' because it's a magic number for > > somebody's formatting purposes, maybe it needs to be a #define?) > > > > Ouch, this is worse than that. this code was cut & pasted almost > directly from the Linux kernel (kernel/trace/trace_function_graph.c). > And it looks like any bug here is also a bug there. The difference is > that if we trigger the bug there we crash the kernel :-p I must be missing the purpose of this patch. log10(nsecs_rem) can't exceed 3 characters as it is the rest of a division per 1000. The goal of this: if (len < 7) { snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem) is to avoid having a duration that exceeds 7 characters, so formatted nsecs be shrinked on need. For example: 75000.567 would be shrinked to 75000.56, and that's the point. if (len < 7) is not a security guard, it is a formatting convenience to get a fixed column length. The security guard is the mathematics that tells us log10(n % 1000) < 4. In fact nsecs_str could be even of size 4 rather than 5. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-15 12:49 ` Frederic Weisbecker @ 2010-06-15 13:04 ` Chase Douglas 2010-06-15 13:10 ` Frederic Weisbecker 0 siblings, 1 reply; 10+ messages in thread From: Chase Douglas @ 2010-06-15 13:04 UTC (permalink / raw) To: Frederic Weisbecker; +Cc: Steven Rostedt, Valdis.Kletnieks, linux-kernel On Tue, 2010-06-15 at 14:49 +0200, Frederic Weisbecker wrote: > On Mon, Jun 14, 2010 at 08:16:03PM -0400, Steven Rostedt wrote: > > On Mon, 2010-06-14 at 17:40 -0400, Valdis.Kletnieks@vt.edu wrote: > > > On Sun, 13 Jun 2010 17:01:34 EDT, Chase Douglas said: > > > > On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > > > > > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > > > > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > > > > > condition. We know the exact size of nsecs_str, so use it instead of > > > > > > math that may overflow. > > > > > > > > > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > > > > > if ((s->len - len) < 7) { > > > > > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > > > > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > > > > > > > > > We only get into this code after we've checked that the length is under 7 > > > > > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > > > > > sane size (like at least 8 chars)? Probably a better bet would be doing the > > > > > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? > > > > > > > > nsecs_str is a local variable defined just above this block of code as: > > > > > > > > char nsecs_str[5]; > > > > > > > > I was hitting cases where s->len == 64 and len == 63, leading to the > > > > size argument of snprintf being 7 on a 5 byte string. I didn't delve too > > > > much into the reasoning for the if statement, but I think it's math is > > > > not actually related to the size of nsecs_rem but to some other string > > > > length. > > > > > > This is starting to smell like that patch is just papering over a bug... > > > > > > I saw that '8 -' and made the rash assumption that was the size of the array. > > > Is 5 in fact big enough and the 's->len - len' calculation is broken, or > > > should it be bigger? As you noted, that length calculation is looking a tad > > > sketchy. (And if we're stuck with '5' because it's a magic number for > > > somebody's formatting purposes, maybe it needs to be a #define?) > > > > > > > Ouch, this is worse than that. this code was cut & pasted almost > > directly from the Linux kernel (kernel/trace/trace_function_graph.c). > > And it looks like any bug here is also a bug there. The difference is > > that if we trigger the bug there we crash the kernel :-p > > > I must be missing the purpose of this patch. > > log10(nsecs_rem) can't exceed 3 characters as it is the rest of > a division per 1000. > > The goal of this: > > if (len < 7) { > snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem) > > is to avoid having a duration that exceeds 7 characters, so formatted nsecs > be shrinked on need. > > For example: > > 75000.567 > > would be shrinked to 75000.56, and that's the point. > > if (len < 7) is not a security guard, it is a formatting convenience > to get a fixed column length. > > The security guard is the mathematics that tells us log10(n % 1000) < 4. > In fact nsecs_str could be even of size 4 rather than 5. I agree that there is no *real* security issue here because of the length of the string that snprintf would generate. However, glibc still barfs when you pass in a size parameter larger than the string. Without this patch, trace-cmd is unusable for me; glibc aborts as soon as the condition is hit. I found this as I was packaging trace-cmd for Ubuntu, so maybe glibc in other distributions is behaving differently? -- Chase ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-15 13:04 ` Chase Douglas @ 2010-06-15 13:10 ` Frederic Weisbecker 2010-06-15 13:20 ` Chase Douglas 0 siblings, 1 reply; 10+ messages in thread From: Frederic Weisbecker @ 2010-06-15 13:10 UTC (permalink / raw) To: Chase Douglas; +Cc: Steven Rostedt, Valdis.Kletnieks, linux-kernel On Tue, Jun 15, 2010 at 09:04:58AM -0400, Chase Douglas wrote: > On Tue, 2010-06-15 at 14:49 +0200, Frederic Weisbecker wrote: > > On Mon, Jun 14, 2010 at 08:16:03PM -0400, Steven Rostedt wrote: > > > On Mon, 2010-06-14 at 17:40 -0400, Valdis.Kletnieks@vt.edu wrote: > > > > On Sun, 13 Jun 2010 17:01:34 EDT, Chase Douglas said: > > > > > On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > > > > > > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > > > > > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > > > > > > condition. We know the exact size of nsecs_str, so use it instead of > > > > > > > math that may overflow. > > > > > > > > > > > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > > > > > > if ((s->len - len) < 7) { > > > > > > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > > > > > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > > > > > > > > > > > We only get into this code after we've checked that the length is under 7 > > > > > > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > > > > > > sane size (like at least 8 chars)? Probably a better bet would be doing the > > > > > > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? > > > > > > > > > > nsecs_str is a local variable defined just above this block of code as: > > > > > > > > > > char nsecs_str[5]; > > > > > > > > > > I was hitting cases where s->len == 64 and len == 63, leading to the > > > > > size argument of snprintf being 7 on a 5 byte string. I didn't delve too > > > > > much into the reasoning for the if statement, but I think it's math is > > > > > not actually related to the size of nsecs_rem but to some other string > > > > > length. > > > > > > > > This is starting to smell like that patch is just papering over a bug... > > > > > > > > I saw that '8 -' and made the rash assumption that was the size of the array. > > > > Is 5 in fact big enough and the 's->len - len' calculation is broken, or > > > > should it be bigger? As you noted, that length calculation is looking a tad > > > > sketchy. (And if we're stuck with '5' because it's a magic number for > > > > somebody's formatting purposes, maybe it needs to be a #define?) > > > > > > > > > > Ouch, this is worse than that. this code was cut & pasted almost > > > directly from the Linux kernel (kernel/trace/trace_function_graph.c). > > > And it looks like any bug here is also a bug there. The difference is > > > that if we trigger the bug there we crash the kernel :-p > > > > > > I must be missing the purpose of this patch. > > > > log10(nsecs_rem) can't exceed 3 characters as it is the rest of > > a division per 1000. > > > > The goal of this: > > > > if (len < 7) { > > snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem) > > > > is to avoid having a duration that exceeds 7 characters, so formatted nsecs > > be shrinked on need. > > > > For example: > > > > 75000.567 > > > > would be shrinked to 75000.56, and that's the point. > > > > if (len < 7) is not a security guard, it is a formatting convenience > > to get a fixed column length. > > > > The security guard is the mathematics that tells us log10(n % 1000) < 4. > > In fact nsecs_str could be even of size 4 rather than 5. > > I agree that there is no *real* security issue here because of the > length of the string that snprintf would generate. However, glibc still > barfs when you pass in a size parameter larger than the string. Without > this patch, trace-cmd is unusable for me; glibc aborts as soon as the > condition is hit. I found this as I was packaging trace-cmd for Ubuntu, > so maybe glibc in other distributions is behaving differently? Ah, I see what you mean. So the check is made on runtime, right? But your patch breaks the nsec adaptive size reduction that keeps a fixed column size. What about: if (len < 7) { - snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem); + snprintf(nsecs_str, min(sizeof(nsecs_rem), 8 - len), "%03lu", nsecs_rem); ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] trace-cmd: prevent print_graph_duration buffer overflow 2010-06-15 13:10 ` Frederic Weisbecker @ 2010-06-15 13:20 ` Chase Douglas 0 siblings, 0 replies; 10+ messages in thread From: Chase Douglas @ 2010-06-15 13:20 UTC (permalink / raw) To: Frederic Weisbecker; +Cc: Steven Rostedt, Valdis.Kletnieks, linux-kernel On Tue, 2010-06-15 at 15:10 +0200, Frederic Weisbecker wrote: > On Tue, Jun 15, 2010 at 09:04:58AM -0400, Chase Douglas wrote: > > On Tue, 2010-06-15 at 14:49 +0200, Frederic Weisbecker wrote: > > > On Mon, Jun 14, 2010 at 08:16:03PM -0400, Steven Rostedt wrote: > > > > On Mon, 2010-06-14 at 17:40 -0400, Valdis.Kletnieks@vt.edu wrote: > > > > > On Sun, 13 Jun 2010 17:01:34 EDT, Chase Douglas said: > > > > > > On Sun, 2010-06-13 at 16:52 -0400, Valdis.Kletnieks@vt.edu wrote: > > > > > > > On Sun, 13 Jun 2010 13:11:48 EDT, Chase Douglas said: > > > > > > > > Passing n > sizeof(string) to snprintf can cause a glibc buffer overflow > > > > > > > > condition. We know the exact size of nsecs_str, so use it instead of > > > > > > > > math that may overflow. > > > > > > > > > > > > > > > /* Print nsecs (we don't want to exceed 7 numbers) */ > > > > > > > > if ((s->len - len) < 7) { > > > > > > > > - snprintf(nsecs_str, 8 - (s->len - len), "%03lu", nsecs_rem); > > > > > > > > + snprintf(nsecs_str, sizeof(nsecs_str), "%03lu", nsecs_rem); > > > > > > > > > > > > > > We only get into this code after we've checked that the length is under 7 > > > > > > > characters. How much overflow can happen as long as the sizeof(nsecs_str) is a > > > > > > > sane size (like at least 8 chars)? Probably a better bet would be doing the > > > > > > > right thing and 'BUILD_BUG_ON(sizeof(nsecs_str) < 8);'? > > > > > > > > > > > > nsecs_str is a local variable defined just above this block of code as: > > > > > > > > > > > > char nsecs_str[5]; > > > > > > > > > > > > I was hitting cases where s->len == 64 and len == 63, leading to the > > > > > > size argument of snprintf being 7 on a 5 byte string. I didn't delve too > > > > > > much into the reasoning for the if statement, but I think it's math is > > > > > > not actually related to the size of nsecs_rem but to some other string > > > > > > length. > > > > > > > > > > This is starting to smell like that patch is just papering over a bug... > > > > > > > > > > I saw that '8 -' and made the rash assumption that was the size of the array. > > > > > Is 5 in fact big enough and the 's->len - len' calculation is broken, or > > > > > should it be bigger? As you noted, that length calculation is looking a tad > > > > > sketchy. (And if we're stuck with '5' because it's a magic number for > > > > > somebody's formatting purposes, maybe it needs to be a #define?) > > > > > > > > > > > > > Ouch, this is worse than that. this code was cut & pasted almost > > > > directly from the Linux kernel (kernel/trace/trace_function_graph.c). > > > > And it looks like any bug here is also a bug there. The difference is > > > > that if we trigger the bug there we crash the kernel :-p > > > > > > > > > I must be missing the purpose of this patch. > > > > > > log10(nsecs_rem) can't exceed 3 characters as it is the rest of > > > a division per 1000. > > > > > > The goal of this: > > > > > > if (len < 7) { > > > snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem) > > > > > > is to avoid having a duration that exceeds 7 characters, so formatted nsecs > > > be shrinked on need. > > > > > > For example: > > > > > > 75000.567 > > > > > > would be shrinked to 75000.56, and that's the point. > > > > > > if (len < 7) is not a security guard, it is a formatting convenience > > > to get a fixed column length. > > > > > > The security guard is the mathematics that tells us log10(n % 1000) < 4. > > > In fact nsecs_str could be even of size 4 rather than 5. > > > > I agree that there is no *real* security issue here because of the > > length of the string that snprintf would generate. However, glibc still > > barfs when you pass in a size parameter larger than the string. Without > > this patch, trace-cmd is unusable for me; glibc aborts as soon as the > > condition is hit. I found this as I was packaging trace-cmd for Ubuntu, > > so maybe glibc in other distributions is behaving differently? > > > Ah, I see what you mean. So the check is made on runtime, right? > But your patch breaks the nsec adaptive size reduction that keeps a fixed > column size. > > What about: > > if (len < 7) { > - snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem); > + snprintf(nsecs_str, min(sizeof(nsecs_rem), 8 - len), "%03lu", nsecs_rem); That seems good to me, I'll send another patch around for trace-cmd. Thanks, -- Chase ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2010-06-15 13:20 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2010-06-13 17:11 [PATCH] trace-cmd: append to CFLAGS instead of being overriden Chase Douglas 2010-06-13 17:11 ` [PATCH] trace-cmd: prevent print_graph_duration buffer overflow Chase Douglas 2010-06-13 20:52 ` Valdis.Kletnieks 2010-06-13 21:01 ` Chase Douglas 2010-06-14 21:40 ` Valdis.Kletnieks 2010-06-15 0:16 ` Steven Rostedt 2010-06-15 12:49 ` Frederic Weisbecker 2010-06-15 13:04 ` Chase Douglas 2010-06-15 13:10 ` Frederic Weisbecker 2010-06-15 13:20 ` Chase Douglas
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).