Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp2172329ybm; Thu, 23 May 2019 12:32:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqy0PMf/o+wOMXIXybZbKkZcq7lwuldoKbue8XrSoOD3FUhhPDSbButqxcGJEUcYcIZj8F7o X-Received: by 2002:a17:902:704a:: with SMTP id h10mr11042626plt.294.1558639971715; Thu, 23 May 2019 12:32:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558639971; cv=none; d=google.com; s=arc-20160816; b=hWxbeuQ31hc9mPkAtEeVvVx2uxMrzyqWjuPz5xOu8Urndb8PGYXzmk8u1KdGpk7nvd vd/sJUkTK2hqC7aCmt3GIgNJcV1Dxlt5ShoHshqxTPipUesYwiDtvy+L8Ql/e/PAHq3M Bkv8lNayu8NYZCmdoyYvx9JId09XodCnIxrbNF5hoGvdP5XxCEQbFA8eV/I9ukAvnGIN sO+TC0TAkwDuvNDo71tnGp4Diz8LTxLz1fxyXiu3kBQwV99M4qetpsw7lhiV5XNkC42i 4V9FXIN/GvO4+0XjrkIOL0Y++HIA2nV3zccrC6IllxswgiR911qW5I1dCJby0AgKfScD oSIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=YyavnhFQJurF4Lo2HCk2JdOBeK+6Q29B4m8DtUV+PL0=; b=CCFFCp3E6vrOQFaeVL4vyZfEFb4uUYgSwJNUJvoLjMOR8e9mDTmjOD/b50hcHV9g9M jv6ZrLJIgJQj+g0uDetXPwPxQbgjF5cXR6N56VXPzXDKUyaQaEonWk4N5KxyHRiFlUvA qsBYiMuP6ZjvQ+ZInmogbY2OHQsNYaNCjO8GFUAYCudbXy3Z1BTiRa2F62BDg/BTPPLE 0vpQA9vNqT9VZ/cyttQEi18ZAdSHw8jiwd17HICT6OLJTU+imd+eXcSK7ovQ5qaoV+Lw Dd8wKkPHY2+39Sh80LsHmXLFZZD7tCMQ2ZZfyPf40KzMuP4MhDkVC1OeAmxQMA0y/xTq q9Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=S1Ghhxow; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u133si357138pfc.72.2019.05.23.12.32.36; Thu, 23 May 2019 12:32:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=S1Ghhxow; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403845AbfEWTb1 (ORCPT + 99 others); Thu, 23 May 2019 15:31:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:45246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392017AbfEWTb0 (ORCPT ); Thu, 23 May 2019 15:31:26 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C9C5E2054F; Thu, 23 May 2019 19:31:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558639885; bh=Jfx6KeEU+O9GHSS13gWnKqZraw3gOIgsyaXRRt0RtWM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S1GhhxowjzyHBUGmDvU1/l70m+Pa/MSaIFQI+KwBEIDG4a0Uy4O01qj36AdZgItHW UWq1Y5NY7Ws5k08kseqJukBzwvZ8SsjN8v1pdb7hDJKLvFhmZuIY9BulOg/qamysIj Gt7Hze7njXTAqtr9eYPDq3xadqkzmdWcNlY28ao8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qu Wenruo , David Sterba Subject: [PATCH 5.1 120/122] btrfs: reloc: Fix NULL pointer dereference due to expanded reloc_root lifespan Date: Thu, 23 May 2019 21:07:22 +0200 Message-Id: <20190523181721.457475819@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523181705.091418060@linuxfoundation.org> References: <20190523181705.091418060@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo commit 10995c0491204c861948c9850939a7f4e90760a4 upstream. Commit d2311e698578 ("btrfs: relocation: Delay reloc tree deletion after merge_reloc_roots()") expands the life span of root->reloc_root. This breaks certain checs of fs_info->reloc_ctl. Before that commit, if we have a root with valid reloc_root, then it's ensured to have fs_info->reloc_ctl. But now since reloc_root doesn't always mean a valid fs_info->reloc_ctl, such check is unreliable and can cause the following NULL pointer dereference: BUG: unable to handle kernel NULL pointer dereference at 00000000000005c1 IP: btrfs_reloc_pre_snapshot+0x20/0x50 [btrfs] PGD 0 P4D 0 Oops: 0000 [#1] SMP PTI CPU: 0 PID: 10379 Comm: snapperd Not tainted Call Trace: create_pending_snapshot+0xd7/0xfc0 [btrfs] create_pending_snapshots+0x8e/0xb0 [btrfs] btrfs_commit_transaction+0x2ac/0x8f0 [btrfs] btrfs_mksubvol+0x561/0x570 [btrfs] btrfs_ioctl_snap_create_transid+0x189/0x190 [btrfs] btrfs_ioctl_snap_create_v2+0x102/0x150 [btrfs] btrfs_ioctl+0x5c9/0x1e60 [btrfs] do_vfs_ioctl+0x90/0x5f0 SyS_ioctl+0x74/0x80 do_syscall_64+0x7b/0x150 entry_SYSCALL_64_after_hwframe+0x3d/0xa2 RIP: 0033:0x7fd7cdab8467 Fix it by explicitly checking fs_info->reloc_ctl other than using the implied root->reloc_root. Fixes: d2311e698578 ("btrfs: relocation: Delay reloc tree deletion after merge_reloc_roots") Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/relocation.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4667,14 +4667,12 @@ int btrfs_reloc_cow_block(struct btrfs_t void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, u64 *bytes_to_reserve) { - struct btrfs_root *root; - struct reloc_control *rc; + struct btrfs_root *root = pending->root; + struct reloc_control *rc = root->fs_info->reloc_ctl; - root = pending->root; - if (!root->reloc_root) + if (!root->reloc_root || !rc) return; - rc = root->fs_info->reloc_ctl; if (!rc->merge_reloc_tree) return; @@ -4703,10 +4701,10 @@ int btrfs_reloc_post_snapshot(struct btr struct btrfs_root *root = pending->root; struct btrfs_root *reloc_root; struct btrfs_root *new_root; - struct reloc_control *rc; + struct reloc_control *rc = root->fs_info->reloc_ctl; int ret; - if (!root->reloc_root) + if (!root->reloc_root || !rc) return 0; rc = root->fs_info->reloc_ctl;