ubiformat: get buffer from malloc
PATCH 1/1 ubiformat: get buffer from malloc
There was a erase block sized (here 131072) char buf array on the stack. Changed this to get the space from malloc preventing stack overflows. Also fix a wrong return without clean up.
--- commands/ubiformat.c | 22 +++++++++++++++------- 1 files changed, 15 insertions(+), 7 deletions(-)
commands/ubiformat.c +15 -7
@@ -296,13 +296,20 @@ static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, instatic int flash_image (const struct mtd_dev_info *mtd,const struct ubigen_info *ui,struct ubi_scan_info *si) { -int fd, img_ebs, eb, written_ebs =0 , divisor; +int fd, img_ebs, eb, written_ebs =0 , divisor, ret =-1 ;off_t st_size; +char *buf =NULL ; fd =open_file (&st_size);if (fd <0 )return fd; + buf =malloc (mtd->eb_size); +if (!buf) { +sys_errmsg ("cannot allocate %d bytes of memory" , mtd->eb_size); + goto out_close; + } + img_ebs = st_size / mtd->eb_size;if (img_ebs > si->good_cnt) {
@@ -312,8 +319,9 @@ static int flash_image(const struct mtd_dev_info *mtd, }if (st_size % mtd->eb_size) { -return sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of ""eraseblock size (%d bytes)" , - args.image, (long long )st_size, mtd->eb_size); +sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of " +"eraseblock size (%d bytes)" , + args.image, (long long )st_size, mtd->eb_size); goto out_close; }
@@ -321,7 +329,6 @@ static int flash_image(const struct mtd_dev_info *mtd, divisor = img_ebs;for (eb =0 ; eb < mtd->eb_cnt; eb++) {int err, new_len; -char buf[mtd->eb_size];long long ec;if (!args.quiet && !args.verbose) {
@@ -404,12 +411,13 @@ static int flash_image(const struct mtd_dev_info *mtd,if (!args.quiet && !args.verbose)printf ("\n" ); -close (fd); -return eb +1 ; + + ret = eb +1 ; out_close: +free (buf);close (fd); -return -1 ; +return ret; } static int format(const struct mtd_dev_info *mtd,
Signature · 7 lines
1.7.0.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Am Montag, den 25.03.2013, 16:15 +0100 schrieb Jan Weitzel:
Quoted 4 lines
There was a erase block sized (here 131072) char buf array on the stack. Changed this to get the space from malloc preventing stack overflows. Also fix a wrong return without clean up.
btw the command works fine with the stack overflow till CONFIG_MMU_EARLY was turned on.
Jan
Quoted 58 lines
Signed-off-by Jan Weitzel <[email protected]>--- commands/ubiformat.c | 22 +++++++++++++++------- 1 files changed, 15 insertions(+), 7 deletions(-)commands/ubiformat.c +12
-6@@ -296,13 +296,20 @@ static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, instatic int flash_image (const struct mtd_dev_info *mtd,const struct ubigen_info *ui,struct ubi_scan_info *si) { -int fd, img_ebs, eb, written_ebs =0 , divisor; +int fd, img_ebs, eb, written_ebs =0 , divisor, ret =-1 ;off_t st_size; +char *buf =NULL ; fd =open_file (&st_size);if (fd <0 )return fd; + buf =malloc (mtd->eb_size); +if (!buf) { +sys_errmsg ("cannot allocate %d bytes of memory" , mtd->eb_size); + goto out_close; + } + img_ebs = st_size / mtd->eb_size;if (img_ebs > si->good_cnt) {@@ -312,8 +319,9 @@ static int flash_image(const struct mtd_dev_info *mtd, }if (st_size % mtd->eb_size) { -return sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of ""eraseblock size (%d bytes)" , - args.image, (long long )st_size, mtd->eb_size); +sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of " +"eraseblock size (%d bytes)" , + args.image, (long long )st_size, mtd->eb_size); goto out_close; }@@ -321,7 +329,6 @@ static int flash_image(const struct mtd_dev_info *mtd, divisor = img_ebs;for (eb =0 ; eb < mtd->eb_cnt; eb++) {int err, new_len; -char buf[mtd->eb_size];long long ec;if (!args.quiet && !args.verbose) {@@ -404,12 +411,13 @@ static int flash_image(const struct mtd_dev_info *mtd,if (!args.quiet && !args.verbose)printf ("\n" ); -close (fd); -return eb +1 ; +
commands/ubiformat.c static int flash_image(const struct mtd_dev_info *mtd, · line 423
Quoted 10 lines
commands/ubiformat.c +3
-1+ ret = eb +1 ; out_close: +free (buf);close (fd); -return -1 ; +return ret; } static int format(const struct mtd_dev_info *mtd,
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Hi,
Quoted 20 lines · Jan Weitzel
There was a erase block sized (here 131072) char buf array on the stack. Changed this to get the space from malloc preventing stack overflows. Also fix a wrong return without clean up.
Signed-off-by Jan Weitzel <[email protected]>--- commands/ubiformat.c | 22 +++++++++++++++------- 1 files changed, 15 insertions(+), 7 deletions(-)commands/ubiformat.c +1
-1@@ -296,13 +296,20 @@ static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, instatic int flash_image (const struct mtd_dev_info *mtd,const struct ubigen_info *ui,struct ubi_scan_info *si) { -int fd, img_ebs, eb, written_ebs =0 , divisor; +int fd, img_ebs, eb, written_ebs =0 , divisor, ret =-1 ;off_t st_size;
commands/ubiformat.c static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, in · line 310
Quoted 10 lines
commands/ubiformat.c +5
-0+char *buf =NULL ; fd =open_file (&st_size);if (fd <0 )return fd; + buf =malloc (mtd->eb_size); +if (!buf) { +sys_errmsg ("cannot allocate %d bytes of memory" , mtd->eb_size); + goto out_close;
meep, out_close will call free(buf). You need to add a new label above free(buf);
Quoted 26 lines
commands/ubiformat.c +5
-3+ } + img_ebs = st_size / mtd->eb_size;if (img_ebs > si->good_cnt) {@@ -312,8 +319,9 @@ static int flash_image(const struct mtd_dev_info *mtd, }if (st_size % mtd->eb_size) { -return sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of ""eraseblock size (%d bytes)" , - args.image, (long long )st_size, mtd->eb_size); +sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of " +"eraseblock size (%d bytes)" , + args.image, (long long )st_size, mtd->eb_size); goto out_close; }@@ -321,7 +329,6 @@ static int flash_image(const struct mtd_dev_info *mtd, divisor = img_ebs;for (eb =0 ; eb < mtd->eb_cnt; eb++) {int err, new_len; -char buf[mtd->eb_size];long long ec;if (!args.quiet && !args.verbose) {
commands/ubiformat.c static int flash_image(const struct mtd_dev_info *mtd, · line 418
Quoted 10 lines
commands/ubiformat.c +3
-2@@ -404,12 +411,13 @@ static int flash_image(const struct mtd_dev_info *mtd,if (!args.quiet && !args.verbose)printf ("\n" ); -close (fd); -return eb +1 ; + + ret = eb +1 ; out_close: +free (buf);
here!
Quoted 10 lines
commands/ubiformat.c +1
-1close (fd); -return -1 ; +return ret; } static int format(const struct mtd_dev_info *mtd,Signature · 1 line
1.7.0.4
Alex
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Quoted 29 lines · Alexander Aring
Hi,
Quoted 26 lines · Jan Weitzel
There was a erase block sized (here 131072) char buf array on the stack. Changed this to get the space from malloc preventing stack overflows. Also fix a wrong return without clean up.
Signed-off-by Jan Weitzel <[email protected]>--- commands/ubiformat.c | 22 +++++++++++++++------- 1 files changed, 15 insertions(+), 7 deletions(-)commands/ubiformat.c +2
-1@@ -296,13 +296,20 @@ static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, instatic int flash_image (const struct mtd_dev_info *mtd,const struct ubigen_info *ui,struct ubi_scan_info *si) { -int fd, img_ebs, eb, written_ebs =0 , divisor; +int fd, img_ebs, eb, written_ebs =0 , divisor, ret =-1 ;off_t st_size; +char *buf =NULL ; fd =open_file (&st_size);if (fd <0 )return fd;
Quoted 8 lines
commands/ubiformat.c static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, in · line 310
Quoted 4 lines
commands/ubiformat.c +4
-0+ buf =malloc (mtd->eb_size); +if (!buf) { +sys_errmsg ("cannot allocate %d bytes of memory" , mtd->eb_size); + goto out_close;meep,
out_closewill call free(buf). You need to add a new label above free(buf);
ah, free is null proofed sry.
Alex
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedQuoted 5 lines · Jan Weitzel
There was a erase block sized (here 131072) char buf array on the stack. Changed this to get the space from malloc preventing stack overflows. Also fix a wrong return without clean up.
Signed-off-by Jan Weitzel <[email protected]>
Applied, thanks
Sascha
Quoted 76 lines
--- commands/ubiformat.c | 22 +++++++++++++++------- 1 files changed, 15 insertions(+), 7 deletions(-)commands/ubiformat.c +15
-7@@ -296,13 +296,20 @@ static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, instatic int flash_image (const struct mtd_dev_info *mtd,const struct ubigen_info *ui,struct ubi_scan_info *si) { -int fd, img_ebs, eb, written_ebs =0 , divisor; +int fd, img_ebs, eb, written_ebs =0 , divisor, ret =-1 ;off_t st_size; +char *buf =NULL ; fd =open_file (&st_size);if (fd <0 )return fd; + buf =malloc (mtd->eb_size); +if (!buf) { +sys_errmsg ("cannot allocate %d bytes of memory" , mtd->eb_size); + goto out_close; + } + img_ebs = st_size / mtd->eb_size;if (img_ebs > si->good_cnt) {@@ -312,8 +319,9 @@ static int flash_image(const struct mtd_dev_info *mtd, }if (st_size % mtd->eb_size) { -return sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of ""eraseblock size (%d bytes)" , - args.image, (long long )st_size, mtd->eb_size); +sys_errmsg ("file \"%s\" (size %lld bytes) is not multiple of " +"eraseblock size (%d bytes)" , + args.image, (long long )st_size, mtd->eb_size); goto out_close; }@@ -321,7 +329,6 @@ static int flash_image(const struct mtd_dev_info *mtd, divisor = img_ebs;for (eb =0 ; eb < mtd->eb_cnt; eb++) {int err, new_len; -char buf[mtd->eb_size];long long ec;if (!args.quiet && !args.verbose) {@@ -404,12 +411,13 @@ static int flash_image(const struct mtd_dev_info *mtd,if (!args.quiet && !args.verbose)printf ("\n" ); -close (fd); -return eb +1 ; + + ret = eb +1 ; out_close: +free (buf);close (fd); -return -1 ; +return ret; } static int format(const struct mtd_dev_info *mtd,Signature · 7 lines
1.7.0.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox