Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3749078rdg; Wed, 18 Oct 2023 05:11:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEmW9OD0SIQrxA4fH8YiWyT0BF90D3isFGn4pE1vzpPR7nZr3sa4KMriNrRuCHYwtfapAKE X-Received: by 2002:a17:903:2301:b0:1ca:71:ea41 with SMTP id d1-20020a170903230100b001ca0071ea41mr7026186plh.22.1697631109294; Wed, 18 Oct 2023 05:11:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697631109; cv=none; d=google.com; s=arc-20160816; b=ml2ddYs1rK+m0J74dD5wZBifr8+4snaKIufsWnKaoPMW/RgMOZzRuVijmvSRmX0/X9 80hoUP6Kl0K3eq+FE3vXMV6f/tkeqqmJA2eT0YZUnosY/2zL+JGzHRxcJaV0S95q8rI8 tUTnqL5BGC1FOokRn8NZhW0WYta6eyogugKmqiuEOi0Av9ROWcqA9wYfOXSt3SqdEJAN u8Uujc/WHPU4Oj0mbPDr+s95HBqNhN3xYvwWl3mT6XluM7XhOaSEpjW2Jotdh+qo9Ckl a+jy8BpSVXU9n8Nhcn1grNR1Mvvhzu414X/ufSJTjvgn8CbqOu2AuKOhWajIU7gyGbtm U8RA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=nvGZMvEnPRwvYuvczIiaejQPwiqqK2seS7L+oGbwdz4=; fh=/ohAfTUKI1wJh4U5JIWSEVUuaOuXh75w955cDnKl9nk=; b=05yI0jvKCBQKWQ55bSgVwoUV5w8wcpsZAK/HT0c36FZNAjpuMP4hx8UgLmnPr+qALQ LEwWU8e8qSgDgNTlyiPnl9MDKnbeGtoqhJh9cm2eTCJHjtJEwNCZSQmfEzd+Ape2Dy99 ccDUMdc/0tXji333vMsUF2UgZXdNbrh54zYP0mHow6IPaYLDao2cEE4OhdwmehKrv4YZ CptY6LMSS1S4xVZIlM0/QleV6TAo8iV7JFAUQEmtJCV0A0053xCzxaU6WfVWxzw54Cqb oFj59RgR/Zpt3HQyAbdoaH8+m15JcVDte75ax3Nsjdi57rwMy5XahudBvhNQ5TqrTsiy +rCA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id q18-20020a17090311d200b001c61817e049si4132923plh.67.2023.10.18.05.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 05:11:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 06C02817ABEA; Wed, 18 Oct 2023 05:11:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230345AbjJRMLi (ORCPT + 99 others); Wed, 18 Oct 2023 08:11:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230313AbjJRMLh (ORCPT ); Wed, 18 Oct 2023 08:11:37 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06420118 for ; Wed, 18 Oct 2023 05:11:34 -0700 (PDT) Received: from dggpemm500009.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4S9V2F6kF4zvQJ3; Wed, 18 Oct 2023 20:06:45 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpemm500009.china.huawei.com (7.185.36.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Wed, 18 Oct 2023 20:11:30 +0800 From: ZhaoLong Wang To: , , , , CC: , , , , , Subject: [PATCH v2] ubi: gluebi: Fix NULL pointer dereference caused by ftl notifier Date: Wed, 18 Oct 2023 20:16:18 +0800 Message-ID: <20231018121618.778385-1-wangzhaolong1@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 18 Oct 2023 05:11:46 -0700 (PDT) If both flt.ko and gluebi.ko are loaded, the notiier of ftl triggers NULL pointer dereference when trying to access ‘gluebi->desc’ in gluebi_read(). ubi_gluebi_init ubi_register_volume_notifier ubi_enumerate_volumes ubi_notify_all gluebi_notify nb->notifier_call() gluebi_create mtd_device_register mtd_device_parse_register add_mtd_device blktrans_notify_add not->add() ftl_add_mtd tr->add_mtd() scan_header mtd_read mtd_read mtd_read_oob gluebi_read mtd->read() gluebi->desc - NULL Detailed reproduction information available at the link[1], In the normal case, obtain gluebi->desc in the gluebi_get_device(), and accesses gluebi->desc in the gluebi_read(). However, gluebi_get_device() is not executed in advance in the ftl_add_mtd() process, which leads to NULL pointer dereference. The value of gluebi->desc may also be a negative error code, which triggers the page fault error. This patch has the following modifications: 1. Do not assign gluebi->desc to the error code. Use the NULL instead. 2. Always check the validity of gluebi->desc in gluebi_read() If the gluebi->desc is NULL, try to get MTD device. Such a modification currently works because the mutex "mtd_table_mutex" is held on all necessary paths, including the ftl_add_mtd() call path, open and close paths. Therefore, many race condition can be avoided. Fixes: 2ba3d76a1e29 ("UBI: make gluebi a separate module") Link: https://bugzilla.kernel.org/show_bug.cgi?id=217992 [1] Signed-off-by: ZhaoLong Wang --- drivers/mtd/ubi/gluebi.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/ubi/gluebi.c b/drivers/mtd/ubi/gluebi.c index 1b980d15d9fb..0ca7f104adbf 100644 --- a/drivers/mtd/ubi/gluebi.c +++ b/drivers/mtd/ubi/gluebi.c @@ -85,6 +85,7 @@ static int gluebi_get_device(struct mtd_info *mtd) { struct gluebi_device *gluebi; int ubi_mode = UBI_READONLY; + struct ubi_volume_desc *vdesc; if (mtd->flags & MTD_WRITEABLE) ubi_mode = UBI_READWRITE; @@ -109,12 +110,14 @@ static int gluebi_get_device(struct mtd_info *mtd) * This is the first reference to this UBI volume via the MTD device * interface. Open the corresponding volume in read-write mode. */ - gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id, + vdesc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id, ubi_mode); - if (IS_ERR(gluebi->desc)) { + if (IS_ERR(vdesc)) { + gluebi->desc = NULL; mutex_unlock(&devices_mutex); - return PTR_ERR(gluebi->desc); + return PTR_ERR(vdesc); } + gluebi->desc = vdesc; gluebi->refcnt += 1; mutex_unlock(&devices_mutex); return 0; @@ -134,8 +137,10 @@ static void gluebi_put_device(struct mtd_info *mtd) gluebi = container_of(mtd, struct gluebi_device, mtd); mutex_lock(&devices_mutex); gluebi->refcnt -= 1; - if (gluebi->refcnt == 0) + if (gluebi->refcnt == 0) { ubi_close_volume(gluebi->desc); + gluebi->desc = NULL; + } mutex_unlock(&devices_mutex); } @@ -154,9 +159,26 @@ static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, unsigned char *buf) { int err = 0, lnum, offs, bytes_left; - struct gluebi_device *gluebi; + struct gluebi_device *gluebi = container_of(mtd, struct gluebi_device, + mtd); + int no_desc = gluebi->desc == NULL ? 1 : 0; + + /** + * In normal case, the UBI volume desc has been initialized by + * ->_get_device(). However, in the ftl notifier process, the + * ->_get_device() is not executed in advance and the MTD device + * is directly scanned which cause NULL pointer dereference. + * Therefore, try to get the MTD device here. + */ + if (unlikely(no_desc)) { + err = __get_mtd_device(mtd); + if (err) { + err_msg("cannot get MTD device %d, UBI device %d, volume %d, error %d", + mtd->index, gluebi->ubi_num, gluebi->vol_id, err); + return err; + } + } - gluebi = container_of(mtd, struct gluebi_device, mtd); lnum = div_u64_rem(from, mtd->erasesize, &offs); bytes_left = len; while (bytes_left) { @@ -176,6 +198,9 @@ static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len, } *retlen = len - bytes_left; + + if (unlikely(no_desc)) + __put_mtd_device(mtd); return err; } -- 2.31.1