From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev,
	Zhang Wensheng <zhangwensheng5@huawei.com>,
	Yu Kuai <yukuai3@huawei.com>, Josef Bacik <josef@toxicpanda.com>,
	Jens Axboe <axboe@kernel.dk>,
	Wen Yang <wenyang.linux@foxmail.com>
Subject: [PATCH 5.15 28/36] nbd: fix possible overflow on first_minor in nbd_dev_add()
Date: Thu, 23 Feb 2023 14:07:04 +0100	[thread overview]
Message-ID: <20230223130430.370121519@linuxfoundation.org> (raw)
In-Reply-To: <20230223130429.072633724@linuxfoundation.org>
From: Zhang Wensheng <zhangwensheng5@huawei.com>
commit 858f1bf65d3d9c00b5e2d8ca87dc79ed88267c98 upstream.
When 'index' is a big numbers, it may become negative which forced
to 'int'. then 'index << part_shift' might overflow to a positive
value that is not greater than '0xfffff', then sysfs might complains
about duplicate creation. Because of this, move the 'index' judgment
to the front will fix it and be better.
Fixes: b0d9111a2d53 ("nbd: use an idr to keep track of nbd devices")
Fixes: 940c264984fd ("nbd: fix possible overflow for 'first_minor' in nbd_dev_add()")
Signed-off-by: Zhang Wensheng <zhangwensheng5@huawei.com>
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Link: https://lore.kernel.org/r/20220521073749.3146892-6-yukuai3@huawei.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Wen Yang <wenyang.linux@foxmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/block/nbd.c |   23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1756,17 +1756,7 @@ static struct nbd_device *nbd_dev_add(in
 	refcount_set(&nbd->refs, 0);
 	INIT_LIST_HEAD(&nbd->list);
 	disk->major = NBD_MAJOR;
-
-	/* Too big first_minor can cause duplicate creation of
-	 * sysfs files/links, since index << part_shift might overflow, or
-	 * MKDEV() expect that the max bits of first_minor is 20.
-	 */
 	disk->first_minor = index << part_shift;
-	if (disk->first_minor < index || disk->first_minor > MINORMASK) {
-		err = -EINVAL;
-		goto out_free_work;
-	}
-
 	disk->minors = 1 << part_shift;
 	disk->fops = &nbd_fops;
 	disk->private_data = nbd;
@@ -1871,8 +1861,19 @@ static int nbd_genl_connect(struct sk_bu
 	if (!netlink_capable(skb, CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (info->attrs[NBD_ATTR_INDEX])
+	if (info->attrs[NBD_ATTR_INDEX]) {
 		index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
+
+		/*
+		 * Too big first_minor can cause duplicate creation of
+		 * sysfs files/links, since index << part_shift might overflow, or
+		 * MKDEV() expect that the max bits of first_minor is 20.
+		 */
+		if (index < 0 || index > MINORMASK >> part_shift) {
+			printk(KERN_ERR "nbd: illegal input index %d\n", index);
+			return -EINVAL;
+		}
+	}
 	if (!info->attrs[NBD_ATTR_SOCKETS]) {
 		printk(KERN_ERR "nbd: must specify at least one socket\n");
 		return -EINVAL;
next prev parent reply	other threads:[~2023-02-23 13:12 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-23 13:06 [PATCH 5.15 00/36] 5.15.96-rc1 review Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 01/36] drm/etnaviv: dont truncate physical page address Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 02/36] wifi: rtl8xxxu: gen2: Turn on the rate control Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 03/36] drm/edid: Fix minimum bpc supported with DSC1.2 for HDMI sink Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 04/36] clk: mxl: Switch from direct readl/writel based IO to regmap based IO Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 05/36] clk: mxl: Remove redundant spinlocks Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 06/36] clk: mxl: Add option to override gate clks Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 07/36] clk: mxl: Fix a clk entry by adding relevant flags Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 08/36] powerpc: dts: t208x: Mark MAC1 and MAC2 as 10G Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 09/36] clk: mxl: syscon_node_to_regmap() returns error pointers Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 10/36] random: always mix cycle counter in add_latent_entropy() Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 11/36] KVM: x86: Fail emulation during EMULTYPE_SKIP on any exception Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 12/36] KVM: SVM: Skip WRMSR fastpath on VM-Exit if next RIP isnt valid Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 13/36] KVM: VMX: Execute IBPB on emulated VM-exit when guest has IBRS Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 14/36] can: kvaser_usb: hydra: help gcc-13 to figure out cmd_len Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 15/36] powerpc: dts: t208x: Disable 10G on MAC1 and MAC2 Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 16/36] powerpc: use generic version of arch_is_kernel_initmem_freed() Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 17/36] powerpc/vmlinux.lds: Ensure STRICT_ALIGN_SIZE is at least page aligned Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 18/36] powerpc/vmlinux.lds: Add an explicit symbol for the SRWX boundary Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 19/36] powerpc/64s/radix: Fix crash with unaligned relocated kernel Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 20/36] powerpc/64s/radix: Fix RWX mapping with " Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 21/36] drm/i915/gvt: fix double free bug in split_2MB_gtt_entry Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 22/36] uaccess: Add speculation barrier to copy_from_user() Greg Kroah-Hartman
2023-02-23 13:06 ` [PATCH 5.15 23/36] binder: read pre-translated fds from sender buffer Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 24/36] binder: defer copies of pre-patched txn data Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 25/36] binder: fix pointer cast warning Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 26/36] binder: Address corner cases in deferred copy and fixup Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 27/36] binder: Gracefully handle BINDER_TYPE_FDA objects with num_fds=0 Greg Kroah-Hartman
2023-02-23 13:07 ` Greg Kroah-Hartman [this message]
2023-02-23 13:07 ` [PATCH 5.15 29/36] wifi: mwifiex: Add missing compatible string for SD8787 Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 30/36] audit: update the mailing list in MAINTAINERS Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 31/36] ext4: Fix function prototype mismatch for ext4_feat_ktype Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 32/36] kbuild: Add CONFIG_PAHOLE_VERSION Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 33/36] scripts/pahole-flags.sh: Use pahole-version.sh Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 34/36] lib/Kconfig.debug: Use CONFIG_PAHOLE_VERSION Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 35/36] lib/Kconfig.debug: Allow BTF + DWARF5 with pahole 1.21+ Greg Kroah-Hartman
2023-02-23 13:07 ` [PATCH 5.15 36/36] Revert "net/sched: taprio: make qdisc_leaf() see the per-netdev-queue pfifo child qdiscs" Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=20230223130430.370121519@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=axboe@kernel.dk \
    --cc=josef@toxicpanda.com \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=wenyang.linux@foxmail.com \
    --cc=yukuai3@huawei.com \
    --cc=zhangwensheng5@huawei.com \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).