Skip to contentSkip to conversation

device property: set fwnode->secondary to NULL in fwnode_init()

PATCH In review Bartosz Golaszewski 7 participants 11 messages May 6 – May 22

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

PATCH 1/1 device property: set fwnode->secondary to NULL in fwnode_init()

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR() and so may end up being dereferenced (for
example: in dev_to_swnode()). Set fwnode->secondary to NULL on initialization.

Fixes 01bb86b380a3 ("driver core: Add fwnode_init()")
---
 include/linux/fwnode.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/fwnode.h +1 -0
index 80b38fbf2121..31df7608737e 100644
@@ -208,6 +208,7 @@ struct fwnode_operations {
 static inline void fwnode_init(struct fwnode_handle *fwnode,
 			       const struct fwnode_operations *ops)
 {
+	fwnode->secondary = NULL;
 	fwnode->ops = ops;
 	INIT_LIST_HEAD(&fwnode->consumers);
 	INIT_LIST_HEAD(&fwnode->suppliers);
Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Bartosz Golaszewski

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR() and so may end up being dereferenced (for
example: in dev_to_swnode()). Set fwnode->secondary to NULL on initialization.

Hmm... Are you going to use that outside of fw_devlink?

The patch itself looks good to me, but I'm not sure I understand how it's related to all the work you are doing WRT fwnode core implementation.

FWIW,

But Saravana is the best person to actually tell if this patch makes sense.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR() and so may end up being dereferenced (for
example: in dev_to_swnode()). Set fwnode->secondary to NULL on initialization.

Hmm... Are you going to use that outside of fw_devlink?

Quoted 8 lines

The patch itself looks good to me, but I'm not sure I understand how it's related to all the work you are doing WRT fwnode core implementation.

FWIW,

But Saravana is the best person to actually tell if this patch makes sense.

I just stumbled upon this crash working on adding support for fw_devlink for software nodes. I figured it makes sense fixing it even if we have never hit it before.

Bart

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Bartosz Golaszewski
Quoted 9 lines · Bartosz Golaszewski

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR() and so may end up being dereferenced (for
example: in dev_to_swnode()). Set fwnode->secondary to NULL on initialization.

Quoted 3 lines
Fixes 01bb86b380a3 ("driver core: Add fwnode_init()")
Quoted 20 lines
---
 include/linux/fwnode.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/fwnode.h +1 -1
index 80b38fbf2121..31df7608737e 100644
@@ -208,6 +208,7 @@ struct fwnode_operations {
 static inline void fwnode_init(struct fwnode_handle *fwnode,
                               const struct fwnode_operations *ops)
 {
+       fwnode->secondary = NULL;
        fwnode->ops = ops;
        INIT_LIST_HEAD(&fwnode->consumers);
        INIT_LIST_HEAD(&fwnode->suppliers);
--

2.47.3

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Bartosz Golaszewski
Quoted 11 lines · Bartosz Golaszewski

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR() and so may end up being dereferenced (for
example: in dev_to_swnode()). Set fwnode->secondary to NULL on initialization.

Fixes 01bb86b380a3 ("driver core: Add fwnode_init()")
Signature · 1 line
Sakari Ailus
maintainer
#5 lore raw
In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR().

I see why secondary is generally more prone to this, but if the justification of this change is to not rely on the caller to zero out the memory, then we might just want to initialize all fields.

For instance, if the caller is allowed to not zero-initialize the memory then having flags with a random value isn't correct either; all accessors are atomic bitwise operations that never zero the whole field.

#6 lore raw
Quoted 7 lines · Danilo Krummrich
Quoted 5 lines · Bartosz Golaszewski

If a firmware node is allocated on the stack (for instance: temporary software node whose life-time we control) or on the heap - but using a non-zeroing allocation function - and initialized using fwnode_init(), its secondary pointer will contain uninitalized memory which likely will be neither NULL nor IS_ERR().

Quoted 8 lines

I see why secondary is generally more prone to this, but if the justification of this change is to not rely on the caller to zero out the memory, then we might just want to initialize all fields.

For instance, if the caller is allowed to not zero-initialize the memory then having flags with a random value isn't correct either; all accessors are atomic bitwise operations that never zero the whole field.

Sure, but for now I'll go take this one.

thanks,

greg k-h

maintainer
#7 lore raw
Quoted 1 line · Greg Kroah-Hartman

Sure, but for now I'll go take this one.

The follow-up commit 7eba000621ff ("device property: initialize the remaining fields of fwnode_handle in fwnode_init()") is already in driver-core-next.

maintainer
#8 lore raw
Quoted 4 lines · Danilo Krummrich
Quoted 1 line · Greg Kroah-Hartman

Sure, but for now I'll go take this one.

The follow-up commit 7eba000621ff ("device property: initialize the remaining fields of fwnode_handle in fwnode_init()") is already in driver-core-next.

s/follow-up/v2/

https://lore.kernel.org/all/[email protected]/

#9 lore raw
Quoted 10 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich
Quoted 1 line · Greg Kroah-Hartman

Sure, but for now I'll go take this one.

The follow-up commit 7eba000621ff ("device property: initialize the remaining fields of fwnode_handle in fwnode_init()") is already in driver-core-next.

s/follow-up/v2/

https://lore.kernel.org/all/[email protected]/

Ugh, ok, we will have a merge conflict, but at least the bugfix will propagate to stable trees :)

thanks,

greg "digging out from a huge email backlog" k-h

#10 lore raw
Quoted 13 lines · Greg Kroah-Hartman
Quoted 10 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich
Quoted 1 line · Greg Kroah-Hartman

Sure, but for now I'll go take this one.

The follow-up commit 7eba000621ff ("device property: initialize the remaining fields of fwnode_handle in fwnode_init()") is already in driver-core-next.

s/follow-up/v2/

https://lore.kernel.org/all/[email protected]/

Quoted 6 lines

Ugh, ok, we will have a merge conflict, but at least the bugfix will propagate to stable trees :)

thanks,

greg "digging out from a huge email backlog" k-h

There's no need for you to queue this one, the v2 Danilo linked contains this change and some more hardening on top for consistency.

Bart