Skip to contentSkip to conversation

thunderbolt: Fix to check the return value of kmemdup

PATCH Superseded Aditya Pakki 2 participants 2 messages Mar 23, 2019 – Mar 25, 2019 Thunderbolt driver

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v1: Change error handling from 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..be1174d86cef 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) {
+		WARN_ONCE(1, "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
#1 lore raw
Quoted 16 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.

---
v1: Change error handling from WARN_ONCE to return after clean up.

---
 drivers/thunderbolt/icm.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/thunderbolt/icm.c +0 -0
index e3fc920af682..be1174d86cef 100644

drivers/thunderbolt/icm.c static void add_switch(struct tb_switch *parent_sw, u64 route, · line 477

Quoted 5 lines
drivers/thunderbolt/icm.c +2 -0
@@ -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) {
+		WARN_ONCE(1, "cannot allocate memory for switch\n");

Please use tb_sw_WARN() or tb_sw_warn() here instead.

Quoted 8 lines
drivers/thunderbolt/icm.c +3 -0
+		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