Skip to contentSkip to conversation

Intel Wired LAN Driver Updates 2023-01-09 (ice)

GIT PULL In review Tony Nguyen 4 participants 8 messages Jan 9, 2023 – Jan 11, 2023

Subsystems (3)
lore
Share & tools
lore

Tony Nguyen

author maintainer
#0 lore raw

This series contains updates to ice driver only.

Jiasheng Jiang frees allocated cmd_buf if write_buf allocation failed to prevent memory leak.

Yuan Can adds check, and proper cleanup, of gnss_tty_port allocation call to avoid memory leaks.

The following are changes since commit 7d6ceeb1875cc08dc3d1e558e191434d94840cd5:
af_unix: selftest: Fix the size of the parameter to connect()
and are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/tnguy/net-queue 100GbE

Jiasheng Jiang (1):
ice: Add check for kzalloc

Yuan Can (1):
ice: Fix potential memory leak in ice_gnss_tty_write()

 drivers/net/ethernet/intel/ice/ice_gnss.c | 24 ++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
Signature · 1 line
2.38.1
author maintainer
#1 lore raw

PATCH 1/2 ice: Fix potential memory leak in ice_gnss_tty_write()

From: Yuan Can <[email protected]>

The ice_gnss_tty_write() return directly if the write_buf alloc failed, leaking the cmd_buf.

Fix by free cmd_buf if write_buf alloc failed.

Fixes d6b98c8d242a ("ice: add write functionality for GNSS TTY")
---
 drivers/net/ethernet/intel/ice/ice_gnss.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -0
index b5a7f246d230..a1915551c69a 100644
@@ -363,6 +363,7 @@ ice_gnss_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
 	/* Send the data out to a hardware port */
 	write_buf = kzalloc(sizeof(*write_buf), GFP_KERNEL);
 	if (!write_buf) {
+		kfree(cmd_buf);
 		err = -ENOMEM;
 		goto exit;
 	}
Signature · 1 line
2.38.1
author maintainer
#2 lore raw

PATCH 2/2 ice: Add check for kzalloc

In reply to Tony Nguyen

From: Jiasheng Jiang <[email protected]>

Add the check for the return value of kzalloc in order to avoid NULL pointer dereference.
Moreover, use the goto-label to share the clean code.

Fixes d6b98c8d242a ("ice: add write functionality for GNSS TTY")
---
 drivers/net/ethernet/intel/ice/ice_gnss.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/net/ethernet/intel/ice/ice_gnss.c +14 -9
index a1915551c69a..43e199b5b513 100644
@@ -461,6 +461,9 @@ static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
 	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
 		pf->gnss_tty_port[i] = kzalloc(sizeof(*pf->gnss_tty_port[i]),
 					       GFP_KERNEL);
+		if (!pf->gnss_tty_port[i])
+			goto err_out;
+
 		pf->gnss_serial[i] = NULL;
 
 		tty_port_init(pf->gnss_tty_port[i]);
@@ -470,21 +473,23 @@ static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
 	err = tty_register_driver(tty_driver);
 	if (err) {
 		dev_err(dev, "Failed to register TTY driver err=%d\n", err);
-
-		for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
-			tty_port_destroy(pf->gnss_tty_port[i]);
-			kfree(pf->gnss_tty_port[i]);
-		}
-		kfree(ttydrv_name);
-		tty_driver_kref_put(pf->ice_gnss_tty_driver);
-
-		return NULL;
+		goto err_out;
 	}
 
 	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++)
 		dev_info(dev, "%s%d registered\n", ttydrv_name, i);
 
 	return tty_driver;
+
+err_out:
+	while (i--) {
+		tty_port_destroy(pf->gnss_tty_port[i]);
+		kfree(pf->gnss_tty_port[i]);
+	}
+	kfree(ttydrv_name);
+	tty_driver_kref_put(pf->ice_gnss_tty_driver);
+
+	return NULL;
 }
 
 /**
Signature · 1 line
2.38.1
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH net 2/2] ice: Add check for kzalloc

Quoted 6 lines · Tony Nguyen

From: Jiasheng Jiang <[email protected]>

Add the check for the return value of kzalloc in order to avoid NULL pointer dereference.
Moreover, use the goto-label to share the clean code.

Quoted 9 lines
Fixes d6b98c8d242a ("ice: add write functionality for GNSS TTY")
---
 drivers/net/ethernet/intel/ice/ice_gnss.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

Thanks,

maintainer
#4 lore raw

Re: [PATCH net 2/2] ice: Add check for kzalloc

In reply to Tony Nguyen
Quoted 4 lines · On Mon, 9 Jan 2023 14:53:58 -0800 Tony Nguyen wrote:
drivers/net/ethernet/intel/ice/ice_gnss.c +0 -0
@@ -470,21 +473,23 @@ static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
 	err = tty_register_driver(tty_driver);
 	if (err) {
 		dev_err(dev, "Failed to register TTY driver err=%d\n", err);

drivers/net/ethernet/intel/ice/ice_gnss.c static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf) · line 476

Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -9
-
-		for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
-			tty_port_destroy(pf->gnss_tty_port[i]);
-			kfree(pf->gnss_tty_port[i]);
-		}
-		kfree(ttydrv_name);
-		tty_driver_kref_put(pf->ice_gnss_tty_driver);
-
-		return NULL;
+		goto err_out;

FTR I think that depending on i == ICE_GNSS_TTY_MINOR_DEVICES here is fragile. I can merge as is, since the code is technically correct, but what you should have done is crate a new label, say err_unreg_all_ports, do:

	goto err_unreg_all_ports;

drivers/net/ethernet/intel/ice/ice_gnss.c static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf) · line 483

Quoted 7 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -0
 	}
 
 	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++)
 		dev_info(dev, "%s%d registered\n", ttydrv_name, i);
 
 	return tty_driver;
+

And here add:

err_unreg_all_ports:

	i = ICE_GNSS_TTY_MINOR_DEVICES;
Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +9 -0
+err_out:
+	while (i--) {
+		tty_port_destroy(pf->gnss_tty_port[i]);
+		kfree(pf->gnss_tty_port[i]);
+	}
+	kfree(ttydrv_name);
+	tty_driver_kref_put(pf->ice_gnss_tty_driver);
+
+	return NULL;
author maintainer
#5 lore raw

Re: [PATCH net 2/2] ice: Add check for kzalloc

Quoted 11 lines · Jakub Kicinski
Quoted 10 lines · On Mon, 9 Jan 2023 14:53:58 -0800 Tony Nguyen wrote:
drivers/net/ethernet/intel/ice/ice_gnss.c +0 -6
@@ -470,21 +473,23 @@ static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
  	err = tty_register_driver(tty_driver);
  	if (err) {
  		dev_err(dev, "Failed to register TTY driver err=%d\n", err);
-
-		for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
-			tty_port_destroy(pf->gnss_tty_port[i]);
-			kfree(pf->gnss_tty_port[i]);
-		}
-		kfree(ttydrv_name);
Quoted 8 lines

drivers/net/ethernet/intel/ice/ice_gnss.c static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf) · line 476

Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -3
-		tty_driver_kref_put(pf->ice_gnss_tty_driver);
-
-		return NULL;
+		goto err_out;

FTR I think that depending on i == ICE_GNSS_TTY_MINOR_DEVICES here is fragile. I can merge as is, since the code is technically correct

Hi Jakub,

Thanks for the suggestion/improvement. This won't be here much longer as we will be converting to use the kernel infrastructure [1] soon, but will keep this in mind for the future.

This was mainly to ensure the existing implementations got corrected/could be backported.

Thanks,
Tony

[1] https://lore.kernel.org/netdev/[email protected]/

maintainer
#6 lore raw

Re: [PATCH net 2/2] ice: Add check for kzalloc

Quoted 8 lines · On Wed, 11 Jan 2023 11:31:55 -0800 Tony Nguyen wrote:

Hi Jakub,

Thanks for the suggestion/improvement. This won't be here much longer as we will be converting to use the kernel infrastructure [1] soon, but will keep this in mind for the future.

This was mainly to ensure the existing implementations got corrected/could be backported.

Ack, to be clear - I did not mean to request a follow up. Only to suggest a better way of dealing with the error path for future code.

#7 lore raw
In reply to Tony Nguyen
This series was applied to netdev/net.git (master) by Tony Nguyen <[email protected]>:

Hello:

This series was applied to netdev/net.git (master) by Tony Nguyen <[email protected]>:

Quoted 9 lines · On Mon, 9 Jan 2023 14:53:56 -0800 you wrote:

This series contains updates to ice driver only.

Jiasheng Jiang frees allocated cmd_buf if write_buf allocation failed to prevent memory leak.

Yuan Can adds check, and proper cleanup, of gnss_tty_port allocation call to avoid memory leaks.

[...]

Here is the summary with links:
- [net,1/2] ice: Fix potential memory leak in ice_gnss_tty_write()

    https://git.kernel.org/netdev/net/c/f58985620f55
  - [net,2/2] ice: Add check for kzalloc
    https://git.kernel.org/netdev/net/c/40543b3d9d2c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html