Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp309490pxf; Thu, 25 Mar 2021 04:48:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfbVBKz6/EjSxUO25XMgFfPfBO4HoOGANCQq5BdOq89es1QKq1jgfHnK6X+TlROCqi71G1 X-Received: by 2002:aa7:cf02:: with SMTP id a2mr8552046edy.59.1616672886050; Thu, 25 Mar 2021 04:48:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616672886; cv=none; d=google.com; s=arc-20160816; b=bVoxD3pxGDqv3Ga3+UWT1dgou0tDrcC77u5SfbnX8QiRzhdhU4vhp8z4cmtYm1Cj80 Z3pfYg95luUushxdPLnhHvwLQwFZarrbq5ymT9tiKAXkhKyWk0jqxSgyARyIl/f9JCH3 pXGmRDw92iRZUJxJrtQ+thMveg8hdHJdxBQD5cH5yO8qbt2utOF1gHpIPQyKYC+4HeEI MwWt/hMswbLqFINlYSn2smXOmoduh0CSAuOK3Elvx0n4xh43bUVywed1YxQJlJwRMon7 K0iwAy1OMYgXiYAHFC7tMqJ+qHe87RP9isDQOafnN69XWcO7OTDX8tZ8y/VzxIkqyjyu /fow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:user-agent:date:message-id:subject:from:cc:to; bh=KgiBVR10PJ0DcTB0ANZm5UoqIlDEsntkIpYLBfAkCTw=; b=BQighH8CLFR2WOuC76o2558BlSpkq043wV8HgfZnzxarELijKYaTvQquoZd08w/MWO rkgjiYIzGnw7xuXXHv5VfOQtHzXtacNVmw3bQ+fvgBRepCC+JqekS0bdOsUSe7gAGziy 6N0RhAcuaCsR3WWEb9jyn94CBxzK/Dqx9HkfFO+LzqodrfISCEdoy2VOPbAuBZrZ/b3V /2RavRSo7zTv8wujpTGFns2Txi7E6va9fRmL8PGuRkAde5x2aOh7LWlBzWHglVO9c7ks rc4b6f1ulmg8NIBuxniq5+1W15NogqmafA237bFNg8pbC6BVnTl8SqV5A7P0QU+fQRuQ GyOQ== 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 d5si3891107edr.381.2021.03.25.04.47.37; Thu, 25 Mar 2021 04:48:06 -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 S229995AbhCYLpj (ORCPT + 99 others); Thu, 25 Mar 2021 07:45:39 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:14534 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230210AbhCYLpL (ORCPT ); Thu, 25 Mar 2021 07:45:11 -0400 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4F5jrr38VmzPkKf; Thu, 25 Mar 2021 19:42:36 +0800 (CST) Received: from [10.174.178.35] (10.174.178.35) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.498.0; Thu, 25 Mar 2021 19:45:02 +0800 To: , , CC: linfeilong , "liuzhiqiang (I)" From: lixiaokeng Subject: [PATCH] brd: fix integer overflow in brd_check_and_reset_par Message-ID: <99e9da32-9372-ada2-8197-26602fe242c8@huawei.com> Date: Thu, 25 Mar 2021 19:45:02 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.178.35] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The max_part may overflow. For example, modprobe brd rd_nr=3 rd_size=102400 max_part=1073741824(2^30) Expected result NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT ram0 1:0 0 100M 0 disk ram1 1:256 0 100M 0 disk ram2 1:512 0 100M 0 disk Actual result NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT ram0 259:0 0 100M 0 disk ram1 259:1 0 100M 0 disk ram2 259:2 0 100M 0 disk Fix it. Signed-off-by: Lixiaokeng Signed-off-by: Zhiqiang Liu --- drivers/block/brd.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/block/brd.c b/drivers/block/brd.c index c43a6ab4b1f3..c91831cd5d2a 100644 --- a/drivers/block/brd.c +++ b/drivers/block/brd.c @@ -457,21 +457,19 @@ static void brd_del_one(struct brd_device *brd) static inline void brd_check_and_reset_par(void) { - if (unlikely(!max_part)) + if (unlikely(max_part <= 0)) max_part = 1; /* * make sure 'max_part' can be divided exactly by (1U << MINORBITS), * otherwise, it is possiable to get same dev_t when adding partitions. */ - if ((1U << MINORBITS) % max_part != 0) - max_part = 1UL << fls(max_part); - if (max_part > DISK_MAX_PARTS) { pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n", DISK_MAX_PARTS, DISK_MAX_PARTS); max_part = DISK_MAX_PARTS; - } + } else if ((1U << MINORBITS) % max_part != 0) + max_part = 1UL << fls(max_part); } static int __init brd_init(void) --