Skip to contentSkip to conversation

raw/ifpga: Fix coverity defects

PATCH Superseded Wei Huang 1 participant 3 messages Oct 29, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Huang

author
#0 lore raw

cover raw/ifpga: Fix coverity defects

From: "Wei Huang" <[email protected]>

These two patches fix defects found by coverity scan.

Wei Huang (2):
raw/ifpga: terminate string filled by readlink with null raw/ifpga: use trusted buffer to free

 drivers/raw/ifpga/ifpga_rawdev.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.3
author
#1 lore raw

PATCH 1/2 raw/ifpga: terminate string filled by readlink with null

readlink() does not terminate string, add a null character at the end of the string if readlink() succeeds.

Fixes 9c006c45d0c5 ("raw/ifpga: scan PCIe BDF device tree")
---
 drivers/raw/ifpga/ifpga_rawdev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/raw/ifpga/ifpga_rawdev.c +2 -1
index 0385514..bf0613e 100644
@@ -230,8 +230,9 @@ static int ifpga_rawdev_fill_info(struct ifpga_rawdev *ifpga_dev,
 	memset(link, 0, sizeof(link));
 	memset(link1, 0, sizeof(link1));
 	ret = readlink(path, link, (sizeof(link)-1));
-	if (ret == -1)
+	if ((ret < 0) || ((unsigned)ret > (sizeof(link)-1)))
 		return -1;
+	link[ret] = 0;   /* terminate string with null character */
 	strlcpy(link1, link, sizeof(link1));
 	memset(ifpga_dev->parent_bdf, 0, 16);
 	point = strlen(link);
Signature · 1 line
2.7.3
author
#2 lore raw

PATCH 2/2 raw/ifpga: use trusted buffer to free

In reply to Wei Huang

In rte_fpga_do_pr, calling function read() may taints argument buffer which turn to an untrusted value as argumen of rte_free().

---
 drivers/raw/ifpga/ifpga_rawdev.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +4 -3
index bf0613e..357f07b 100644
@@ -786,7 +786,7 @@ rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 	int file_fd;
 	int ret = 0;
 	ssize_t buffer_size;
-	void *buffer;
+	void *buffer, *buf_to_free;
 	u64 pr_error;
 
 	if (!file_name)
@@ -818,6 +818,7 @@ rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 		ret = -ENOMEM;
 		goto close_fd;
 	}
+	buf_to_free = buffer;
 
 	/*read the raw data*/
 	if (buffer_size != read(file_fd, (void *)buffer, buffer_size)) {
@@ -835,8 +836,8 @@ rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 	}
 
 free_buffer:
-	if (buffer)
-		rte_free(buffer);
+	if (buf_to_free)
+		rte_free(buf_to_free);
 close_fd:
 	close(file_fd);
 	file_fd = 0;
Signature · 1 line
2.7.3