2019-03-18 22:45:51

by Aditya Pakki

[permalink] [raw]
Subject: [PATCH] thunderbolt: Fix to check for kmemdup failure

Memory allocated via kmemdup might fail and return a NULL pointer.
This patch adds a check on the return value of kmemdup and passes the
error upstream.

Signed-off-by: Aditya Pakki <[email protected]>
---
drivers/thunderbolt/switch.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index cd96994dc094..c05d77073ee7 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -1294,13 +1294,13 @@ int tb_switch_configure(struct tb_switch *sw)
return tb_plug_events_active(sw, true);
}

-static void tb_switch_set_uuid(struct tb_switch *sw)
+static int tb_switch_set_uuid(struct tb_switch *sw)
{
u32 uuid[4];
int cap;

if (sw->uuid)
- return;
+ return 0;

/*
* The newer controllers include fused UUID as part of link
@@ -1323,6 +1323,9 @@ static void tb_switch_set_uuid(struct tb_switch *sw)
}

sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
+ if (!sw->uuid)
+ return -ENOMEM;
+ return 0;
}

static int tb_switch_add_dma_port(struct tb_switch *sw)
@@ -1372,7 +1375,9 @@ static int tb_switch_add_dma_port(struct tb_switch *sw)

if (status) {
tb_sw_info(sw, "switch flash authentication failed\n");
- tb_switch_set_uuid(sw);
+ ret = tb_switch_set_uuid(sw);
+ if (ret)
+ return ret;
nvm_set_auth_status(sw, status);
}

@@ -1422,7 +1427,9 @@ int tb_switch_add(struct tb_switch *sw)
}
tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);

- tb_switch_set_uuid(sw);
+ ret = tb_switch_set_uuid(sw);
+ if (ret)
+ return ret;

for (i = 0; i <= sw->config.max_port_number; i++) {
if (sw->ports[i].disabled) {
--
2.17.1



2019-03-20 10:47:49

by Mika Westerberg

[permalink] [raw]
Subject: Re: [PATCH] thunderbolt: Fix to check for kmemdup failure

On Mon, Mar 18, 2019 at 05:44:17PM -0500, Aditya Pakki wrote:
> Memory allocated via kmemdup might fail and return a NULL pointer.
> This patch adds a check on the return value of kmemdup and passes the
> error upstream.
>
> Signed-off-by: Aditya Pakki <[email protected]>

Applied, thanks!

2019-03-20 12:10:59

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH] thunderbolt: Fix to check for kmemdup failure


On 3/19/2019 4:14 AM, Aditya Pakki wrote:
> Memory allocated via kmemdup might fail and return a NULL pointer.
> This patch adds a check on the return value of kmemdup and passes the
> error upstream.
>
> Signed-off-by: Aditya Pakki <[email protected]>
> ---
> drivers/thunderbolt/switch.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
> index cd96994dc094..c05d77073ee7 100644
> --- a/drivers/thunderbolt/switch.c
> +++ b/drivers/thunderbolt/switch.c
> @@ -1294,13 +1294,13 @@ int tb_switch_configure(struct tb_switch *sw)
> return tb_plug_events_active(sw, true);
> }
>
> -static void tb_switch_set_uuid(struct tb_switch *sw)
> +static int tb_switch_set_uuid(struct tb_switch *sw)
> {
> u32 uuid[4];
> int cap;
>
> if (sw->uuid)
> - return;
> + return 0;
>
> /*
> * The newer controllers include fused UUID as part of link
> @@ -1323,6 +1323,9 @@ static void tb_switch_set_uuid(struct tb_switch *sw)
> }
>
> sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
> + if (!sw->uuid)
> + return -ENOMEM;
> + return 0;
> }
>

Change looks good to me.

Reviewed-by: Mukesh Ojha <[email protected]>

but, we should add check for `tb_sw_read  ` this as well.


Cheers,
Mukesh


> static int tb_switch_add_dma_port(struct tb_switch *sw)
> @@ -1372,7 +1375,9 @@ static int tb_switch_add_dma_port(struct tb_switch *sw)
>
> if (status) {
> tb_sw_info(sw, "switch flash authentication failed\n");
> - tb_switch_set_uuid(sw);
> + ret = tb_switch_set_uuid(sw);
> + if (ret)
> + return ret;
> nvm_set_auth_status(sw, status);
>
>
> @@ -1Reviewed-by: Andy Shevchenko<[email protected]>422,7 +1427,9 @@ int tb_switch_add(struct tb_switch *sw)
> }
> tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
>
> - tb_switch_set_uuid(sw);
> + ret = tb_switch_set_uuid(sw);
> + if (ret)
> + return ret;
>
> for (i = 0; i <= sw->config.max_port_number; i++) {
> if (sw->ports[i].disabled) {