Skip to contentSkip to conversation

tee: optee: fix possible memory leak in optee_register_device()

PATCH In review Yang Yingliang 3 participants 3 messages Nov 9, 2022 – Nov 18, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tee: optee: fix possible memory leak in optee_register_device()

If device_register() returns error in optee_register_device(), the name allocated by dev_set_name() need be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup(), and optee_device is freed in optee_release_device().

Fixes c3fa24af9244 ("tee: optee: add TEE bus device enumeration support")
---
 drivers/tee/optee/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tee/optee/device.c +1 -1
index f3947be13e2e..64f0e047c23d 100644
@@ -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;
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Yang Yingliang

If device_register() returns error in optee_register_device(), the name allocated by dev_set_name() need be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup(), and optee_device is freed in optee_release_device().

Quoted 6 lines
Fixes c3fa24af9244 ("tee: optee: add TEE bus device enumeration support")
---
 drivers/tee/optee/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

-Sumit

Quoted 16 lines
drivers/tee/optee/device.c +1 -2
index f3947be13e2e..64f0e047c23d 100644
@@ -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

#2 lore raw
Quoted 10 lines · Sumit Garg
Quoted 8 lines · Yang Yingliang

If device_register() returns error in optee_register_device(), the name allocated by dev_set_name() need be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup(), and optee_device is freed in optee_release_device().

Quoted 8 lines
Quoted 6 lines
Fixes c3fa24af9244 ("tee: optee: add TEE bus device enumeration support")
---
 drivers/tee/optee/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

I'm picking up this.

Thanks,
Jens

Quoted 19 lines

-Sumit

Quoted 16 lines
drivers/tee/optee/device.c +1 -2
index f3947be13e2e..64f0e047c23d 100644
@@ -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