Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp69880rdb; Wed, 18 Oct 2023 18:57:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSNmO1M4Fys7Y5CqNF4u5KSTdbo72zaIfEHfa6r3ULAdCu8alSbT1ySyid/JgTcR1rJqgB X-Received: by 2002:a17:90a:f2d1:b0:27d:a14c:eba6 with SMTP id gt17-20020a17090af2d100b0027da14ceba6mr923287pjb.21.1697680666928; Wed, 18 Oct 2023 18:57:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697680666; cv=none; d=google.com; s=arc-20160816; b=KlhjiWLPxXu8KFC+83NjvaNpAcYVSL4css6+KlHUtL08Y4G4XSn3jzthwpGPrFQb2E 5dtFDNEeBH2cmtm3iZhNgAWD78U4aLjAUka1oHmej7nWvgsSZ8LlXTl6F6WF/mrm4BwE tG2Wp4+KtsbolHC+6F3uKU7al/c0E/XzHylnt51IxsmNhLjeFozAZL5HVSpwdDuwifIX pKZ96dEO40pTqSQJ56PJCZyRg95cW+kT+Gn75VxkzGrf8uOvy7EcvvnU16Nb7JTfKMxF zPy/fMqJlCapDXX4/Q8KczbsbZzDkJaONKBsxkPMrLyvHMQKqjfaouOMu6goEwgOsIWz gKBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=WQqjsdfNk9A033qcUzZ1yTq22TWnDLk9hyxy0ob1sLE=; fh=CWLmHhriLTIlpnf4K+EbG/haTCs2L38KnoMUjUMRjm0=; b=tpLIM6o9AUaYQkGFgyKrcHiEtZIDNWxKWSdaTOZPEQhACFwzn9F3fKxdjRf5yc40EF nxgMdrbrlqmJNyeIW2hkUYVNSjjt/uYr2CrWNmWiSIv17sXEvTSUyRnb7hQUHTUh3tLJ nR3LND1phJ11A7ABO+YNtA7dlbFedAfOJAn9VvJkqzJimau121K7MJzsN7bU0urcf2Nf 3drPoAg9HpnSf7YgQNCHeQwe8SKsReEqEJTOgzj7t4fdZaXgdEqnUzCfyQx3oXnSmJ8Y AGJClysSrFrAKMPjDk/pzVz44LE2OWwaP5KzBpG+SNjnZj8m9L0CzsSluvmk5V8MGIJA jvPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id oc6-20020a17090b1c0600b0027d047d9070si1188668pjb.91.2023.10.18.18.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 18:57:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 8EC3A8263E89; Wed, 18 Oct 2023 18:57:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232047AbjJSB50 (ORCPT + 99 others); Wed, 18 Oct 2023 21:57:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229726AbjJSB5Z (ORCPT ); Wed, 18 Oct 2023 21:57:25 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E728EFE for ; Wed, 18 Oct 2023 18:57:22 -0700 (PDT) Received: from kwepemm000013.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4S9rNL6LdzzRt4l; Thu, 19 Oct 2023 09:53:38 +0800 (CST) Received: from [10.174.178.46] (10.174.178.46) by kwepemm000013.china.huawei.com (7.193.23.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Thu, 19 Oct 2023 09:57:19 +0800 Subject: Re: [PATCH v2] ubi: gluebi: Fix NULL pointer dereference caused by ftl notifier To: ZhaoLong Wang , , , , , CC: , , , References: <20231018121618.778385-1-wangzhaolong1@huawei.com> From: Zhihao Cheng Message-ID: Date: Thu, 19 Oct 2023 09:57:18 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20231018121618.778385-1-wangzhaolong1@huawei.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.46] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm000013.china.huawei.com (7.193.23.81) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,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 fry.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 (fry.vger.email [0.0.0.0]); Wed, 18 Oct 2023 18:57:40 -0700 (PDT) 在 2023/10/18 20:16, ZhaoLong Wang 写道: > 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(-) > Reviewed-by: Zhihao Cheng > 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; > } > >