Skip to content

Commit 2b8aa4c

Browse files
dinghaoliumiquelraynal
authored andcommitted
mtd: rawnand: diskonchip: fix a potential double free in doc_probe
When nand_scan() fails, it has cleaned up related resources in its error paths. Therefore, the following nand_cleanup() may lead to a double-free. One possible trace is: doc_probe |-> nand_scan | |-> nand_scan_with_ids | |-> nand_scan_tail | |-> kfree(chip->data_buf) [First free] | |-> nand_cleanup |-> kfree(chip->data_buf) [Double free here] Fix this by removing nand_cleanup() on failure of nand_scan(). Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Link: https://lore.kernel.org/linux-mtd/20231214072946.10285-1-dinghao.liu@zju.edu.cn
1 parent b6c985d commit 2b8aa4c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/mtd/nand/raw/diskonchip.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,10 +1491,12 @@ static int __init doc_probe(unsigned long physadr)
14911491
else
14921492
numchips = doc2001_init(mtd);
14931493

1494-
if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
1495-
/* DBB note: i believe nand_cleanup is necessary here, as
1496-
buffers may have been allocated in nand_base. Check with
1497-
Thomas. FIX ME! */
1494+
ret = nand_scan(nand, numchips);
1495+
if (ret)
1496+
goto fail;
1497+
1498+
ret = doc->late_init(mtd);
1499+
if (ret) {
14981500
nand_cleanup(nand);
14991501
goto fail;
15001502
}

0 commit comments

Comments
 (0)