net/nfp: fix UB in BAR size shift operations
PATCH 1/1 net/nfp: fix UB in BAR size shift operations
The literal '1' is a signed 32-bit int. Shifting it by bar->bitsize is undefined behavior when bitsize >= 31, and sign-extends when bitsize == 31 (producing a wrong upper-bound check). BAR aperture sizes from hardware can exceed this range.
Fix by using RTE_BIT64() which produces a 64-bit unsigned value, matching the type of the operands (uint64_t base, uint64_t offset).
--- drivers/net/nfp/nfpcore/nfp6000_pcie.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/nfp/nfpcore/nfp6000_pcie.c +4 -4
@@ -55,11 +55,11 @@*/ #define NFP_PCI_MIN_MAP_SIZE 0x080000/* 512K */ -#define NFP_PCIE_P2C_FIXED_SIZE (bar) (1 << (bar)->bitsize) -#define NFP_PCIE_P2C_BULK_SIZE (bar) (1 << (bar)->bitsize) +#define NFP_PCIE_P2C_FIXED_SIZE (bar) RTE_BIT64((bar)->bitsize) +#define NFP_PCIE_P2C_BULK_SIZE (bar) RTE_BIT64((bar)->bitsize)#define NFP_PCIE_P2C_GENERAL_TARGET_OFFSET (bar, x) ((x) << ((bar)->bitsize - 2))#define NFP_PCIE_P2C_GENERAL_TOKEN_OFFSET (bar, x) ((x) << ((bar)->bitsize - 4)) -#define NFP_PCIE_P2C_GENERAL_SIZE (bar) (1 << ((bar)->bitsize - 4)) +#define NFP_PCIE_P2C_GENERAL_SIZE (bar) RTE_BIT64(((bar)->bitsize - 4))#define NFP_PCIE_P2C_EXPBAR_OFFSET (bar_index) ((bar_index) * 4)
@@ -443,7 +443,7 @@ matching_bar_exist(struct nfp_bar *bar, (bar_token <0 || bar_token == token) && bar_action == action && bar->base <= offset && - (bar->base + (1 << bar->bitsize)) >= (offset + size)) + (bar->base + RTE_BIT64(bar->bitsize)) >= (offset + size)) return true;/* No match */
Signature · 1 line
2.34.1
Thomas Monjalon
Applied07/07/2026 16:01, Alexey Simakov:
Quoted 5 lines
The literal '1' is a signed 32-bit int. Shifting it by
bar->bitsizeis undefined behavior when bitsize >= 31, and sign-extends when bitsize == 31 (producing a wrong upper-bound check). BAR aperture sizes from hardware can exceed this range.
Quoted 8 lines
Fix by using
RTE_BIT64()which produces a 64-bit unsigned value, matching the type of the operands (uint64_tbase,uint64_toffset).Fixes c7e9729da6b5 ("net/nfp: support CPP")Fixes 1fbe51cd9c3a ("net/nfp: extend usage of BAR from 8 to 24")Signed-off-by Alexey Simakov <[email protected]>
Applied, thanks.