ethdev: fix int overflow in descriptor count logic
PATCH v2 1/1 ethdev: fix int overflow in descriptor count logic
Addressed a specific overflow issue in the eth_dev_adjust_nb_desc() function where the uint16_t variable nb_desc would overflow when its value was greater than (2^16 - nb_align). This overflow caused nb_desc to incorrectly wrap around between 0 and nb_align-1, leading to the function setting nb_desc to nb_min instead of the expected nb_max.
To give an example, let nb_desc=UINT16_MAX, nb_align=32, nb_max=4096 and nb_min=64. RTE_ALIGN_CEIL(nb_desc, nb_align) calls RTE_ALIGN_FLOOR(nb_desc + nb_align - 1, nb_align). This results in an overflow of nb_desc, leading to nb_desc being set to 30 and then 0 when the macros return. As a result of this, nb_desc is then set to nb_min later on.
The resolution involves upcasting nb_desc to a uint32_t before the RTE_ALIGN_CEIL macro is applied. This change ensures that the subsequent call to RTE_ALIGN_FLOOR(nb_desc + (nb_align - 1), nb_align) does not result in an overflow, as it would when nb_desc is a uint16_t. By using a uint32_t for these operations, the correct behavior is maintained without the risk of overflow.
---
v2:
* add example of issue to commit message
--- .mailmap | 1 + lib/ethdev/rte_ethdev.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-)
.mailmap +1 -0
@@ -1053,6 +1053,7 @@ Nelson Escobar <[email protected]> Nemanja Marjanovic <[email protected]> Netanel Belgazal <[email protected]> Netanel Gonen <[email protected]> +Niall Meade <[email protected]> Niall Power <[email protected]> Nicholas Pratte <[email protected]> Nick Connolly <[email protected]> <[email protected]>
lib/ethdev/rte_ethdev.c +9 -3
@@ -6577,13 +6577,19 @@ static voideth_dev_adjust_nb_desc (uint16_t *nb_desc, const struct rte_eth_desc_lim *desc_lim) { +/* Upcast to uint32 to avoid potential overflow with RTE_ALIGN_CEIL(). */ +uint32_t nb_desc_32 = *nb_desc; +if (desc_lim->nb_align !=0 ) - *nb_desc =RTE_ALIGN_CEIL (*nb_desc, desc_lim->nb_align); + nb_desc_32 =RTE_ALIGN_CEIL (nb_desc_32, desc_lim->nb_align);if (desc_lim->nb_max !=0 ) - *nb_desc =RTE_MIN (*nb_desc, desc_lim->nb_max); + nb_desc_32 =RTE_MIN (nb_desc_32, desc_lim->nb_max); + + nb_desc_32 =RTE_MAX (nb_desc_32, desc_lim->nb_min); - *nb_desc =RTE_MAX (*nb_desc, desc_lim->nb_min); +/* Assign clipped u32 back to u16. */ + *nb_desc = nb_desc_32; }int
Signature · 13 lines
2.34.1 -------------------------------------------------------------- Intel Research and Development Ireland Limited Registered in Ireland Registered Office: Collinstown Industrial Park, Leixlip, County Kildare Registered Number: 308263 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 59 lines · Niall Meade
Addressed a specific overflow issue in the
eth_dev_adjust_nb_desc()function where theuint16_tvariablenb_descwould overflow when its value was greater than (2^16 -nb_align). This overflow causednb_descto incorrectly wrap around between 0 and nb_align-1, leading to the function settingnb_desctonb_mininstead of the expectednb_max.To give an example, let nb_desc=UINT16_MAX, nb_align=32, nb_max=4096 and nb_min=64.
RTE_ALIGN_CEIL(nb_desc, nb_align)callsRTE_ALIGN_FLOOR(nb_desc + nb_align - 1, nb_align). This results in an overflow ofnb_desc, leading tonb_descbeing set to 30 and then 0 when the macros return. As a result of this,nb_descis then set tonb_minlater on.The resolution involves upcasting
nb_descto auint32_tbefore theRTE_ALIGN_CEILmacro is applied. This change ensures that the subsequent call toRTE_ALIGN_FLOOR(nb_desc + (nb_align - 1), nb_align)does not result in an overflow, as it would whennb_descis auint16_t. By using auint32_tfor these operations, the correct behavior is maintained without the risk of overflow.Fixes 0f67fc3baeb9 ("ethdev: add function to adjust number of descriptors")Signed-off-by Niall Meade <[email protected]>---
v2:
* add example of issue to commit message--- .mailmap | 1 + lib/ethdev/rte_ethdev.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-).mailmap +1
-0@@ -1053,6 +1053,7 @@ Nelson Escobar <[email protected]> Nemanja Marjanovic <[email protected]> Netanel Belgazal <[email protected]> Netanel Gonen <[email protected]> +Niall Meade <[email protected]> Niall Power <[email protected]> Nicholas Pratte <[email protected]> Nick Connolly <[email protected]> <[email protected]>lib/ethdev/rte_ethdev.c +4
-1@@ -6577,13 +6577,19 @@ static voideth_dev_adjust_nb_desc (uint16_t *nb_desc, const struct rte_eth_desc_lim *desc_lim) { +/* Upcast to uint32 to avoid potential overflow with RTE_ALIGN_CEIL(). */ +uint32_t nb_desc_32 = *nb_desc; +if (desc_lim->nb_align !=0 ) - *nb_desc =RTE_ALIGN_CEIL (*nb_desc, desc_lim->nb_align); + nb_desc_32 =RTE_ALIGN_CEIL (nb_desc_32, desc_lim->nb_align);
lib/ethdev/rte_ethdev.c static void · line 6593
Quoted 10 lines
lib/ethdev/rte_ethdev.c +5
-2if (desc_lim->nb_max !=0 ) - *nb_desc =RTE_MIN (*nb_desc, desc_lim->nb_max); + nb_desc_32 =RTE_MIN (nb_desc_32, desc_lim->nb_max); + + nb_desc_32 =RTE_MAX (nb_desc_32, desc_lim->nb_min); - *nb_desc =RTE_MAX (*nb_desc, desc_lim->nb_min); +/* Assign clipped u32 back to u16. */ + *nb_desc = nb_desc_32;
Better to add explicit cast here, in case compiler has '-Wconversion' flag etc..
Except from above, lgtm.
PATCH v3 1/1 ethdev: fix int overflow in descriptor count logic
In reply to Niall MeadeAddressed a specific overflow issue in the eth_dev_adjust_nb_desc() function where the uint16_t variable nb_desc would overflow when its value was greater than (2^16 - nb_align). This overflow caused nb_desc to incorrectly wrap around between 0 and nb_align-1, leading to the function setting nb_desc to nb_min instead of the expected nb_max.
To give an example, let nb_desc=UINT16_MAX, nb_align=32, nb_max=4096 and nb_min=64. RTE_ALIGN_CEIL(nb_desc, nb_align) calls RTE_ALIGN_FLOOR(nb_desc + nb_align - 1, nb_align). This results in an overflow of nb_desc, leading to nb_desc being set to 30 and then 0 when the macros return. As a result of this, nb_desc is then set to nb_min later on.
The resolution involves upcasting nb_desc to a uint32_t before the RTE_ALIGN_CEIL macro is applied. This change ensures that the subsequent call to RTE_ALIGN_FLOOR(nb_desc + (nb_align - 1), nb_align) does not result in an overflow, as it would when nb_desc is a uint16_t. By using a uint32_t for these operations, the correct behavior is maintained without the risk of overflow.
---
v3:
* changed to explicit cast
v2:
* add example of issue to commit message
--- .mailmap | 1 + lib/ethdev/rte_ethdev.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-)
.mailmap +1 -0
@@ -1053,6 +1053,7 @@ Nelson Escobar <[email protected]> Nemanja Marjanovic <[email protected]> Netanel Belgazal <[email protected]> Netanel Gonen <[email protected]> +Niall Meade <[email protected]> Niall Power <[email protected]> Nicholas Pratte <[email protected]> Nick Connolly <[email protected]> <[email protected]>
lib/ethdev/rte_ethdev.c +9 -3
@@ -6577,13 +6577,19 @@ static voideth_dev_adjust_nb_desc (uint16_t *nb_desc, const struct rte_eth_desc_lim *desc_lim) { +/* Upcast to uint32 to avoid potential overflow with RTE_ALIGN_CEIL(). */ +uint32_t nb_desc_32 = (uint32_t )*nb_desc; +if (desc_lim->nb_align !=0 ) - *nb_desc =RTE_ALIGN_CEIL (*nb_desc, desc_lim->nb_align); + nb_desc_32 =RTE_ALIGN_CEIL (nb_desc_32, desc_lim->nb_align);if (desc_lim->nb_max !=0 ) - *nb_desc =RTE_MIN (*nb_desc, desc_lim->nb_max); + nb_desc_32 =RTE_MIN (nb_desc_32, desc_lim->nb_max); + + nb_desc_32 =RTE_MAX (nb_desc_32, desc_lim->nb_min); - *nb_desc =RTE_MAX (*nb_desc, desc_lim->nb_min); +/* Assign clipped u32 back to u16. */ + *nb_desc = (uint16_t )nb_desc_32; }int
Signature · 13 lines
2.34.1 -------------------------------------------------------------- Intel Research and Development Ireland Limited Registered in Ireland Registered Office: Collinstown Industrial Park, Leixlip, County Kildare Registered Number: 308263 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 20 lines · Niall Meade
Addressed a specific overflow issue in the
eth_dev_adjust_nb_desc()function where theuint16_tvariablenb_descwould overflow when its value was greater than (2^16 -nb_align). This overflow causednb_descto incorrectly wrap around between 0 and nb_align-1, leading to the function settingnb_desctonb_mininstead of the expectednb_max.To give an example, let nb_desc=UINT16_MAX, nb_align=32, nb_max=4096 and nb_min=64.
RTE_ALIGN_CEIL(nb_desc, nb_align)callsRTE_ALIGN_FLOOR(nb_desc + nb_align - 1, nb_align). This results in an overflow ofnb_desc, leading tonb_descbeing set to 30 and then 0 when the macros return. As a result of this,nb_descis then set tonb_minlater on.The resolution involves upcasting
nb_descto auint32_tbefore theRTE_ALIGN_CEILmacro is applied. This change ensures that the subsequent call toRTE_ALIGN_FLOOR(nb_desc + (nb_align - 1), nb_align)does not result in an overflow, as it would whennb_descis auint16_t. By using auint32_tfor these operations, the correct behavior is maintained without the risk of overflow.
Quoted 4 lines
Fixes 0f67fc3baeb9 ("ethdev: add function to adjust number of descriptors")Signed-off-by Niall Meade <[email protected]>
Applied to dpdk-next-net/main, thanks.