i40e: hold mac_filter_hash_lock while validating VF cloud filters
Subsystems (3)
PATCH 1/1 i40e: hold mac_filter_hash_lock while validating VF cloud filters
i40e_validate_cloud_filter() runs in the VF message path, which holds neither RTNL nor vsi->mac_filter_hash_lock. It looks up the destination MAC with i40e_find_mac() and walks vsi->mac_filter_hash for the VLAN without the lock. Meanwhile the host can change the VF MAC or port VLAN with i40e_ndo_set_vf_mac() or i40e_ndo_set_vf_port_vlan(), which delete filters under the lock and free them in __i40e_del_filter() or i40e_sync_vsi_filters(). The lookup and the walk can then read a freed struct i40e_mac_filter.
Do the lookup and the walk under mac_filter_hash_lock, as i40e_vc_add_mac_addr_msg() does, and do not use the filter after dropping the lock.
---
Found with a static analysis tool we are developing.
Compile-tested only, with x86_64_defconfig plus PCI_IOV, I40E and W=1.
drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c +6 -0
@@ -3624,9 +3624,11 @@ static int i40e_validate_cloud_filter(struct i40e_vf *vf,*/ if (mask.dst_mac[0 ] && !mask.dst_ip[0 ]) { vsi = pf->vsi[vf->lan_vsi_idx]; +spin_lock_bh (&vsi->mac_filter_hash_lock); f =i40e_find_mac (vsi, data.dst_mac);if (!f) { +spin_unlock_bh (&vsi->mac_filter_hash_lock);dev_info (&pf->pdev->dev,"Destination MAC %pM doesn't belong to VF %d\n" , data.dst_mac, vf->vf_id);
@@ -3641,6 +3643,10 @@ static int i40e_validate_cloud_filter(struct i40e_vf *vf,break ; } } + } +spin_unlock_bh (&vsi->mac_filter_hash_lock); + +if (mask.vlan_id) {if (!found) { dev_info(&pf->pdev->dev, "VF %d doesn't have any VLAN id %u\n",
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.43.0
Quoted 13 lines · Changyul Lee
i40e_validate_cloud_filter()runs in the VF message path, which holds neither RTNL norvsi->mac_filter_hash_lock. It looks up the destination MAC withi40e_find_mac()and walksvsi->mac_filter_hashfor the VLAN without the lock. Meanwhile the host can change the VF MAC or port VLAN withi40e_ndo_set_vf_mac()ori40e_ndo_set_vf_port_vlan(), which delete filters under the lock and free them in__i40e_del_filter()ori40e_sync_vsi_filters(). The lookup and the walk can then read a freed structi40e_mac_filter.Do the lookup and the walk under
mac_filter_hash_lock, asi40e_vc_add_mac_addr_msg()does, and do not use the filter after dropping the lock.
Quoted 6 lines
Fixes e284fc280473 ("i40e: Add and delete cloud filter")Signed-off-by Changyul Lee <[email protected]>---
Found with a static analysis tool we are developing.Compile-tested only, with
x86_64_defconfigplusPCI_IOV, I40E and W=1.
Thanks, as a fix this approach looks good to me.
I do wonder if, as a follow-up for net-next, it would be worth factoring-out the basic filter check. It seems to lead to easier handling of unlocking on error. And as a bonus, tighter scoping of local variables.
Something along these lines (compile tested only!):
drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c +59 -36
@@ -3579,6 +3579,62 @@ static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg) aq_ret); } +/** +* i40e_validate_basic_filter +* @vf: pointer to VF structure +* @tc_filter: pointer to filter requested +* +* This function validates a basic cloud filter programmed as TC filter for ADq +* +* Return: 0 on success, negative value on failure +**/ +static int i40e_validate_basic_filter (struct i40e_vf *vf, +const struct virtchnl_filter *tc_filter) +{ +struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec; +struct virtchnl_l4_spec data = tc_filter->data.tcp_spec; +struct i40e_pf *pf = vf->pf; +struct i40e_mac_filter *f; +struct i40e_vsi *vsi; +struct hlist_node *h; +bool found = false; +int bkt, err; + + vsi = pf->vsi[vf->lan_vsi_idx]; +spin_lock_bh (&vsi->mac_filter_hash_lock); + f =i40e_find_mac (vsi, data.dst_mac); + +if (!f) { +dev_info (&pf->pdev->dev, +"Destination MAC %pM doesn't belong to VF %d\n" , + data.dst_mac, vf->vf_id); + err = -EIO ; + goto out_unlock; + } + +if (!mask.vlan_id) + goto out_unlock; + +hash_for_each_safe (vsi->mac_filter_hash, bkt, h, f, hlist) { +if (f->vlan ==ntohs (data.vlan_id)) { + found = true; +break ; + } + } + +if (!found) { +dev_info (&pf->pdev->dev,"VF %d doesn't have any VLAN id %u\n" , + vf->vf_id,ntohs (data.vlan_id)); + err = -EIO ; + goto out_unlock; + } + + err =0 ; +out_unlock: +spin_unlock_bh (&vsi->mac_filter_hash_lock); +return err; +} +/** * i40e_validate_cloud_filter * @vf: pointer to VF structure
@@ -3592,11 +3648,6 @@ static int i40e_validate_cloud_filter(struct i40e_vf *vf,struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;struct i40e_pf *pf = vf->pf; -struct i40e_vsi *vsi =NULL ; -struct i40e_mac_filter *f; -struct hlist_node *h; -bool found = false; -int bkt;if (tc_filter->action !=VIRTCHNL_ACTION_TC_REDIRECT ) { dev_info(&pf->pdev->dev,
@@ -3623,37 +3674,9 @@ static int i40e_validate_cloud_filter(struct i40e_vf *vf,* Trusted. */ if (mask.dst_mac[0 ] && !mask.dst_ip[0 ]) { - vsi = pf->vsi[vf->lan_vsi_idx]; -spin_lock_bh (&vsi->mac_filter_hash_lock); - f =i40e_find_mac (vsi, data.dst_mac); - -if (!f) { -spin_unlock_bh (&vsi->mac_filter_hash_lock); -dev_info (&pf->pdev->dev, -"Destination MAC %pM doesn't belong to VF %d\n" , - data.dst_mac, vf->vf_id); - goto err; - } - -if (mask.vlan_id) { -hash_for_each_safe (vsi->mac_filter_hash, bkt, h, f, - hlist) { -if (f->vlan ==ntohs (data.vlan_id)) { - found = true; -break ; - } - } - } -spin_unlock_bh (&vsi->mac_filter_hash_lock); - -if (mask.vlan_id) { -if (!found) { -dev_info (&pf->pdev->dev, -"VF %d doesn't have any VLAN id %u\n" , - vf->vf_id,ntohs (data.vlan_id)); - goto err; - } - } +int err =i40e_validate_basic_filter (vf, tc_filter); +if (err <0 ) +return err; }else {/* Check if VF is trusted */ if (!test_bit (I40E_VIRTCHNL_VF_CAP_PRIVILEGE , &vf->vf_caps)) {