Skip to contentSkip to conversation

Fix coverity issues reported in DPDK-26380

PATCH Applied Wei Huang 5 participants 15 messages Mar 17, 2021 – Apr 7, 2021

lore
Share & tools
lore

Wei Huang

author
#0 lore raw

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
author
#1 lore raw

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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index 28198abd78..d32f1eccb1 100644
@@ -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
author
#5 lore raw

PATCH 2/4 raw/ifpga/base: check return value of lseek

In reply to Wei Huang

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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index d32f1eccb1..a4cb2f54ba 100644
@@ -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
author
#8 lore raw

PATCH 3/4 raw/ifpga/base: assign unsigned value to length

In reply to Wei Huang

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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index a4cb2f54ba..79ee37c282 100644
@@ -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
author
#11 lore raw

PATCH 4/4 raw/ifpga/base: check pointer before dereferencing

In reply to Wei Huang

In 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")
---
 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
index 4cf1db3049..15fb5b6d4d 100644
@@ -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
Acked-by on v1 1/4
#2 lore raw

Re: [dpdk-dev] [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free

In reply to Wei Huang
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 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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index 28198abd78..d32f1eccb1 100644
@@ -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 -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;
drivers/raw/ifpga/base/ifpga_fme_rsu.c +1 -1
 	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;
Quoted 1 line

}

Quoted 2 lines

--
2.29.2

Acked-by on v1 2/4
#6 lore raw

Re: [dpdk-dev] [PATCH v1 2/4] raw/ifpga/base: check return value of lseek

In reply to Wei Huang
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 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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index d32f1eccb1..a4cb2f54ba 100644
@@ -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",
--

Acked-by on v1 3/4
#9 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] raw/ifpga/base: assign unsigned value to length

In reply to Wei Huang
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 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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index a4cb2f54ba..79ee37c282 100644
@@ -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 -1
 	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 "
Quoted 2 lines

"[%u<%u]\n", smgr->max10_dev->staging_area_size,
--

Acked-by on v1 4/4
#12 lore raw

Re: [dpdk-dev] [PATCH v1 4/4] raw/ifpga/base: check pointer before dereferencing

In reply to Wei Huang
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 dereferencing

In 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")
---
 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
index 4cf1db3049..15fb5b6d4d 100644

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;
--
Acked-by on v1 1/4
#3 lore raw

Re: [dpdk-dev] [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free

In reply to Wei Huang

Hi,

-----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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index 28198abd78..d32f1eccb1 100644
@@ -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

Acked-by on v1 2/4
#7 lore raw

Re: [dpdk-dev] [PATCH v1 2/4] raw/ifpga/base: check return value of lseek

In reply to Wei Huang

Hi,

-----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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index d32f1eccb1..a4cb2f54ba 100644
@@ -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

Acked-by on v1 3/4
#10 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] raw/ifpga/base: assign unsigned value to length

In reply to Wei Huang

Hi,

-----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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index a4cb2f54ba..79ee37c282 100644
@@ -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

Acked-by on v1 4/4
#13 lore raw

Re: [dpdk-dev] [PATCH v1 4/4] raw/ifpga/base: check pointer before dereferencing

In reply to Wei Huang

Hi,

-----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

Fixes 7a4f3993f269 ("raw/ifpga: add FPGA RSU APIs")
---
 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
index 4cf1db3049..15fb5b6d4d 100644
@@ -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

Applied
#14 lore raw
In reply to Wei Huang
Quoted 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-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

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

#4 lore raw

Re: [dpdk-dev] [dpdk-stable] [PATCH v1 1/4] raw/ifpga/base: use trusted buffer to free

In reply to Wei Huang
Quoted 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
---
  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
index 28198abd78..d32f1eccb1 100644
@@ -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;
  }