net/bnxt: fix two ULP parser/mapper safety issues
cover net/bnxt: fix two ULP parser/mapper safety issues
Two unrelated hardening fixes in the ULP parser/mapper found during review:
- Patch 1 caps a header-field memcpy() at the field's spec size, fixing a potential overflow when the source value is larger than the destination field.
- Patch 2 replaces a variable-length array sized by caller input with a heap allocation, avoiding unbounded stack growth.
Dakota Sicher (2):
net/bnxt: cap memcpy at field size
net/bnxt: replace VLA with heap allocation
drivers/net/bnxt/tf_ulp/ulp_mapper_tfc.c | 10 ++++++++-- drivers/net/bnxt/tf_ulp/ulp_rte_parser.c | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/2 net/bnxt: cap memcpy at field size
From: Dakota Sicher <[email protected]>
The memcpy() copying a header field's value in the parser does not check whether the source size exceeds the field's spec size, so an oversized value overflows the destination field.
--- drivers/net/bnxt/tf_ulp/ulp_rte_parser.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/net/bnxt/tf_ulp/ulp_rte_parser.c +10 -0
@@ -66,6 +66,11 @@ ulp_rte_parser_fld_copy(struct ulp_rte_hdr_field *field, const void *buffer, uint32_t size) { + if (unlikely(size > RTE_PARSER_FLOW_HDR_FIELD_SIZE)) { + BNXT_DRV_DBG(ERR, "Field size %u exceeds max of %u, clamping", + size, RTE_PARSER_FLOW_HDR_FIELD_SIZE); + size =RTE_PARSER_FLOW_HDR_FIELD_SIZE ; + } field->size = size;memcpy (field->spec, buffer, field->size); field++;
@@ -106,6 +111,11 @@ ulp_rte_prsr_fld_mask(struct ulp_rte_parser_params *params, {struct ulp_rte_hdr_field *field = ¶ms->hdr_field[*idx]; +if (unlikely (size >RTE_PARSER_FLOW_HDR_FIELD_SIZE )) { +BNXT_DRV_DBG (ERR ,"Field size %u exceeds max of %u, clamping" , + size,RTE_PARSER_FLOW_HDR_FIELD_SIZE ); + size =RTE_PARSER_FLOW_HDR_FIELD_SIZE ; + }/* update the field size */ field->size = size;
Signature · 1 line
2.31.1
PATCH 2/2 net/bnxt: replace VLA with heap allocation
In reply to Manish KurupFrom: Dakota Sicher <[email protected]>
ulp_mapper_blob_block_swap() used a variable-length array on the stack sized by caller input for swap memory. Use a heap allocation instead to avoid unbounded stack growth.
--- drivers/net/bnxt/tf_ulp/ulp_mapper_tfc.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/net/bnxt/tf_ulp/ulp_mapper_tfc.c +8 -2
@@ -388,9 +388,8 @@ static const char * const mpc_error_str[] = {static int32_t ulp_mapper_blob_block_swap (struct ulp_blob *blob,uint32_t block_sz) { -uint8_t data[block_sz];/* size of a block for temp storage */ uint16_t num_words, data_sz; -uint8_t *pdata; +uint8_t *pdata, *data;int i;/* Shouldn't happen since it is internal function, but check anyway */
@@ -407,6 +406,12 @@ ulp_mapper_blob_block_swap(struct ulp_blob *blob, uint32_t block_sz)return -EINVAL ; } + data =rte_zmalloc (NULL , block_sz,0 ); +if (unlikely (!data)) { +BNXT_DRV_DBG (ERR ,"Failed to allocate swap buffer" ); +return -ENOMEM ; + } + num_words = data_sz / block_sz;for (i =0 ; i < num_words /2 ; i++) {memcpy (data, &pdata[i * block_sz], block_sz);
@@ -415,6 +420,7 @@ ulp_mapper_blob_block_swap(struct ulp_blob *blob, uint32_t block_sz)memcpy (&pdata[(num_words -1 - i) * block_sz], data, block_sz); } +rte_free (data);return 0 ; }
Signature · 1 line
2.31.1
v2 cover net/bnxt: fix two ULP parser/mapper safety issues
In reply to Manish KurupFrom: Mohammad Shuab Siddique <[email protected]>
Two independent hardening changes in the ULP parser and mapper, found by static analysis.
v2:
- 1/2: reworded as hardening: all current callers pass sizeof() of a protocol field, so there is no known overflow; drop Fixes and Cc:
stable. No code change.
- 2/2: swap the blocks in place instead of allocating a temporary buffer, so there is no per-flow heap allocation and no -ENOMEM path; retitled accordingly; drop Fixes and Cc: stable.
Dakota Sicher (2):
net/bnxt: cap memcpy at field size
net/bnxt: remove VLA from EM key block swap
drivers/net/bnxt/tf_ulp/ulp_mapper_tfc.c | 18 ++++++++++-------- drivers/net/bnxt/tf_ulp/ulp_rte_parser.c | 10 ++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/2 net/bnxt: cap memcpy at field size
From: Dakota Sicher <[email protected]>
ulp_rte_parser_fld_copy() and ulp_rte_prsr_fld_mask() copy a caller-supplied size into fixed 16-byte spec and mask arrays without checking it. All current callers pass sizeof() of a protocol field, but nothing enforces that. Clamp the size to the field storage and log an error.
--- drivers/net/bnxt/tf_ulp/ulp_rte_parser.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/net/bnxt/tf_ulp/ulp_rte_parser.c +10 -0
@@ -66,6 +66,11 @@ ulp_rte_parser_fld_copy(struct ulp_rte_hdr_field *field, const void *buffer, uint32_t size) { + if (unlikely(size > RTE_PARSER_FLOW_HDR_FIELD_SIZE)) { + BNXT_DRV_DBG(ERR, "Field size %u exceeds max of %u, clamping", + size, RTE_PARSER_FLOW_HDR_FIELD_SIZE); + size =RTE_PARSER_FLOW_HDR_FIELD_SIZE ; + } field->size = size;memcpy (field->spec, buffer, field->size); field++;
@@ -106,6 +111,11 @@ ulp_rte_prsr_fld_mask(struct ulp_rte_parser_params *params, {struct ulp_rte_hdr_field *field = ¶ms->hdr_field[*idx]; +if (unlikely (size >RTE_PARSER_FLOW_HDR_FIELD_SIZE )) { +BNXT_DRV_DBG (ERR ,"Field size %u exceeds max of %u, clamping" , + size,RTE_PARSER_FLOW_HDR_FIELD_SIZE ); + size =RTE_PARSER_FLOW_HDR_FIELD_SIZE ; + }/* update the field size */ field->size = size;
Signature · 1 line
2.47.3
PATCH v2 2/2 net/bnxt: remove VLA from EM key block swap
In reply to Mohammad Shuab SiddiqueFrom: Dakota Sicher <[email protected]>
ulp_mapper_blob_block_swap() used a variable-length array on the stack as scratch space for the block swap. The block size comes from the device table and is small, but a VLA is still undesirable. Swap the blocks in place instead, which needs no scratch buffer.
--- drivers/net/bnxt/tf_ulp/ulp_mapper_tfc.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/net/bnxt/tf_ulp/ulp_mapper_tfc.c +10 -8
@@ -388,10 +388,9 @@ static const char * const mpc_error_str[] = {static int32_t ulp_mapper_blob_block_swap (struct ulp_blob *blob,uint32_t block_sz) { -uint8_t data[block_sz];/* size of a block for temp storage */ uint16_t num_words, data_sz; -uint8_t *pdata; -int i; +uint8_t *pdata, *lo, *hi, tmp; +uint32_t i, j;/* Shouldn't happen since it is internal function, but check anyway */ if (unlikely (blob ==NULL || !block_sz)) {
@@ -407,13 +406,16 @@ ulp_mapper_blob_block_swap(struct ulp_blob *blob, uint32_t block_sz)return -EINVAL ; } +/* Swap the blocks in place, one byte at a time, no temp buffer */ num_words = data_sz / block_sz;for (i =0 ; i < num_words /2 ; i++) { -memcpy (data, &pdata[i * block_sz], block_sz); -memcpy (&pdata[i * block_sz], - &pdata[(num_words -1 - i) * block_sz], block_sz); -memcpy (&pdata[(num_words -1 - i) * block_sz], - data, block_sz); + lo = &pdata[i * block_sz]; + hi = &pdata[(num_words -1 - i) * block_sz]; +for (j =0 ; j < block_sz; j++) { + tmp = lo[j]; + lo[j] = hi[j]; + hi[j] = tmp; + } }return 0 ; }
Signature · 1 line
2.47.3