Skip to contentSkip to conversation

rpmsg: virtio: fix posseble double free

PATCH In review Hangyu Hua 2 participants 7 messages Apr 26, 2022 – Apr 27, 2022 Remote processor messaging (rpmsg)

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

v2 cover rpmsg: virtio: fix posseble double free

There are double free in rpmsg_probe() and rpmsg_virtio_add_ctrl_dev(). In addition, rpmsg_virtio_del_ctrl_dev() didn't use correct function to free the object.

v2:
Use a cover letter to integrate the three patches. Add comments in rpmsg_probe() and rpmsg_virtio_add_ctrl_dev().

Hangyu Hua (2):
rpmsg: virtio: fix possible double free in rpmsg_probe()
rpmsg: virtio: fix possible double free in rpmsg_virtio_add_ctrl_dev()
Arnaud Pouliquen (1):
rpmsg: virtio: Fix the unregistration of the device rpmsg_ctrl

 drivers/rpmsg/virtio_rpmsg_bus.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/3 rpmsg: virtio: fix possible double free in rpmsg_probe()

vch will be free in virtio_rpmsg_release_device() when rpmsg_ns_register_device() fails. There is no need to call kfree() again.

Fix this by changing error path from free_vch to free_ctrldev.

Fixes c486682ae1e2 ("rpmsg: virtio: Register the rpmsg_char device")
---
 drivers/rpmsg/virtio_rpmsg_bus.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/rpmsg/virtio_rpmsg_bus.c +2 -3
index 3ede25b1f2e4..0130d0ba548c 100644
@@ -973,7 +973,8 @@ static int rpmsg_probe(struct virtio_device *vdev)
 
 		err = rpmsg_ns_register_device(rpdev_ns);
 		if (err)
-			goto free_vch;
+			/* vch will be free in virtio_rpmsg_release_device() */
+			goto free_ctrldev;
 	}
 
 	/*
@@ -997,8 +998,6 @@ static int rpmsg_probe(struct virtio_device *vdev)
 
 	return 0;
 
-free_vch:
-	kfree(vch);
 free_ctrldev:
 	rpmsg_virtio_del_ctrl_dev(rpdev_ctrl);
 free_coherent:
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v2 2/3 rpmsg: virtio: fix possible double free in rpmsg_virtio_add_ctrl_dev()

In reply to Hangyu Hua

vch will be free in virtio_rpmsg_release_device() when rpmsg_ctrldev_register_device() fails. There is no need to call kfree() again.

Fixes c486682ae1e2 ("rpmsg: virtio: Register the rpmsg_char device")
---
 drivers/rpmsg/virtio_rpmsg_bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rpmsg/virtio_rpmsg_bus.c +1 -1
index 0130d0ba548c..291fc1cfab7f 100644
@@ -851,7 +851,7 @@ static struct rpmsg_device *rpmsg_virtio_add_ctrl_dev(struct virtio_device *vdev
 
 	err = rpmsg_ctrldev_register_device(rpdev_ctrl);
 	if (err) {
-		kfree(vch);
+		/* vch will be free in virtio_rpmsg_release_device() */
 		return ERR_PTR(err);
 	}
 
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v2 3/3 rpmsg: virtio: Fix the unregistration of the device rpmsg_ctrl

In reply to Hangyu Hua

Unregister the rpmsg_ctrl device instead of just freeing the the virtio_rpmsg_channel structure.
This will properly unregister the device and call virtio_rpmsg_release_device() that frees the structure.

Fixes c486682ae1e2 ("rpmsg: virtio: Register the rpmsg_char device")
Cc Hangyu Hua <[email protected]>
---
 drivers/rpmsg/virtio_rpmsg_bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rpmsg/virtio_rpmsg_bus.c +1 -1
index 291fc1cfab7f..485e95f506df 100644
@@ -862,7 +862,7 @@ static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
 {
 	if (!rpdev_ctrl)
 		return;
-	kfree(to_virtio_rpmsg_channel(rpdev_ctrl));
+	device_unregister(&rpdev_ctrl->dev);
 }
 
 static int rpmsg_probe(struct virtio_device *vdev)
Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH v2 3/3] rpmsg: virtio: Fix the unregistration of the device rpmsg_ctrl

Quoted 18 lines · Hangyu Hua

Unregister the rpmsg_ctrl device instead of just freeing the the virtio_rpmsg_channel structure.
This will properly unregister the device and call virtio_rpmsg_release_device() that frees the structure.

Fixes c486682ae1e2 ("rpmsg: virtio: Register the rpmsg_char device")
Cc Hangyu Hua <[email protected]>
---
 drivers/rpmsg/virtio_rpmsg_bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rpmsg/virtio_rpmsg_bus.c +0 -0
index 291fc1cfab7f..485e95f506df 100644

drivers/rpmsg/virtio_rpmsg_bus.c static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl) · line 865

Quoted 5 lines
drivers/rpmsg/virtio_rpmsg_bus.c +1 -1
@@ -862,7 +862,7 @@ static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
 {
 	if (!rpdev_ctrl)
 		return;
-	kfree(to_virtio_rpmsg_channel(rpdev_ctrl));
+	device_unregister(&rpdev_ctrl->dev);

The author of this patch should have been Arnaud, something I have fixed before applying this set.

Thanks,
Mathieu

Quoted 6 lines
drivers/rpmsg/virtio_rpmsg_bus.c +0 -0
 }
 
 static int rpmsg_probe(struct virtio_device *vdev)
Signature · 1 line
2.25.1
author
#5 lore raw

Re: [PATCH v2 3/3] rpmsg: virtio: Fix the unregistration of the device rpmsg_ctrl

Quoted 28 lines · Mathieu Poirier
Quoted 18 lines · Hangyu Hua

Unregister the rpmsg_ctrl device instead of just freeing the the virtio_rpmsg_channel structure.
This will properly unregister the device and call virtio_rpmsg_release_device() that frees the structure.

Fixes c486682ae1e2 ("rpmsg: virtio: Register the rpmsg_char device")
Cc Hangyu Hua <[email protected]>
---
  drivers/rpmsg/virtio_rpmsg_bus.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rpmsg/virtio_rpmsg_bus.c +0 -0
index 291fc1cfab7f..485e95f506df 100644

drivers/rpmsg/virtio_rpmsg_bus.c static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl) · line 865

Quoted 5 lines
drivers/rpmsg/virtio_rpmsg_bus.c +1 -1
@@ -862,7 +862,7 @@ static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
  {
  	if (!rpdev_ctrl)
  		return;
-	kfree(to_virtio_rpmsg_channel(rpdev_ctrl));
+	device_unregister(&rpdev_ctrl->dev);

The author of this patch should have been Arnaud, something I have fixed before applying this set.

Quoted 3 lines

Thanks,
Mathieu

I get it. I'm sorry i thought Signed-off-by and a description in cover letter are enough to express. Do i need to do anything else?

Thanks,
Hangyu

Quoted 6 lines
Quoted 6 lines
drivers/rpmsg/virtio_rpmsg_bus.c +0 -0
  }
 
  static int rpmsg_probe(struct virtio_device *vdev)
Signature · 1 line
2.25.1
maintainer
#6 lore raw

Re: [PATCH v2 3/3] rpmsg: virtio: Fix the unregistration of the device rpmsg_ctrl

Quoted 30 lines · Hangyu Hua
Quoted 28 lines · Mathieu Poirier
Quoted 18 lines · Hangyu Hua

Unregister the rpmsg_ctrl device instead of just freeing the the virtio_rpmsg_channel structure.
This will properly unregister the device and call virtio_rpmsg_release_device() that frees the structure.

Fixes c486682ae1e2 ("rpmsg: virtio: Register the rpmsg_char device")
Cc Hangyu Hua <[email protected]>
---
  drivers/rpmsg/virtio_rpmsg_bus.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rpmsg/virtio_rpmsg_bus.c +0 -0
index 291fc1cfab7f..485e95f506df 100644

drivers/rpmsg/virtio_rpmsg_bus.c static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl) · line 865

Quoted 5 lines
drivers/rpmsg/virtio_rpmsg_bus.c +1 -1
@@ -862,7 +862,7 @@ static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
  {
     if (!rpdev_ctrl)
             return;
-    kfree(to_virtio_rpmsg_channel(rpdev_ctrl));
+    device_unregister(&rpdev_ctrl->dev);

The author of this patch should have been Arnaud, something I have fixed before applying this set.

Quoted 6 lines
Quoted 3 lines

Thanks,
Mathieu

I get it. I'm sorry i thought Signed-off-by and a description in cover letter are enough to express. Do i need to do anything else?

I don't.

Quoted 9 lines

Thanks,
Hangyu

Quoted 6 lines
Quoted 6 lines
drivers/rpmsg/virtio_rpmsg_bus.c +0 -1
  }
 
  static int rpmsg_probe(struct virtio_device *vdev)
--

2.25.1