mailbox: tegra-hsp: Fix null-ptr-deref in tegra_hsp_doorbell_create()
Subsystems (2)
PATCH 1/1 mailbox: tegra-hsp: Fix null-ptr-deref in tegra_hsp_doorbell_create()
devm_kstrdup_const() returns NULL when memory allocation fails. Currently, tegra_hsp_doorbell_create() does not check for this case, which results in a NULL pointer dereference.
--- drivers/mailbox/tegra-hsp.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/mailbox/tegra-hsp.c +2 -0
@@ -293,6 +293,8 @@ tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, db->channel.hsp = hsp; db->name =devm_kstrdup_const (hsp->dev, name,GFP_KERNEL ); +if (!db->name) +return ERR_PTR (-ENOMEM ); db->master = master; db->index = index;
Signature · 1 line
2.34.1
Quoted 1 line
devm_kstrdup_const()returns NULL when memory allocation fails.
…
failed?
Can a summary phrase like “Prevent null pointer dereference in tegra_hsp_doorbell_create()” be a bit nicer?
…
drivers/mailbox/tegra-hsp.c tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, · line 297
Quoted 7 lines
+++
b/drivers/mailbox/tegra-hsp.cdrivers/mailbox/tegra-hsp.c +2
-0@@ -293,6 +293,8 @@ tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, db->channel.hsp = hsp; db->name =devm_kstrdup_const (hsp->dev, name,GFP_KERNEL ); +if (!db->name) +return ERR_PTR (-ENOMEM );
Can a blank line be desirable after such a statement?
Regards,
Markus
Quoted 15 lines · Henry Martin
devm_kstrdup_const()returns NULL when memory allocation fails. Currently,tegra_hsp_doorbell_create()does not check for this case, which results in a NULL pointer dereference.Fixes a54d03ed01b4 ("mailbox: tegra-hsp: use devm_kstrdup_const()")Signed-off-by Henry Martin <[email protected]>--- drivers/mailbox/tegra-hsp.c | 2 ++ 1 file changed, 2 insertions(+)drivers/mailbox/tegra-hsp.c +0
-0
drivers/mailbox/tegra-hsp.c tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, · line 297
Quoted 5 lines
drivers/mailbox/tegra-hsp.c +2
-0@@ -293,6 +293,8 @@ tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, db->channel.hsp = hsp; db->name =devm_kstrdup_const (hsp->dev, name,GFP_KERNEL ); +if (!db->name) +return ERR_PTR (-ENOMEM );
I don't think this is needed. First and foremost, db->name ends up not being used. It was meant to be used by debug code that never ended up being written, so at this point it's mostly here as a way to document what the doorbell mapping is (though even that's somewhat redundant since we already have macros that match the strings).
Secondly, these strings always come from tegra186_hsp_db_map, which is rodata and so the allocation path should never be taken, and hence the allocation can never fail.
So instead of trying to fix a non-existent problem we have two other
options: one is to remove all traces of db->name (as well as the string in the mapping table), or we turn this into an assignment since we know that it's always rodata, so there's no need to copy it.
Alternatively we could just leave this as-is. But then it's just a matter of time before someone else comes around to "fix" the same thing.
Thierry
Quoted 29 lines · Thierry Reding <[email protected]> 于2025年5月9日周五 05:54写道:
Quoted 15 lines · Henry Martin
devm_kstrdup_const()returns NULL when memory allocation fails. Currently,tegra_hsp_doorbell_create()does not check for this case, which results in a NULL pointer dereference.Fixes a54d03ed01b4 ("mailbox: tegra-hsp: use devm_kstrdup_const()")Signed-off-by Henry Martin <[email protected]>--- drivers/mailbox/tegra-hsp.c | 2 ++ 1 file changed, 2 insertions(+)drivers/mailbox/tegra-hsp.c +0
-0drivers/mailbox/tegra-hsp.c tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, · line 297
Quoted 5 lines
drivers/mailbox/tegra-hsp.c +2
-0@@ -293,6 +293,8 @@ tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name, db->channel.hsp = hsp; db->name =devm_kstrdup_const (hsp->dev, name,GFP_KERNEL ); +if (!db->name) +return ERR_PTR (-ENOMEM );I don't think this is needed. First and foremost,
db->nameends up not being used. It was meant to be used by debug code that never ended up being written, so at this point it's mostly here as a way to document what the doorbell mapping is (though even that's somewhat redundant since we already have macros that match the strings).
Quoted 9 lines
Secondly, these strings always come from
tegra186_hsp_db_map, which is rodata and so the allocation path should never be taken, and hence the allocation can never fail.So instead of trying to fix a non-existent problem we have two other
options: one is to remove all traces ofdb->name(as well as the string in the mapping table), or we turn this into an assignment since we know that it's always rodata, so there's no need to copy it.
Agreed. Since the strings are always from rodata, changing to direct assignment makes more sense.
Regards,
Henry
Quoted 4 lines
Alternatively we could just leave this as-is. But then it's just a matter of time before someone else comes around to "fix" the same thing.
Thierry