Skip to contentSkip to conversation

media: allegro: Fix use after free on error

PATCH In review Dan Carpenter 3 participants 3 messages Dec 14, 2020 – Dec 16, 2020

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

PATCH 1/1 media: allegro: Fix use after free on error

The "channel" is added to the "dev->channels" but then if v4l2_m2m_ctx_init() fails then we free "channel" but it's still on the list so it could lead to a use after free. Let's not add it to the list until after v4l2_m2m_ctx_init() succeeds.

Fixes cc62c74749a3 ("media: allegro: add missed checks in allegro_open()")

---
From static analysis. Not tested.

 drivers/staging/media/allegro-dvt/allegro-core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/staging/media/allegro-dvt/allegro-core.c +1 -2
index 9f718f43282b..640451134072 100644
@@ -2483,8 +2483,6 @@ static int allegro_open(struct file *file)
 	INIT_LIST_HEAD(&channel->buffers_reference);
 	INIT_LIST_HEAD(&channel->buffers_intermediate);
 
-	list_add(&channel->list, &dev->channels);
-
 	channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
 						allegro_queue_init);
 
@@ -2493,6 +2491,7 @@ static int allegro_open(struct file *file)
 		goto error;
 	}
 
+	list_add(&channel->list, &dev->channels);
 	file->private_data = &channel->fh;
 	v4l2_fh_add(&channel->fh);
 
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Dan Carpenter

The "channel" is added to the "dev->channels" but then if v4l2_m2m_ctx_init() fails then we free "channel" but it's still on the list so it could lead to a use after free. Let's not add it to the list until after v4l2_m2m_ctx_init() succeeds.

Thanks.

The patch conflicts with the series that moves the driver from staging to mainline [0]. I'm not sure, which patch should go in first.

It is also correct to not change the order of list_del and v4l2_m2m_ctx_release in allegro_release. The list is used to relate messages from the VCU to their destination channel and this should be possible until the context has been released and no further messages are expected for that channel.

[0] https://lore.kernel.org/linux-media/[email protected]/

Quoted 3 lines
Fixes cc62c74749a3 ("media: allegro: add missed checks in allegro_open()")
Quoted 31 lines

---
From static analysis. Not tested.

 drivers/staging/media/allegro-dvt/allegro-core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/staging/media/allegro-dvt/allegro-core.c +1 -2
index 9f718f43282b..640451134072 100644
@@ -2483,8 +2483,6 @@ static int allegro_open(struct file *file)
 	INIT_LIST_HEAD(&channel->buffers_reference);
 	INIT_LIST_HEAD(&channel->buffers_intermediate);
 
-	list_add(&channel->list, &dev->channels);
-
 	channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
 						allegro_queue_init);
 
@@ -2493,6 +2491,7 @@ static int allegro_open(struct file *file)
 		goto error;
 	}
 
+	list_add(&channel->list, &dev->channels);
 	file->private_data = &channel->fh;
 	v4l2_fh_add(&channel->fh);
 
Signature · 1 line
2.29.2
#2 lore raw
Quoted 10 lines · Michael Tretter
Quoted 4 lines · Dan Carpenter

The "channel" is added to the "dev->channels" but then if v4l2_m2m_ctx_init() fails then we free "channel" but it's still on the list so it could lead to a use after free. Let's not add it to the list until after v4l2_m2m_ctx_init() succeeds.

Thanks.

The patch conflicts with the series that moves the driver from staging to mainline [0]. I'm not sure, which patch should go in first.

I'll take care of the conflict.

Regards,

Hans

Quoted 46 lines

It is also correct to not change the order of list_del and v4l2_m2m_ctx_release in allegro_release. The list is used to relate messages from the VCU to their destination channel and this should be possible until the context has been released and no further messages are expected for that channel.

[0] https://lore.kernel.org/linux-media/[email protected]/

Quoted 3 lines
Fixes cc62c74749a3 ("media: allegro: add missed checks in allegro_open()")
Quoted 31 lines

---
From static analysis. Not tested.

 drivers/staging/media/allegro-dvt/allegro-core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/staging/media/allegro-dvt/allegro-core.c +1 -2
index 9f718f43282b..640451134072 100644
@@ -2483,8 +2483,6 @@ static int allegro_open(struct file *file)
 	INIT_LIST_HEAD(&channel->buffers_reference);
 	INIT_LIST_HEAD(&channel->buffers_intermediate);
 
-	list_add(&channel->list, &dev->channels);
-
 	channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
 						allegro_queue_init);
 
@@ -2493,6 +2491,7 @@ static int allegro_open(struct file *file)
 		goto error;
 	}
 
+	list_add(&channel->list, &dev->channels);
 	file->private_data = &channel->fh;
 	v4l2_fh_add(&channel->fh);
 
Signature · 1 line
2.29.2