From patchwork Mon Aug 7 12:37:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Volkov X-Patchwork-Id: 9885335 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0E00B603B4 for ; Mon, 7 Aug 2017 12:48:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F416B2867C for ; Mon, 7 Aug 2017 12:48:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E8FFA28681; Mon, 7 Aug 2017 12:48:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8AB2D2867C for ; Mon, 7 Aug 2017 12:48:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751758AbdHGMsC (ORCPT ); Mon, 7 Aug 2017 08:48:02 -0400 Received: from bran.ispras.ru ([83.149.199.196]:34252 "EHLO smtp.ispras.ru" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751704AbdHGMsC (ORCPT ); Mon, 7 Aug 2017 08:48:02 -0400 X-Greylist: delayed 591 seconds by postgrey-1.27 at vger.kernel.org; Mon, 07 Aug 2017 08:48:02 EDT Received: from berg.intra.ispras.ru (berg.intra.ispras.ru [10.10.2.208]) by smtp.ispras.ru (Postfix) with ESMTP id CBEFC60916; Mon, 7 Aug 2017 15:38:08 +0300 (MSK) From: Anton Volkov To: tom.leiming@gmail.com Cc: axboe@fb.com, osandov@fb.com, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, ldv-project@linuxtesting.org, khoroshilov@ispras.ru, Anton Volkov Subject: [PATCH] loop: fix to a race condition due to the early registration of device Date: Mon, 7 Aug 2017 15:37:50 +0300 Message-Id: <1502109470-2945-1-git-send-email-avolkov@ispras.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The early device registration made possible a race leading to allocations of disks with wrong minors. This patch moves the device registration further down the loop_init function to make the race infeasible. Found by Linux Driver Verification project (linuxtesting.org). Signed-off-by: Anton Volkov Reviewed-by: Ming Lei Reviewed-by: Johannes Thumshirn --- drivers/block/loop.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index ef83349..2fbd4089 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1996,10 +1996,6 @@ static int __init loop_init(void) struct loop_device *lo; int err; - err = misc_register(&loop_misc); - if (err < 0) - return err; - part_shift = 0; if (max_part > 0) { part_shift = fls(max_part); @@ -2017,12 +2013,12 @@ static int __init loop_init(void) if ((1UL << part_shift) > DISK_MAX_PARTS) { err = -EINVAL; - goto misc_out; + goto err_out; } if (max_loop > 1UL << (MINORBITS - part_shift)) { err = -EINVAL; - goto misc_out; + goto err_out; } /* @@ -2041,6 +2037,11 @@ static int __init loop_init(void) range = 1UL << MINORBITS; } + err = misc_register(&loop_misc); + if (err < 0) + goto err_out; + + if (register_blkdev(LOOP_MAJOR, "loop")) { err = -EIO; goto misc_out; @@ -2060,6 +2061,7 @@ static int __init loop_init(void) misc_out: misc_deregister(&loop_misc); +err_out: return err; }