Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2281685imm; Thu, 7 Jun 2018 08:08:49 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ2X9z7/bGVgj08KBSfzLfH2J+wr8naqY+1W1L2VIw7tnW7sBPnguhWIdyVeEab41wp9DBQ X-Received: by 2002:a17:902:1a8:: with SMTP id b37-v6mr2502990plb.326.1528384129875; Thu, 07 Jun 2018 08:08:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528384129; cv=none; d=google.com; s=arc-20160816; b=rpHYRjFAoCGGdlfQQ2dWEbTCyvkNBrwgr3oU2qFIPZvOAyIQrgjkCl6E3thhj6dlUg UwOE1cPMnFbnc8HlAi10D90/ypcCR/q9Ex8QXRcUf5/vNcvbc6YS8uEpaxYEwLJ4k6dj Dvkae7rXh1rH45fp51D8oLfO3H8U7JLRUeSf0Ww5CLVnm3CFQrp5efd7qMgwwHUdgqQQ L8bJyvNFkGXWWyvKi2dAR0cD2c4xz8Yo/gAljhRp4NloWobcxaAluT7+V0Ly1K3AlgQY NcIW8waz/FDnAm4WSvHE2dmrC5ZUjeTF/jirpT8d0d8oekP7ETMnIAjwEZSxme80BFs7 KSRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=+n5+pg+9c64S16D95SFigL1TpBC3Qx8vuHlFx1FBA9k=; b=P4Fp4cpgRzcJ+C7OWZbHyg+g/19WXp0P3S1pFu9m9eB9kI10P9oYF13pkP0wA1lsHU fPrkZKrnitrhogxugAgDsiksROa9HsjJ9xNg5YvkfkwaJ25V8QReOY+cmeG8ZJqmpnZd Sfn2X7NwGShxvUobXmMKLPFcRbHsSxyUv9IS4otaiRcPetwRtGatMz8ky0tLabh5NyHG NmZf6SkGQibIXaAwawJ/QR1Tg7qgsT/xb6rZHrG5UfpmMNjZYpD+YmROOwjj7KJTvV2w ndbtLAIBBwcmbafF0oybcOy3JioPxVyZSZH/c1uq2MFv6uCPe1VO4VV32nCv3FAyb1cj lliQ== 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 k187-v6si27857220pge.421.2018.06.07.08.08.35; Thu, 07 Jun 2018 08:08:49 -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; 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 S936008AbeFGPD7 (ORCPT + 99 others); Thu, 7 Jun 2018 11:03:59 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41545 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935962AbeFGPDx (ORCPT ); Thu, 7 Jun 2018 11:03:53 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbj-0005Zj-97; Thu, 07 Jun 2018 15:09:43 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb5-0002ya-BS; Thu, 07 Jun 2018 15:09:03 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Josef Bacik" , "Liu Bo" , "David Sterba" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 189/410] Btrfs: fix use-after-free on root->orphan_block_rsv In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 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. Fixes: 703c88e03524 ("Btrfs: fix tracking of orphan inode count") Signed-off-by: Liu Bo Reviewed-by: Josef Bacik Signed-off-by: David Sterba [bwh: Backported to 3.16: Drop the added comment in a path that's unreachable here] Signed-off-by: Ben Hutchings --- fs/btrfs/inode.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3082,12 +3082,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, &BTRFS_I(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, &BTRFS_I(inode)->runtime_flags); @@ -3119,28 +3124,26 @@ static int btrfs_orphan_del(struct btrfs { struct btrfs_root *root = BTRFS_I(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, &BTRFS_I(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, &BTRFS_I(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; }