Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp819087imn; Sat, 30 Jul 2022 04:28:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR5vGTypwE68U6fVqzqjF4rIgN5IRcRaQXZ2FvPMWOQ1Om9tDhI5v3cUd5WPLFwiALhWDrWT X-Received: by 2002:a05:6402:b88:b0:43d:6175:761f with SMTP id cf8-20020a0564020b8800b0043d6175761fmr1082873edb.237.1659180493218; Sat, 30 Jul 2022 04:28:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659180493; cv=none; d=google.com; s=arc-20160816; b=Am2YkjpwDxlhHOBfidDWZ9G/vjYKRvVMLQI5EUm/2XpgLazPRYpO99CKKA15xuF61l ubB+nP9Swb1yCeXA0REvZ+YqKmZvJhZEiNR7fwQSrfOzeQ3XuoBwKEx9io9KDw7MuYkj RYvzaWsKisxaosAd+bGtFqfwyepvMBbD/EUjlqUtLGqzCkW3emVpzuiot7NSOiuIeAVB DaJw8M9B2E+6D34NlHTZBoIgNKL3hamK0pk49nTSd2ALZS9u8pKa2zKuvYek6jRpcUH7 ZQOopH/47XN5FIqVA0tjJ1LzsRWHnE9BaG5q16+3882gtkSY6A+uGRlPlduGu3d8wVLK pi6g== 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=tJQU2OSpy4HTOukWE3Bs6f8gJDMZKoygAvAl1bTg7s4=; b=QrHiZp8bcJDOBaC5aedDgRBSi5XzMmZIJ8nd5nY5vyb2meU+j+IciRDRu2t7potIH4 9mWr6UB3UDknM0C1qFrmmOF3bHnHiyvXX4iQBCRt9CHbdbnd344tsK9bXlH9VN4Hb5zV vCsHIIk3Vr82S4B/l7j0JNJ2tHF9rkP9QS4ih12waYgBcdAvjaf17NbQjfqLuAr1IEFU MtDT33kJO+P/lT5wl3j8cgbcvaoRFTx//uoKkHvxKD5yrxKvHigjGmlVZZENipiC/ep0 LJFmxK6s/40EWL9Eu6az1pUhcJx+3iVO6/qpa4TFPtfPmAJQk3jGwzxl6B3dL6zuY723 bl2w== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hw10-20020a170907a0ca00b00711c9e99bdbsi4470423ejc.258.2022.07.30.04.27.48; Sat, 30 Jul 2022 04:28: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 S234160AbiG3LQn (ORCPT + 99 others); Sat, 30 Jul 2022 07:16:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234131AbiG3LQl (ORCPT ); Sat, 30 Jul 2022 07:16:41 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 174871EC40 for ; Sat, 30 Jul 2022 04:16:40 -0700 (PDT) Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Lw1yN4yqkz9svc; Sat, 30 Jul 2022 19:15:24 +0800 (CST) Received: from kwepemm600013.china.huawei.com (7.193.23.68) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 30 Jul 2022 19:16:38 +0800 Received: from huawei.com (10.175.127.227) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 30 Jul 2022 19:16:37 +0800 From: Zhihao Cheng To: , , , , , CC: , , , Subject: [PATCH] ubi: Fix UAF wear-leveling entry in eraseblk_count_seq_show() Date: Sat, 30 Jul 2022 19:28:37 +0800 Message-ID: <20220730112837.51184-1-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600013.china.huawei.com (7.193.23.68) 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 Wear-leveling entry could be freed in error path, which may be accessed again in eraseblk_count_seq_show(), for example: __erase_worker eraseblk_count_seq_show wl = ubi->lookuptbl[*block_number] if (wl) wl_entry_destroy ubi->lookuptbl[e->pnum] = NULL kmem_cache_free(ubi_wl_entry_slab, e) erase_count = wl->ec // UAF! Wear-leveling entry updating/accessing in ubi->lookuptbl should be protected by ubi->wl_lock, fix it by adding ubi->wl_lock to serialize wl entry accessing between wl_entry_destroy() and eraseblk_count_seq_show(). Fetch a reproducer in [Link]. Link: https://bugzilla.kernel.org/show_bug.cgi?id=216305 Fixes: 7bccd12d27b7e3 ("ubi: Add debugfs file for tracking PEB state") Fixes: 801c135ce73d5d ("UBI: Unsorted Block Images") Signed-off-by: Zhihao Cheng --- drivers/mtd/ubi/wl.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c index ee0100740869..5218e6edde58 100644 --- a/drivers/mtd/ubi/wl.c +++ b/drivers/mtd/ubi/wl.c @@ -890,8 +890,11 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk, err = do_sync_erase(ubi, e1, vol_id, lnum, 0); if (err) { - if (e2) + if (e2) { + spin_lock(&ubi->wl_lock); wl_entry_destroy(ubi, e2); + spin_unlock(&ubi->wl_lock); + } goto out_ro; } @@ -1130,14 +1133,18 @@ static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk) /* Re-schedule the LEB for erasure */ err1 = schedule_erase(ubi, e, vol_id, lnum, 0, false); if (err1) { + spin_lock(&ubi->wl_lock); wl_entry_destroy(ubi, e); + spin_unlock(&ubi->wl_lock); err = err1; goto out_ro; } return err; } + spin_lock(&ubi->wl_lock); wl_entry_destroy(ubi, e); + spin_unlock(&ubi->wl_lock); if (err != -EIO) /* * If this is not %-EIO, we have no idea what to do. Scheduling -- 2.31.1