public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/xen: avoid freeing static 'name' when kasprintf() fails
@ 2015-01-05 13:06 Vitaly Kuznetsov
  2015-01-05 13:19 ` [Xen-devel] " David Vrabel
  2015-01-05 15:27 ` [PATCH v2] " Vitaly Kuznetsov
  0 siblings, 2 replies; 6+ messages in thread
From: Vitaly Kuznetsov @ 2015-01-05 13:06 UTC (permalink / raw)
  To: x86
  Cc: Konrad Rzeszutek Wilk, Boris Ostrovsky, David Vrabel,
	Thomas Gleixner, Ingo Molnar, H. Peter Anvin, xen-devel,
	linux-kernel, Laszlo Ersek, Andrew Jones

In case kasprintf() fails in xen_setup_timer() we assign name to the static
string "<timer kasprintf failed>". We, however, don't check that fact before
issuing kfree() in xen_teardown_timer(), kernel is supposed to crash with
'kernel BUG at mm/slub.c:3341!'

Solve the issue by making name a fixed length string inside struct
xen_clock_event_device. 16 bytes should be enough.

The issue was discovered by Laszlo Ersek.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 arch/x86/xen/time.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c
index f473d26..221ebb6 100644
--- a/arch/x86/xen/time.c
+++ b/arch/x86/xen/time.c
@@ -391,7 +391,7 @@ static const struct clock_event_device *xen_clockevent =
 
 struct xen_clock_event_device {
 	struct clock_event_device evt;
-	char *name;
+	char name[16];
 };
 static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
 
@@ -420,14 +420,11 @@ void xen_teardown_timer(int cpu)
 	if (evt->irq >= 0) {
 		unbind_from_irqhandler(evt->irq, NULL);
 		evt->irq = -1;
-		kfree(per_cpu(xen_clock_events, cpu).name);
-		per_cpu(xen_clock_events, cpu).name = NULL;
 	}
 }
 
 void xen_setup_timer(int cpu)
 {
-	char *name;
 	struct clock_event_device *evt;
 	int irq;
 
@@ -438,21 +435,19 @@ void xen_setup_timer(int cpu)
 
 	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
 
-	name = kasprintf(GFP_KERNEL, "timer%d", cpu);
-	if (!name)
-		name = "<timer kasprintf failed>";
+	snprintf(per_cpu(xen_clock_events, cpu).name, 16, "timer%d", cpu);
 
 	irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
 				      IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
 				      IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
-				      name, NULL);
+				      per_cpu(xen_clock_events, cpu).name,
+				      NULL);
 	(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
 
 	memcpy(evt, xen_clockevent, sizeof(*evt));
 
 	evt->cpumask = cpumask_of(cpu);
 	evt->irq = irq;
-	per_cpu(xen_clock_events, cpu).name = name;
 }
 
 
-- 
1.9.3


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

* Re: [Xen-devel] [PATCH] x86/xen: avoid freeing static 'name' when kasprintf() fails
  2015-01-05 13:06 [PATCH] x86/xen: avoid freeing static 'name' when kasprintf() fails Vitaly Kuznetsov
@ 2015-01-05 13:19 ` David Vrabel
  2015-01-05 13:58   ` Laszlo Ersek
  2015-01-05 15:27 ` [PATCH v2] " Vitaly Kuznetsov
  1 sibling, 1 reply; 6+ messages in thread
From: David Vrabel @ 2015-01-05 13:19 UTC (permalink / raw)
  To: Vitaly Kuznetsov, x86
  Cc: Andrew Jones, linux-kernel, Ingo Molnar, David Vrabel,
	H. Peter Anvin, xen-devel, Boris Ostrovsky, Laszlo Ersek,
	Thomas Gleixner

On 05/01/15 13:06, Vitaly Kuznetsov wrote:
> In case kasprintf() fails in xen_setup_timer() we assign name to the static
> string "<timer kasprintf failed>". We, however, don't check that fact before
> issuing kfree() in xen_teardown_timer(), kernel is supposed to crash with
> 'kernel BUG at mm/slub.c:3341!'
> 
> Solve the issue by making name a fixed length string inside struct
> xen_clock_event_device. 16 bytes should be enough.
> 
> The issue was discovered by Laszlo Ersek.

Add Reported-by: Laszlo ... tag perhaps?

> --- a/arch/x86/xen/time.c
> +++ b/arch/x86/xen/time.c
> @@ -391,7 +391,7 @@ static const struct clock_event_device *xen_clockevent =
>  
>  struct xen_clock_event_device {
>  	struct clock_event_device evt;
> -	char *name;
> +	char name[16];
>  };
>  static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
>  
> @@ -420,14 +420,11 @@ void xen_teardown_timer(int cpu)
>  	if (evt->irq >= 0) {
>  		unbind_from_irqhandler(evt->irq, NULL);
>  		evt->irq = -1;
> -		kfree(per_cpu(xen_clock_events, cpu).name);
> -		per_cpu(xen_clock_events, cpu).name = NULL;
>  	}
>  }
>  
>  void xen_setup_timer(int cpu)
>  {
> -	char *name;
>  	struct clock_event_device *evt;

struct xen_clock_event_device *xevt = ...;

>  	int irq;
>  
> @@ -438,21 +435,19 @@ void xen_setup_timer(int cpu)
>  
>  	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
>  
> -	name = kasprintf(GFP_KERNEL, "timer%d", cpu);
> -	if (!name)
> -		name = "<timer kasprintf failed>";
> +	snprintf(per_cpu(xen_clock_events, cpu).name, 16, "timer%d", cpu);

Use sizeof(xevt->name) here.

David

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

* Re: [Xen-devel] [PATCH] x86/xen: avoid freeing static 'name' when kasprintf() fails
  2015-01-05 13:19 ` [Xen-devel] " David Vrabel
@ 2015-01-05 13:58   ` Laszlo Ersek
  0 siblings, 0 replies; 6+ messages in thread
From: Laszlo Ersek @ 2015-01-05 13:58 UTC (permalink / raw)
  To: David Vrabel, Vitaly Kuznetsov
  Cc: x86, Andrew Jones, linux-kernel, Ingo Molnar, H. Peter Anvin,
	xen-devel, Boris Ostrovsky, Thomas Gleixner

On 01/05/15 14:19, David Vrabel wrote:
> On 05/01/15 13:06, Vitaly Kuznetsov wrote:
>> In case kasprintf() fails in xen_setup_timer() we assign name to the static
>> string "<timer kasprintf failed>". We, however, don't check that fact before
>> issuing kfree() in xen_teardown_timer(), kernel is supposed to crash with
>> 'kernel BUG at mm/slub.c:3341!'
>>
>> Solve the issue by making name a fixed length string inside struct
>> xen_clock_event_device. 16 bytes should be enough.
>>
>> The issue was discovered by Laszlo Ersek.
> 
> Add Reported-by: Laszlo ... tag perhaps?
> 
>> --- a/arch/x86/xen/time.c
>> +++ b/arch/x86/xen/time.c
>> @@ -391,7 +391,7 @@ static const struct clock_event_device *xen_clockevent =
>>  
>>  struct xen_clock_event_device {
>>  	struct clock_event_device evt;
>> -	char *name;
>> +	char name[16];
>>  };
>>  static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
>>  
>> @@ -420,14 +420,11 @@ void xen_teardown_timer(int cpu)
>>  	if (evt->irq >= 0) {
>>  		unbind_from_irqhandler(evt->irq, NULL);
>>  		evt->irq = -1;
>> -		kfree(per_cpu(xen_clock_events, cpu).name);
>> -		per_cpu(xen_clock_events, cpu).name = NULL;
>>  	}
>>  }
>>  
>>  void xen_setup_timer(int cpu)
>>  {
>> -	char *name;
>>  	struct clock_event_device *evt;
> 
> struct xen_clock_event_device *xevt = ...;
> 
>>  	int irq;
>>  
>> @@ -438,21 +435,19 @@ void xen_setup_timer(int cpu)
>>  
>>  	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
>>  
>> -	name = kasprintf(GFP_KERNEL, "timer%d", cpu);
>> -	if (!name)
>> -		name = "<timer kasprintf failed>";
>> +	snprintf(per_cpu(xen_clock_events, cpu).name, 16, "timer%d", cpu);
> 
> Use sizeof(xevt->name) here.

Yes, I wanted to recommend sizeof too.

Also the "standard" Reported-by tag would be nice.

Thanks!
Laszlo


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

* [PATCH v2] x86/xen: avoid freeing static 'name' when kasprintf() fails
  2015-01-05 13:06 [PATCH] x86/xen: avoid freeing static 'name' when kasprintf() fails Vitaly Kuznetsov
  2015-01-05 13:19 ` [Xen-devel] " David Vrabel
@ 2015-01-05 15:27 ` Vitaly Kuznetsov
  2015-01-05 15:36   ` Laszlo Ersek
  2015-01-08 14:36   ` [Xen-devel] " David Vrabel
  1 sibling, 2 replies; 6+ messages in thread
From: Vitaly Kuznetsov @ 2015-01-05 15:27 UTC (permalink / raw)
  To: x86
  Cc: Konrad Rzeszutek Wilk, Boris Ostrovsky, David Vrabel,
	Thomas Gleixner, Ingo Molnar, H. Peter Anvin, xen-devel,
	linux-kernel, Laszlo Ersek, Andrew Jones

In case kasprintf() fails in xen_setup_timer() we assign name to the static
string "<timer kasprintf failed>". We, however, don't check that fact before
issuing kfree() in xen_teardown_timer(), kernel is supposed to crash with
'kernel BUG at mm/slub.c:3341!'

Solve the issue by making name a fixed length string inside struct
xen_clock_event_device. 16 bytes should be enough.

Suggested-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
Changes from v1 (David Vrabel):
- add 'struct xen_clock_event_device *xevt' for covenience
- sizeof(xevt->name) in snprintf() call
- Suggested-by: Laszlo Ersek
---
 arch/x86/xen/time.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c
index f473d26..9f743f4 100644
--- a/arch/x86/xen/time.c
+++ b/arch/x86/xen/time.c
@@ -391,7 +391,7 @@ static const struct clock_event_device *xen_clockevent =
 
 struct xen_clock_event_device {
 	struct clock_event_device evt;
-	char *name;
+	char name[16];
 };
 static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
 
@@ -420,39 +420,33 @@ void xen_teardown_timer(int cpu)
 	if (evt->irq >= 0) {
 		unbind_from_irqhandler(evt->irq, NULL);
 		evt->irq = -1;
-		kfree(per_cpu(xen_clock_events, cpu).name);
-		per_cpu(xen_clock_events, cpu).name = NULL;
 	}
 }
 
 void xen_setup_timer(int cpu)
 {
-	char *name;
-	struct clock_event_device *evt;
+	struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
+	struct clock_event_device *evt = &xevt->evt;
 	int irq;
 
-	evt = &per_cpu(xen_clock_events, cpu).evt;
 	WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
 	if (evt->irq >= 0)
 		xen_teardown_timer(cpu);
 
 	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
 
-	name = kasprintf(GFP_KERNEL, "timer%d", cpu);
-	if (!name)
-		name = "<timer kasprintf failed>";
+	snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
 
 	irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
 				      IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
 				      IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
-				      name, NULL);
+				      xevt->name, NULL);
 	(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
 
 	memcpy(evt, xen_clockevent, sizeof(*evt));
 
 	evt->cpumask = cpumask_of(cpu);
 	evt->irq = irq;
-	per_cpu(xen_clock_events, cpu).name = name;
 }
 
 
-- 
1.9.3


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

* Re: [PATCH v2] x86/xen: avoid freeing static 'name' when kasprintf() fails
  2015-01-05 15:27 ` [PATCH v2] " Vitaly Kuznetsov
@ 2015-01-05 15:36   ` Laszlo Ersek
  2015-01-08 14:36   ` [Xen-devel] " David Vrabel
  1 sibling, 0 replies; 6+ messages in thread
From: Laszlo Ersek @ 2015-01-05 15:36 UTC (permalink / raw)
  To: Vitaly Kuznetsov, x86
  Cc: Konrad Rzeszutek Wilk, Boris Ostrovsky, David Vrabel,
	Thomas Gleixner, Ingo Molnar, H. Peter Anvin, xen-devel,
	linux-kernel, Andrew Jones

On 01/05/15 16:27, Vitaly Kuznetsov wrote:
> In case kasprintf() fails in xen_setup_timer() we assign name to the static
> string "<timer kasprintf failed>". We, however, don't check that fact before
> issuing kfree() in xen_teardown_timer(), kernel is supposed to crash with
> 'kernel BUG at mm/slub.c:3341!'
> 
> Solve the issue by making name a fixed length string inside struct
> xen_clock_event_device. 16 bytes should be enough.
> 
> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
> Changes from v1 (David Vrabel):
> - add 'struct xen_clock_event_device *xevt' for covenience
> - sizeof(xevt->name) in snprintf() call
> - Suggested-by: Laszlo Ersek
> ---
>  arch/x86/xen/time.c | 16 +++++-----------
>  1 file changed, 5 insertions(+), 11 deletions(-)
> 
> diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c
> index f473d26..9f743f4 100644
> --- a/arch/x86/xen/time.c
> +++ b/arch/x86/xen/time.c
> @@ -391,7 +391,7 @@ static const struct clock_event_device *xen_clockevent =
>  
>  struct xen_clock_event_device {
>  	struct clock_event_device evt;
> -	char *name;
> +	char name[16];
>  };
>  static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
>  
> @@ -420,39 +420,33 @@ void xen_teardown_timer(int cpu)
>  	if (evt->irq >= 0) {
>  		unbind_from_irqhandler(evt->irq, NULL);
>  		evt->irq = -1;
> -		kfree(per_cpu(xen_clock_events, cpu).name);
> -		per_cpu(xen_clock_events, cpu).name = NULL;
>  	}
>  }
>  
>  void xen_setup_timer(int cpu)
>  {
> -	char *name;
> -	struct clock_event_device *evt;
> +	struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
> +	struct clock_event_device *evt = &xevt->evt;
>  	int irq;
>  
> -	evt = &per_cpu(xen_clock_events, cpu).evt;
>  	WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
>  	if (evt->irq >= 0)
>  		xen_teardown_timer(cpu);
>  
>  	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
>  
> -	name = kasprintf(GFP_KERNEL, "timer%d", cpu);
> -	if (!name)
> -		name = "<timer kasprintf failed>";
> +	snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
>  
>  	irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
>  				      IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
>  				      IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
> -				      name, NULL);
> +				      xevt->name, NULL);
>  	(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
>  
>  	memcpy(evt, xen_clockevent, sizeof(*evt));
>  
>  	evt->cpumask = cpumask_of(cpu);
>  	evt->irq = irq;
> -	per_cpu(xen_clock_events, cpu).name = name;
>  }
>  
>  
> 

Looks good to me (but I defer to Xen people of course :))

Reviewed-by: Laszlo Ersek <lersek@redhat.com>

Thanks!
Laszlo

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

* Re: [Xen-devel] [PATCH v2] x86/xen: avoid freeing static 'name' when kasprintf() fails
  2015-01-05 15:27 ` [PATCH v2] " Vitaly Kuznetsov
  2015-01-05 15:36   ` Laszlo Ersek
@ 2015-01-08 14:36   ` David Vrabel
  1 sibling, 0 replies; 6+ messages in thread
From: David Vrabel @ 2015-01-08 14:36 UTC (permalink / raw)
  To: Vitaly Kuznetsov, x86
  Cc: Andrew Jones, linux-kernel, Ingo Molnar, David Vrabel,
	H. Peter Anvin, xen-devel, Boris Ostrovsky, Laszlo Ersek,
	Thomas Gleixner

On 05/01/15 15:27, Vitaly Kuznetsov wrote:
> In case kasprintf() fails in xen_setup_timer() we assign name to the static
> string "<timer kasprintf failed>". We, however, don't check that fact before
> issuing kfree() in xen_teardown_timer(), kernel is supposed to crash with
> 'kernel BUG at mm/slub.c:3341!'
> 
> Solve the issue by making name a fixed length string inside struct
> xen_clock_event_device. 16 bytes should be enough.

Applied to stable/for-linus-3.19, thanks.

David

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

end of thread, other threads:[~2015-01-08 14:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-05 13:06 [PATCH] x86/xen: avoid freeing static 'name' when kasprintf() fails Vitaly Kuznetsov
2015-01-05 13:19 ` [Xen-devel] " David Vrabel
2015-01-05 13:58   ` Laszlo Ersek
2015-01-05 15:27 ` [PATCH v2] " Vitaly Kuznetsov
2015-01-05 15:36   ` Laszlo Ersek
2015-01-08 14:36   ` [Xen-devel] " David Vrabel

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