Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1886405pxb; Mon, 13 Sep 2021 07:32:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRbBaCOTwbfNfIAcDUNa7paOHEDrxoBeZ9JeALhKzGsT5bc+296CRvu0h2TRwW4J8cY8Lr X-Received: by 2002:a05:6638:3392:: with SMTP id h18mr8694272jav.98.1631543548733; Mon, 13 Sep 2021 07:32:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631543548; cv=none; d=google.com; s=arc-20160816; b=v9zSRKppcIGKau8yC7IanEHCW5nfVib4ynscIE3YXc1iq7mfzWOEIYCQWGLXpBBRXA ej2sBtItZqRgYdctY17ANFJavUrAhsO0sG5FtnPwwEuMJihgpUPUAEM9OjrA+oV1h/I5 81ZWtF1iFqs2xrSmY1sjJO47ERWj4ry+ltOm50G4bwVn/J12UCYLIW7n98ZZJ6UkzeE+ a8LDTw8udIIXWn93zwvqliboiCa8j/k0gUDK8DmIll5uldhUuMuYAIPUl2TFqyMGjx8Q P9vkopco7MjGFsgM05LpD5zlAG6C1SZzMfoGKvE/EzDi5htjXklvssmzMT+ONpPDNtxh 3VVQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=04uCzNz4kKfAainrrqZCB4MTWs23SbrPj4IowkZyWZw=; b=h/58AxYs7OWqnrBQPXIrKxHYUi6yRClJw5oo+CblHYQezBBD/Uk8WStcgbkjedfEIx TLA9XLeJlMyOIXt0I5yedXZh5f4OyUk1zVrKdFEp612/jDquaSU+cF+qOUM4XRXyBlas ToV3vWa+Ppz+jy/AIsGGMa0RlCM3YCnylefm3sSNQAUHJFg+2zzJCWgpG4XvfD8Zf/ZP QdTidOJ2bAjCVUiSUXoqD26+DRs9R7lt3rq+8UqoOryirpsly+bNRD6zgTf0H4TrYVHs P9pvVkYEAnes/ymrllcqEIFXrW8zdHDQxB9x/5/sZmKKdbLm3Jc8dbuG409VHoZU27++ gKLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xjpxf6tj; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q15si8559629jaj.16.2021.09.13.07.32.15; Mon, 13 Sep 2021 07:32:28 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xjpxf6tj; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347248AbhIMOag (ORCPT + 99 others); Mon, 13 Sep 2021 10:30:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:46952 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345453AbhIMO0c (ORCPT ); Mon, 13 Sep 2021 10:26:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5B1C861B5F; Mon, 13 Sep 2021 13:49:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631540943; bh=p8yyQc5/4w+JVWmWQWdhpaKX6u20yEqXCmCwgkXq114=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xjpxf6tjo7bBwQm0kioNjXuWX53DOgpNKLpTwRiQScJ0FuC9YQDZXuWBHqNPYWHw6 bcg9htcxcWCOhZVXRbNxQqEcqYSh4+Qr/Yd//Lej7arqzhj6mAEPkDyfipOUAyWLcD 3JR0eD3zhoFFXpFoXvzV6GmsqBYlEZYfCu2Ked5A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+9937dc42271cd87d4b98@syzkaller.appspotmail.com, Christoph Hellwig , Pavel Skripkin , Jens Axboe , Sasha Levin Subject: [PATCH 5.14 066/334] block: nbd: add sanity check for first_minor Date: Mon, 13 Sep 2021 15:12:00 +0200 Message-Id: <20210913131115.641480769@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131113.390368911@linuxfoundation.org> References: <20210913131113.390368911@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Skripkin [ Upstream commit b1a811633f7321cf1ae2bb76a66805b7720e44c9 ] Syzbot hit WARNING in internal_create_group(). The problem was in too big disk->first_minor. disk->first_minor is initialized by value, which comes from userspace and there wasn't any sanity checks about value correctness. It can cause duplicate creation of sysfs files/links, because disk->first_minor will be passed to MKDEV() which causes truncation to byte. Since maximum minor value is 0xff, let's check if first_minor is correct minor number. NOTE: the root case of the reported warning was in wrong error handling in register_disk(), but we can avoid passing knowingly wrong values to sysfs API, because sysfs error messages can confuse users. For example: user passed 1048576 as index, but sysfs complains about duplicate creation of /dev/block/43:0. It's not obvious how 1048576 becomes 0. Log and reproducer for above example can be found on syzkaller bug report page. Link: https://syzkaller.appspot.com/bug?id=03c2ae9146416edf811958d5fd7acfab75b143d1 Fixes: b0d9111a2d53 ("nbd: use an idr to keep track of nbd devices") Reported-by: syzbot+9937dc42271cd87d4b98@syzkaller.appspotmail.com Reviewed-by: Christoph Hellwig Signed-off-by: Pavel Skripkin Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/nbd.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 7ed888e99f09..93708b1938e8 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -1770,7 +1770,17 @@ static int nbd_dev_add(int index) refcount_set(&nbd->refs, 1); INIT_LIST_HEAD(&nbd->list); disk->major = NBD_MAJOR; + + /* Too big first_minor can cause duplicate creation of + * sysfs files/links, since first_minor will be truncated to + * byte in __device_add_disk(). + */ disk->first_minor = index << part_shift; + if (disk->first_minor > 0xff) { + err = -EINVAL; + goto out_free_idr; + } + disk->minors = 1 << part_shift; disk->fops = &nbd_fops; disk->private_data = nbd; -- 2.30.2