Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4895684pxb; Tue, 2 Nov 2021 18:22:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqFF6SYLWlL6gwBYrTACkIa1CcdLbO+aUPokJ8icBPUQ9NvFleDir6EZODjt1YRHWgydjD X-Received: by 2002:a50:da48:: with SMTP id a8mr56067133edk.146.1635902575731; Tue, 02 Nov 2021 18:22:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635902575; cv=none; d=google.com; s=arc-20160816; b=UT0g3kN1wnzz72Ib4CGi0eM0mjytweyBEvEAa8GfY8QrXxFzJJNh9Fp9+7mhymqRHd aC1oxFIuIjhqgB/6tQdgHKJhA2kJc6Q8g0fWGIlxDZ0mAdwrdeQGOMMWIJjSXgorrIal 5Ubi9X3ODE5FYNtFWQZMoUr88gp8ub5+HaqVijnQ+b2HaZaa3bJKjYXRW0VB5iypB3sZ 4Y2rIofbfm3qHKQG8SNT52RHFjKgnwcTvJQFuJMInlIaXmscsr6uafVKhCK/XiAs5zBb G4Wsxa1fA+8Eyx0wDK9Cmyq1aptc7AqHraitGvxHUEQZc4akJC2lCSBYFJqMWNSKXGyx h9zw== 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=uM//MJ2T8uEV86KspMyzHVzvMNPiwav0v7eVPv5XGhQ=; b=OTkF1Uvp6cgoNppd/zhDEBDVG9KF/mZlQDifzAlIqa44BA6Du1ZvcHAsc6K1hwFB1R eWJAF9ySuwE7QVmj114km2hwTV1zhl18c1xE+LxT52nYFtN5xQ+xYOZfB98/BW43pGp7 peue+RpEPI5V0FI18dSJWHgUq63Ix5iPOrMXMhxKm7XRQc+786DDHIxcyrngk4b0grL6 Aj37dYliX5/d2ApuheIUGJX06Uzw6YRjYWxrXPWkNricgZdbmFD7LkoGitNPhxwjnLN6 bdp+WOq0qcdWh85NVr6rYidDqMi2Gll/stYfisXd/+HzANAncGmisLsLvcxhrCtPQDNT yTUw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qb2si1226644ejc.706.2021.11.02.18.22.09; Tue, 02 Nov 2021 18:22:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233196AbhKCBWv (ORCPT + 99 others); Tue, 2 Nov 2021 21:22:51 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:25346 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231847AbhKCBWr (ORCPT ); Tue, 2 Nov 2021 21:22:47 -0400 Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HkTNF3r9FzbhGy; Wed, 3 Nov 2021 09:15:25 +0800 (CST) Received: from dggpeml500020.china.huawei.com (7.185.36.88) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Wed, 3 Nov 2021 09:20:09 +0800 Received: from huawei.com (10.175.127.227) by dggpeml500020.china.huawei.com (7.185.36.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Wed, 3 Nov 2021 09:20:09 +0800 From: Baokun Li To: , , , , CC: , , , , Hulk Robot Subject: [PATCH -next V2 1/2] ubi: fix race between volume operations and uif_init Date: Wed, 3 Nov 2021 09:32:48 +0800 Message-ID: <20211103013249.498805-2-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211103013249.498805-1-libaokun1@huawei.com> References: <20211103013249.498805-1-libaokun1@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500020.china.huawei.com (7.185.36.88) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hulk Robot reported a KASAN report about use-after-free: ================================================================== BUG: KASAN: use-after-free in __list_del_entry_valid+0x13d/0x160 Read of size 8 at addr ffff888035e37d98 by task ubiattach/1385 [...] Call Trace: klist_dec_and_del+0xa7/0x4a0 klist_put+0xc7/0x1a0 device_del+0x4d4/0xed0 cdev_device_del+0x1a/0x80 ubi_attach_mtd_dev+0x2951/0x34b0 [ubi] ctrl_cdev_ioctl+0x286/0x2f0 [ubi] Allocated by task 1414: device_add+0x60a/0x18b0 cdev_device_add+0x103/0x170 ubi_create_volume+0x1118/0x1a10 [ubi] ubi_cdev_ioctl+0xb7f/0x1ba0 [ubi] Freed by task 1385: cdev_device_del+0x1a/0x80 ubi_remove_volume+0x438/0x6c0 [ubi] ubi_cdev_ioctl+0xbf4/0x1ba0 [ubi] [...] ================================================================== The following race could cause the use-after-free problem: cpu1 cpu2 cpu3 _______________________|________________________|______________________ ctrl_cdev_ioctl ubi_attach_mtd_dev uif_init ubi_cdev_ioctl ubi_create_volume cdev_device_add ubi_add_volume // sysfs exist kill_volumes ubi_cdev_ioctl ubi_remove_volume cdev_device_del // first free ubi_free_volume cdev_del // double free cdev_device_del The lock held by ctrl_cdev_ioctl is ubi_devices_mutex, but the lock held by ubi_cdev_ioctl is ubi->device_mutex. Therefore, the two locks can be concurrent. However, volume addition and deletion operations are performed in both ctrl_cdev_ioctl and ubi_cdev_ioctl. When these operations are performed concurrently in both ioctl, a race condition may be triggered.The UAF stack above is one such case. To solve this problem, add spin_lock(&ubi->volumes_lock) to ctrl_cdev_ioctl where volumes are added and deleted. Fixes: 801c135ce73d ("UBI: Unsorted Block Images") Reported-by: Hulk Robot Signed-off-by: Baokun Li --- drivers/mtd/ubi/build.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index a7e3eb9befb6..708b1b96de01 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -467,6 +467,7 @@ static int uif_init(struct ubi_device *ubi) if (err) goto out_unreg; + spin_lock(&ubi->volumes_lock); for (i = 0; i < ubi->vtbl_slots; i++) if (ubi->volumes[i]) { err = ubi_add_volume(ubi, ubi->volumes[i]); @@ -475,11 +476,13 @@ static int uif_init(struct ubi_device *ubi) goto out_volumes; } } + spin_unlock(&ubi->volumes_lock); return 0; out_volumes: kill_volumes(ubi); + spin_unlock(&ubi->volumes_lock); cdev_device_del(&ubi->cdev, &ubi->dev); out_unreg: unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1); -- 2.31.1