Received: by 10.223.185.116 with SMTP id b49csp1021156wrg; Wed, 21 Feb 2018 10:42:08 -0800 (PST) X-Google-Smtp-Source: AH8x227YfW+lN1tT9cf77HbyZdQHR7gfBFSEU1nCDJkxu/DtDq3Rw/12CKAHgSPEnr5z6kMy6p9K X-Received: by 2002:a17:902:b43:: with SMTP id 61-v6mr4114463plq.270.1519238528665; Wed, 21 Feb 2018 10:42:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519238528; cv=none; d=google.com; s=arc-20160816; b=LrYx4zjXBnBjEfVJIkFMgd77QVLjjQ24xObH3vMT+K56gJgzUBSsmSdG/qzBCSPZeI yNdJeEZgc/jZxJEzX0RPVWbCoOzPFOHpFlUosaXn/F0jQ5K0lJ6evicBl0dQ4od1c+4C AXYT4Zwz3o/J/rhMU+2ICDgf7ZyfZ8mrr2tTASaT7K4yneMqCTIKLXaTb/HxA+JSGM/k MYANbnHfFVELfWvX7I1rXneBaaBtEtaytP3vilnNv5Xo1ctx1b1xdFnMHlfvz/raXxdv piMvnpZupcrsmEjzzvHqOBrApgq5cas7naiuNfAOQqKENilXsAPDppRY6NCUbiEiR8xt 72UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=4al5IxdK9izE6k5npkY4XN6QHP9zWm+1pT5fuZDh2wM=; b=rqpI1olFBiXdYoThbbi1CPUKD910X9p6QtuaiohxTUI+POe9YNs64+p15dUYsh9st6 3hHtPaPkx4R+11+Bc5pWRh8OIwTTK82BTmWCln1Eds8aP1xKdT5Sd8dcIGpI5uFOtwGO sPQ+yzgNXXBm1veC2686qRMWTqAbQkDfbXvkZ8sAP8dgqQf9Vvswv7co6u+1gm9wYRTz awlXJOMU5HJSiCNZ54pw3uj3Afsj7AjjQvOI1B7Bn6xg8z4DoqpQlT4rr7M/gtTqPD1i yWMyYKFlvfe9/7VclexgEWEsnwvPvGokrnVLVHvvg6PXnpGeoEin8N0kxTYrtre6+ZLN NQ1Q== ARC-Authentication-Results: i=1; mx.google.com; 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 f3-v6si6064883plb.79.2018.02.21.10.41.54; Wed, 21 Feb 2018 10:42:08 -0800 (PST) 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; 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 S936619AbeBUNWl (ORCPT + 99 others); Wed, 21 Feb 2018 08:22:41 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:45486 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936437AbeBUNLz (ORCPT ); Wed, 21 Feb 2018 08:11:55 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id AD319E29; Wed, 21 Feb 2018 13:11:54 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liu Bo , Josef Bacik , David Sterba Subject: [PATCH 4.15 139/163] Btrfs: fix use-after-free on root->orphan_block_rsv Date: Wed, 21 Feb 2018 13:49:28 +0100 Message-Id: <20180221124537.837845430@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180221124529.931834518@linuxfoundation.org> References: <20180221124529.931834518@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Liu Bo commit 1a932ef4e47984dee227834667b5ff5a334e4805 upstream. I got these from running generic/475, WARNING: CPU: 0 PID: 26384 at fs/btrfs/inode.c:3326 btrfs_orphan_commit_root+0x1ac/0x2b0 [btrfs] BUG: unable to handle kernel NULL pointer dereference at 0000000000000010 IP: btrfs_block_rsv_release+0x1c/0x70 [btrfs] Call Trace: btrfs_orphan_release_metadata+0x9f/0x200 [btrfs] btrfs_orphan_del+0x10d/0x170 [btrfs] btrfs_setattr+0x500/0x640 [btrfs] notify_change+0x7ae/0x870 do_truncate+0xca/0x130 vfs_truncate+0x2ee/0x3d0 do_sys_truncate+0xaf/0xf0 SyS_truncate+0xe/0x10 entry_SYSCALL_64_fastpath+0x1f/0x96 The race is between btrfs_orphan_commit_root and btrfs_orphan_del, t1 t2 btrfs_orphan_commit_root btrfs_orphan_del spin_lock check (&root->orphan_inodes) root->orphan_block_rsv = NULL; spin_unlock atomic_dec(&root->orphan_inodes); access root->orphan_block_rsv Accessing root->orphan_block_rsv must be done before decreasing root->orphan_inodes. cc: v3.12+ Fixes: 703c88e03524 ("Btrfs: fix tracking of orphan inode count") Signed-off-by: Liu Bo Reviewed-by: Josef Bacik Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/inode.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3369,6 +3369,11 @@ int btrfs_orphan_add(struct btrfs_trans_ ret = btrfs_orphan_reserve_metadata(trans, inode); ASSERT(!ret); if (ret) { + /* + * dec doesn't need spin_lock as ->orphan_block_rsv + * would be released only if ->orphan_inodes is + * zero. + */ atomic_dec(&root->orphan_inodes); clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, &inode->runtime_flags); @@ -3383,12 +3388,17 @@ int btrfs_orphan_add(struct btrfs_trans_ if (insert >= 1) { ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); if (ret) { - atomic_dec(&root->orphan_inodes); if (reserve) { clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, &inode->runtime_flags); btrfs_orphan_release_metadata(inode); } + /* + * btrfs_orphan_commit_root may race with us and set + * ->orphan_block_rsv to zero, in order to avoid that, + * decrease ->orphan_inodes after everything is done. + */ + atomic_dec(&root->orphan_inodes); if (ret != -EEXIST) { clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, &inode->runtime_flags); @@ -3420,28 +3430,26 @@ static int btrfs_orphan_del(struct btrfs { struct btrfs_root *root = inode->root; int delete_item = 0; - int release_rsv = 0; int ret = 0; - spin_lock(&root->orphan_lock); if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, &inode->runtime_flags)) delete_item = 1; + if (delete_item && trans) + ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); + if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, &inode->runtime_flags)) - release_rsv = 1; - spin_unlock(&root->orphan_lock); + btrfs_orphan_release_metadata(inode); - if (delete_item) { + /* + * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv + * to zero, in order to avoid that, decrease ->orphan_inodes after + * everything is done. + */ + if (delete_item) atomic_dec(&root->orphan_inodes); - if (trans) - ret = btrfs_del_orphan_item(trans, root, - btrfs_ino(inode)); - } - - if (release_rsv) - btrfs_orphan_release_metadata(inode); return ret; }