zxdh_get_res_info() copies a device-supplied reply length into the
caller's buffer without knowing that buffer's size. Both callers pass
the address of a uint8_t local, so a reply longer than one byte
overwrites the caller's stack. Nothing bounds the length either: it
sits in a nested TBL reply header, which
zxdh_bar_chan_sync_msg_send() does not look at, and a value above
ZXDH_RSC_TBL_CONTENT_LEN_MAX also overruns recv_buf.

Pass the destination capacity to the function and reject a reply that
exceeds it or the recv_buf size, rather than truncating a malformed
length into a wrong panel or hash id.

While here, report a reply whose status byte is not the success code
as ZXDH_BAR_MSG_ERR_REPLY and log that byte: such a reply was returned
to the caller as success. Copy with memcpy() rather than rte_memcpy():
the length is already checked, and the inlined body makes gcc
-fanalyzer report a false out-of-bounds write on the caller's
one-byte object.

Fixes: 6310e3976b ("net/zxdh: get device backend infos")
Cc: [email protected]

Signed-off-by: Junlong Wang <[email protected]>
---
 drivers/net/zxdh/zxdh_common.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/net/zxdh/zxdh_common.c b/drivers/net/zxdh/zxdh_common.c
index 2b821b1dc8..22cdfbacc4 100644
--- a/drivers/net/zxdh/zxdh_common.c
+++ b/drivers/net/zxdh/zxdh_common.c
@@ -183,13 +183,14 @@ zxdh_fill_res_para(struct rte_eth_dev *dev, struct 
zxdh_res_para *param)
 }
 
 static int
-zxdh_get_res_info(struct zxdh_res_para *dev, uint8_t field, uint8_t *res, 
uint16_t *len)
+zxdh_get_res_info(struct zxdh_res_para *dev, uint8_t field,
+                 uint8_t *res, uint16_t max_len, uint16_t *len)
 {
        struct zxdh_pci_bar_msg in = {0};
        uint8_t recv_buf[ZXDH_RSC_TBL_CONTENT_LEN_MAX + 8] = {0};
        int ret = 0;
 
-       if (!res || !dev)
+       if (!res || !dev || !len)
                return ZXDH_BAR_MSG_ERR_NULL;
 
        struct zxdh_tbl_msg_header tbl_msg = {
@@ -224,11 +225,18 @@ zxdh_get_res_info(struct zxdh_res_para *dev, uint8_t 
field, uint8_t *res, uint16
 
        if (tbl_reps->check != ZXDH_TBL_MSG_PRO_SUCCESS) {
                PMD_DRV_LOG(ERR,
-                       "get resource_field failed. pcieid: 0x%x, ret: %d", 
dev->pcie_id, ret);
-               return ret;
+                       "get resource_field failed. pcieid: 0x%x, check: %u",
+                       dev->pcie_id, tbl_reps->check);
+               return ZXDH_BAR_MSG_ERR_REPLY;
+       }
+       if (tbl_reps->len > max_len || tbl_reps->len > 
ZXDH_RSC_TBL_CONTENT_LEN_MAX) {
+               PMD_DRV_LOG(ERR,
+                       "invalid resource_field len: %u (max: %u). pcieid: 
0x%x",
+                       tbl_reps->len, max_len, dev->pcie_id);
+               return -EINVAL;
        }
        *len = tbl_reps->len;
-       rte_memcpy(res, (recv_buf + ZXDH_REPS_HEADER_OFFSET +
+       memcpy(res, (recv_buf + ZXDH_REPS_HEADER_OFFSET +
                sizeof(struct zxdh_tbl_msg_reps_header)), *len);
        return ret;
 }
@@ -239,7 +247,8 @@ zxdh_get_res_panel_id(struct zxdh_res_para *in, uint8_t 
*panel_id)
        uint8_t reps = 0;
        uint16_t reps_len = 0;
 
-       if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_PNLID, &reps, &reps_len) != 
ZXDH_BAR_MSG_OK)
+       if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_PNLID, &reps, sizeof(reps),
+                             &reps_len) != ZXDH_BAR_MSG_OK)
                return -1;
 
        *panel_id = reps;
@@ -262,7 +271,8 @@ zxdh_get_res_hash_id(struct zxdh_res_para *in, uint8_t 
*hash_id)
        uint8_t reps = 0;
        uint16_t reps_len = 0;
 
-       if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_HASHID, &reps, &reps_len) != 
ZXDH_BAR_MSG_OK)
+       if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_HASHID, &reps, sizeof(reps),
+                             &reps_len) != ZXDH_BAR_MSG_OK)
                return -1;
 
        *hash_id = reps;
-- 
2.27.0

Reply via email to