public inbox for trinity@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix buffer overflow in output() when pid slot is not found
@ 2014-05-23  9:09 Michael Ellerman
  2014-05-24  0:51 ` Dave Jones
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Ellerman @ 2014-05-23  9:09 UTC (permalink / raw)
  To: trinity

In output() we sprintf() the result of find_pid_slot(). We print the pid
slot to the buffer with %u and have space for two digits of pid slot.
find_pid_slot() potentially returns PIDSLOT_NOT_FOUND (-1), which when
printed with %u is 4294967295 - ten digits.

Fix it two ways, use snprintf() - truncated output is better than a
buffer overflow. And allocate more space in the buffer, 32 bytes is a
nice round size, and gives us space for everything.
---
 log.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/log.c b/log.c
index 91b4f63..584901c 100644
--- a/log.c
+++ b/log.c
@@ -291,7 +291,7 @@ void output(unsigned char level, const char *fmt, ...)
 	char watchdog_prefix[]="[watchdog]";
 	char init_prefix[]="[init]";
 	char main_prefix[]="[main]";
-	char child_prefix[]="[childNN:1234567890]";
+	char child_prefix[32];
 
 	if (logging == FALSE && level >= quiet_level)
 		return;
@@ -311,7 +311,7 @@ void output(unsigned char level, const char *fmt, ...)
 		unsigned int slot;
 
 		slot = find_pid_slot(pid);
-		sprintf(child_prefix, "[child%u:%u]", slot, pid);
+		snprintf(child_prefix, sizeof(child_prefix), "[child%u:%u]", slot, pid);
 		prefix = child_prefix;
 	}
 
-- 
1.9.1

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

* Re: [PATCH] Fix buffer overflow in output() when pid slot is not found
  2014-05-23  9:09 [PATCH] Fix buffer overflow in output() when pid slot is not found Michael Ellerman
@ 2014-05-24  0:51 ` Dave Jones
  2014-05-26  5:11   ` Michael Ellerman
  0 siblings, 1 reply; 3+ messages in thread
From: Dave Jones @ 2014-05-24  0:51 UTC (permalink / raw)
  To: Michael Ellerman; +Cc: trinity

On Fri, May 23, 2014 at 07:09:03PM +1000, Michael Ellerman wrote:
 > In output() we sprintf() the result of find_pid_slot(). We print the pid
 > slot to the buffer with %u and have space for two digits of pid slot.
 > find_pid_slot() potentially returns PIDSLOT_NOT_FOUND (-1), which when
 > printed with %u is 4294967295 - ten digits.
 > 
 > Fix it two ways, use snprintf() - truncated output is better than a
 > buffer overflow. And allocate more space in the buffer, 32 bytes is a
 > nice round size, and gives us space for everything.

heh, this has been nagging me from time to time, but it wasn't a problem
until recently.   I'm curious why you're hitting that PIDSLOT_NOT_FOUND
case though, as it's a "should never happen" case.

Anyway, it's the right thing to do, so I pushed this out.

 > @@ -311,7 +311,7 @@ void output(unsigned char level, const char *fmt, ...)
 >  		unsigned int slot;
 >  
 >  		slot = find_pid_slot(pid);
 > -		sprintf(child_prefix, "[child%u:%u]", slot, pid);
 > +		snprintf(child_prefix, sizeof(child_prefix), "[child%u:%u]", slot, pid);
 >  		prefix = child_prefix;

might be worth it to add something later to print PIDSLOT_NOT_FOUND entries as '?'
rather than 4294967295.

thanks,

	Dave

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

* Re: [PATCH] Fix buffer overflow in output() when pid slot is not found
  2014-05-24  0:51 ` Dave Jones
@ 2014-05-26  5:11   ` Michael Ellerman
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Ellerman @ 2014-05-26  5:11 UTC (permalink / raw)
  To: Dave Jones; +Cc: trinity

On Fri, 2014-05-23 at 20:51 -0400, Dave Jones wrote:
> On Fri, May 23, 2014 at 07:09:03PM +1000, Michael Ellerman wrote:
>  > In output() we sprintf() the result of find_pid_slot(). We print the pid
>  > slot to the buffer with %u and have space for two digits of pid slot.
>  > find_pid_slot() potentially returns PIDSLOT_NOT_FOUND (-1), which when
>  > printed with %u is 4294967295 - ten digits.
>  > 
>  > Fix it two ways, use snprintf() - truncated output is better than a
>  > buffer overflow. And allocate more space in the buffer, 32 bytes is a
>  > nice round size, and gives us space for everything.
> 
> heh, this has been nagging me from time to time, but it wasn't a problem
> until recently.   I'm curious why you're hitting that PIDSLOT_NOT_FOUND
> case though, as it's a "should never happen" case.

Yeah, I figured it wasn't meant to happen.

> Anyway, it's the right thing to do, so I pushed this out.
> 
>  > @@ -311,7 +311,7 @@ void output(unsigned char level, const char *fmt, ...)
>  >  		unsigned int slot;
>  >  
>  >  		slot = find_pid_slot(pid);
>  > -		sprintf(child_prefix, "[child%u:%u]", slot, pid);
>  > +		snprintf(child_prefix, sizeof(child_prefix), "[child%u:%u]", slot, pid);
>  >  		prefix = child_prefix;
> 
> might be worth it to add something later to print PIDSLOT_NOT_FOUND entries as '?'
> rather than 4294967295.

I thought about it, but figured "4294967295" was actually a good eye-catcher.
If you'd prefer "?" then let me know and I can do a patch for that. I haven't
actually hit that case again though.

cheers


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

end of thread, other threads:[~2014-05-26  5:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-23  9:09 [PATCH] Fix buffer overflow in output() when pid slot is not found Michael Ellerman
2014-05-24  0:51 ` Dave Jones
2014-05-26  5:11   ` Michael Ellerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox