All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, Filipe Manana <fdmanana@suse.com>,
	David Sterba <dsterba@suse.com>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.4 23/74] btrfs: fix inode list leak during backref walking at find_parent_nodes()
Date: Tue,  8 Nov 2022 14:38:51 +0100	[thread overview]
Message-ID: <20221108133334.676187298@linuxfoundation.org> (raw)
In-Reply-To: <20221108133333.659601604@linuxfoundation.org>

From: Filipe Manana <fdmanana@suse.com>

[ Upstream commit 92876eec382a0f19f33d09d2c939e9ca49038ae5 ]

During backref walking, at find_parent_nodes(), if we are dealing with a
data extent and we get an error while resolving the indirect backrefs, at
resolve_indirect_refs(), or in the while loop that iterates over the refs
in the direct refs rbtree, we end up leaking the inode lists attached to
the direct refs we have in the direct refs rbtree that were not yet added
to the refs ulist passed as argument to find_parent_nodes(). Since they
were not yet added to the refs ulist and prelim_release() does not free
the lists, on error the caller can only free the lists attached to the
refs that were added to the refs ulist, all the remaining refs get their
inode lists never freed, therefore leaking their memory.

Fix this by having prelim_release() always free any attached inode list
to each ref found in the rbtree, and have find_parent_nodes() set the
ref's inode list to NULL once it transfers ownership of the inode list
to a ref added to the refs ulist passed to find_parent_nodes().

Fixes: 86d5f9944252 ("btrfs: convert prelimary reference tracking to use rbtrees")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 fs/btrfs/backref.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 4809cc07a885..3cbca2ebdeb0 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -287,8 +287,10 @@ static void prelim_release(struct preftree *preftree)
 	struct prelim_ref *ref, *next_ref;
 
 	rbtree_postorder_for_each_entry_safe(ref, next_ref,
-					     &preftree->root.rb_root, rbnode)
+					     &preftree->root.rb_root, rbnode) {
+		free_inode_elem_list(ref->inode_list);
 		free_pref(ref);
+	}
 
 	preftree->root = RB_ROOT_CACHED;
 	preftree->count = 0;
@@ -1383,6 +1385,12 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
 				if (ret < 0)
 					goto out;
 				ref->inode_list = eie;
+				/*
+				 * We transferred the list ownership to the ref,
+				 * so set to NULL to avoid a double free in case
+				 * an error happens after this.
+				 */
+				eie = NULL;
 			}
 			ret = ulist_add_merge_ptr(refs, ref->parent,
 						  ref->inode_list,
@@ -1408,6 +1416,14 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
 				eie->next = ref->inode_list;
 			}
 			eie = NULL;
+			/*
+			 * We have transferred the inode list ownership from
+			 * this ref to the ref we added to the 'refs' ulist.
+			 * So set this ref's inode list to NULL to avoid
+			 * use-after-free when our caller uses it or double
+			 * frees in case an error happens before we return.
+			 */
+			ref->inode_list = NULL;
 		}
 		cond_resched();
 	}
-- 
2.35.1




  parent reply	other threads:[~2022-11-08 13:48 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-08 13:38 [PATCH 5.4 00/74] 5.4.224-rc1 review Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 01/74] RDMA/cma: Use output interface for net_dev check Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 02/74] IB/hfi1: Correctly move list in sc_disable() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 03/74] NFSv4.1: Handle RECLAIM_COMPLETE trunking errors Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 04/74] NFSv4.1: We must always send RECLAIM_COMPLETE after a reboot Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 05/74] nfs4: Fix kmemleak when allocate slot failed Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 06/74] net: dsa: Fix possible memory leaks in dsa_loop_init() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 07/74] RDMA/core: Fix null-ptr-deref in ib_core_cleanup() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 08/74] RDMA/qedr: clean up work queue on failure in qedr_alloc_resources() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 09/74] nfc: s3fwrn5: Fix potential memory leak in s3fwrn5_nci_send() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 10/74] nfc: nfcmrvl: Fix potential memory leak in nfcmrvl_i2c_nci_send() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 11/74] net: fec: fix improper use of NETDEV_TX_BUSY Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 12/74] ata: pata_legacy: fix pdc20230_set_piomode() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 13/74] net: sched: Fix use after free in red_enqueue() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 14/74] net: tun: fix bugs for oversize packet when napi frags enabled Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 15/74] netfilter: nf_tables: release flow rule object from commit path Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 16/74] ipvs: use explicitly signed chars Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 17/74] ipvs: fix WARNING in __ip_vs_cleanup_batch() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 18/74] ipvs: fix WARNING in ip_vs_app_net_cleanup() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 19/74] rose: Fix NULL pointer dereference in rose_send_frame() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 20/74] mISDN: fix possible memory leak in mISDN_register_device() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 21/74] isdn: mISDN: netjet: fix wrong check of device registration Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 22/74] btrfs: fix inode list leak during backref walking at resolve_indirect_refs() Greg Kroah-Hartman
2022-11-08 13:38 ` Greg Kroah-Hartman [this message]
2022-11-08 13:38 ` [PATCH 5.4 24/74] btrfs: fix ulist leaks in error paths of qgroup self tests Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 25/74] Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 26/74] Bluetooth: L2CAP: fix use-after-free in l2cap_conn_del() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 27/74] net: mdio: fix undefined behavior in bit shift for __mdiobus_register Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 28/74] net, neigh: Fix null-ptr-deref in neigh_table_clear() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 29/74] ipv6: fix WARNING in ip6_route_net_exit_late() Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 30/74] media: s5p_cec: limit msg.len to CEC_MAX_MSG_SIZE Greg Kroah-Hartman
2022-11-08 13:38 ` [PATCH 5.4 31/74] media: cros-ec-cec: " Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 32/74] media: dvb-frontends/drxk: initialize err to 0 Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 33/74] media: meson: vdec: fix possible refcount leak in vdec_probe() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 34/74] scsi: core: Restrict legal sdev_state transitions via sysfs Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 35/74] HID: saitek: add madcatz variant of MMO7 mouse device ID Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 36/74] i2c: xiic: Add platform module alias Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 37/74] xfs: dont fail verifier on empty attr3 leaf block Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 38/74] xfs: use ordered buffers to initialize dquot buffers during quotacheck Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 39/74] xfs: gut error handling in xfs_trans_unreserve_and_mod_sb() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 40/74] xfs: group quota should return EDQUOT when prj quota enabled Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 41/74] xfs: dont fail unwritten extent conversion on writeback due to edquot Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 42/74] xfs: Add the missed xfs_perag_put() for xfs_ifree_cluster() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 43/74] Bluetooth: L2CAP: Fix attempting to access uninitialized memory Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 44/74] block, bfq: protect bfqd->queued by bfqd->lock Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 45/74] tcp/udp: Fix memory leak in ipv6_renew_options() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 46/74] memcg: enable accounting of ipc resources Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 47/74] binder: fix UAF of alloc->vma in race with munmap() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 48/74] btrfs: fix type of parameter generation in btrfs_get_dentry Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 49/74] tcp/udp: Make early_demux back namespacified Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 50/74] kprobe: reverse kp->flags when arm_kprobe failed Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 51/74] tools/nolibc/string: Fix memcmp() implementation Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 52/74] tracing/histogram: Update document for KEYS_MAX size Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 53/74] capabilities: fix potential memleak on error path from vfs_getxattr_alloc() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 54/74] fuse: add file_modified() to fallocate Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 55/74] efi: random: reduce seed size to 32 bytes Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 56/74] perf/x86/intel: Fix pebs event constraints for ICL Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 57/74] perf/x86/intel: Add Cooper Lake stepping to isolation_ucodes[] Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 58/74] ALSA: usb-audio: Add quirks for MacroSilicon MS2100/MS2106 devices Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 59/74] parisc: Make 8250_gsc driver dependend on CONFIG_PARISC Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 60/74] parisc: Export iosapic_serial_irq() symbol for serial port driver Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 61/74] parisc: Avoid printing the hardware path twice Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 62/74] ext4: fix warning in ext4_da_release_space Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 63/74] ext4: fix BUG_ON() when directory entry has invalid rec_len Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 64/74] KVM: x86: Mask off reserved bits in CPUID.8000001AH Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 65/74] KVM: x86: Mask off reserved bits in CPUID.80000008H Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 66/74] KVM: x86: emulator: em_sysexit should update ctxt->mode Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 67/74] KVM: x86: emulator: introduce emulator_recalc_and_set_mode Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 68/74] KVM: x86: emulator: update the emulation mode after CR0 write Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 69/74] mtd: rawnand: gpmi: Set WAIT_FOR_READY timeout based on program/erase times Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 70/74] drm/rockchip: dsi: Force synchronous probe Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 71/74] drm/i915/sdvo: Filter out invalid outputs more sensibly Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 72/74] drm/i915/sdvo: Setup DDC fully before output init Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 73/74] wifi: brcmfmac: Fix potential buffer overflow in brcmf_fweh_event_worker() Greg Kroah-Hartman
2022-11-08 13:39 ` [PATCH 5.4 74/74] ipc: remove memcg accounting for sops objects in do_semtimedop() Greg Kroah-Hartman
2022-11-08 19:02 ` [PATCH 5.4 00/74] 5.4.224-rc1 review Florian Fainelli
2022-11-09  2:57 ` Guenter Roeck
2022-11-09 10:47 ` Jon Hunter
2022-11-09 13:31 ` Naresh Kamboju
2022-11-10  1:55 ` Shuah Khan
2022-11-10 10:49 ` Sudip Mukherjee
2022-11-11  1:19 ` zhouzhixiu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221108133334.676187298@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=patches@lists.linux.dev \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.