From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id F3ACA432C2; Tue, 7 Nov 2023 09:38:23 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C7D72402BC; Tue, 7 Nov 2023 09:38:23 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by mails.dpdk.org (Postfix) with ESMTP id 1EBE94003C for ; Tue, 7 Nov 2023 09:38:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699346302; x=1730882302; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=bF3dSSNbxyaITVQo8KyivpE0Y8Hln3HiJMwn0d9vuPs=; b=jBm/fCynffISkwJYFSpB4tdV+9MUeLT7iqsINhIeLw2UBY0KD+qlTw7M L1EXplGSNMltatXBidfMf8nQLkqmuT7Nqcdn/VMVAfZWLKxw+kTiM5jNm Z6TqqRhogJmc9TnxTfomBDrj87mtmBI7MVE30wqc+rXkjrgcmXC7OngHy i4EsebP7Xp2TCyADIXJTcDqstlDX1toJ5BvWmqe5zhFSDMpUDWDGKMjQE 5l5cKZ0YjDtFrKOfv9CmjCn9+Yi34ohfKdwjjtCYxpQPIC0oQHXUqUnJm fJHbSqjdHQ1BAWgsYRTy0Ruf1lDvUVtYfbYiy6GGmdWnydh0r7o7RUYPz Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10886"; a="2457705" X-IronPort-AV: E=Sophos;i="6.03,283,1694761200"; d="scan'208";a="2457705" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2023 00:38:20 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10886"; a="756125029" X-IronPort-AV: E=Sophos;i="6.03,283,1694761200"; d="scan'208";a="756125029" Received: from dpdk-wenjing-02.sh.intel.com ([10.67.118.228]) by orsmga007.jf.intel.com with ESMTP; 07 Nov 2023 00:38:17 -0800 From: wenjing.qiao@intel.com To: jingjing.wu@intel.com, beilei.xing@intel.com Cc: dev@dpdk.org, Wenjing Qiao Subject: [PATCH] net/cpfl: refactor flow parser Date: Tue, 7 Nov 2023 08:34:58 +0000 Message-Id: <20231107083456.1259255-1-wenjing.qiao@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Wenjing Qiao Use strncpy instead of memcpy for string copy and rename macro. Coverity issue: 405350 Fixes: 6cc97c9971d7 ("net/cpfl: build action mapping rules from JSON") Signed-off-by: Wenjing Qiao --- drivers/net/cpfl/cpfl_flow_parser.c | 34 +++++++++++++++------ drivers/net/cpfl/cpfl_flow_parser.h | 46 ++++++++++++++--------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/drivers/net/cpfl/cpfl_flow_parser.c b/drivers/net/cpfl/cpfl_flow_parser.c index 1e0ba289c2..a8f0488f21 100644 --- a/drivers/net/cpfl/cpfl_flow_parser.c +++ b/drivers/net/cpfl/cpfl_flow_parser.c @@ -205,11 +205,11 @@ cpfl_flow_js_pattern_key_proto_field(json_t *ob_fields, PMD_DRV_LOG(ERR, "Can not parse string 'name'."); goto err; } - if (strlen(name) > CPFL_FLOW_JSON_STR_SIZE_MAX) { + if (strlen(name) > CPFL_JS_STR_SIZE - 1) { PMD_DRV_LOG(ERR, "The 'name' is too long."); goto err; } - memcpy(js_field->fields[i].name, name, strlen(name)); + strncpy(js_field->fields[i].name, name, CPFL_JS_STR_SIZE - 1); if (js_field->type == RTE_FLOW_ITEM_TYPE_ETH || js_field->type == RTE_FLOW_ITEM_TYPE_IPV4) { @@ -218,11 +218,11 @@ cpfl_flow_js_pattern_key_proto_field(json_t *ob_fields, PMD_DRV_LOG(ERR, "Can not parse string 'mask'."); goto err; } - if (strlen(mask) > CPFL_FLOW_JSON_STR_SIZE_MAX) { + if (strlen(mask) > CPFL_JS_STR_SIZE - 1) { PMD_DRV_LOG(ERR, "The 'mask' is too long."); goto err; } - memcpy(js_field->fields[i].mask, mask, strlen(mask)); + strncpy(js_field->fields[i].mask, mask, CPFL_JS_STR_SIZE - 1); } else { uint32_t mask_32b; int ret; @@ -633,8 +633,12 @@ cpfl_flow_js_mr_key(json_t *ob_mr_keys, struct cpfl_flow_js_mr_key *js_mr_key) PMD_DRV_LOG(ERR, "Can not parse string 'name'."); goto err; } + if (strlen(name) > CPFL_JS_STR_SIZE - 1) { + PMD_DRV_LOG(ERR, "The 'name' is too long."); + goto err; + } strncpy(js_mr_key->actions[i].prog.name, name, - CPFL_FLOW_JSON_STR_SIZE_MAX - 1); + CPFL_JS_STR_SIZE - 1); } ob_param = json_object_get(object, "parameters"); @@ -655,8 +659,12 @@ cpfl_flow_js_mr_key(json_t *ob_mr_keys, struct cpfl_flow_js_mr_key *js_mr_key) PMD_DRV_LOG(ERR, "Can not parse string 'name'."); goto err; } + if (strlen(name) > CPFL_JS_STR_SIZE - 1) { + PMD_DRV_LOG(ERR, "The 'name' is too long."); + goto err; + } strncpy(js_mr_key->actions[i].prog.params[j].name, name, - CPFL_FLOW_JSON_STR_SIZE_MAX - 1); + CPFL_JS_STR_SIZE - 1); } ret = cpfl_json_t_to_uint16(subobject, "size", &value); if (ret < 0) { @@ -719,7 +727,11 @@ cpfl_flow_js_mr_layout(json_t *ob_layouts, struct cpfl_flow_js_mr_action_mod *js PMD_DRV_LOG(ERR, "Can not parse string 'hint'."); goto err; } - memcpy(js_mod->layout[i].hint, hint, strlen(hint)); + if (strlen(hint) > CPFL_JS_STR_SIZE - 1) { + PMD_DRV_LOG(ERR, "The 'hint' is too long."); + goto err; + } + strncpy(js_mod->layout[i].hint, hint, CPFL_JS_STR_SIZE - 1); } return 0; @@ -762,7 +774,11 @@ cpfl_flow_js_mr_content(json_t *ob_content, struct cpfl_flow_js_mr_action_mod *j PMD_DRV_LOG(ERR, "Can not parse string 'type'."); goto err; } - strncpy(js_mod->content.fields[i].type, type, CPFL_FLOW_JSON_STR_SIZE_MAX - 1); + if (strlen(type) > CPFL_JS_STR_SIZE - 1) { + PMD_DRV_LOG(ERR, "The 'type' is too long."); + goto err; + } + strncpy(js_mod->content.fields[i].type, type, CPFL_JS_STR_SIZE - 1); ret = cpfl_json_t_to_uint16(object, "start", &start); if (ret < 0) { PMD_DRV_LOG(ERR, "Can not parse 'start'."); @@ -1698,7 +1714,7 @@ cpfl_parse_check_prog_action(struct cpfl_flow_js_mr_key_action *key_act, if (param->has_name) { mr_key_prog->has_name = TRUE; strncpy(mr_key_prog->name[param->index], param->name, - CPFL_FLOW_JSON_STR_SIZE_MAX - 1); + CPFL_JS_STR_SIZE - 1); } } diff --git a/drivers/net/cpfl/cpfl_flow_parser.h b/drivers/net/cpfl/cpfl_flow_parser.h index c9a9772f13..23904e39f1 100644 --- a/drivers/net/cpfl/cpfl_flow_parser.h +++ b/drivers/net/cpfl/cpfl_flow_parser.h @@ -9,13 +9,13 @@ #ifndef _CPFL_FLOW_PARSER_H_ #define _CPFL_FLOW_PARSER_H_ -#define CPFL_FLOW_JSON_STR_SIZE_MAX 100 -#define CPFL_MAX_SEM_FV_KEY_SIZE 64 -#define CPFL_FLOW_JS_PROTO_SIZE 16 -#define CPFL_MOD_KEY_NUM_MAX 8 -#define CPFL_PROG_CONTENT_FIELD_NUM_MAX 64 -#define CPFL_PROG_CONSTANT_VALUE_NUM_MAX 8 -#define CPFL_PROG_PARAM_NUM_MAX 10 +#define CPFL_JS_STR_SIZE 100 +#define CPFL_JS_SEM_FV_KEY_NUM_MAX 64 +#define CPFL_JS_PROTO_NUM_MAX 16 +#define CPFL_JS_MOD_KEY_NUM_MAX 8 +#define CPFL_JS_PROG_CONTENT_FIELD_NUM_MAX 64 +#define CPFL_JS_PROG_CONSTANT_VALUE_NUM_MAX 8 +#define CPFL_JS_PROG_PARAM_NUM_MAX 10 /* Pattern Rules Storage */ enum cpfl_flow_pr_action_type { @@ -30,9 +30,9 @@ struct cpfl_flow_js_pr_key_attr { }; struct cpfl_flow_js_pr_key_proto_field { - char name[CPFL_FLOW_JSON_STR_SIZE_MAX]; + char name[CPFL_JS_STR_SIZE]; union { - char mask[CPFL_FLOW_JSON_STR_SIZE_MAX]; + char mask[CPFL_JS_STR_SIZE]; uint32_t mask_32b; }; }; @@ -80,7 +80,7 @@ struct cpfl_flow_js_fv { struct cpfl_flow_js_pr_action_sem { uint16_t prof; /* SEM profile ID */ uint16_t subprof; /* SEM subprofile ID */ - uint16_t keysize; /* extract key size in bytes */ + uint16_t keysize; /* extract key size in bytes */ struct cpfl_flow_js_fv *fv; /* A SEM field vector array */ int fv_size; }; @@ -116,23 +116,23 @@ struct cpfl_flow_js_pr { * of data. */ struct cpfl_flow_js_mr_key_action_vxlan_encap { - enum rte_flow_item_type protocols[CPFL_FLOW_JS_PROTO_SIZE]; + enum rte_flow_item_type protocols[CPFL_JS_PROTO_NUM_MAX]; int proto_size; }; struct cpfl_flow_js_prog_parameter { bool has_name; uint16_t index; - char name[CPFL_FLOW_JSON_STR_SIZE_MAX]; + char name[CPFL_JS_STR_SIZE]; uint16_t size; }; struct cpfl_flow_js_mr_key_action_prog { bool has_name; uint32_t id; - char name[CPFL_FLOW_JSON_STR_SIZE_MAX]; + char name[CPFL_JS_STR_SIZE]; uint32_t param_size; - struct cpfl_flow_js_prog_parameter params[CPFL_PROG_PARAM_NUM_MAX]; + struct cpfl_flow_js_prog_parameter params[CPFL_JS_PROG_PARAM_NUM_MAX]; }; /* A set of modification rte_flow_action_xxx objects can be defined as a type / data pair. */ @@ -151,24 +151,24 @@ struct cpfl_flow_js_mr_key { struct cpfl_flow_js_mr_layout { int index; /* links to the element of the actions array */ - char hint[CPFL_FLOW_JSON_STR_SIZE_MAX]; /* where the data to copy from */ + char hint[CPFL_JS_STR_SIZE]; /* where the data to copy from */ uint16_t offset; /* the start byte of the data to copy from */ uint16_t size; /* bytes of the data to be copied to the memory region */ }; struct cpfl_flow_js_mr_field { - char type[CPFL_FLOW_JSON_STR_SIZE_MAX]; + char type[CPFL_JS_STR_SIZE]; uint16_t start; uint16_t width; union { uint16_t index; - uint8_t value[CPFL_PROG_CONSTANT_VALUE_NUM_MAX]; + uint8_t value[CPFL_JS_PROG_CONSTANT_VALUE_NUM_MAX]; }; }; struct cpfl_flow_js_mr_content { uint16_t size; - struct cpfl_flow_js_mr_field fields[CPFL_PROG_CONTENT_FIELD_NUM_MAX]; + struct cpfl_flow_js_mr_field fields[CPFL_JS_PROG_CONTENT_FIELD_NUM_MAX]; int field_size; }; @@ -182,7 +182,7 @@ struct cpfl_flow_js_mr_action_mod { bool is_content; union { struct { - struct cpfl_flow_js_mr_layout layout[CPFL_FLOW_JS_PROTO_SIZE]; + struct cpfl_flow_js_mr_layout layout[CPFL_JS_PROTO_NUM_MAX]; int layout_size; }; struct cpfl_flow_js_mr_content content; @@ -227,7 +227,7 @@ struct cpfl_flow_pr_action_sem { uint16_t prof; uint16_t subprof; uint16_t keysize; - uint8_t cpfl_flow_pr_fv[CPFL_MAX_SEM_FV_KEY_SIZE]; + uint8_t cpfl_flow_pr_fv[CPFL_JS_SEM_FV_KEY_NUM_MAX]; }; struct cpfl_flow_pr_action { @@ -239,7 +239,7 @@ struct cpfl_flow_pr_action { /* Modification Rules */ struct cpfl_flow_mr_key_action_vxlan_encap { - enum rte_flow_item_type protocols[CPFL_FLOW_JS_PROTO_SIZE]; + enum rte_flow_item_type protocols[CPFL_JS_PROTO_NUM_MAX]; uint16_t proto_size; const struct rte_flow_action *action; }; @@ -247,7 +247,7 @@ struct cpfl_flow_mr_key_action_vxlan_encap { struct cpfl_flow_mr_key_action_prog { const struct rte_flow_action_prog *prog; bool has_name; - char name[CPFL_PROG_PARAM_NUM_MAX][CPFL_FLOW_JSON_STR_SIZE_MAX]; + char name[CPFL_JS_PROG_PARAM_NUM_MAX][CPFL_JS_STR_SIZE]; }; struct cpfl_flow_mr_key_mod { @@ -256,7 +256,7 @@ struct cpfl_flow_mr_key_mod { }; struct cpfl_flow_mr_key_action { - struct cpfl_flow_mr_key_mod mods[CPFL_MOD_KEY_NUM_MAX]; + struct cpfl_flow_mr_key_mod mods[CPFL_JS_MOD_KEY_NUM_MAX]; struct cpfl_flow_mr_key_action_prog prog; }; -- 2.34.1