Skip to contentSkip to conversation

net/mlx5e: TC, Fix internal port memory leak

PATCH Applied Artem Dinaburg 3 participants 3 messages Sep 24 – Sep 25

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/mlx5e: TC, Fix internal port memory leak

From: Jianbo Liu <[email protected]>

[ Upstream commit ac5da544a3c2047cbfd715acd9cec8380d7fe5c6 ]

The flow rule can be splited, and the extra post_act rules are added to post_act table. It's possible to trigger memleak when the rule forwards packets from internal port and over tunnel, in the case that, for example, CT 'new' state offload is allowed. As int_port object is assigned to the flow attribute of post_act rule, and its refcnt is incremented by mlx5e_tc_int_port_get(), but mlx5e_tc_int_port_put() is not called, the refcnt is never decremented, then int_port is never freed.

The kmemleak reports the following error:
unreferenced object 0xffff888128204b80 (size 64):
comm "handler20", pid 50121, jiffies 4296973009 (age 642.932s) hex dump (first 32 bytes):

    01 00 00 00 19 00 00 00 03 f0 00 00 04 00 00 00  ................
    98 77 67 41 81 88 ff ff 98 77 67 41 81 88 ff ff  .wgA.....wgA....
  backtrace:
    [<00000000e992680d>] kmalloc_trace+0x27/0x120
    [<000000009e945a98>] mlx5e_tc_int_port_get+0x3f3/0xe20 [mlx5_core]
    [<0000000035a537f0>] mlx5e_tc_add_fdb_flow+0x473/0xcf0 [mlx5_core]
    [<0000000070c2cec6>] __mlx5e_add_fdb_flow+0x7cf/0xe90 [mlx5_core]
    [<000000005cc84048>] mlx5e_configure_flower+0xd40/0x4c40 [mlx5_core]
    [<000000004f8a2031>] mlx5e_rep_indr_offload.isra.0+0x10e/0x1c0 [mlx5_core]
    [<000000007df797dc>] mlx5e_rep_indr_setup_tc_cb+0x90/0x130 [mlx5_core]
    [<0000000016c15cc3>] tc_setup_cb_add+0x1cf/0x410
    [<00000000a63305b4>] fl_hw_replace_filter+0x38f/0x670 [cls_flower]
    [<000000008bc9e77c>] fl_change+0x1fd5/0x4430 [cls_flower]
    [<00000000e7f766e4>] tc_new_tfilter+0x867/0x2010
    [<00000000e101c0ef>] rtnetlink_rcv_msg+0x6fc/0x9f0
    [<00000000e1111d44>] netlink_rcv_skb+0x12c/0x360
    [<0000000082dd6c8b>] netlink_unicast+0x438/0x710
    [<00000000fc568f70>] netlink_sendmsg+0x794/0xc50
    [<0000000016e92590>] sock_sendmsg+0xc5/0x190

So fix this by moving int_port cleanup code to the flow attribute free helper, which is used by all the attribute free cases.

Fixes 8300f225268b ("net/mlx5e: Create new flow attr for multi table actions")

[ Backport to 6.1.y: this tree already releases the primary flow-attribute
references in mlx5e_tc_del_fdb_flow() and lacks mlx5_free_flow_attr_actions(); release only cloned post-action attribute references from the corresponding free_flow_post_acts() cleanup. ]

Assisted-by LLM

---
Hi Greg, Sasha, and mlx5e maintainers,

I am continuing CVE backports still missing from 6.1.y.
This fix is inherited by v6.6 and every later mainline release, but 6.1.y still has the affected code. The target-specific adjustment is described in the bracketed note above.

Could you please queue it for 6.1.y?

Thanks,
Artem Dinaburg

CVE: CVE-2023-53999

Build: This patch was included in an x86_64 allmodconfig and CONFIG_WERROR=y build.
It produced vmlinux and modules with no new warnings or errors.

AI assistance: An LLM helped find, adapt, and validate this backport; I reviewed the patch and test output.

 drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +11 -0
index 05888942ef276a..d2f226a09a0c7 100644
@@ -3692,6 +3692,7 @@ free_flow_post_acts(struct mlx5e_tc_flow *flow)
 {
 	struct mlx5_core_dev *counter_dev = get_flow_counter_dev(flow);
 	struct mlx5e_post_act *post_act = get_post_action(flow->priv);
+	struct mlx5_esw_flow_attr *esw_attr;
 	struct mlx5_flow_attr *attr, *tmp;
 	bool vf_tun;
 
@@ -3713,6 +3714,16 @@ free_flow_post_acts(struct mlx5e_tc_flow *flow)
 				mlx5_modify_header_dealloc(flow->priv->mdev, attr->modify_hdr);
 		}
 
+		if (mlx5e_is_eswitch_flow(flow)) {
+			esw_attr = attr->esw_attr;
+			if (esw_attr->int_port)
+				mlx5e_tc_int_port_put(mlx5e_get_int_port_priv(flow->priv),
+						      esw_attr->int_port);
+			if (esw_attr->dest_int_port)
+				mlx5e_tc_int_port_put(mlx5e_get_int_port_priv(flow->priv),
+						      esw_attr->dest_int_port);
+		}
+
 		list_del(&attr->list);
 		kvfree(attr->parse_attr);
 		kfree(attr);
Signature · 1 line
2.39.5
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mlx5e: double free of int_port in flow post actions backport --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mlx5e: double free of int_port in flow post actions backport
--

commit 31e3e836a049949a2f6ef2d83fb49017f4be37e3
Author: Jianbo Liu <[email protected]>

net/mlx5e: TC, Fix internal port memory leak

This commit attempts to fix a memory leak of the internal port object when flow rules forward packets from an internal port over a tunnel. It moves the cleanup into free_flow_post_acts() to release references on cloned post-action attributes.

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +0 -0
index 05888942ef276..d2f226a09a0c7 100644

[ ... ]

drivers/net/ethernet/mellanox/mlx5/core/en_tc.c free_flow_post_acts(struct mlx5e_tc_flow *flow) · line 3724

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +8 -0
@@ -3713,6 +3714,16 @@ free_flow_post_acts(struct mlx5e_tc_flow *flow)
 				mlx5_modify_header_dealloc(flow->priv->mdev, attr->modify_hdr);
 		}
 
+		if (mlx5e_is_eswitch_flow(flow)) {
+			esw_attr = attr->esw_attr;
+			if (esw_attr->int_port)
+				mlx5e_tc_int_port_put(mlx5e_get_int_port_priv(flow->priv),
+						      esw_attr->int_port);
+			if (esw_attr->dest_int_port)
+				mlx5e_tc_int_port_put(mlx5e_get_int_port_priv(flow->priv),
+						      esw_attr->dest_int_port);
                                                      ^^^^^^^^^^^^^^^^^^^^^^^

[Severity: High]
Can this cause a double free of the mlx5e_tc_int_port object?

When parsing a multi-table action, mlx5e_clone_flow_attr_for_post_act() performs a shallow copy of the primary attribute:

mlx5e_clone_flow_attr_for_post_act() {
	...
	memcpy(attr2, attr, attr_sz);
	...
}

This duplicates the dest_int_port and int_port pointers into the cloned attribute without acquiring new references for them.

During flow deletion, mlx5e_tc_del_fdb_flow() already correctly puts the primary attribute's dest_int_port, dropping the refcount and freeing the internal port object:

mlx5e_tc_del_fdb_flow() {
	...
	if (esw_attr->dest_int_port)
		mlx5e_tc_int_port_put(mlx5e_get_int_port_priv(priv),
				      esw_attr->dest_int_port);
	...
}

Since the cloned attributes never took their own references, won't putting the pointers again here in free_flow_post_acts() result in a use-after-free and double free of the already-freed mlx5e_tc_int_port object?

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +2 -0
+		}
+
 		list_del(&attr->list);
 		kvfree(attr->parse_attr);
 		kfree(attr);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw
In reply to Artem Dinaburg
Quoted 4 lines

[ Backport to 6.1.y: this tree already releases the primary flow-attribute
references in mlx5e_tc_del_fdb_flow() and lacks mlx5_free_flow_attr_actions(); release only cloned post-action attribute references from the corresponding free_flow_post_acts() cleanup. ]

Queued for 6.1, thanks.

Signature · 2 lines
Thanks,
Sasha