Skip to content

Commit

Permalink
net/mlx5: fix VLAN handling in meter split
Browse files Browse the repository at this point in the history
[ upstream commit 5d2301a222d77e7bac3a085aa17f8ef7a3af7ffe ]

On the attempt to create a flow rule with:

- matching on REPRESENTED_PORT,
- matching on outer VLAN tag,
- matching on inner VLAN tag,
- METER action,

flow splitting mechanism for handling metering flows was causing
memory corruption. It was assumed that suffix flow will have a single
VLAN item (used for translation of OF_PUSH_VLAN/OF_SET_VLAN_VID
actions), however during flow_meter_split_prep() 2 VLAN items were
parsed. This caused the buffer overflow on allocated
suffix flow item buffer.

This patch fixes this overflow, by account for number of VLAN items
in flow rule pattern when allocating items for suffix flow.

Fixes: 50f576d ("net/mlx5: fix VLAN actions in meter")

Signed-off-by: Dariusz Sosnowski <dsosnowski@nvidia.com>
Acked-by: Suanming Mou <suanmingm@nvidia.com>
  • Loading branch information
sodar authored and kevintraynor committed Mar 8, 2024
1 parent c52e6e0 commit 03c7f0a
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions drivers/net/mlx5/mlx5_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -5082,8 +5082,8 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
struct mlx5_rte_flow_item_tag *tag_item_spec;
struct mlx5_rte_flow_item_tag *tag_item_mask;
uint32_t tag_id = 0;
struct rte_flow_item *vlan_item_dst = NULL;
const struct rte_flow_item *vlan_item_src = NULL;
bool vlan_actions;
struct rte_flow_item *orig_sfx_items = sfx_items;
const struct rte_flow_item *orig_items = items;
struct rte_flow_action *hw_mtr_action;
struct rte_flow_action *action_pre_head = NULL;
Expand All @@ -5100,6 +5100,7 @@ flow_meter_split_prep(struct rte_eth_dev *dev,

/* Prepare the suffix subflow items. */
tag_item = sfx_items++;
tag_item->type = (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_TAG;
for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
int item_type = items->type;

Expand All @@ -5120,10 +5121,13 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
sfx_items++;
break;
case RTE_FLOW_ITEM_TYPE_VLAN:
/* Determine if copy vlan item below. */
vlan_item_src = items;
vlan_item_dst = sfx_items++;
vlan_item_dst->type = RTE_FLOW_ITEM_TYPE_VOID;
/*
* Copy VLAN items in case VLAN actions are performed.
* If there are no VLAN actions, these items will be VOID.
*/
memcpy(sfx_items, items, sizeof(*sfx_items));
sfx_items->type = (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_VLAN;
sfx_items++;
break;
default:
break;
Expand All @@ -5140,6 +5144,7 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
tag_action = actions_pre++;
}
/* Prepare the actions for prefix and suffix flow. */
vlan_actions = false;
for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
struct rte_flow_action *action_cur = NULL;

Expand Down Expand Up @@ -5170,16 +5175,7 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
break;
case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
if (vlan_item_dst && vlan_item_src) {
memcpy(vlan_item_dst, vlan_item_src,
sizeof(*vlan_item_dst));
/*
* Convert to internal match item, it is used
* for vlan push and set vid.
*/
vlan_item_dst->type = (enum rte_flow_item_type)
MLX5_RTE_FLOW_ITEM_TYPE_VLAN;
}
vlan_actions = true;
break;
case RTE_FLOW_ACTION_TYPE_COUNT:
if (fm->def_policy)
Expand All @@ -5194,6 +5190,14 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
actions_sfx++ : actions_pre++;
memcpy(action_cur, actions, sizeof(struct rte_flow_action));
}
/* If there are no VLAN actions, convert VLAN items to VOID in suffix flow items. */
if (!vlan_actions) {
struct rte_flow_item *it = orig_sfx_items;

for (; it->type != RTE_FLOW_ITEM_TYPE_END; it++)
if (it->type == (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
it->type = RTE_FLOW_ITEM_TYPE_VOID;
}
/* Add end action to the actions. */
actions_sfx->type = RTE_FLOW_ACTION_TYPE_END;
if (priv->sh->meter_aso_en) {
Expand Down Expand Up @@ -5283,8 +5287,6 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
tag_action->type = (enum rte_flow_action_type)
MLX5_RTE_FLOW_ACTION_TYPE_TAG;
tag_action->conf = set_tag;
tag_item->type = (enum rte_flow_item_type)
MLX5_RTE_FLOW_ITEM_TYPE_TAG;
tag_item->spec = tag_item_spec;
tag_item->last = NULL;
tag_item->mask = tag_item_mask;
Expand Down Expand Up @@ -6111,6 +6113,19 @@ flow_meter_create_drop_flow_with_org_pattern(struct rte_eth_dev *dev,
&drop_split_info, error);
}

static int
flow_count_vlan_items(const struct rte_flow_item items[])
{
int items_n = 0;

for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
if (items->type == RTE_FLOW_ITEM_TYPE_VLAN ||
items->type == (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
items_n++;
}
return items_n;
}

/**
* The splitting for meter feature.
*
Expand Down Expand Up @@ -6166,6 +6181,7 @@ flow_create_split_meter(struct rte_eth_dev *dev,
size_t act_size;
size_t item_size;
int actions_n = 0;
int vlan_items_n = 0;
int ret = 0;

if (priv->mtr_en)
Expand Down Expand Up @@ -6227,9 +6243,11 @@ flow_create_split_meter(struct rte_eth_dev *dev,
act_size = (sizeof(struct rte_flow_action) *
(actions_n + METER_PREFIX_ACTION)) +
sizeof(struct mlx5_rte_flow_action_set_tag);
/* Suffix items: tag, vlan, port id, end. */
#define METER_SUFFIX_ITEM 4
item_size = sizeof(struct rte_flow_item) * METER_SUFFIX_ITEM +
/* Flow can have multiple VLAN items. Account for them in suffix items. */
vlan_items_n = flow_count_vlan_items(items);
/* Suffix items: tag, [vlans], port id, end. */
#define METER_SUFFIX_ITEM 3
item_size = sizeof(struct rte_flow_item) * (METER_SUFFIX_ITEM + vlan_items_n) +
sizeof(struct mlx5_rte_flow_item_tag) * 2;
sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size + item_size),
0, SOCKET_ID_ANY);
Expand Down

0 comments on commit 03c7f0a

Please sign in to comment.