Skip to contentSkip to conversation

mtd: mtd_torturetest can cause stack overflows

PATCH Superseded Al Cooper 3 participants 3 messages Feb 4, 2013 – Feb 5, 2013 Memory technology devices (MTD)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Al Cooper

author
#0 lore raw

PATCH 1/1 mtd: mtd_torturetest can cause stack overflows

mtd_torturetest uses the module parm "ebcnt" to control the size of a stack based array of int's. When "ebcnt" is large, Ex: 1000, it causes stack overflows on systems with small kernel stacks. The fix is to move the array from the stack to kmalloc memory.

---
 drivers/mtd/tests/mtd_torturetest.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
drivers/mtd/tests/mtd_torturetest.c +9 -1
index c4cde1e..a777cc8 100644
@@ -208,7 +208,7 @@ static inline int write_pattern(int ebnum, void *buf)
 static int __init tort_init(void)
 {
 	int err = 0, i, infinite = !cycles_count;
-	int bad_ebs[ebcnt];
+	int *bad_ebs;
 
 	printk(KERN_INFO "\n");
 	printk(KERN_INFO "=================================================\n");
@@ -273,6 +273,12 @@ static int __init tort_init(void)
 		goto out_patt_FF;
 	}
 
+	bad_ebs = kmalloc(sizeof(*bad_ebs) * ebcnt, GFP_KERNEL);
+	if (!bad_ebs) {
+		pr_err("error: cannot allocate memory\n");
+		goto out_check_buf;
+	}
+
 	err = 0;
 
 	/* Initialize patterns */
@@ -394,6 +400,8 @@ out:
 
 	pr_info("finished after %u erase cycles\n",
 	       erase_cycles);
+	kfree(bad_ebs);
+out_check_buf:
 	kfree(check_buf);
 out_patt_FF:
 	kfree(patt_FF);
Signature · 1 line
1.7.6
#1 lore raw
Quoted 14 lines · Al Cooper
drivers/mtd/tests/mtd_torturetest.c +1 -1
index c4cde1e..a777cc8 100644
@@ -208,7 +208,7 @@ static inline int write_pattern(int ebnum, void *buf)
  static int __init tort_init(void)
  {
  	int err = 0, i, infinite = !cycles_count;
-	int bad_ebs[ebcnt];
+	int *bad_ebs;
 
  	printk(KERN_INFO "\n");
  	printk(KERN_INFO "=================================================\n");

drivers/mtd/tests/mtd_torturetest.c static int __init tort_init(void) · line 276

Quoted 4 lines
drivers/mtd/tests/mtd_torturetest.c +1 -0
@@ -273,6 +273,12 @@ static int __init tort_init(void)
  		goto out_patt_FF;
  	}
 
+	bad_ebs = kmalloc(sizeof(*bad_ebs) * ebcnt, GFP_KERNEL);

I think it's better to use the kcalloc() here.

thanks
Huang Shijie

#2 lore raw
In reply to Al Cooper
Quoted 24 lines · Al Cooper

mtd_torturetest uses the module parm "ebcnt" to control the size of a stack based array of int's. When "ebcnt" is large, Ex: 1000, it causes stack overflows on systems with small kernel stacks. The fix is to move the array from the stack to kmalloc memory.

---
 drivers/mtd/tests/mtd_torturetest.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
drivers/mtd/tests/mtd_torturetest.c +1 -1
index c4cde1e..a777cc8 100644
@@ -208,7 +208,7 @@ static inline int write_pattern(int ebnum, void *buf)
 static int __init tort_init(void)
 {
        int err = 0, i, infinite = !cycles_count;
-       int bad_ebs[ebcnt];
+       int *bad_ebs;
 
        printk(KERN_INFO "\n");
        printk(KERN_INFO "=================================================\n");

drivers/mtd/tests/mtd_torturetest.c static int __init tort_init(void) · line 278

Quoted 6 lines
drivers/mtd/tests/mtd_torturetest.c +3 -0
@@ -273,6 +273,12 @@ static int __init tort_init(void)
                goto out_patt_FF;
        }
 
+       bad_ebs = kmalloc(sizeof(*bad_ebs) * ebcnt, GFP_KERNEL);
+       if (!bad_ebs) {
+               pr_err("error: cannot allocate memory\n");

You don't want to print this error. See:

http://www.spinics.net/lists/newbies/msg48792.html
http://archive.linuxvirtualserver.org/html/lvs-devel/2011-08/msg00001.html

Signature · 1 line
    Ezequiel