Skip to contentSkip to conversation

raw/ifpga: fix use of untrusted scalar value

PATCH Superseded Zhang, Tianfei 3 participants 6 messages Jun 4, 2019

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 raw/ifpga: fix use of untrusted scalar value

Add checking the buffer size and use
const char * for buffer declaration.

Coverity issue: 279449
Cc: [email protected]

---
 drivers/raw/ifpga_rawdev/base/ifpga_api.c     |  4 +--
 drivers/raw/ifpga_rawdev/base/ifpga_api.h     |  2 +-
 .../raw/ifpga_rawdev/base/ifpga_feature_dev.h |  2 +-
 drivers/raw/ifpga_rawdev/base/ifpga_fme_pr.c  | 27 +++++++++++--------
 drivers/raw/ifpga_rawdev/base/opae_hw_api.c   |  4 +--
 drivers/raw/ifpga_rawdev/base/opae_hw_api.h   |  4 +--
 drivers/raw/ifpga_rawdev/ifpga_rawdev.c       |  7 ++++-
 7 files changed, 30 insertions(+), 20 deletions(-)
drivers/raw/ifpga_rawdev/base/ifpga_api.c +2 -2
index 3ddbcdc2a..53d101daf 100644
@@ -182,7 +182,7 @@ struct opae_bridge_ops ifpga_br_ops = {
 };
 
 /* Manager APIs */
-static int ifpga_mgr_flash(struct opae_manager *mgr, int id, void *buf,
+static int ifpga_mgr_flash(struct opae_manager *mgr, int id, const char *buf,
 			   u32 size, u64 *status)
 {
 	struct ifpga_fme_hw *fme = mgr->data;
@@ -324,7 +324,7 @@ struct opae_adapter_ops ifpga_adapter_ops = {
  *   - 0: Success, partial reconfiguration finished.
  *   - <0: Error code returned in partial reconfiguration.
  **/
-int ifpga_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
+int ifpga_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer, u32 size,
 	     u64 *status)
 {
 	if (!is_valid_port_id(hw, port_id))
drivers/raw/ifpga_rawdev/base/ifpga_api.h +1 -1
index 4a247698c..051ab8276 100644
@@ -23,7 +23,7 @@ int ifpga_set_irq(struct ifpga_hw *hw, u32 fiu_id, u32 port_id,
 		  u32 feature_id, void *irq_set);
 
 /* FME APIs */
-int ifpga_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
+int ifpga_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer, u32 size,
 	     u64 *status);
 
 #endif /* _IFPGA_API_H_ */
drivers/raw/ifpga_rawdev/base/ifpga_feature_dev.h +1 -1
index bb9fcc289..e243d4273 100644
@@ -149,7 +149,7 @@ static inline int fpga_port_reset(struct ifpga_port_hw *port)
 	return ret;
 }
 
-int do_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
+int do_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer, u32 size,
 	  u64 *status);
 
 int fme_get_prop(struct ifpga_fme_hw *fme, struct feature_prop *prop);
drivers/raw/ifpga_rawdev/base/ifpga_fme_pr.c +16 -11
index efa72660f..9997942d2 100644
@@ -223,8 +223,8 @@ static int fpga_pr_buf_load(struct ifpga_fme_hw *fme_dev,
 	return 0;
 }
 
-static int fme_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
-		  u64 *status)
+static int fme_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer,
+		u32 size, u64 *status)
 {
 	struct feature_fme_header *fme_hdr;
 	struct feature_fme_capability fme_capability;
@@ -269,7 +269,7 @@ static int fme_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
 	/* Disable Port before PR */
 	fpga_port_disable(port);
 
-	ret = fpga_pr_buf_load(fme, &info, (void *)buffer, size);
+	ret = fpga_pr_buf_load(fme, &info, buffer, size);
 
 	*status = info.pr_err;
 
@@ -280,27 +280,32 @@ static int fme_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
 	return ret;
 }
 
-int do_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size, u64 *status)
+int do_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer,
+		u32 size, u64 *status)
 {
-	struct bts_header *bts_hdr;
-	void *buf;
+	const struct bts_header *bts_hdr;
+	const char *buf;
 	struct ifpga_port_hw *port;
 	int ret;
+	u32 header_size;
 
 	if (!buffer || size == 0) {
 		dev_err(hw, "invalid parameter\n");
 		return -EINVAL;
 	}
 
-	bts_hdr = (struct bts_header *)buffer;
+	bts_hdr = (const struct bts_header *)buffer;
 
 	if (is_valid_bts(bts_hdr)) {
 		dev_info(hw, "this is a valid bitsteam..\n");
-		size -= (sizeof(struct bts_header) +
-				     bts_hdr->metadata_len);
-		buf = (u8 *)buffer + sizeof(struct bts_header) +
-			       bts_hdr->metadata_len;
+		header_size = sizeof(struct bts_header) +
+			bts_hdr->metadata_len;
+		if (size < header_size)
+			return -EINVAL;
+		size -= header_size;
+		buf = buffer + header_size;
 	} else {
+		dev_err(hw, "this is an invalid bitstream..\n");
 		return -EINVAL;
 	}
 
drivers/raw/ifpga_rawdev/base/opae_hw_api.c +2 -2
index 0e117d05e..8964e7984 100644
@@ -244,8 +244,8 @@ opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
  *
  * Return: 0 on success, otherwise error code.
  */
-int opae_manager_flash(struct opae_manager *mgr, int id, void *buf, u32 size,
-		       u64 *status)
+int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
+		u32 size, u64 *status)
 {
 	if (!mgr)
 		return -EINVAL;
drivers/raw/ifpga_rawdev/base/opae_hw_api.h +2 -2
index 383e751cb..63405a471 100644
@@ -44,7 +44,7 @@ struct opae_manager {
 
 /* FIXME: add more management ops, e.g power/thermal and etc */
 struct opae_manager_ops {
-	int (*flash)(struct opae_manager *mgr, int id, void *buffer,
+	int (*flash)(struct opae_manager *mgr, int id, const char *buffer,
 		     u32 size, u64 *status);
 	int (*get_eth_group_region_info)(struct opae_manager *mgr,
 			struct opae_eth_group_region_info *info);
@@ -74,7 +74,7 @@ struct opae_manager *
 opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
 		struct opae_manager_networking_ops *network_ops, void *data);
 #define opae_manager_free(mgr) opae_free(mgr)
-int opae_manager_flash(struct opae_manager *mgr, int acc_id, void *buf,
+int opae_manager_flash(struct opae_manager *mgr, int acc_id, const char *buf,
 		       u32 size, u64 *status);
 int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
 		u8 group_id, struct opae_eth_group_region_info *info);
drivers/raw/ifpga_rawdev/ifpga_rawdev.c +6 -1
index 41be1a205..01aa917de 100644
@@ -225,7 +225,7 @@ ifpga_rawdev_reset(struct rte_rawdev *dev)
 }
 
 static int
-fpga_pr(struct rte_rawdev *raw_dev, u32 port_id, u64 *buffer, u32 size,
+fpga_pr(struct rte_rawdev *raw_dev, u32 port_id, const char *buffer, u32 size,
 			u64 *status)
 {
 
@@ -296,6 +296,11 @@ rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 		goto close_fd;
 	}
 	buffer_size = file_stat.st_size;
+	if (buffer_size <= 0) {
+		ret = -EINVAL;
+		goto close_fd;
+	}
+
 	IFPGA_RAWDEV_PMD_INFO("bitstream file size: %zu\n", buffer_size);
 	buffer = rte_malloc(NULL, buffer_size, 0);
 	if (!buffer) {
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 raw/ifpga: fix logically dead code

add temporary variable in max10_reg_write().

Coverity issue: 337927
Cc: [email protected]

---
 drivers/raw/ifpga_rawdev/base/opae_intel_max10.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/raw/ifpga_rawdev/base/opae_intel_max10.c +4 -2
index f354ee4b6..3ff6575d7 100644
@@ -17,11 +17,13 @@ int max10_reg_read(unsigned int reg, unsigned int *val)
 
 int max10_reg_write(unsigned int reg, unsigned int val)
 {
+	unsigned int tmp = val;
+
 	if (!g_max10)
 		return -ENODEV;
 
 	return spi_transaction_write(g_max10->spi_tran_dev,
-			reg, 4, (unsigned char *)&val);
+			reg, 4, (unsigned char *)&tmp);
 }
 
 struct intel_max10_device *
@@ -57,7 +59,7 @@ intel_max10_device_probe(struct altera_spi_device *spi,
 
 	/* set PKVL Polling manually in BBS */
 	ret = max10_reg_write(PKVL_POLLING_CTRL, 0x3);
-	if (ret) {
+	if (ret != 0) {
 		dev_err(dev, "%s set PKVL polling fail\n", __func__);
 		goto spi_tran_fail;
 	}
Signature · 1 line
2.17.1
#2 lore raw

Re: [dpdk-dev] [PATCH v1 2/2] raw/ifpga: fix logically dead code

Quoted 4 lines · Tianfei

add temporary variable in max10_reg_write().

Coverity issue: 337927
Cc: [email protected]

Missing a Fixes line?

Quoted 37 lines

---
drivers/raw/ifpga_rawdev/base/opae_intel_max10.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
drivers/raw/ifpga_rawdev/base/opae_intel_max10.c +3 -1
index f354ee4b6..3ff6575d7 100644
@@ -17,11 +17,13 @@ int max10_reg_read(unsigned int reg, unsigned int *val)
 
 int max10_reg_write(unsigned int reg, unsigned int val)
 {
+	unsigned int tmp = val;
+
 	if (!g_max10)
 		return -ENODEV;
 
 	return spi_transaction_write(g_max10->spi_tran_dev,
-			reg, 4, (unsigned char *)&val);
+			reg, 4, (unsigned char *)&tmp);
 }
 

struct intel_max10_device *

drivers/raw/ifpga_rawdev/base/opae_intel_max10.c +1 -1
@@ -57,7 +59,7 @@ intel_max10_device_probe(struct altera_spi_device *spi,
 
 	/* set PKVL Polling manually in BBS */
 	ret = max10_reg_write(PKVL_POLLING_CTRL, 0x3);
-	if (ret) {
+	if (ret != 0) {
 		dev_err(dev, "%s set PKVL polling fail\n", __func__);
 		goto spi_tran_fail;
 	}
Signature · 1 line
2.17.1
#3 lore raw

Re: [dpdk-dev] [dpdk-stable] [PATCH v1 2/2] raw/ifpga: fix logically dead code

Quoted 11 lines

-----Original Message-----
From: stable [mailto:[email protected]] On Behalf Of Ye Xiaolong
Sent: Tuesday, June 4, 2019 8:27 AM
To: Zhang, Tianfei <[email protected]>
Cc: [email protected]; Xu, Rosen <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-stable] [dpdk-dev] [PATCH v1 2/2] raw/ifpga: fix logically dead code

Quoted 8 lines
Quoted 4 lines · Tianfei

add temporary variable in max10_reg_write().

Coverity issue: 337927
Cc: [email protected]

Missing a Fixes line?

In this case it would be:

Fixes 96ebfcf8125c ("raw/ifpga/base: add SPI and MAX10 device driver")

Coverity issue: 337927

John

#4 lore raw
In reply to Zhang, Tianfei
Quoted 5 lines · Tianfei

Add checking the buffer size and use
const char * for buffer declaration.

Coverity issue: 279449
Cc: [email protected]

Should also add a Fixes line.

Thanks,
Xiaolong

Quoted 190 lines

---

drivers/raw/ifpga_rawdev/base/ifpga_api.c     |  4 +--
drivers/raw/ifpga_rawdev/base/ifpga_api.h     |  2 +-
.../raw/ifpga_rawdev/base/ifpga_feature_dev.h |  2 +-
drivers/raw/ifpga_rawdev/base/ifpga_fme_pr.c  | 27 +++++++++++--------
drivers/raw/ifpga_rawdev/base/opae_hw_api.c   |  4 +--
drivers/raw/ifpga_rawdev/base/opae_hw_api.h   |  4 +--
drivers/raw/ifpga_rawdev/ifpga_rawdev.c       |  7 ++++-
7 files changed, 30 insertions(+), 20 deletions(-)
drivers/raw/ifpga_rawdev/base/ifpga_api.c +2 -2
index 3ddbcdc2a..53d101daf 100644
@@ -182,7 +182,7 @@ struct opae_bridge_ops ifpga_br_ops = {
 };
 
 /* Manager APIs */
-static int ifpga_mgr_flash(struct opae_manager *mgr, int id, void *buf,
+static int ifpga_mgr_flash(struct opae_manager *mgr, int id, const char *buf,
 			   u32 size, u64 *status)
 {
 	struct ifpga_fme_hw *fme = mgr->data;
@@ -324,7 +324,7 @@ struct opae_adapter_ops ifpga_adapter_ops = {
 *   - 0: Success, partial reconfiguration finished.
 *   - <0: Error code returned in partial reconfiguration.
 **/
-int ifpga_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
+int ifpga_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer, u32 size,
 	     u64 *status)
 {
 	if (!is_valid_port_id(hw, port_id))
drivers/raw/ifpga_rawdev/base/ifpga_api.h +1 -1
index 4a247698c..051ab8276 100644
@@ -23,7 +23,7 @@ int ifpga_set_irq(struct ifpga_hw *hw, u32 fiu_id, u32 port_id,
 		  u32 feature_id, void *irq_set);
 
 /* FME APIs */
-int ifpga_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
+int ifpga_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer, u32 size,
 	     u64 *status);
 
 #endif /* _IFPGA_API_H_ */
drivers/raw/ifpga_rawdev/base/ifpga_feature_dev.h +1 -1
index bb9fcc289..e243d4273 100644
@@ -149,7 +149,7 @@ static inline int fpga_port_reset(struct ifpga_port_hw *port)
 	return ret;
 }
 
-int do_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
+int do_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer, u32 size,
 	  u64 *status);
 
 int fme_get_prop(struct ifpga_fme_hw *fme, struct feature_prop *prop);
drivers/raw/ifpga_rawdev/base/ifpga_fme_pr.c +16 -11
index efa72660f..9997942d2 100644
@@ -223,8 +223,8 @@ static int fpga_pr_buf_load(struct ifpga_fme_hw *fme_dev,
 	return 0;
 }
 
-static int fme_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
-		  u64 *status)
+static int fme_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer,
+		u32 size, u64 *status)
 {
 	struct feature_fme_header *fme_hdr;
 	struct feature_fme_capability fme_capability;
@@ -269,7 +269,7 @@ static int fme_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
 	/* Disable Port before PR */
 	fpga_port_disable(port);
 
-	ret = fpga_pr_buf_load(fme, &info, (void *)buffer, size);
+	ret = fpga_pr_buf_load(fme, &info, buffer, size);
 
 	*status = info.pr_err;
 
@@ -280,27 +280,32 @@ static int fme_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size,
 	return ret;
 }
 
-int do_pr(struct ifpga_hw *hw, u32 port_id, void *buffer, u32 size, u64 *status)
+int do_pr(struct ifpga_hw *hw, u32 port_id, const char *buffer,
+		u32 size, u64 *status)
 {
-	struct bts_header *bts_hdr;
-	void *buf;
+	const struct bts_header *bts_hdr;
+	const char *buf;
 	struct ifpga_port_hw *port;
 	int ret;
+	u32 header_size;
 
 	if (!buffer || size == 0) {
 		dev_err(hw, "invalid parameter\n");
 		return -EINVAL;
 	}
 
-	bts_hdr = (struct bts_header *)buffer;
+	bts_hdr = (const struct bts_header *)buffer;
 
 	if (is_valid_bts(bts_hdr)) {
 		dev_info(hw, "this is a valid bitsteam..\n");
-		size -= (sizeof(struct bts_header) +
-				     bts_hdr->metadata_len);
-		buf = (u8 *)buffer + sizeof(struct bts_header) +
-			       bts_hdr->metadata_len;
+		header_size = sizeof(struct bts_header) +
+			bts_hdr->metadata_len;
+		if (size < header_size)
+			return -EINVAL;
+		size -= header_size;
+		buf = buffer + header_size;
 	} else {
+		dev_err(hw, "this is an invalid bitstream..\n");
 		return -EINVAL;
 	}
 
drivers/raw/ifpga_rawdev/base/opae_hw_api.c +2 -2
index 0e117d05e..8964e7984 100644
@@ -244,8 +244,8 @@ opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
 *
 * Return: 0 on success, otherwise error code.
 */
-int opae_manager_flash(struct opae_manager *mgr, int id, void *buf, u32 size,
-		       u64 *status)
+int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
+		u32 size, u64 *status)
 {
 	if (!mgr)
 		return -EINVAL;
drivers/raw/ifpga_rawdev/base/opae_hw_api.h +2 -2
index 383e751cb..63405a471 100644
@@ -44,7 +44,7 @@ struct opae_manager {
 
 /* FIXME: add more management ops, e.g power/thermal and etc */
 struct opae_manager_ops {
-	int (*flash)(struct opae_manager *mgr, int id, void *buffer,
+	int (*flash)(struct opae_manager *mgr, int id, const char *buffer,
 		     u32 size, u64 *status);
 	int (*get_eth_group_region_info)(struct opae_manager *mgr,
 			struct opae_eth_group_region_info *info);
@@ -74,7 +74,7 @@ struct opae_manager *
 opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
 		struct opae_manager_networking_ops *network_ops, void *data);
 #define opae_manager_free(mgr) opae_free(mgr)
-int opae_manager_flash(struct opae_manager *mgr, int acc_id, void *buf,
+int opae_manager_flash(struct opae_manager *mgr, int acc_id, const char *buf,
 		       u32 size, u64 *status);
 int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
 		u8 group_id, struct opae_eth_group_region_info *info);
drivers/raw/ifpga_rawdev/ifpga_rawdev.c +0 -0
index 41be1a205..01aa917de 100644
@@ -225,7 +225,7 @@ ifpga_rawdev_reset(struct rte_rawdev *dev)
 }
 

static int

drivers/raw/ifpga_rawdev/ifpga_rawdev.c +1 -1
-fpga_pr(struct rte_rawdev *raw_dev, u32 port_id, u64 *buffer, u32 size,
+fpga_pr(struct rte_rawdev *raw_dev, u32 port_id, const char *buffer, u32 size,
			u64 *status)

{

drivers/raw/ifpga_rawdev/ifpga_rawdev.c +5 -0
@@ -296,6 +296,11 @@ rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 		goto close_fd;
 	}
 	buffer_size = file_stat.st_size;
+	if (buffer_size <= 0) {
+		ret = -EINVAL;
+		goto close_fd;
+	}
+
 	IFPGA_RAWDEV_PMD_INFO("bitstream file size: %zu\n", buffer_size);
 	buffer = rte_malloc(NULL, buffer_size, 0);
 	if (!buffer) {
Signature · 1 line
2.17.1
#5 lore raw
Quoted 10 lines

-----Original Message-----
From: Ye, Xiaolong
Sent: Tuesday, June 4, 2019 3:30 PM
To: Zhang, Tianfei <[email protected]>
Cc: [email protected]; Xu, Rosen <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH v1 1/2] raw/ifpga: fix use of untrusted scalar value

Quoted 8 lines
Quoted 5 lines · Tianfei

Add checking the buffer size and use
const char * for buffer declaration.

Coverity issue: 279449
Cc: [email protected]

Should also add a Fixes line.

Ok, thanks point it, I will add Fixes and resend it.