linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rafael@kernel.org>
To: Ziyang Xuan <william.xuanziyang@huawei.com>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	"Zhang, Rui" <rui.zhang@intel.com>,
	Linux PM <linux-pm@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] thermal/core: fix a UAF bug in __thermal_cooling_device_register()
Date: Thu, 14 Oct 2021 13:08:26 +0200	[thread overview]
Message-ID: <CAJZ5v0iiSWUN7OCD2CdkHpWOe8WvBAZ+Z+3bFeacnQ8+tsHjtA@mail.gmail.com> (raw)
In-Reply-To: <20211014084700.202420-1-william.xuanziyang@huawei.com>

On Thu, Oct 14, 2021 at 10:47 AM Ziyang Xuan
<william.xuanziyang@huawei.com> wrote:
>
> When device_register() return failed, program will goto out_kfree_type
> to release 'cdev->device' by put_device(). That will call thermal_release()
> to free 'cdev'. But the follow-up processes access 'cdev' continually.
> That trggers the UAF bug.
>
> ====================================================================
> BUG: KASAN: use-after-free in __thermal_cooling_device_register+0x75b/0xa90
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
> Call Trace:
>  dump_stack_lvl+0xe2/0x152
>  print_address_description.constprop.0+0x21/0x140
>  ? __thermal_cooling_device_register+0x75b/0xa90
>  kasan_report.cold+0x7f/0x11b
>  ? __thermal_cooling_device_register+0x75b/0xa90
>  __thermal_cooling_device_register+0x75b/0xa90
>  ? memset+0x20/0x40
>  ? __sanitizer_cov_trace_pc+0x1d/0x50
>  ? __devres_alloc_node+0x130/0x180
>  devm_thermal_of_cooling_device_register+0x67/0xf0
>  max6650_probe.cold+0x557/0x6aa
> ......
>
> Freed by task 258:
>  kasan_save_stack+0x1b/0x40
>  kasan_set_track+0x1c/0x30
>  kasan_set_free_info+0x20/0x30
>  __kasan_slab_free+0x109/0x140
>  kfree+0x117/0x4c0
>  thermal_release+0xa0/0x110
>  device_release+0xa7/0x240
>  kobject_put+0x1ce/0x540
>  put_device+0x20/0x30
>  __thermal_cooling_device_register+0x731/0xa90
>  devm_thermal_of_cooling_device_register+0x67/0xf0
>  max6650_probe.cold+0x557/0x6aa [max6650]
>
> Do not use 'cdev' again after put_device() to fix the problem like doing
> in thermal_zone_device_register().
>
> Fixes: 584837618100 ("thermal/drivers/core: Use a char pointer for the cooling device name")
> Signed-off-by: Ziyang Xuan <william.xuanziyang@huawei.com>
> ---
>  drivers/thermal/thermal_core.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 97ef9b040b84..ee7453944ccb 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -888,7 +888,7 @@ __thermal_cooling_device_register(struct device_node *np,
>  {
>         struct thermal_cooling_device *cdev;
>         struct thermal_zone_device *pos = NULL;
> -       int ret;
> +       int id, ret;
>
>         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
>             !ops->set_cur_state)
> @@ -898,10 +898,10 @@ __thermal_cooling_device_register(struct device_node *np,
>         if (!cdev)
>                 return ERR_PTR(-ENOMEM);
>
> -       ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
> -       if (ret < 0)
> +       id = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
> +       if (id < 0)

ret = id;

is missing here.

>                 goto out_kfree_cdev;

However, IMO it would be better to leave the code as is above and do

id = ret;

here.

> -       cdev->id = ret;
> +       cdev->id = id;
>
>         cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
>         if (!cdev->type) {
> @@ -942,8 +942,9 @@ __thermal_cooling_device_register(struct device_node *np,
>  out_kfree_type:
>         kfree(cdev->type);
>         put_device(&cdev->device);
> +       cdev = NULL;
>  out_ida_remove:
> -       ida_simple_remove(&thermal_cdev_ida, cdev->id);
> +       ida_simple_remove(&thermal_cdev_ida, id);
>  out_kfree_cdev:
>         kfree(cdev);
>         return ERR_PTR(ret);
> --

  reply	other threads:[~2021-10-14 11:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-14  8:47 [PATCH] thermal/core: fix a UAF bug in __thermal_cooling_device_register() Ziyang Xuan
2021-10-14 11:08 ` Rafael J. Wysocki [this message]
2021-10-14 14:45 ` kernel test robot
2021-10-14 16:45 ` kernel test robot

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=CAJZ5v0iiSWUN7OCD2CdkHpWOe8WvBAZ+Z+3bFeacnQ8+tsHjtA@mail.gmail.com \
    --to=rafael@kernel.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=william.xuanziyang@huawei.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;
as well as URLs for NNTP newsgroup(s).