From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: Jiasheng Jiang <jiasheng@iscas.ac.cn>
Cc: andreas.noever@gmail.com, michael.jamet@intel.com,
YehezkelShB@gmail.com, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] thunderbolt: Check for null pointer after calling kmemdup
Date: Wed, 5 Jan 2022 09:30:47 +0200 [thread overview]
Message-ID: <YdVJJ3PLZuSfrHzU@lahna> (raw)
In-Reply-To: <20220105064244.2316847-1-jiasheng@iscas.ac.cn>
Hi,
On Wed, Jan 05, 2022 at 02:42:44PM +0800, Jiasheng Jiang wrote:
> As the possible failure of the allocation, kmemdup() may return NULL
> pointer.
> Like alloc_switch(), it might be better to check it.
> Therefore, icm_icl_set_uuid() and icm_handle_event() should also check
> the return value of kmemdup().
> As for icm_icl_set_uuid(), which is assigned to icm->set_uuid, the
> return value of icm->set_uuid needs to check.
> And for icm_handle_event(), just free 'n' and directly return is enough,
> same as the way to handle the failure of kmalloc().
This is doing two things so I suggest sending two patches instead.
However, for the UUID part, I think it works fine if we get NULL (and I
think kmemdup() issues warning too).
> Fixes: 3cdb9446a117 ("thunderbolt: Add support for Intel Ice Lake")
> Fixes: f67cf491175a ("thunderbolt: Add support for Internal Connection Manager (ICM)")
There are probably not needed either since the "fix" here is for pretty
rare case of running out of memory. I think there is not even a NULL
pointer dereference because UUID is optional.
> Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
> ---
> drivers/thunderbolt/icm.c | 20 ++++++++++++++++----
> 1 file changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
> index 2f30b816705a..09ab31ea9128 100644
> --- a/drivers/thunderbolt/icm.c
> +++ b/drivers/thunderbolt/icm.c
> @@ -109,7 +109,7 @@ struct icm {
> int (*driver_ready)(struct tb *tb,
> enum tb_security_level *security_level,
> u8 *proto_version, size_t *nboot_acl, bool *rpm);
> - void (*set_uuid)(struct tb *tb);
> + int (*set_uuid)(struct tb *tb);
> void (*device_connected)(struct tb *tb,
> const struct icm_pkg_header *hdr);
> void (*device_disconnected)(struct tb *tb,
> @@ -1643,7 +1643,7 @@ icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
> return 0;
> }
>
> -static void icm_icl_set_uuid(struct tb *tb)
> +static int icm_icl_set_uuid(struct tb *tb)
> {
> struct tb_nhi *nhi = tb->nhi;
> u32 uuid[4];
> @@ -1654,6 +1654,10 @@ static void icm_icl_set_uuid(struct tb *tb)
> uuid[3] = 0xffffffff;
>
> tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
> + if (!tb->root_switch->uuid)
> + return -ENOMEM;
> +
> + return 0;
> }
>
> static void
> @@ -1739,6 +1743,11 @@ static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
>
> INIT_WORK(&n->work, icm_handle_notification);
> n->pkg = kmemdup(buf, size, GFP_KERNEL);
> + if (!n->pkg) {
> + kfree(n);
> + return;
> + }
> +
> n->tb = tb;
>
> queue_work(tb->wq, &n->work);
> @@ -2152,8 +2161,11 @@ static int icm_start(struct tb *tb)
> tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
> tb->root_switch->rpm = icm->rpm;
>
> - if (icm->set_uuid)
> - icm->set_uuid(tb);
> + if (icm->set_uuid) {
> + ret = icm->set_uuid(tb);
> + if (ret)
> + return ret;
> + }
>
> ret = tb_switch_add(tb->root_switch);
> if (ret) {
> --
> 2.25.1
prev parent reply other threads:[~2022-01-05 7:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-05 6:42 [PATCH] thunderbolt: Check for null pointer after calling kmemdup Jiasheng Jiang
2022-01-05 7:30 ` Mika Westerberg [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YdVJJ3PLZuSfrHzU@lahna \
--to=mika.westerberg@linux.intel.com \
--cc=YehezkelShB@gmail.com \
--cc=andreas.noever@gmail.com \
--cc=jiasheng@iscas.ac.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=michael.jamet@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox