x86/xen: avoid freeing static 'name' when kasprintf() fails
Subsystems (2)
PATCH 1/1 x86/xen: avoid freeing static 'name' when kasprintf() fails
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.
--- arch/x86/xen/time.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/xen/time.c +4 -9
@@ -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; }
Signature · 1 line
1.9.3
Quoted 9 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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?
Quoted 14 lines
arch/x86/xen/time.c +1
-1@@ -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 );
arch/x86/xen/time.c void xen_teardown_timer(int cpu) · line 428
Quoted 10 lines
arch/x86/xen/time.c +0
-3evt->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 = ...;
arch/x86/xen/time.c void xen_setup_timer(int cpu) · line 438
Quoted 10 lines
arch/x86/xen/time.c +1
-3int 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
Quoted 44 lines · David Vrabel
Quoted 9 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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?
Quoted 14 lines
arch/x86/xen/time.c +1
-1@@ -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 );arch/x86/xen/time.c void xen_teardown_timer(int cpu) · line 428
Quoted 10 lines
arch/x86/xen/time.c +0
-3evt->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 = ...;Quoted 4 lines
arch/x86/xen/time.c +0
-0int irq;@@ -438,21 +435,19 @@ void xen_setup_timer(int cpu)
Quoted 8 lines
arch/x86/xen/time.c void xen_setup_timer(int cpu) · line 438
Quoted 6 lines
arch/x86/xen/time.c +1
-3printk (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
Quoted 44 lines · David Vrabel
Quoted 9 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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?
Quoted 14 lines
arch/x86/xen/time.c +1
-1@@ -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 );arch/x86/xen/time.c void xen_teardown_timer(int cpu) · line 428
Quoted 10 lines
arch/x86/xen/time.c +0
-3evt->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 = ...;Quoted 4 lines
arch/x86/xen/time.c +0
-0int irq;@@ -438,21 +435,19 @@ void xen_setup_timer(int cpu)
Quoted 8 lines
arch/x86/xen/time.c void xen_setup_timer(int cpu) · line 438
Quoted 6 lines
arch/x86/xen/time.c +1
-3printk (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
Quoted 9 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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?
Quoted 14 lines
arch/x86/xen/time.c +1
-1@@ -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 );
arch/x86/xen/time.c void xen_teardown_timer(int cpu) · line 428
Quoted 10 lines
arch/x86/xen/time.c +0
-3evt->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 = ...;
arch/x86/xen/time.c void xen_setup_timer(int cpu) · line 438
Quoted 10 lines
arch/x86/xen/time.c +1
-3int 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
PATCH v2 1/1 x86/xen: avoid freeing static 'name' when kasprintf() fails
In reply to Vitaly KuznetsovIn 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.
---
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(-)
arch/x86/xen/time.c +5 -11
@@ -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; }
Signature · 1 line
1.9.3
PATCH v2 1/1 x86/xen: avoid freeing static 'name' when kasprintf() fails
In reply to Vitaly KuznetsovIn 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.
---
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(-)
arch/x86/xen/time.c +5 -11
@@ -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; }
Signature · 1 line
1.9.3
Quoted 67 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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 <[email protected]>Signed-off-by Vitaly Kuznetsov <[email protected]>---
Changes from v1 (David Vrabel):
- add 'structxen_clock_event_device*xevt' for covenience
-sizeof(xevt->name)insnprintf()call
- Suggested-by: Laszlo Ersek--- arch/x86/xen/time.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)arch/x86/xen/time.c +5
-10@@ -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 );
arch/x86/xen/time.c void xen_teardown_timer(int cpu) · line 452
Quoted 10 lines
arch/x86/xen/time.c +0
-1(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 :))
Thanks!
Laszlo
Quoted 67 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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 <[email protected]>Signed-off-by Vitaly Kuznetsov <[email protected]>---
Changes from v1 (David Vrabel):
- add 'structxen_clock_event_device*xevt' for covenience
-sizeof(xevt->name)insnprintf()call
- Suggested-by: Laszlo Ersek--- arch/x86/xen/time.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)arch/x86/xen/time.c +5
-10@@ -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 );
arch/x86/xen/time.c void xen_teardown_timer(int cpu) · line 452
Quoted 10 lines
arch/x86/xen/time.c +0
-1(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 :))
Thanks!
Laszlo
David Vrabel
AppliedQuoted 7 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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
David Vrabel
AppliedQuoted 7 lines · Vitaly Kuznetsov
In case
kasprintf()fails inxen_setup_timer()we assign name to the static string "<timer kasprintf failed>". We, however, don't check that fact before issuingkfree()inxen_teardown_timer(), kernel is supposed to crash with 'kernel BUG atmm/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