Skip to contentSkip to conversation

media: Set entity->links NULL in cleanup

PATCH In review Sakari Ailus 3 participants 6 messages May 27, 2014 – Sep 26, 2014 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: Set entity->links NULL in cleanup

Calling media_entity_cleanup() on a cleaned-up entity would result into double free of the entity->links pointer and likely memory corruption as well. Setting entity->links as NULL right after the kfree() avoids this.

---
 drivers/media/media-entity.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/media-entity.c +1 -0
index 37c334e..c404354 100644
@@ -83,6 +83,7 @@ void
 media_entity_cleanup(struct media_entity *entity)
 {
 	kfree(entity->links);
+	entity->links = NULL;
 }
 EXPORT_SYMBOL_GPL(media_entity_cleanup);
 
Signature · 1 line
1.8.3.2
#1 lore raw

Hi Sakari,

Thank you for the patch.

Quoted 3 lines · On Tuesday 27 May 2014 16:27:49 Sakari Ailus wrote:

Calling media_entity_cleanup() on a cleaned-up entity would result into double free of the entity->links pointer and likely memory corruption as well.

My first question is, why would anyone do that ? :-)

Quoted 18 lines

Setting entity->links as NULL right after the kfree() avoids this.

---
 drivers/media/media-entity.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/media-entity.c +1 -0
index 37c334e..c404354 100644
@@ -83,6 +83,7 @@ void
 media_entity_cleanup(struct media_entity *entity)
 {
 	kfree(entity->links);
+	entity->links = NULL;
 }
 EXPORT_SYMBOL_GPL(media_entity_cleanup);
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

Hi Laurent,

Quoted 6 lines · Laurent Pinchart
Quoted 3 lines · On Tuesday 27 May 2014 16:27:49 Sakari Ailus wrote:

Calling media_entity_cleanup() on a cleaned-up entity would result into double free of the entity->links pointer and likely memory corruption as well.

My first question is, why would anyone do that ? :-)

Because it makes error handling easier. Many cleanup functions work this way, but not media_entity_cleanup().

Signature · 2 lines
Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#3 lore raw
Quoted 10 lines · On Thursday 17 July 2014 14:53:49 Sakari Ailus wrote:
Quoted 6 lines · Laurent Pinchart
Quoted 3 lines · On Tuesday 27 May 2014 16:27:49 Sakari Ailus wrote:

Calling media_entity_cleanup() on a cleaned-up entity would result into double free of the entity->links pointer and likely memory corruption as well.

My first question is, why would anyone do that ? :-)

Because it makes error handling easier. Many cleanup functions work this way, but not media_entity_cleanup().

Do the cleanup functions support being called multiple times, or do they just support being called on memory that has been zeroed and not further initialized ? The media_entity_cleanup() function supports the latter.

Signature · 3 lines
Regards,

Laurent Pinchart
#4 lore raw

Hi Laurent,

Oops. this got buried in my inbox...

Quoted 7 lines · Laurent Pinchart
Quoted 6 lines · On Thursday 17 July 2014 14:53:49 Sakari Ailus wrote:
Quoted 5 lines · Laurent Pinchart
Quoted 3 lines · On Tuesday 27 May 2014 16:27:49 Sakari Ailus wrote:

Calling media_entity_cleanup() on a cleaned-up entity would result into double free of the entity->links pointer and likely memory corruption as well.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

My first question is, why would anyone do that ? :-)

Because it makes error handling easier. Many cleanup functions work this way, but not media_entity_cleanup().

Do the cleanup functions support being called multiple times, or do they just support being called on memory that has been zeroed and not further initialized ? The media_entity_cleanup() function supports the latter.

I'd hope they wouldn't be called multiple times, or on memory that's not been zeroed, but in that case it's better to behave rather than corrupt system memory. That could be an indication of other problems, too, so one could consider adding WARN_ON() to this as well. What do you think?

Signature · 4 lines
Kind regards,

Sakari Ailus
[email protected]
#5 lore raw

Hi Sakari,

Quoted 17 lines · On Wednesday 24 September 2014 12:17:45 Sakari Ailus wrote:

Hi Laurent,

Oops. this got buried in my inbox...

Quoted 12 lines · Laurent Pinchart
Quoted 10 lines · On Thursday 17 July 2014 14:53:49 Sakari Ailus wrote:
Quoted 6 lines · Laurent Pinchart
Quoted 3 lines · On Tuesday 27 May 2014 16:27:49 Sakari Ailus wrote:

Calling media_entity_cleanup() on a cleaned-up entity would result into double free of the entity->links pointer and likely memory corruption as well.

My first question is, why would anyone do that ? :-)

Because it makes error handling easier. Many cleanup functions work this way, but not media_entity_cleanup().

Quoted 8 lines
Quoted 3 lines

Do the cleanup functions support being called multiple times, or do they just support being called on memory that has been zeroed and not further initialized ? The media_entity_cleanup() function supports the latter.

I'd hope they wouldn't be called multiple times, or on memory that's not been zeroed, but in that case it's better to behave rather than corrupt system memory. That could be an indication of other problems, too, so one could consider adding WARN_ON() to this as well. What do you think?

I agree that calling the cleanup function on uninitialized memory simplifies error paths, that's a good feature. Regarding double calls, I have no strong opinion. I don't think they should happen in the first place though.

Signature · 3 lines
Regards,

Laurent Pinchart