Re: [PATCH] tee: optee: fix possible memory leak in optee_register_device()

From: Sumit Garg
Date: Thu Nov 17 2022 - 05:12:47 EST


Hi,

On Thu, 17 Nov 2022 at 08:55, ruanjinjie <ruanjinjie@xxxxxxxxxx> wrote:
>
> If device_register() returns error in optee_register_device(),
> name of kobject which is allocated in dev_set_name() called in device_add()
> is leaked.
>
> Never directly free @dev after calling device_register(), even
> if it returned an error! Always use put_device() to give up the
> reference initialized.
>
> Fixes: c3fa24af9244 ("tee: optee: add TEE bus device enumeration support")
> Signed-off-by: ruanjinjie <ruanjinjie@xxxxxxxxxx>
> ---
> drivers/tee/optee/device.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>

There's already a prior patch posted to fix this here [1].

[1] https://lists.trustedfirmware.org/archives/list/op-tee@xxxxxxxxxxxxxxxxxxxxxxxxx/thread/BMADRGTZ63GP5ORMMISW7RJPMZKINXE7/

-Sumit

> diff --git a/drivers/tee/optee/device.c b/drivers/tee/optee/device.c
> index f3947be13e2e..64f0e047c23d 100644
> --- a/drivers/tee/optee/device.c
> +++ b/drivers/tee/optee/device.c
> @@ -80,7 +80,7 @@ static int optee_register_device(const uuid_t *device_uuid)
> rc = device_register(&optee_device->dev);
> if (rc) {
> pr_err("device registration failed, err: %d\n", rc);
> - kfree(optee_device);
> + put_device(&optee_device->dev);
> }
>
> return rc;
> --
> 2.25.1
>