Fix coverity issues reported in DPDK-26380
cover Fix coverity issues reported in DPDK-26380
Below coverity issues are fixed in this patch set. 367477, 367478, 367481, 367483
Wei Huang (4):
raw/ifpga/base: use trusted buffer to free raw/ifpga/base: check return value of lseek raw/ifpga/base: assign unsigned value to length raw/ifpga/base: check pointer before dereferencing
drivers/raw/ifpga/base/ifpga_fme_rsu.c | 26 +++++++++++++++++++++++--- drivers/raw/ifpga/base/ifpga_sec_mgr.c | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-)
Signature · 1 line
2.29.2
PATCH 1/4 raw/ifpga/base: use trusted buffer to free
In write_flash_image(), calling function "read" may taints variable "buf" which turn to an untrusted value as argument of "rte_free".
Coverity issue: 367477
--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_fme_rsu.c +3 -1
@@ -92,6 +92,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image, uint32_t offset) {void *buf =NULL ; +void *buf_to_free =NULL ;int retry =0 ;uint32_t length =0 ;uint32_t to_transfer =0 ;
@@ -122,6 +123,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,close (fd);return -ENOMEM ; } + buf_to_free = buf; length = smgr->rsu_length; one_percent = length /100 ;
@@ -177,7 +179,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,printf ("\n" ); end: -free (buf); +free (buf_to_free);close (fd);return ret; }
Signature · 1 line
2.29.2
Re: [dpdk-dev] [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free
Quoted 54 lines
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: 2021年3月17日 16:22
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to freeIn
write_flash_image(), calling function "read" may taints variable "buf" which turn to an untrusted value as argument of "rte_free".Coverity issue: 367477
Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")Signed-off-by Wei Huang <[email protected]>--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/raw/ifpga/base/ifpga_fme_rsu.c +1
-0@@ -92,6 +92,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr,const char *image, uint32_t offset) { void *buf = NULL; +void *buf_to_free =NULL ;int retry =0 ;uint32_t length =0 ;drivers/raw/ifpga/base/ifpga_fme_rsu.c +1
-0uint32_t to_transfer =0 ;@@ -122,6 +123,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,close (fd);return -ENOMEM ; } + buf_to_free = buf; length = smgr->rsu_length;drivers/raw/ifpga/base/ifpga_fme_rsu.c +1
-1one_percent = length /100 ;@@ -177,7 +179,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,printf ("\n" ); end: -free (buf); +free (buf_to_free);close (fd);return ret;
Quoted 1 line
}
Quoted 2 lines
--
2.29.2
Re: [dpdk-dev] [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free
In reply to Wei HuangHi,
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: Wednesday, March 17, 2021 4:22 PM
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free
In write_flash_image(), calling function "read" may taints variable "buf" which turn to an untrusted value as argument of "rte_free".
Coverity issue: 367477
--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_fme_rsu.c +3 -1
@@ -92,6 +92,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image, uint32_t offset) {void *buf =NULL ; +void *buf_to_free =NULL ;int retry =0 ;uint32_t length =0 ;uint32_t to_transfer =0 ;
@@ -122,6 +123,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,close (fd);return -ENOMEM ; } + buf_to_free = buf; length = smgr->rsu_length; one_percent = length /100 ;
@@ -177,7 +179,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,printf ("\n" ); end: -free (buf); +free (buf_to_free);close (fd);return ret; }
--
2.29.2
Re: [dpdk-dev] [dpdk-stable] [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free
In reply to Wei HuangQuoted 6 lines · Wei Huang
In
write_flash_image(), calling function "read" may taints variable "buf" which turn to an untrusted value as argument of "rte_free".Coverity issue: 367477
Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
Hi Huang, Rosen,
I checked the coverity issue but still not clear about the problem. What does 'read' taints 'buf' mean?
The 'buf' passed as an argument to read, so all 'read' can do is change the memory that 'buf' points, so why it should affect the 'free' at all? If the memory is overflow etc, your change is just hiding the error not fixing it.
And the error message mentions from 'rte_free', not 'free', not sure how 'rte_free' is involved in the problem, any idea?
Quoted 35 lines
Signed-off-by Wei Huang <[email protected]>--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/raw/ifpga/base/ifpga_fme_rsu.c +3
-1@@ -92,6 +92,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image, uint32_t offset) {void *buf =NULL ; +void *buf_to_free =NULL ;int retry =0 ;uint32_t length =0 ;uint32_t to_transfer =0 ;@@ -122,6 +123,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,close (fd);return -ENOMEM ; } + buf_to_free = buf; length = smgr->rsu_length; one_percent = length /100 ;@@ -177,7 +179,7 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,printf ("\n" ); end: -free (buf); +free (buf_to_free);close (fd);return ret; }
PATCH 2/4 raw/ifpga/base: check return value of lseek
In reply to Wei HuangIn write_flash_image(), calling function "lseek" without checking return value has risk. Negative return value should be handled as an error condition.
Coverity issue: 367478
--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_fme_rsu.c +6 -1
@@ -130,7 +130,12 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image, do { to_transfer = (length > IFPGA_RSU_DATA_BLK_SIZE) ? IFPGA_RSU_DATA_BLK_SIZE : length; - lseek(fd, offset, SEEK_SET); + if (lseek(fd, offset, SEEK_SET) < 0) { + dev_err(smgr, "Failed to seek in \'%s\' [e:%s]\n", + image, strerror(errno)); + ret = -EIO; + goto end; + } read_size = read(fd, buf, to_transfer); if (read_size < 0) { dev_err(smgr, "Failed to read from \'%s\' [e:%s]\n",
Signature · 1 line
2.29.2
Re: [dpdk-dev] [PATCH v1 2/4] raw/ifpga/base: check return value of lseek
Quoted 41 lines
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: 2021年3月17日 16:22
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 2/4] raw/ifpga/base: check return value of lseekIn
write_flash_image(), calling function "lseek" without checking return value has risk. Negative return value should be handled as an error condition.Coverity issue: 367478
Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")Signed-off-by Wei Huang <[email protected]>--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/raw/ifpga/base/ifpga_fme_rsu.c +6
-1@@ -130,7 +130,12 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image, do { to_transfer = (length >IFPGA_RSU_DATA_BLK_SIZE ) ?IFPGA_RSU_DATA_BLK_SIZE : length; -lseek (fd, offset,SEEK_SET ); +if (lseek (fd, offset,SEEK_SET ) <0 ) { +dev_err (smgr,"Failed to seek in \'%s\' [e:%s]\n" , + image,strerror (errno)); + ret = -EIO ; + goto end; + } read_size =read (fd, buf, to_transfer);if (read_size <0 ) {
Quoted 2 lines
dev_err(smgr, "Failed to read from \'%s\' [e:%s]\n",
--
Re: [dpdk-dev] [PATCH v1 2/4] raw/ifpga/base: check return value of lseek
In reply to Wei HuangHi,
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: Wednesday, March 17, 2021 4:22 PM
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 2/4] raw/ifpga/base: check return value of lseek
In write_flash_image(), calling function "lseek" without checking return value has risk. Negative return value should be handled as an error condition.
Coverity issue: 367478
--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_fme_rsu.c +6 -1
@@ -130,7 +130,12 @@ static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image, do { to_transfer = (length > IFPGA_RSU_DATA_BLK_SIZE) ? IFPGA_RSU_DATA_BLK_SIZE : length; - lseek(fd, offset, SEEK_SET); + if (lseek(fd, offset, SEEK_SET) < 0) { + dev_err(smgr, "Failed to seek in \'%s\' [e:%s]\n", + image, strerror(errno)); + ret = -EIO; + goto end; + } read_size = read(fd, buf, to_transfer); if (read_size < 0) { dev_err(smgr, "Failed to read from \'%s\' [e:%s]\n",
--
2.29.2
PATCH 3/4 raw/ifpga/base: assign unsigned value to length
In reply to Wei HuangIn fpga_update_flash(), "smgr->rsu_length" is passed to a parameter that cannot be negative. So return value of function "lseek" should be checked before being assigned to "smgr->rsu_length".
Coverity issue: 367481
--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_fme_rsu.c +14 -1
@@ -277,6 +277,7 @@ int fpga_update_flash(struct ifpga_fme_hw *fme, const char *image,struct ifpga_sec_mgr *smgr =NULL ;uint32_t rsu_stat =0 ;int fd =-1 ; +off_t len =0 ;struct sigaction old_sigint_action;struct sigaction sa;time_t start;
@@ -320,9 +321,21 @@ int fpga_update_flash(struct ifpga_fme_hw *fme, const char *image, image, strerror(errno));return -EIO ; } - smgr->rsu_length =lseek (fd,0 ,SEEK_END ); + len =lseek (fd,0 ,SEEK_END );close (fd); +if (len <0 ) { +dev_err (smgr, +"Failed to get file length of \'%s\' [e:%s]\n" , + image,strerror (errno)); +return -EIO ; + } +if (len ==0 ) { +dev_err (smgr,"Length of file \'%s\' is invalid\n" , image); +return -EINVAL ; + } + smgr->rsu_length = len; +if (smgr->max10_dev->staging_area_size < smgr->rsu_length) { dev_err(dev, "Size of staging area is small than image length " "[%u<%u]\n", smgr->max10_dev->staging_area_size,
Signature · 1 line
2.29.2
Re: [dpdk-dev] [PATCH v1 3/4] raw/ifpga/base: assign unsigned value to length
Quoted 60 lines
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: 2021年3月17日 16:22
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 3/4] raw/ifpga/base: assign unsigned value to lengthIn
fpga_update_flash(), "smgr->rsu_length" is passed to a parameter that cannot be negative. So return value of function "lseek" should be checked before being assigned to "smgr->rsu_length".Coverity issue: 367481
Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")Signed-off-by Wei Huang <[email protected]>--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)drivers/raw/ifpga/base/ifpga_fme_rsu.c +1
-0@@ -277,6 +277,7 @@ int fpga_update_flash(struct ifpga_fme_hw *fme,const char *image, struct ifpga_sec_mgr *smgr = NULL;uint32_t rsu_stat =0 ;int fd =-1 ; +off_t len =0 ;struct sigaction old_sigint_action;struct sigaction sa;drivers/raw/ifpga/base/ifpga_fme_rsu.c +13
-1time_t start;@@ -320,9 +321,21 @@ int fpga_update_flash(struct ifpga_fme_hw *fme,const char *image, image, strerror(errno));return -EIO ; } - smgr->rsu_length =lseek (fd,0 ,SEEK_END ); + len =lseek (fd,0 ,SEEK_END );close (fd); +if (len <0 ) { +dev_err (smgr, +"Failed to get file length of \'%s\' [e:%s]\n" , + image,strerror (errno)); +return -EIO ; + } +if (len ==0 ) { +dev_err (smgr,"Length of file \'%s\' is invalid\n" , image); +return -EINVAL ; + } + smgr->rsu_length = len; +if (smgr->max10_dev->staging_area_size < smgr->rsu_length) { dev_err(dev, "Size of staging area is small than image length "
Quoted 2 lines
"[%u<%u]\n",
smgr->max10_dev->staging_area_size,
--
Re: [dpdk-dev] [PATCH v1 3/4] raw/ifpga/base: assign unsigned value to length
In reply to Wei HuangHi,
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: Wednesday, March 17, 2021 4:22 PM
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 3/4] raw/ifpga/base: assign unsigned value to length
In fpga_update_flash(), "smgr->rsu_length" is passed to a parameter that cannot be negative. So return value of function "lseek" should be checked before being assigned to "smgr->rsu_length".
Coverity issue: 367481
--- drivers/raw/ifpga/base/ifpga_fme_rsu.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_fme_rsu.c +14 -1
@@ -277,6 +277,7 @@ int fpga_update_flash(struct ifpga_fme_hw *fme, const char *image,struct ifpga_sec_mgr *smgr =NULL ;uint32_t rsu_stat =0 ;int fd =-1 ; +off_t len =0 ;struct sigaction old_sigint_action;struct sigaction sa;time_t start;
@@ -320,9 +321,21 @@ int fpga_update_flash(struct ifpga_fme_hw *fme, const char *image, image, strerror(errno));return -EIO ; } - smgr->rsu_length =lseek (fd,0 ,SEEK_END ); + len =lseek (fd,0 ,SEEK_END );close (fd); +if (len <0 ) { +dev_err (smgr, +"Failed to get file length of \'%s\' [e:%s]\n" , + image,strerror (errno)); +return -EIO ; + } +if (len ==0 ) { +dev_err (smgr,"Length of file \'%s\' is invalid\n" , image); +return -EINVAL ; + } + smgr->rsu_length = len; +if (smgr->max10_dev->staging_area_size < smgr->rsu_length) { dev_err(dev, "Size of staging area is small than image length " "[%u<%u]\n", smgr->max10_dev->staging_area_size,
--
2.29.2
PATCH 4/4 raw/ifpga/base: check pointer before dereferencing
In reply to Wei HuangIn init_sec_mgr(), pointer "hw" may be NULL, so "hw" should be checked before dereferencing.
Coverity issue: 367483
--- drivers/raw/ifpga/base/ifpga_sec_mgr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_sec_mgr.c +1 -1
@@ -610,7 +610,7 @@ int init_sec_mgr(struct ifpga_fme_hw *fme) smgr->rsu_status =NULL ; } -if ((hw->pci_data->device_id ==IFPGA_N3000_DID ) && +if (hw && (hw->pci_data->device_id ==IFPGA_N3000_DID ) && (hw->pci_data->vendor_id ==IFPGA_N3000_VID )) { smgr->ops = &n3000_sec_ops; smgr->copy_speed =IFPGA_N3000_COPY_SPEED ;
Signature · 1 line
2.29.2
Re: [dpdk-dev] [PATCH v1 4/4] raw/ifpga/base: check pointer before dereferencing
Quoted 28 lines
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: 2021年3月17日 16:22
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 4/4] raw/ifpga/base: check pointer before dereferencingIn
init_sec_mgr(), pointer "hw" may be NULL, so "hw" should be checked before dereferencing.Coverity issue: 367483
Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")Signed-off-by Wei Huang <[email protected]>--- drivers/raw/ifpga/base/ifpga_sec_mgr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/raw/ifpga/base/ifpga_sec_mgr.c +0
-0
drivers/raw/ifpga/base/ifpga_sec_mgr.c int init_sec_mgr(struct ifpga_fme_hw *fme)
Quoted 9 lines
drivers/raw/ifpga/base/ifpga_sec_mgr.c +1
-2@@ -610,7 +610,7 @@ int init_sec_mgr(struct ifpga_fme_hw *fme) smgr->rsu_status =NULL ; } -if ((hw->pci_data->device_id ==IFPGA_N3000_DID ) && +if (hw && (hw->pci_data->device_id ==IFPGA_N3000_DID ) && (hw->pci_data->vendor_id ==IFPGA_N3000_VID )) { smgr->ops = &n3000_sec_ops; smgr->copy_speed =IFPGA_N3000_COPY_SPEED ; --
Re: [dpdk-dev] [PATCH v1 4/4] raw/ifpga/base: check pointer before dereferencing
In reply to Wei HuangHi,
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: Wednesday, March 17, 2021 4:22 PM
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 4/4] raw/ifpga/base: check pointer before dereferencing
In init_sec_mgr(), pointer "hw" may be NULL, so "hw" should be checked before dereferencing.
Coverity issue: 367483
--- drivers/raw/ifpga/base/ifpga_sec_mgr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/raw/ifpga/base/ifpga_sec_mgr.c +1 -1
@@ -610,7 +610,7 @@ int init_sec_mgr(struct ifpga_fme_hw *fme) smgr->rsu_status =NULL ; } -if ((hw->pci_data->device_id ==IFPGA_N3000_DID ) && +if (hw && (hw->pci_data->device_id ==IFPGA_N3000_DID ) && (hw->pci_data->vendor_id ==IFPGA_N3000_VID )) { smgr->ops = &n3000_sec_ops; smgr->copy_speed =IFPGA_N3000_COPY_SPEED ;
--
2.29.2
Zhang, Qi Z
AppliedQuoted 20 lines
-----Original Message-----
From: Huang, Wei <[email protected]>
Sent: Wednesday, March 17, 2021 4:22 PM
To: [email protected]; Xu, Rosen <[email protected]>; Zhang, Qi Z <[email protected]>
Cc: [email protected]; Zhang, Tianfei <[email protected]>; Huang, Wei <[email protected]>
Subject: [PATCH v1 0/4] Fix coverity issues reported in DPDK-26380Below coverity issues are fixed in this patch set. 367477, 367478, 367481, 367483
Wei Huang (4):
raw/ifpga/base: use trusted buffer to free raw/ifpga/base: check return value of lseek raw/ifpga/base: assign unsigned value to length raw/ifpga/base: check pointer before dereferencing
Quoted 6 lines
drivers/raw/ifpga/base/ifpga_fme_rsu.c | 26 +++++++++++++++++++++++--- drivers/raw/ifpga/base/ifpga_sec_mgr.c | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-)--
2.29.2
Applied to dpdk-next-net-intel.
Thanks
Qi