Skip to contentSkip to conversation

thunderbolt: Fix to check the return value of kmemdup

PATCH Applied Aditya Pakki 4 participants 7 messages Mar 25, 2019 – Oct 11, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 thunderbolt: Fix to check the return value of kmemdup

uuid in add_switch is allocted via kmemdup which can fail. The patch logs the error and cleans up the allocated memory for switch.

---
v2: replace WARN_ONCE with tb_sw_warn, as suggested by Mika
v1: Change warn_once to return after clean up.

---
 drivers/thunderbolt/icm.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/thunderbolt/icm.c +5 -0
index e3fc920af682..8b7f9131e9d1 100644
@@ -473,6 +473,11 @@ static void add_switch(struct tb_switch *parent_sw, u64 route,
 		goto out;
 
 	sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
+	if (!sw->uuid) {
+		tb_sw_warn(sw, "cannot allocate memory for switch\n");
+		tb_switch_put(sw);
+		goto out;
+	}
 	sw->connection_id = connection_id;
 	sw->connection_key = connection_key;
 	sw->link = link;
Signature · 1 line
2.17.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 4 lines · Aditya Pakki

uuid in add_switch is allocted via kmemdup which can fail. The patch logs the error and cleans up the allocated memory for switch.

-Mukesh

Quoted 24 lines

---
v2: replace WARN_ONCE with tb_sw_warn, as suggested by Mika
v1: Change warn_once to return after clean up.

---
  drivers/thunderbolt/icm.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/thunderbolt/icm.c +5 -0
index e3fc920af682..8b7f9131e9d1 100644
@@ -473,6 +473,11 @@ static void add_switch(struct tb_switch *parent_sw, u64 route,
  		goto out;
 
  	sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
+	if (!sw->uuid) {
+		tb_sw_warn(sw, "cannot allocate memory for switch\n");
+		tb_switch_put(sw);
+		goto out;
+	}
  	sw->connection_id = connection_id;
  	sw->connection_key = connection_key;
  	sw->link = link;
#3 lore raw
In reply to Aditya Pakki
Quoted 1 line

uuid in add_switch is allocted via kmemdup which can fail.

I have tried another script for the semantic patch language out. This source code analysis approach points out that the implementation of the function “icm_handle_event” contains still an unchecked call of the function “kmemdup”.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/thunderbolt/icm.c?id=3cdb9446a117d5d63af823bde6fe6babc312e77b#n1627
https://elixir.bootlin.com/linux/v5.4-rc2/source/drivers/thunderbolt/icm.c#L1627

How do you think about to improve it?

Regards,
Markus

#4 lore raw
Quoted 8 lines · Markus Elfring
Quoted 1 line

uuid in add_switch is allocted via kmemdup which can fail.

I have tried another script for the semantic patch language out. This source code analysis approach points out that the implementation of the function “icm_handle_event” contains still an unchecked call of the function “kmemdup”.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/thunderbolt/icm.c?id<db9446a117d5d63af823bde6fe6babc312e77b#n1627
https://elixir.bootlin.com/linux/v5.4-rc2/source/drivers/thunderbolt/icm.c#L1627

Right it misses that.

Quoted 1 line

How do you think about to improve it?

Feel free to send a patch fixing it ;-) Or I can do that myself.

#5 lore raw
Quoted 7 lines
Quoted 5 lines

This source code analysis approach points out that the implementation of the function “icm_handle_event” contains still an unchecked call of the function “kmemdup”.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/thunderbolt/icm.c?id=3cdb9446a117d5d63af823bde6fe6babc312e77b#n1627
https://elixir.bootlin.com/linux/v5.4-rc2/source/drivers/thunderbolt/icm.c#L1627

Right it misses that.

Thanks for your quick feedback.

Quoted 1 line

Feel free to send a patch fixing it ;-) Or I can do that myself.

Would you like to reconsider also the addition of the function call “tb_sw_warn(sw, "cannot allocate memory for switch\n")”?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=9e208aa06c2109b45eec6be049a8e47034748c20#n878

Regards,
Markus

#6 lore raw
Quoted 2 lines · Markus Elfring

Would you like to reconsider also the addition of the function call “tb_sw_warn(sw, "cannot allocate memory for switch\n")”?

For that I already have a patch as part of my USB4 support v2 series.