net: dsa: Fix stale cpu_switch reference after unbind then bind
Subsystems (3)
PATCH 1/1 net: dsa: Fix stale cpu_switch reference after unbind then bind
Commit 9520ed8fb841 ("net: dsa: use cpu_switch instead of ds[0]") replaced the use of dst->ds[0] with dst->cpu_switch since that is functionally equivalent, however, we can now run into an use after free scenario after unbinding then rebinding the switch driver.
The use after free happens because we do correctly initialize dst->cpu_switch the first time we probe in dsa_cpu_parse(), then we unbind the driver: dsa_dst_unapply() is called, and we rebind again. dst->cpu_switch now points to a freed "ds" structure, and so when we finally dereference it in dsa_cpu_port_ethtool_setup(), we oops.
To fix this, simply set dst->cpu_switch to NULL in dsa_dst_unapply() which guarantees that we always correctly re-assign dst->cpu_switch in dsa_cpu_parse().
---
David, please queue this up for 4.11-stable as well, thanks!
net/dsa/dsa2.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/dsa/dsa2.c +3 -1
@@ -484,8 +484,10 @@ static void dsa_dst_unapply(struct dsa_switch_tree *dst)dsa_ds_unapply (dst, ds); } -if (dst->cpu_switch) +if (dst->cpu_switch) {dsa_cpu_port_ethtool_restore (dst->cpu_switch); + dst->cpu_switch =NULL ; + }pr_info ("DSA: tree %d unapplied\n" , dst->tree); dst->applied = false;
Signature · 1 line
2.9.3
Hi Florian,
Quoted 11 lines · Florian Fainelli
Commit
9520ed8fb841("net: dsa: usecpu_switchinstead of ds[0]") replaced the use of dst->ds[0] withdst->cpu_switchsince that is functionally equivalent, however, we can now run into an use after free scenario after unbinding then rebinding the switch driver.The use after free happens because we do correctly initialize
dst->cpu_switchthe first time we probe indsa_cpu_parse(), then we unbind the driver:dsa_dst_unapply()is called, and we rebind again.dst->cpu_switchnow points to a freed "ds" structure, and so when we finally dereference it indsa_cpu_port_ethtool_setup(), we oops.
Quoted 6 lines
To fix this, simply set
dst->cpu_switchto NULL indsa_dst_unapply()which guarantees that we always correctly re-assigndst->cpu_switchindsa_cpu_parse().Fixes 9520ed8fb841 ("net: dsa: use cpu_switch instead of ds[0]")Signed-off-by Florian Fainelli <[email protected]>
Thanks!
Vivien
David Miller
maintainerFrom: Florian Fainelli <[email protected]>
Date: Fri, 2 Jun 2017 22:05:23 -0700
Quoted 11 lines
Commit
9520ed8fb841("net: dsa: usecpu_switchinstead of ds[0]") replaced the use of dst->ds[0] withdst->cpu_switchsince that is functionally equivalent, however, we can now run into an use after free scenario after unbinding then rebinding the switch driver.The use after free happens because we do correctly initialize
dst->cpu_switchthe first time we probe indsa_cpu_parse(), then we unbind the driver:dsa_dst_unapply()is called, and we rebind again.dst->cpu_switchnow points to a freed "ds" structure, and so when we finally dereference it indsa_cpu_port_ethtool_setup(), we oops.
Quoted 6 lines
To fix this, simply set
dst->cpu_switchto NULL indsa_dst_unapply()which guarantees that we always correctly re-assigndst->cpu_switchindsa_cpu_parse().Fixes 9520ed8fb841 ("net: dsa: use cpu_switch instead of ds[0]")Signed-off-by Florian Fainelli <[email protected]>
Applied and queued up for -stable, thanks.