i40e: Fix potential invalid access when MAC list is empty
Subsystems (3)
PATCH 1/1 i40e: Fix potential invalid access when MAC list is empty
list_first_entry() never returns NULL — if the list is empty, it still returns a pointer to an invalid object, leading to potential invalid memory access when dereferenced.
Fix this by checking list_empty() before calling list_first_entry(), and only copying the MAC address when the list is not empty.
--- drivers/net/ethernet/intel/i40e/i40e_client.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/ethernet/intel/i40e/i40e_client.c +5 -4
@@ -359,12 +359,13 @@ static void i40e_client_add_instance(struct i40e_pf *pf)if (i40e_client_get_params (vsi, &cdev->lan_info.params)) goto free_cdev; - mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, - struct netdev_hw_addr, list); -if (mac) +if (!list_empty (&cdev->lan_info.netdev->dev_addrs.list)) { + mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, + struct netdev_hw_addr, list);ether_addr_copy (cdev->lan_info.lanmac, mac->addr); -else + }else {dev_err (&pf->pdev->dev,"MAC address list is empty!\n" ); + } pf->cinst = cdev;
Signature · 1 line
2.20.1
Przemek Kitszel
maintainerQuoted 6 lines · Zhen Ni
list_first_entry()never returns NULL — if the list is empty, it still returns a pointer to an invalid object, leading to potential invalid memory access when dereferenced.Fix this by checking
list_empty()before callinglist_first_entry(), and only copying the MAC address when the list is not empty.
good observation, thank you for reaching out!
what about using list_first_entry_or_null() instead of splitting into two operations?
next time please tag intel ethernet patches as PATCH iwl-net (for fixes, like here) or PATCH iwl-next (for features/refactors)
Quoted 29 lines
Fixes e3219ce6a775 ("i40e: Add support for client interface for IWARP driver")Signed-off-by Zhen Ni <[email protected]>--- drivers/net/ethernet/intel/i40e/i40e_client.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/net/ethernet/intel/i40e/i40e_client.c +5
-4@@ -359,12 +359,13 @@ static void i40e_client_add_instance(struct i40e_pf *pf)if (i40e_client_get_params (vsi, &cdev->lan_info.params)) goto free_cdev; - mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, - struct netdev_hw_addr, list); -if (mac) +if (!list_empty (&cdev->lan_info.netdev->dev_addrs.list)) { + mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, + struct netdev_hw_addr, list);ether_addr_copy (cdev->lan_info.lanmac, mac->addr); -else + }else {dev_err (&pf->pdev->dev,"MAC address list is empty!\n" ); + } pf->cinst = cdev;
PATCH v2 1/1 i40e: Fix potential invalid access when MAC list is empty
In reply to Zhen Nilist_first_entry() never returns NULL — if the list is empty, it still returns a pointer to an invalid object, leading to potential invalid memory access when dereferenced.
Fix this by using list_first_entry_or_null instead of list_first_entry.
---
Changes in v2:
- Replace the list_empty() pre-check with list_first_entry_or_null()
--- drivers/net/ethernet/intel/i40e/i40e_client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/i40e/i40e_client.c +2 -2
@@ -359,8 +359,8 @@ static void i40e_client_add_instance(struct i40e_pf *pf)if (i40e_client_get_params (vsi, &cdev->lan_info.params)) goto free_cdev; - mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, - struct netdev_hw_addr, list); + mac =list_first_entry_or_null (&cdev->lan_info.netdev->dev_addrs.list, + struct netdev_hw_addr, list);if (mac)ether_addr_copy (cdev->lan_info.lanmac, mac->addr);else
Signature · 1 line
2.20.1
Dear Zhen,
Thank you for your patch.
Am 27.08.25 um 13:56 schrieb Zhen Ni:
Quoted 20 lines
list_first_entry()never returns NULL — if the list is empty, it still returns a pointer to an invalid object, leading to potential invalid memory access when dereferenced.Fix this by using
list_first_entry_or_nullinstead oflist_first_entry.Fixes e3219ce6a775 ("i40e: Add support for client interface for IWARP driver")Signed-off-by Zhen Ni <[email protected]>---
Changes in v2:
- Replace thelist_empty()pre-check withlist_first_entry_or_null()--- drivers/net/ethernet/intel/i40e/i40e_client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/ethernet/intel/i40e/i40e_client.c +0
-0
drivers/net/ethernet/intel/i40e/i40e_client.c static void i40e_client_add_instance(struct i40e_pf *pf) · line 366
Quoted 10 lines
drivers/net/ethernet/intel/i40e/i40e_client.c +2
-2@@ -359,8 +359,8 @@ static void i40e_client_add_instance(struct i40e_pf *pf)if (i40e_client_get_params (vsi, &cdev->lan_info.params)) goto free_cdev; - mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, - struct netdev_hw_addr, list); + mac =list_first_entry_or_null (&cdev->lan_info.netdev->dev_addrs.list, + struct netdev_hw_addr, list);if (mac)ether_addr_copy (cdev->lan_info.lanmac, mac->addr);else
That’s even better.
Kind regards,
Paul
Dear Zhen,
Thank you for your patch.
Am 27.08.25 um 05:23 schrieb Zhen Ni:
Quoted 25 lines
list_first_entry()never returns NULL — if the list is empty, it still returns a pointer to an invalid object, leading to potential invalid memory access when dereferenced.Fix this by checking
list_empty()before callinglist_first_entry(), and only copying the MAC address when the list is not empty.Fixes e3219ce6a775 ("i40e: Add support for client interface for IWARP driver")Signed-off-by Zhen Ni <[email protected]>--- drivers/net/ethernet/intel/i40e/i40e_client.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/net/ethernet/intel/i40e/i40e_client.c +1
-3@@ -359,12 +359,13 @@ static void i40e_client_add_instance(struct i40e_pf *pf)if (i40e_client_get_params (vsi, &cdev->lan_info.params)) goto free_cdev; - mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, - struct netdev_hw_addr, list); -if (mac) +if (!list_empty (&cdev->lan_info.netdev->dev_addrs.list)) {
drivers/net/ethernet/intel/i40e/i40e_client.c static void i40e_client_add_instance(struct i40e_pf *pf) · line 371
Quoted 10 lines
drivers/net/ethernet/intel/i40e/i40e_client.c +4
-1+ mac =list_first_entry (&cdev->lan_info.netdev->dev_addrs.list, + struct netdev_hw_addr, list);ether_addr_copy (cdev->lan_info.lanmac, mac->addr); - else + }else {dev_err (&pf->pdev->dev,"MAC address list is empty!\n" ); + } pf->cinst = cdev;
Kind regards,
Paul