Skip to content

Commit 2818ac5

Browse files
air237jonasjelonek
authored andcommitted
mtd: check malloc() return value in mtd_check()
After malloc(erasesize) in mtd_check(), the result was never checked for NULL. On allocation failure, the buf pointer remained NULL and would later be used in image_check() via read(imagefd, buf + buflen, ...), causing a NULL pointer dereference. Add a NULL check after the allocation and return early. The early return path also closes the just-opened fd and frees the strdup'd colon-separated device-list copy to avoid leaks. Signed-off-by: Anna Kiri <bredcorn@gmail.com> Link: openwrt/openwrt#23705 Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
1 parent 2f809f9 commit 2818ac5

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

  • package/system/mtd/src

package/system/mtd/src/mtd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ static int mtd_check(const char *mtd)
269269

270270
if (!buf)
271271
buf = malloc(erasesize);
272+
if (!buf) {
273+
close(fd);
274+
free(str);
275+
return 0;
276+
}
272277

273278
close(fd);
274279
mtd = next;

0 commit comments

Comments
 (0)