Replace use of the strncpy and strcpy functions with the safer strlcpy
alternative, which both bounds-checks and guarantees null termination.
In the process also replace instances of strcat with strlcat where
appropriate.

Fixes: 2d823ecd671c ("net/cpfl: support device initialization")
Fixes: c4c59ae62793 ("net/cpfl: refactor flow parser")
Fixes: c10881d3ee74 ("net/cpfl: support flow prog action")
Fixes: 9481b0902efe ("net/ice: send driver version to firmware")
Fixes: 7f7cbf80bdb7 ("net/ice: factorize firmware loading")
Fixes: 549343c25db8 ("net/idpf: support device initialization")
Fixes: 484f8e407a94 ("net/igb: support xstats by ID")
Fixes: fca82a8accf9 ("net/ixgbe: support xstats by ID")
Fixes: e163c18a15b0 ("net/i40e: update ptype and pctype info")
Cc: [email protected]

Signed-off-by: Bruce Richardson <[email protected]>
---
 drivers/net/intel/cpfl/cpfl_ethdev.c      |  2 +-
 drivers/net/intel/cpfl/cpfl_flow_parser.c | 16 +++++++---------
 drivers/net/intel/e1000/igb_ethdev.c      |  5 +++--
 drivers/net/intel/i40e/i40e_ethdev.c      |  3 +--
 drivers/net/intel/ice/ice_ethdev.c        | 16 +++++++---------
 drivers/net/intel/idpf/idpf_ethdev.c      |  2 +-
 drivers/net/intel/ixgbe/ixgbe_ethdev.c    |  5 +++--
 7 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/drivers/net/intel/cpfl/cpfl_ethdev.c 
b/drivers/net/intel/cpfl/cpfl_ethdev.c
index 7ac8797490..4315adb68c 100644
--- a/drivers/net/intel/cpfl/cpfl_ethdev.c
+++ b/drivers/net/intel/cpfl/cpfl_ethdev.c
@@ -2534,7 +2534,7 @@ cpfl_adapter_ext_init(struct rte_pci_device *pci_dev, 
struct cpfl_adapter_ext *a
        hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
        adapter->host_id = get_running_host_id();
 
-       strncpy(adapter->name, pci_dev->device.name, PCI_PRI_STR_SIZE);
+       strlcpy(adapter->name, pci_dev->device.name, sizeof(adapter->name));
 
        memcpy(&base->caps, &req_caps, sizeof(struct 
virtchnl2_get_capabilities));
 
diff --git a/drivers/net/intel/cpfl/cpfl_flow_parser.c 
b/drivers/net/intel/cpfl/cpfl_flow_parser.c
index dfaddc9ec5..b1d06725e9 100644
--- a/drivers/net/intel/cpfl/cpfl_flow_parser.c
+++ b/drivers/net/intel/cpfl/cpfl_flow_parser.c
@@ -211,7 +211,7 @@ cpfl_flow_js_pattern_key_proto_field(json_t *ob_fields,
                        PMD_DRV_LOG(ERR, "The 'name' is too long.");
                        goto err;
                }
-               strncpy(js_field->fields[i].name, name, CPFL_JS_STR_SIZE - 1);
+               strlcpy(js_field->fields[i].name, name, CPFL_JS_STR_SIZE);
 
                if (js_field->type == RTE_FLOW_ITEM_TYPE_ETH ||
                    js_field->type == RTE_FLOW_ITEM_TYPE_IPV4) {
@@ -716,8 +716,7 @@ cpfl_flow_js_mr_key(json_t *ob_mr_keys, struct 
cpfl_flow_js_mr_key *js_mr_key)
                                        PMD_DRV_LOG(ERR, "The 'name' is too 
long.");
                                        goto err;
                                }
-                               strncpy(js_mr_key->actions[i].prog.name, name,
-                                       CPFL_JS_STR_SIZE - 1);
+                               strlcpy(js_mr_key->actions[i].prog.name, name, 
CPFL_JS_STR_SIZE);
                        }
 
                        ob_param = json_object_get(object, "parameters");
@@ -742,8 +741,8 @@ cpfl_flow_js_mr_key(json_t *ob_mr_keys, struct 
cpfl_flow_js_mr_key *js_mr_key)
                                                PMD_DRV_LOG(ERR, "The 'name' is 
too long.");
                                                goto err;
                                        }
-                                       
strncpy(js_mr_key->actions[i].prog.params[j].name, name,
-                                               CPFL_JS_STR_SIZE - 1);
+                                       
strlcpy(js_mr_key->actions[i].prog.params[j].name, name,
+                                               CPFL_JS_STR_SIZE);
                                }
                                ret = cpfl_json_t_to_uint16(subobject, "size", 
&value);
                                if (ret < 0) {
@@ -810,7 +809,7 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct 
cpfl_flow_js_mr_action_mod *js
                        PMD_DRV_LOG(ERR, "The 'hint' is too long.");
                        goto err;
                }
-               strncpy(js_mod->layout[i].hint, hint, CPFL_JS_STR_SIZE - 1);
+               strlcpy(js_mod->layout[i].hint, hint, CPFL_JS_STR_SIZE);
        }
 
        return 0;
@@ -856,7 +855,7 @@ cpfl_flow_js_mr_content(json_t *ob_content, struct 
cpfl_flow_js_mr_action_mod *j
                        PMD_DRV_LOG(ERR, "The 'type' is too long.");
                        goto err;
                }
-               strncpy(js_mod->content.fields[i].type, type, CPFL_JS_STR_SIZE 
- 1);
+               strlcpy(js_mod->content.fields[i].type, type, CPFL_JS_STR_SIZE);
                ret = cpfl_json_t_to_uint16(object, "start", &start);
                if (ret < 0) {
                        PMD_DRV_LOG(ERR, "Can not parse 'start'.");
@@ -1806,8 +1805,7 @@ cpfl_parse_check_prog_action(struct 
cpfl_flow_js_mr_key_action *key_act,
                        return -EINVAL;
                if (param->has_name) {
                        mr_key_prog->has_name = TRUE;
-                       strncpy(mr_key_prog->name[param->index], param->name,
-                               CPFL_JS_STR_SIZE - 1);
+                       strlcpy(mr_key_prog->name[param->index], param->name, 
CPFL_JS_STR_SIZE);
                }
        }
 
diff --git a/drivers/net/intel/e1000/igb_ethdev.c 
b/drivers/net/intel/e1000/igb_ethdev.c
index a4370fe32b..524c030be6 100644
--- a/drivers/net/intel/e1000/igb_ethdev.c
+++ b/drivers/net/intel/e1000/igb_ethdev.c
@@ -2047,8 +2047,9 @@ static int eth_igb_xstats_get_names_by_id(struct 
rte_eth_dev *dev,
                                PMD_INIT_LOG(ERR, "id value isn't valid");
                                return -1;
                        }
-                       strcpy(xstats_names[i].name,
-                                       xstats_names_copy[ids[i]].name);
+                       strlcpy(xstats_names[i].name,
+                                       xstats_names_copy[ids[i]].name,
+                                       sizeof(xstats_names[i].name));
                }
                return limit;
        }
diff --git a/drivers/net/intel/i40e/i40e_ethdev.c 
b/drivers/net/intel/i40e/i40e_ethdev.c
index 1370db68f3..b2694cd33a 100644
--- a/drivers/net/intel/i40e/i40e_ethdev.c
+++ b/drivers/net/intel/i40e/i40e_ethdev.c
@@ -11916,8 +11916,7 @@ i40e_update_customized_ptype(struct rte_eth_dev *dev, 
uint8_t *pkg,
                        for (n = 0; n < proto_num; n++) {
                                if (proto[n].proto_id != proto_id)
                                        continue;
-                               memset(name, 0, sizeof(name));
-                               strcpy(name, proto[n].name);
+                               strlcpy(name, proto[n].name, sizeof(name));
                                PMD_DRV_LOG(INFO, "name = %s", name);
                                if (!strncasecmp(name, "PPPOE", 5))
                                        ptype_mapping[i].sw_ptype |=
diff --git a/drivers/net/intel/ice/ice_ethdev.c 
b/drivers/net/intel/ice/ice_ethdev.c
index ad9c49b339..99305b604b 100644
--- a/drivers/net/intel/ice/ice_ethdev.c
+++ b/drivers/net/intel/ice/ice_ethdev.c
@@ -1897,7 +1897,7 @@ ice_send_driver_ver(struct ice_hw *hw)
        dv.minor_ver = 0;
        dv.build_ver = 0;
        dv.subbuild_ver = 0;
-       strncpy((char *)dv.driver_string, "dpdk", sizeof(dv.driver_string));
+       strlcpy((char *)dv.driver_string, "dpdk", sizeof(dv.driver_string));
 
        return ice_aq_send_driver_ver(hw, &dv, NULL);
 }
@@ -2054,24 +2054,22 @@ int ice_load_pkg(struct ice_adapter *adapter, bool 
use_dsn, uint64_t dsn)
        if (!use_dsn)
                goto no_dsn;
 
-       strncpy(pkg_file, ICE_PKG_FILE_SEARCH_PATH_UPDATES,
-               ICE_MAX_PKG_FILENAME_SIZE);
-       strcat(pkg_file, opt_ddp_filename);
+       strlcpy(pkg_file, ICE_PKG_FILE_SEARCH_PATH_UPDATES, 
ICE_MAX_PKG_FILENAME_SIZE);
+       strlcat(pkg_file, opt_ddp_filename, ICE_MAX_PKG_FILENAME_SIZE);
        if (ice_firmware_read(pkg_file, &buf, &bufsz) == 0)
                goto load_fw;
 
-       strncpy(pkg_file, ICE_PKG_FILE_SEARCH_PATH_DEFAULT,
-               ICE_MAX_PKG_FILENAME_SIZE);
-       strcat(pkg_file, opt_ddp_filename);
+       strlcpy(pkg_file, ICE_PKG_FILE_SEARCH_PATH_DEFAULT, 
ICE_MAX_PKG_FILENAME_SIZE);
+       strlcat(pkg_file, opt_ddp_filename, ICE_MAX_PKG_FILENAME_SIZE);
        if (ice_firmware_read(pkg_file, &buf, &bufsz) == 0)
                goto load_fw;
 
 no_dsn:
-       strncpy(pkg_file, ICE_PKG_FILE_UPDATES, ICE_MAX_PKG_FILENAME_SIZE);
+       strlcpy(pkg_file, ICE_PKG_FILE_UPDATES, ICE_MAX_PKG_FILENAME_SIZE);
        if (ice_firmware_read(pkg_file, &buf, &bufsz) == 0)
                goto load_fw;
 
-       strncpy(pkg_file, ICE_PKG_FILE_DEFAULT, ICE_MAX_PKG_FILENAME_SIZE);
+       strlcpy(pkg_file, ICE_PKG_FILE_DEFAULT, ICE_MAX_PKG_FILENAME_SIZE);
        if (ice_firmware_read(pkg_file, &buf, &bufsz) < 0) {
                PMD_INIT_LOG(ERR, "Failed to load default DDP package " 
ICE_PKG_FILE_DEFAULT);
                return -1;
diff --git a/drivers/net/intel/idpf/idpf_ethdev.c 
b/drivers/net/intel/idpf/idpf_ethdev.c
index fc761c6094..c13505416a 100644
--- a/drivers/net/intel/idpf/idpf_ethdev.c
+++ b/drivers/net/intel/idpf/idpf_ethdev.c
@@ -1497,7 +1497,7 @@ idpf_adapter_ext_init(struct rte_pci_device *pci_dev, 
struct idpf_adapter_ext *a
        hw->device_id = pci_dev->id.device_id;
        hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
 
-       strncpy(adapter->name, pci_dev->device.name, PCI_PRI_STR_SIZE);
+       strlcpy(adapter->name, pci_dev->device.name, sizeof(adapter->name));
 
        memcpy(&base->caps, &req_caps, sizeof(struct 
virtchnl2_get_capabilities));
 
diff --git a/drivers/net/intel/ixgbe/ixgbe_ethdev.c 
b/drivers/net/intel/ixgbe/ixgbe_ethdev.c
index f9de95e4fc..b36867d18d 100644
--- a/drivers/net/intel/ixgbe/ixgbe_ethdev.c
+++ b/drivers/net/intel/ixgbe/ixgbe_ethdev.c
@@ -3635,8 +3635,9 @@ static int ixgbe_dev_xstats_get_names_by_id(
                        PMD_INIT_LOG(ERR, "id value isn't valid");
                        return -1;
                }
-               strcpy(xstats_names[i].name,
-                               xstats_names_copy[ids[i]].name);
+               strlcpy(xstats_names[i].name,
+                               xstats_names_copy[ids[i]].name,
+                               sizeof(xstats_names[i].name));
        }
        return limit;
 }
-- 
2.53.0

Reply via email to