fix xa_alloc_cyclic() return checks
Subsystems (6)
v2 cover fix xa_alloc_cyclic() return checks
Pierre Riteau <[email protected]> found suspicious handling an error from xa_alloc_cyclic() in scheduler code [1]. The same is done in few other places.
v1 --> v2: [2]
* add fixes tags
* fix also the same usage in dpll and phy
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
Michal Swiatkowski (3):
devlink: fix xa_alloc_cyclic() error handling
dpll: fix xa_alloc_cyclic() error handling
phy: fix xa_alloc_cyclic() error handling
drivers/dpll/dpll_core.c | 2 +- drivers/net/phy/phy_link_topology.c | 2 +- net/devlink/core.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.42.0
PATCH v2 1/3 devlink: fix xa_alloc_cyclic() error handling
In case of returning 1 from xa_alloc_cyclic() (wrapping) ERR_PTR(1) will be returned, which will cause IS_ERR() to be false. Which can lead to dereference not allocated pointer (rel).
Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
--- net/devlink/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/devlink/core.c +1 -1
@@ -117,7 +117,7 @@ static struct devlink_rel *devlink_rel_alloc(void) err =xa_alloc_cyclic (&devlink_rels, &rel->index, rel, xa_limit_32b, &next,GFP_KERNEL ); -if (err) { +if (err <0 ) {kfree (rel);return ERR_PTR (err); }
Signature · 1 line
2.42.0
Re: [PATCH net v2 1/3] devlink: fix xa_alloc_cyclic() error handling
Quoted 10 lines · Michal Swiatkowski
In case of returning 1 from
xa_alloc_cyclic()(wrapping)ERR_PTR(1)will be returned, which will causeIS_ERR()to be false. Which can lead to dereference not allocated pointer (rel).Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
Fixes c137743bce02 ("devlink: introduce object and nested devlink relationship infra")Signed-off-by Michal Swiatkowski <[email protected]>
Andrew
Re: [PATCH net v2 1/3] devlink: fix xa_alloc_cyclic() error handling
In reply to Michal SwiatkowskiQuoted 10 lines · Wed, Mar 12, 2025 at 10:52:49AM +0100, [email protected] wrote:
In case of returning 1 from
xa_alloc_cyclic()(wrapping)ERR_PTR(1)will be returned, which will causeIS_ERR()to be false. Which can lead to dereference not allocated pointer (rel).Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
Fixes c137743bce02 ("devlink: introduce object and nested devlink relationship infra")Signed-off-by Michal Swiatkowski <[email protected]>
PATCH v2 2/3 dpll: fix xa_alloc_cyclic() error handling
In reply to Michal SwiatkowskiIn case of returning 1 from xa_alloc_cyclic() (wrapping) ERR_PTR(1) will be returned, which will cause IS_ERR() to be false. Which can lead to dereference not allocated pointer (pin).
Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
--- drivers/dpll/dpll_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dpll/dpll_core.c +1 -1
@@ -505,7 +505,7 @@ dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,xa_init_flags (&pin->parent_refs,XA_FLAGS_ALLOC ); ret =xa_alloc_cyclic (&dpll_pin_xa, &pin->id, pin, xa_limit_32b, &dpll_pin_xa_id,GFP_KERNEL ); -if (ret) +if (ret <0 ) goto err_xa_alloc;return pin; err_xa_alloc:
Signature · 1 line
2.42.0
Re: [PATCH net v2 2/3] dpll: fix xa_alloc_cyclic() error handling
Quoted 19 lines · Michal Swiatkowski
In case of returning 1 from
xa_alloc_cyclic()(wrapping)ERR_PTR(1)will be returned, which will causeIS_ERR()to be false. Which can lead to dereference not allocated pointer (pin).Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
Fixes 97f265ef7f5b ("dpll: allocate pin ids in cycle")Signed-off-by Michal Swiatkowski <[email protected]>--- drivers/dpll/dpll_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/dpll/dpll_core.c +0
-0
drivers/dpll/dpll_core.c dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module, · line 511
Quoted 8 lines
drivers/dpll/dpll_core.c +1
-1@@ -505,7 +505,7 @@ dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,xa_init_flags (&pin->parent_refs,XA_FLAGS_ALLOC ); ret =xa_alloc_cyclic (&dpll_pin_xa, &pin->id, pin, xa_limit_32b, &dpll_pin_xa_id,GFP_KERNEL ); -if (ret) +if (ret <0 ) goto err_xa_alloc;return pin; err_xa_alloc:
RE: [PATCH net v2 2/3] dpll: fix xa_alloc_cyclic() error handling
In reply to Michal SwiatkowskiQuoted 30 lines
From: Michal Swiatkowski <[email protected]>
Sent: Wednesday, March 12, 2025 10:53 AMIn case of returning 1 from
xa_alloc_cyclic()(wrapping)ERR_PTR(1)will be returned, which will causeIS_ERR()to be false. Which can lead to dereference not allocated pointer (pin).Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
Fixes 97f265ef7f5b ("dpll: allocate pin ids in cycle")Signed-off-by Michal Swiatkowski <[email protected]>---
drivers/dpll/dpll_core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/dpll/dpll_core.c +1
-1@@ -505,7 +505,7 @@ dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,xa_init_flags (&pin->parent_refs,XA_FLAGS_ALLOC ); ret =xa_alloc_cyclic (&dpll_pin_xa, &pin->id, pin, xa_limit_32b, &dpll_pin_xa_id,GFP_KERNEL ); -if (ret) +if (ret <0 ) goto err_xa_alloc;return pin;
Quoted 3 lines
err_xa_alloc:
--
2.42.0
LGTM,
Thanks!
Re: [PATCH net v2 2/3] dpll: fix xa_alloc_cyclic() error handling
In reply to Michal SwiatkowskiQuoted 10 lines · Wed, Mar 12, 2025 at 10:52:50AM +0100, [email protected] wrote:
In case of returning 1 from
xa_alloc_cyclic()(wrapping)ERR_PTR(1)will be returned, which will causeIS_ERR()to be false. Which can lead to dereference not allocated pointer (pin).Fix it by checking if err is lower than zero.
This wasn't found in real usecase, only noticed. Credit to Pierre.
Fixes 97f265ef7f5b ("dpll: allocate pin ids in cycle")Signed-off-by Michal Swiatkowski <[email protected]>
PATCH v2 3/3 phy: fix xa_alloc_cyclic() error handling
In reply to Michal Swiatkowskixa_alloc_cyclic() can return 1, which isn't an error. To prevent situation when the caller of this function will treat it as no error do a check only for negative here.
--- drivers/net/phy/phy_link_topology.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/phy_link_topology.c +1 -1
@@ -73,7 +73,7 @@ int phy_link_topo_add_phy(struct net_device *dev, xa_limit_32b, &topo->next_phy_index, GFP_KERNEL); -if (ret) +if (ret <0 ) goto err;return 0 ;
Signature · 1 line
2.42.0
Re: [PATCH net v2 3/3] phy: fix xa_alloc_cyclic() error handling
Quoted 15 lines · On Wed, 12 Mar 2025 10:52:51 +0100 Michal Swiatkowski…
xa_alloc_cyclic()can return 1, which isn't an error. To prevent situation when the caller of this function will treat it as no error do a check only for negative here.Fixes 384968786909 ("net: phy: Introduce ethernet link topology representation")Signed-off-by Michal Swiatkowski <[email protected]>--- drivers/net/phy/phy_link_topology.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/phy/phy_link_topology.c +0
-0
drivers/net/phy/phy_link_topology.c int phy_link_topo_add_phy(struct net_device *dev, · line 79
Quoted 8 lines
drivers/net/phy/phy_link_topology.c +1
-1@@ -73,7 +73,7 @@ int phy_link_topo_add_phy(struct net_device *dev, xa_limit_32b, &topo->next_phy_index, GFP_KERNEL); -if (ret) +if (ret <0 ) goto err;return 0 ;
Thank you,
Maxime
Quoted 8 lines · Michal Swiatkowski
Pierre Riteau <[email protected]> found suspicious handling an error from
xa_alloc_cyclic()in scheduler code [1]. The same is done in few other places.v1 --> v2: [2]
* add fixes tags
* fix also the same usage in dpll and phy
Quoted 7 lines
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/Michal Swiatkowski (3):
devlink: fixxa_alloc_cyclic()error handling
dpll: fixxa_alloc_cyclic()error handling
phy: fixxa_alloc_cyclic()error handling
Maybe there should be a wrapper around xa_alloc_cyclic() for people who don't care about the 1 return?
int wrapper()
{
ret = xa_alloc_cyclic();
if (ret < 0)
return ret;
rerturn 0;
}regards,
dan carpenter
Quoted 12 lines · Dan Carpenter
Quoted 11 lines · Michal Swiatkowski
Pierre Riteau <[email protected]> found suspicious handling an error from
xa_alloc_cyclic()in scheduler code [1]. The same is done in few other places.v1 --> v2: [2]
* add fixes tags
* fix also the same usage in dpll and phy[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
Quoted 7 lines
Quoted 4 lines
Michal Swiatkowski (3):
devlink: fixxa_alloc_cyclic()error handling
dpll: fixxa_alloc_cyclic()error handling
phy: fixxa_alloc_cyclic()error handlingMaybe there should be a wrapper around
xa_alloc_cyclic()for people who don't care about the 1 return?
What about changing init flags instead, and add a new one for this purpose?, say:XA_FLAGS_ALLOC_RET0
Quoted 12 lines
int
wrapper(){ ret = xa_alloc_cyclic(); if (ret < 0) return ret; rerturn 0; }regards,
dan carpenter
Quoted 18 lines · Przemek Kitszel
Quoted 17 lines · Dan Carpenter
Quoted 15 lines · Michal Swiatkowski
Pierre Riteau <[email protected]> found suspicious handling an error from
xa_alloc_cyclic()in scheduler code [1]. The same is done in few other places.v1 --> v2: [2]
* add fixes tags
* fix also the same usage in dpll and phy[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/Michal Swiatkowski (3):
devlink: fixxa_alloc_cyclic()error handling
dpll: fixxa_alloc_cyclic()error handling
phy: fixxa_alloc_cyclic()error handling
Quoted 6 lines
Quoted 2 lines
Maybe there should be a wrapper around
xa_alloc_cyclic()for people who don't care about the 1 return?What about changing init flags instead, and add a new one for this purpose?, say:
XA_FLAGS_ALLOC_RET0
Right now I have a static checker rule for passing 1 to ERR_PTR(). It's not specific to this function but it catches the bugs here. If we added a XA_FLAGS_ALLOC_RET0 then I'd have to silence the checker rule for xa_alloc_cyclic().
I was also thinking about creating another more specific rule for just this function to warn about when callers which treat 1 and negative error codes the same, but that wouldn't be possible.
On the other hand, people who pass XA_FLAGS_ALLOC_RET0 probably will understand what it means and not introduce bugs so static analysis becomes less important in that case.
regards,
dan carpenter
Quoted 3 lines · Przemek Kitszel
What about changing init flags instead, and add a new one for this purpose?, say:
XA_FLAGS_ALLOC_RET0
No. Dan's suggestion is better. Actually, I'd go further and make xa_alloc_cyclic() always do that. People who want the wrapping information get to call __xa_alloc_cyclic themselves.
Quoted 8 lines · Matthew Wilcox
Quoted 3 lines · Przemek Kitszel
What about changing init flags instead, and add a new one for this purpose?, say:
XA_FLAGS_ALLOC_RET0No. Dan's suggestion is better. Actually, I'd go further and make
xa_alloc_cyclic()always do that. People who want the wrapping information get to call__xa_alloc_cyclicthemselves.
Even better, LGTM!
Quoted 11 lines · Przemek Kitszel
Quoted 8 lines · Matthew Wilcox
Quoted 3 lines · Przemek Kitszel
What about changing init flags instead, and add a new one for this purpose?, say:
XA_FLAGS_ALLOC_RET0No. Dan's suggestion is better. Actually, I'd go further and make
xa_alloc_cyclic()always do that. People who want the wrapping information get to call__xa_alloc_cyclicthemselves.Even better, LGTM!
Is that "I volunteer to do this"?
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Przemek Kitszel
Quoted 5 lines · Matthew Wilcox
Quoted 3 lines · Przemek Kitszel
What about changing init flags instead, and add a new one for this purpose?, say:
XA_FLAGS_ALLOC_RET0
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
No. Dan's suggestion is better. Actually, I'd go further and make
xa_alloc_cyclic()always do that. People who want the wrapping information get to call__xa_alloc_cyclicthemselves.Even better, LGTM!
Is that "I volunteer to do this"?
sure, why not :)
This series was applied to netdev/net.git (main) by David S. Miller <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:
Quoted 9 lines · On Wed, 12 Mar 2025 10:52:48 +0100 you wrote:
Pierre Riteau <[email protected]> found suspicious handling an error from
xa_alloc_cyclic()in scheduler code [1]. The same is done in few other places.v1 --> v2: [2]
* add fixes tags
* fix also the same usage in dpll and phy[...]
Here is the summary with links:
- [net,v2,1/3] devlink: fix xa_alloc_cyclic() error handling
https://git.kernel.org/netdev/net/c/f3b97b7d4bf3 - [net,v2,2/3] dpll: fix xa_alloc_cyclic() error handling https://git.kernel.org/netdev/net/c/3614bf90130d - [net,v2,3/3] phy: fix xa_alloc_cyclic() error handling https://git.kernel.org/netdev/net/c/3178d2b04836
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html