Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5829830rdb; Thu, 14 Dec 2023 00:06:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IGIxwIF0ZngluiZXmAR/t7enYOv2z5b3VgHjwQx1FfdPKU6IDSZOMYyhrE5GAaUlr793FQq X-Received: by 2002:a05:6a20:7f9b:b0:190:1b16:3558 with SMTP id d27-20020a056a207f9b00b001901b163558mr13732117pzj.2.1702541213574; Thu, 14 Dec 2023 00:06:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702541213; cv=none; d=google.com; s=arc-20160816; b=e7brlqVNiuOD7ueuIS3gvzexiGoBhIdYkLF+FFP5uq6KKyRAQZJCSqLsdlHCsTGy8I UTKoIk/BL1PUcoDTk31zbQ2WiByv7ID7OnjmO5vzyLB8bGrwkFDdwjeE6y5OsOLOSiSO eDYGulCgIW7HsCGb08XlbnSCC7vt75L0LKSOhZ+pIS+XsWjbrDN/SkcNLLC1rAC+gzgz dPW8v0ijUR8ks3MaQVh2TcRrakQm5APbp+1cPmff6FfEyfxT+nEoAsnAOGAF+qXiSCNq pzgoZIcrw0BQ0iOLYdAqjTSKoq6SQOsHy5qSdfooQalMnQKGB+wQGZbCjU3qMFX9jX8Z yhjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=MVULWzHVZJZTIpdqr3LteUs500uATn+r+0KfLHRUafo=; fh=LT8Bl2et2AKQqbOc6An9T795hg3NDHic3f1YXqSts4w=; b=TH/uFisT2/YLtylG/IquvGoIRy7yQKzAc94scUfJyNuuAnJKtyyefyqr8YouhFt6eI blFWwqVkWoY3WA9HBz2BWs7zzhiMbd+7g2hzTPUhUpDDaaTScxJwP5pY2M9ePq0IT41f ovfmBMcm0fXLEVQwzAF07ztXNFdolAej4qN8MECah2bAdYRmRpCBZr2IjZWbn2RwtAwi b9VkVBvH3yxmezYLZrWD1luVwKhYw2cCtpgHAPBKQ9v6X5TL/pj7she4nlA0GOGasBF/ jJlGjGkzxAkdhXAwoYMoaceBHP5Sqt77jx4eA/cRdIse9X4x//xFWqMDqq/dzh5Jfc0R z9Jg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id c28-20020a630d1c000000b005c66404de05si10932466pgl.413.2023.12.14.00.06.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:06:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id ABC4380BA97A; Thu, 14 Dec 2023 00:06:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443395AbjLNIGA (ORCPT + 99 others); Thu, 14 Dec 2023 03:06:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234342AbjLNIF6 (ORCPT ); Thu, 14 Dec 2023 03:05:58 -0500 Received: from zju.edu.cn (mail.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 547A7CF for ; Thu, 14 Dec 2023 00:06:03 -0800 (PST) Received: from localhost.localdomain (unknown [10.190.71.46]) by mail-app3 (Coremail) with SMTP id cC_KCgDHP9_8rnpl7+PcAA--.38858S4; Thu, 14 Dec 2023 15:30:10 +0800 (CST) From: Dinghao Liu To: dinghao.liu@zju.edu.cn Cc: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] mtd: rawnand: diskonchip: fix a potential double free in doc_probe Date: Thu, 14 Dec 2023 15:29:43 +0800 Message-Id: <20231214072946.10285-1-dinghao.liu@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: cC_KCgDHP9_8rnpl7+PcAA--.38858S4 X-Coremail-Antispam: 1UD129KBjvJXoW7ury5Kr1Dtw1UCF48CrW8Crg_yoW8JFW7pF WUua42kayUJ3y09a4qvF48GFy5Jw4kGrW7GrZ3Zay7u34rurn3Gr1rCry7ZF4qqa4IqayY q395ta48JF1UtrUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk21xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJr0_GcWl84ACjcxK6I8E 87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c 8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_ Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwI xGrwACjI8F5VA0II8E6IAqYI8I648v4I1l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxF aVAv8VW8uw4UJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjfUoOJ5UUUUU X-CM-SenderInfo: qrrzjiaqtzq6lmxovvfxof0/1tbiAgIIBmV4LpY5mAAGsM X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:06:36 -0800 (PST) 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 --- drivers/mtd/nand/raw/diskonchip.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/mtd/nand/raw/diskonchip.c b/drivers/mtd/nand/raw/diskonchip.c index 5d2ddb037a9a..5243fab9face 100644 --- a/drivers/mtd/nand/raw/diskonchip.c +++ b/drivers/mtd/nand/raw/diskonchip.c @@ -1491,10 +1491,12 @@ static int __init doc_probe(unsigned long physadr) else numchips = doc2001_init(mtd); - if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) { - /* DBB note: i believe nand_cleanup is necessary here, as - buffers may have been allocated in nand_base. Check with - Thomas. FIX ME! */ + ret = nand_scan(nand, numchips); + if (ret) + goto fail; + + ret = doc->late_init(mtd); + if (ret) { nand_cleanup(nand); goto fail; } -- 2.17.1