Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp2513092rwb; Thu, 27 Jul 2023 08:11:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlHFyer2sv7M1bqdzIZxJp5+jqTcvgtMiNXYW2wwAv7Ho+/qv/Dby7Cln+xSSqqoJlue8W3v X-Received: by 2002:a05:6a00:1803:b0:67f:3dcd:bc00 with SMTP id y3-20020a056a00180300b0067f3dcdbc00mr7990169pfa.2.1690470664712; Thu, 27 Jul 2023 08:11:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690470664; cv=none; d=google.com; s=arc-20160816; b=UK90XGuyetUgUNORux8ZQWNRxXMrFOqZv8YbSaGcS459cdhA0p7s1cwq9jUpbFFEYn nK0tnV9NcwsRF4W+qmKT4i2sR5iw5ObAywBga+K+hxt/19+//jYEveVuUAQadhiQeqdb qYLfvAUTxk6/jRHUa0i2mfhdo4+d7nUncT31MrMRUfaNqDPHvaA5ch/YLu30+//oYtOH Q4Dz2GhK3EgcY9cpz+/5TUDX86qf+ok58dgcDzbpl3ppBc+JdvNjjImCfLmp3V2YnwJM uJE+7VBVHJzVTDabCbqu+/JON9Gp/swzmM+ufBV2vb6tFXkoQHB+joSFkRjVF1ewTIjs oY4w== 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:dkim-signature; bh=flLfnzCQiKfGyshH6D95jxjEBoZRhyOxmas2WDkZZkA=; fh=knNrmogx3g7asAA+VjHKlHhzYFCqpGoAqEXi6Cn+liw=; b=loNS5pi5hN9toHKh5TYytXl7t/KjAOjhnrkjxBCFIo027gGXKA+1MrvI+bVyWUHfvA wKZML4iy2UZTCuqJQwU38XBZS/vDweoWsKfjlLqJ5rVVVC/Wa3PcwpnHVM+UHxF4UJuG 5WO40jMkaneJmnLn3QLp0ZkNbJqxkD9mvESH2zHj+C8SUa04zQkftf6ClpTueg+ou0pS X5YzfFJkc84WFS+fbnh1pcXDMl5ctlj5goJ9l8cjcJanHNKOHgzAxG+sslH+MTb7M0ft N7WGw9zn2WzMFLZP6l4FXWNTwdSoEmJGISgtoIDif60RGkMsZWlX+FlDTqpxzyISAuIe /HOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IDcL46EZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds5-20020a056a004ac500b006744cbb3ebesi1487796pfb.127.2023.07.27.08.10.51; Thu, 27 Jul 2023 08:11:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IDcL46EZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234144AbjG0PCd (ORCPT + 99 others); Thu, 27 Jul 2023 11:02:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234111AbjG0PCX (ORCPT ); Thu, 27 Jul 2023 11:02:23 -0400 Received: from mgamail.intel.com (unknown [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D89842D54 for ; Thu, 27 Jul 2023 08:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690470142; x=1722006142; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=cZOumbvz7d5c3xoly7L/DH5QKt5aCye/K29l+UqNRao=; b=IDcL46EZcbxxpsFyvRDWR1kjVMBeNqVyv0IIRyLVyur1Ei4DAFnUUNeA QFq4MMoQbYMJuxU+ZhoVQQKj1M3wNO66in4gpkHjuaR1z0XD2KFPG1Qif Aw+CHTktU3wAlU8zBPdksMCTaW6mIswABGD8lTAn0/vwFwwEFmhJ62pfs AOTkA7fliBjSa/Nvjp6igcqVm74XS1Anzf+GOQZAOdSBklWWKUBiVYCmg faJinEReqEzbz5350M49h/oWnMosfe2FqRESkcTKYjF49/jJfqsfQhOAd KFOPMrolVbAyIHVIfXmGcyf1LnS9ffbcO1hjb0HDOO3K7yzcnhXtkmj73 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="353246758" X-IronPort-AV: E=Sophos;i="6.01,235,1684825200"; d="scan'208";a="353246758" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2023 08:01:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="792404744" X-IronPort-AV: E=Sophos;i="6.01,235,1684825200"; d="scan'208";a="792404744" Received: from sannilnx-dsk.jer.intel.com ([10.12.231.107]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2023 08:01:39 -0700 From: Alexander Usyskin To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Tomas Winkler , Alexander Usyskin , Vitaly Lubart , Andy Shevchenko , Zhang Xiaoxu Subject: [PATCH] mtd: fix use-after-free in mtd release Date: Thu, 27 Jul 2023 17:57:58 +0300 Message-Id: <20230727145758.3880967-1-alexander.usyskin@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I case of partition device_unregister in mtd_device_release calls mtd_release which frees mtd_info structure for partition. All code after device_unregister in mtd_device_release thus works already freed memory. Move part of code to mtd_release and restict mtd->dev cleanup to non-partion object. For partition object such cleanup have no sense as partition mtd_info is removed. Cc: Miquel Raynal Cc: Zhang Xiaoxu Fixes: 19bfa9ebebb5 ("mtd: use refcount to prevent corruption") Reviewed-by: Tomas Winkler Signed-off-by: Alexander Usyskin --- drivers/mtd/mtdcore.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 2466ea466466..46f15f676491 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -93,6 +93,9 @@ static void mtd_release(struct device *dev) struct mtd_info *mtd = dev_get_drvdata(dev); dev_t index = MTD_DEVT(mtd->index); + idr_remove(&mtd_idr, mtd->index); + of_node_put(mtd_get_of_node(mtd)); + if (mtd_is_partition(mtd)) release_mtd_partition(mtd); @@ -103,6 +106,7 @@ static void mtd_release(struct device *dev) static void mtd_device_release(struct kref *kref) { struct mtd_info *mtd = container_of(kref, struct mtd_info, refcnt); + bool is_partition = mtd_is_partition(mtd); debugfs_remove_recursive(mtd->dbg.dfs_dir); @@ -111,11 +115,13 @@ static void mtd_device_release(struct kref *kref) device_unregister(&mtd->dev); - /* Clear dev so mtd can be safely re-registered later if desired */ - memset(&mtd->dev, 0, sizeof(mtd->dev)); - - idr_remove(&mtd_idr, mtd->index); - of_node_put(mtd_get_of_node(mtd)); + /* + * Clear dev so mtd can be safely re-registered later if desired. + * Should not be done for partition, + * as it was already destroyed in device_unregister(). + */ + if (!is_partition) + memset(&mtd->dev, 0, sizeof(mtd->dev)); module_put(THIS_MODULE); } -- 2.34.1