From patchwork Fri Oct 21 10:21:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Zetao X-Patchwork-Id: 6601 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp585734wrr; Fri, 21 Oct 2022 02:26:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7dyP5+iveMZ3kyIxNnIr7ZrzAjp6jkayYRqZfMwtn4376hvvYi36Y4OI/r1qAWo/foHX5z X-Received: by 2002:a05:6402:35d6:b0:45d:a52f:2d30 with SMTP id z22-20020a05640235d600b0045da52f2d30mr15994170edc.4.1666344373242; Fri, 21 Oct 2022 02:26:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666344373; cv=none; d=google.com; s=arc-20160816; b=Jpm+ldcjINIgB4H13AAlr51ZQlPU1YqkkoFAHw56Byndwt9whJJhgyEtWmVODBPlf3 Go54vd2vpuBHJPMXROqxIvJPKu3LDbloAnUTyvapFOv8qiSYtT59CFKhkuGHxfEqkMW8 9F6GkfhmXUxuZpU+lJq46TWjmpOlWQpZBRh256tgF5nAV/dh0iSB6Ajk+lJ7hYFy35KA wS+2G4Nl1B+/0s2rNerohqK1YwI06x5Gy/8ruKjRuvDRt+YINrbvtx3qKuWOCXBFlfjw p18Bt3Dr8N8lpprLIOyQaOXi+uj33ZCpn3DnCo+9zwl+kL7iqt0vWtrFJZpGYkfdTobW GUjg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ejOkwOKkccx9RaPbBpjk/+UIH3MwgWnjnQ1v1Wsn6AE=; b=tBvwZm/9AMxG2VGh8EAPHws8i+neS9GTcv4QJuaVEtNxPyqih94Pv5eFkEAKaClLyF I4FfsgED/Rq6xqCWUtKZpbTfy+c+xsYc9+xoolvizMyHfFOUf0FwblS132Tu64qlvmOR 9W30UXM7lnmcY0t1qACukBzyuCBg3DpndrUNqSksYnW3ig5RP0yvF4sd2R7QfHisfH0R zKNK4brYQ72Y8e7ASgc6UeuwSb3DEAHQ/Vm4ZXFJQXWN+Qu99HtLIuZ7d8mw6Cvf4oyI Xwj8HPv/qX4j81ym/Pbtjs57aIZHDw1DCGNdWAKDH28uCLDSHlf6MTRJ4UtpB8v/gtWT ILFA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js4-20020a17090797c400b00778d193ca81si21880948ejc.550.2022.10.21.02.25.47; Fri, 21 Oct 2022 02:26:13 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231255AbiJUJTw (ORCPT + 99 others); Fri, 21 Oct 2022 05:19:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231219AbiJUJTp (ORCPT ); Fri, 21 Oct 2022 05:19:45 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 185DB2BE09 for ; Fri, 21 Oct 2022 02:19:26 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MtzP725z7zJn2B; Fri, 21 Oct 2022 17:16:43 +0800 (CST) Received: from huawei.com (10.175.101.6) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 17:19:23 +0800 From: Li Zetao To: , , , , , CC: , , , Subject: [PATCH 1/2] ubi: Fix use-after-free when volume resizing failed Date: Fri, 21 Oct 2022 18:21:56 +0800 Message-ID: <20221021102157.1341807-2-lizetao1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221021102157.1341807-1-lizetao1@huawei.com> References: <20221021102157.1341807-1-lizetao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747288717118488501?= X-GMAIL-MSGID: =?utf-8?q?1747288717118488501?= There is an use-after-free problem reported by KASAN: ================================================================== BUG: KASAN: use-after-free in ubi_eba_copy_table+0x11f/0x1c0 [ubi] Read of size 8 at addr ffff888101eec008 by task ubirsvol/4735 CPU: 2 PID: 4735 Comm: ubirsvol Not tainted 6.1.0-rc1-00003-g84fa3304a7fc-dirty #14 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-1.fc33 04/01/2014 Call Trace: dump_stack_lvl+0x34/0x44 print_report+0x171/0x472 kasan_report+0xad/0x130 ubi_eba_copy_table+0x11f/0x1c0 [ubi] ubi_resize_volume+0x4f9/0xbc0 [ubi] ubi_cdev_ioctl+0x701/0x1850 [ubi] __x64_sys_ioctl+0x11d/0x170 do_syscall_64+0x35/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 When ubi_change_vtbl_record() returns an error in ubi_resize_volume(), "new_eba_tbl" will be freed on error handing path, but it is holded by "vol->eba_tbl" in ubi_eba_replace_table(). It means that the liftcycle of "vol->eba_tbl" and "vol" are different, so when resizing volume in next time, it causing an use-after-free fault. Fix it by not freeing "new_eba_tbl" after it replaced in ubi_eba_replace_table(), while will be freed in next volume resizing. Fixes: 801c135ce73d ("UBI: Unsorted Block Images") Signed-off-by: Li Zetao Reviewed-by: Zhihao Cheng --- drivers/mtd/ubi/vmt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index 8fcc0bdf0635..74637575346e 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -464,7 +464,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) for (i = 0; i < -pebs; i++) { err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i); if (err) - goto out_acc; + goto out_free; } spin_lock(&ubi->volumes_lock); ubi->rsvd_pebs += pebs; @@ -512,6 +512,8 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) ubi->avail_pebs += pebs; spin_unlock(&ubi->volumes_lock); } + return err; + out_free: kfree(new_eba_tbl); return err; From patchwork Fri Oct 21 10:21:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Zetao X-Patchwork-Id: 6600 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp583680wrr; Fri, 21 Oct 2022 02:21:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Y58A61nu2cuWl3P+SXZwuwpaloFnASTgaI9KHpfszRv1DFzZYy+1d7keAfUozjcfy2iNn X-Received: by 2002:a05:6402:410:b0:451:ea13:4ed7 with SMTP id q16-20020a056402041000b00451ea134ed7mr17003775edv.262.1666344063730; Fri, 21 Oct 2022 02:21:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666344063; cv=none; d=google.com; s=arc-20160816; b=qLJ7N01OrhW0PPx5IeGaPLFhiziQqSDuMuwGTyU7W2RLTYwQp9GBC45xMOngw0p188 Tddo8os4WVSIJoq/RRt+icJv7YWys28GsJXub6VIFnwPJ73/B0pDPaLJRlp3QcjZRXlL iwFz7ZV5A/0fbCno6grtOfU5dkcehERZGVVXbZUjI3yGpigxoEmSG4wv2dyiQxQjiE/d ht7TrAeUxu1TDT2Z/RGgvjqt3I3VGPOQkucW1SBKGAUJqKuAUlEZ5JuTX/+1f0TWwEU5 9WQOW5Ufg6VR46GDyjgsFD2gz7kCoOLZjUUGdJMS2wSPmLUO7bMrADPLsNRtwdXCYiGW xEpg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=os1k+FRZmSUMAozd5LvXwf8TsiM1RUWyYyNOkLRyi5I=; b=ZM2r9H8GJHRHgDvC4rX6akEBT91Yijf1OqHXkj0/WMq9LHm8l7a4/MQxbVlyDK8VQ3 QgOneKp6bH+KyCAdO0tWnKQRwakORC9B7jn65uWWKe9VZ8KxI5jdRBsezpws6omMOppY L4KRuuxi34XoHt89qycGY4pb0V3sW6LwZvQG1Lpa+rkMLNYbqxvTpHoaSVzKsP8Ur91w +sY6qQaX7NXgbqCqnWjKu4boffgkp6fSEJa04yCMp3aRaP36gdO5H+EB31ntYhFIrueX 4reTWdaeu4xBHauJ4mZNZCyABMIl90uQcmKuyNjRSqWRKWSb6EccA+e1dfOW6RTKsxlG shrw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hr2-20020a1709073f8200b007919b3ad78esi11025042ejc.495.2022.10.21.02.20.38; Fri, 21 Oct 2022 02:21:03 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231138AbiJUJTq (ORCPT + 99 others); Fri, 21 Oct 2022 05:19:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231130AbiJUJTo (ORCPT ); Fri, 21 Oct 2022 05:19:44 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1932E2CE1B for ; Fri, 21 Oct 2022 02:19:27 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MtzS45g4yzHtwg; Fri, 21 Oct 2022 17:19:16 +0800 (CST) Received: from huawei.com (10.175.101.6) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 17:19:24 +0800 From: Li Zetao To: , , , , , CC: , , , Subject: [PATCH 2/2] ubi: Fix unreferenced object reported by kmemleak in ubi_resize_volume() Date: Fri, 21 Oct 2022 18:21:57 +0800 Message-ID: <20221021102157.1341807-3-lizetao1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221021102157.1341807-1-lizetao1@huawei.com> References: <20221021102157.1341807-1-lizetao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747288392931548953?= X-GMAIL-MSGID: =?utf-8?q?1747288392931548953?= There is a memory leaks problem reported by kmemleak: unreferenced object 0xffff888102007a00 (size 128): comm "ubirsvol", pid 32090, jiffies 4298464136 (age 2361.231s) hex dump (first 32 bytes): ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ backtrace: [] __kmalloc+0x4d/0x150 [] ubi_eba_create_table+0x76/0x170 [ubi] [] ubi_resize_volume+0x1be/0xbc0 [ubi] [] ubi_cdev_ioctl+0x701/0x1850 [ubi] [] __x64_sys_ioctl+0x11d/0x170 [] do_syscall_64+0x35/0x80 [] entry_SYSCALL_64_after_hwframe+0x46/0xb0 This is due to a mismatch between create and destroy interfaces, and in detail that "new_eba_tbl" created by ubi_eba_create_table() but destroyed by kfree(), while will causing "new_eba_tbl->entries" not freed. Fix it by replacing kfree(new_eba_tbl) with ubi_eba_destroy_table(new_eba_tbl) Fixes: 799dca34ac54 ("UBI: hide EBA internals") Signed-off-by: Li Zetao Reviewed-by: Zhihao Cheng --- drivers/mtd/ubi/vmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index 74637575346e..9fbc64b997ce 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -515,7 +515,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) return err; out_free: - kfree(new_eba_tbl); + ubi_eba_destroy_table(new_eba_tbl); return err; }