From patchwork Tue Jul 16 22:15:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guilherme G. Piccoli" X-Patchwork-Id: 1132971 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45pF8s48KDz9sDQ; Wed, 17 Jul 2019 08:15:57 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1hnVji-0004c2-1r; Tue, 16 Jul 2019 22:15:50 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1hnVjg-0004bd-3t for kernel-team@lists.ubuntu.com; Tue, 16 Jul 2019 22:15:48 +0000 Received: from mail-qt1-f197.google.com ([209.85.160.197]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1hnVjf-0007Hj-Nq for kernel-team@lists.ubuntu.com; Tue, 16 Jul 2019 22:15:47 +0000 Received: by mail-qt1-f197.google.com with SMTP id x1so19479323qts.9 for ; Tue, 16 Jul 2019 15:15:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lx8doizMrxzxlo1A5C1mRT83H6AwxIiz8sPrDTOsTJ8=; b=BUiE/FDG31tWCVjCUK5QR6GAdeRijJZQubdhnHIWc/H5CcnuVETRF1vTTCkT0ckzuD QuD+Z+oD5rpmvgzYEBZQdUtlnBriHt7fMBUWU3bvHPYvJgc3Rb1n3BdfHE8om/SRGbb4 NDO15+v+NppXWaT0skyaqx4rCE2Xcl0N0TtdoppiCGgAEfdEcMjzeX6hEl4z3NQR1dcu 6lyVVwe5SxD8CJNg2mkDkjhXCn47QyFfmBibLSqr0JaEVxe/qtKVAC/h/jROdmOd4Dkm wgi5/cQ4SFHoI7JVLe7iqJDAmfR7WyM6I/xJJa3jZYrSnVkAcOnfnECF8xc/5R31wTLb YaYw== X-Gm-Message-State: APjAAAUIGHwY29pY1Lth/UYV8d6d2DplZ8F8WGzHe/0oflry0E2kzNvB MIjbVXJbbgRoMOmeDq5uTIRCMDZwXFSqYw/UFtHtayS/l9l/GrXKlkknGTaU4aDpdQhtpZn07TB BmmOsRzGr534cVJt5+dtmGnP3PKtx+D6YXOgFBbjEag== X-Received: by 2002:ac8:290c:: with SMTP id y12mr24086304qty.141.1563315346623; Tue, 16 Jul 2019 15:15:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqybFOswxAjF/RL037ZU5tAkx5JwjC5QcOLHF2jqf1H2Mv9ngQuLMQrxxeW6PwfQBCyQ8OxJsA== X-Received: by 2002:ac8:290c:: with SMTP id y12mr24086291qty.141.1563315346413; Tue, 16 Jul 2019 15:15:46 -0700 (PDT) Received: from localhost (201-42-108-61.dsl.telesp.net.br. [201.42.108.61]) by smtp.gmail.com with ESMTPSA id r5sm10064232qkc.42.2019.07.16.15.15.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Jul 2019 15:15:45 -0700 (PDT) From: "Guilherme G. Piccoli" To: kernel-team@lists.ubuntu.com Subject: [SRU D][PATCH 1/2] block: Fix a NULL pointer dereference in generic_make_request() Date: Tue, 16 Jul 2019 19:15:38 -0300 Message-Id: <20190716221539.31891-2-gpiccoli@canonical.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190716221539.31891-1-gpiccoli@canonical.com> References: <20190716221539.31891-1-gpiccoli@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gpiccoli@canonical.com Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" BugLink: https://bugs.launchpad.net/bugs/1836806 ----------------------------------------------------------------- This patch is not on mainline and is meant to 4.19 stable *only*. After the patch description there's a reasoning about that. ----------------------------------------------------------------- Commit 37f9579f4c31 ("blk-mq: Avoid that submitting a bio concurrently with device removal triggers a crash") introduced a NULL pointer dereference in generic_make_request(). The patch sets q to NULL and enter_succeeded to false; right after, there's an 'if (enter_succeeded)' which is not taken, and then the 'else' will dereference q in blk_queue_dying(q). This patch just moves the 'q = NULL' to a point in which it won't trigger the oops, although the semantics of this NULLification remains untouched. A simple test case/reproducer is as follows: a) Build kernel v4.19.56-stable with CONFIG_BLK_CGROUP=n. b) Create a raid0 md array with 2 NVMe devices as members, and mount it with an ext4 filesystem. c) Run the following oneliner (supposing the raid0 is mounted in /mnt): (dd of=/mnt/tmp if=/dev/zero bs=1M count=999 &); sleep 0.3; echo 1 > /sys/block/nvme1n1/device/device/remove (whereas nvme1n1 is the 2nd array member) This will trigger the following oops: BUG: unable to handle kernel NULL pointer dereference at 0000000000000078 PGD 0 P4D 0 Oops: 0000 [#1] SMP PTI RIP: 0010:generic_make_request+0x32b/0x400 Call Trace: submit_bio+0x73/0x140 ext4_io_submit+0x4d/0x60 ext4_writepages+0x626/0xe90 do_writepages+0x4b/0xe0 [...] This patch has no functional changes and preserves the md/raid0 behavior when a member is removed before kernel v4.17. ---------------------------- Why this is not on mainline? ---------------------------- The patch was originally submitted upstream in linux-raid and linux-block mailing-lists - it was initially accepted by Song Liu, but Christoph Hellwig[0] observed that there was a clean-up series ready to be accepted from Ming Lei[1] that fixed the same issue. The accepted patches from Ming's series in upstream are: commit 47cdee29ef9d ("block: move blk_exit_queue into __blk_release_queue") and commit fe2008640ae3 ("block: don't protect generic_make_request_checks with blk_queue_enter"). Those patches basically do a clean-up in the block layer involving: 1) Putting back blk_exit_queue() logic into __blk_release_queue(); that path was changed in the past and the logic from blk_exit_queue() was added to blk_cleanup_queue(). 2) Removing the guard/protection in generic_make_request_checks() with blk_queue_enter(). The problem with Ming's series for -stable is that it relies in the legacy request IO path removal. So it's "backport-able" to v5.0+, but doing that for early versions (like 4.19) would incur in complex code changes. Hence, it was suggested by Christoph and Song Liu that this patch was submitted to stable only; otherwise merging it upstream would add code to fix a path removed in a subsequent commit. [0] lore.kernel.org/linux-block/20190521172258.GA32702@infradead.org [1] lore.kernel.org/linux-block/20190515030310.20393-1-ming.lei@redhat.com Cc: Christoph Hellwig Cc: Jens Axboe Reviewed-by: Bart Van Assche Reviewed-by: Ming Lei Tested-by: Eric Ren Fixes: 37f9579f4c31 ("blk-mq: Avoid that submitting a bio concurrently with device removal triggers a crash") Signed-off-by: Guilherme G. Piccoli Acked-by: Song Liu Signed-off-by: Greg Kroah-Hartman (cherry picked from commit c9d8d3e9d7a0db238dbef5e85405d41051cb1ff7 linux-stable) Signed-off-by: Guilherme G. Piccoli --- block/blk-core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/block/blk-core.c b/block/blk-core.c index 5bde73a49399..cf5c81e82773 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -1075,10 +1075,8 @@ blk_qc_t generic_make_request(struct bio *bio) flags = 0; if (bio->bi_opf & REQ_NOWAIT) flags = BLK_MQ_REQ_NOWAIT; - if (blk_queue_enter(q, flags) < 0) { + if (blk_queue_enter(q, flags) < 0) enter_succeeded = false; - q = NULL; - } } if (enter_succeeded) { @@ -1109,6 +1107,7 @@ blk_qc_t generic_make_request(struct bio *bio) bio_wouldblock_error(bio); else bio_io_error(bio); + q = NULL; } bio = bio_list_pop(&bio_list_on_stack[0]); } while (bio);