net/sched: cls_u32: Fix reference counter leak leading to overflow
Subsystems (3)
PATCH 1/1 net/sched: cls_u32: Fix reference counter leak leading to overflow
In the event of a failure in tcf_change_indev(), u32_set_parms() will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.
--- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/sched/cls_u32.c +4 -1
@@ -762,8 +762,11 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,if (tb[TCA_U32_INDEV ]) {int ret; ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); -if (ret <0 ) +if (ret <0 ) { +if (tb[TCA_U32_LINK ]) + n->ht_down->refcnt--;return -EINVAL ; + } n->ifindex = ret; }return 0 ;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Quoted 8 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.Cc [email protected] # v4.14+
Please add a Fixes: tag.
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct tcf_proto *tp, · line 764
Quoted 13 lines
Signed-off-by Lee Jones <[email protected]>--- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/sched/cls_u32.c +0
-0@@ -762,8 +762,11 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,if (tb[TCA_U32_INDEV ]) {int ret; ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack);
This call should probably be done earlier in the function, next to tcf_exts_validate_ex()
Otherwise we might ask why the tcf_bind_filter() does not need to be undone.
Something like:
net/sched/cls_u32.c +0 -0
100644
net/sched/cls_u32.c +0 -0
@@ -718,13 +718,18 @@ static int u32_set_parms(struct net *net, struct
tcf_proto *tp,
struct nlattr *est, u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
{
- int err;
+ int err, ifindex = -1;
err = tcf_exts_validate_ex(net, tp, tb, est, &n->exts, flags,
fl_flags, extack);
if (err < 0)
return err;+ if (tb[TCA_U32_INDEV]) {
+ ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
+ if (ifindex < 0)
+ return -EINVAL;
+ }
if (tb[TCA_U32_LINK]) {
u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
struct tc_u_hnode *ht_down = NULL, *ht_old;
@@ -759,13 +764,9 @@ static int u32_set_parms(struct net *net, struct
tcf_proto *tp,
tcf_bind_filter(tp, &n->res, base);
}- if (tb[TCA_U32_INDEV]) {
- int ret; - ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack); - if (ret < 0)
- return -EINVAL;
- n->ifindex = ret;
- }
+ if (ifindex >= 0)
+ n->ifindex = ifindex;
+
return 0;
}
Jamal Hadi Salim
maintainerQuoted 65 lines · Eric Dumazet
Quoted 8 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.Cc [email protected] # v4.14+Please add a Fixes: tag.
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct tcf_proto *tp, · line 764
Quoted 13 lines
Signed-off-by Lee Jones <[email protected]>--- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/sched/cls_u32.c +0
-0@@ -762,8 +762,11 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,if (tb[TCA_U32_INDEV ]) {int ret; ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack);This call should probably be done earlier in the function, next to
tcf_exts_validate_ex()Otherwise we might ask why the
tcf_bind_filter()does not need to be undone.Something like:
net/sched/cls_u32.c +0
-0100644
net/sched/cls_u32.c +6
-1@@ -718,13 +718,18 @@ static int u32_set_parms(struct net *net, structtcf_proto *tp, struct nlattr *est, u32 flags, u32 fl_flags, struct netlink_ext_ack *extack) { - int err; + int err, ifindex = -1; err =tcf_exts_validate_ex (net, tp, tb, est, &n->exts, flags, fl_flags, extack);if (err <0 )return err; +if (tb[TCA_U32_INDEV ]) { + ifindex =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); +if (ifindex <0 ) +return -EINVAL ; + }if (tb[TCA_U32_LINK ]) {u32 handle =nla_get_u32 (tb[TCA_U32_LINK ]);struct tc_u_hnode *ht_down = NULL, *ht_old;net/sched/cls_u32.c +0
-0@@ -759,13 +764,9 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,tcf_bind_filter (tp, &n->res, base); }
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct · line 770
Quoted 10 lines
net/sched/cls_u32.c +3
-7-if (tb[TCA_U32_INDEV ]) { -int ret; - ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); -if (ret <0 ) -return -EINVAL ; - n->ifindex = ret; - } +if (ifindex >=0 ) + n->ifindex = ifindex; +
I guess we crossed paths ;->
Please, add a tdc test as well - it doesnt have to be in this patch, can be a followup.
cheers,
jamal
Quoted 2 lines
net/sched/cls_u32.c +0
-0return 0 ; }
Quoted 10 lines · Jamal Hadi Salim
Quoted 9 lines · Eric Dumazet
Quoted 7 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Cc [email protected] # v4.14+Please add a Fixes: tag.
Why?
From memory, I couldn't identify a specific commit to fix, which is why I used a Cc tag as per the Stable documentation:
Option 1
********
To have the patch automatically included in the stable tree, add the tag
.. code-block:: none
in the sign-off area. Once the patch is merged it will be applied to the stable tree without anything else needing to be done by the author or subsystem maintainer.
Quoted 39 lines
Quoted 39 lines
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct tcf_proto *tp, · line 764
Quoted 13 lines
Signed-off-by Lee Jones <[email protected]>--- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/sched/cls_u32.c +0
-0@@ -762,8 +762,11 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,if (tb[TCA_U32_INDEV ]) {int ret; ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack);This call should probably be done earlier in the function, next to
tcf_exts_validate_ex()Otherwise we might ask why the
tcf_bind_filter()does not need to be undone.Something like:
net/sched/cls_u32.c +0
-0100644
net/sched/cls_u32.c +1
-1@@ -718,13 +718,18 @@ static int u32_set_parms(struct net *net, structtcf_proto *tp, struct nlattr *est, u32 flags, u32 fl_flags, struct netlink_ext_ack *extack) { - int err; + int err, ifindex = -1; err =tcf_exts_validate_ex (net, tp, tb, est, &n->exts, flags, fl_flags, extack);if (err <0 )return err;
Quoted 5 lines
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct · line 733
Quoted 5 lines
net/sched/cls_u32.c +5
-0+if (tb[TCA_U32_INDEV ]) { + ifindex =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); +if (ifindex <0 ) +return -EINVAL ; + }
Thanks for the advice. Leave it with me.
Quoted 21 lines
Quoted 8 lines
net/sched/cls_u32.c +0
-0if (tb[TCA_U32_LINK ]) {u32 handle =nla_get_u32 (tb[TCA_U32_LINK ]);struct tc_u_hnode *ht_down = NULL, *ht_old;net/sched/cls_u32.c +0
-0@@ -759,13 +764,9 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,tcf_bind_filter (tp, &n->res, base); }net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct · line 770
Quoted 10 lines
net/sched/cls_u32.c +3
-7-if (tb[TCA_U32_INDEV ]) { -int ret; - ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); -if (ret <0 ) -return -EINVAL ; - n->ifindex = ret; - } +if (ifindex >=0 ) + n->ifindex = ifindex; +I guess we crossed paths ;->
Quoted 2 lines
Please, add a tdc test as well - it doesnt have to be in this patch, can be a followup.
I don't know how to do that, or even what a 'tdc' is. Is it trivial?
Can you point me towards the documentation please?
Signature · 1 line
Lee Jones [李琼斯]
Quoted 13 lines · Lee Jones
Quoted 10 lines · Jamal Hadi Salim
Quoted 9 lines · Eric Dumazet
Quoted 7 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Cc [email protected] # v4.14+Please add a Fixes: tag.
Why?
How have you identified v4.14+ ?
Probably you did some research/"git archeology".
By adding the Fixes: tag, you allow us to double check immediately, and see if other bugs need to be fixed at the same time.
You can also CC blamed patch authors, to get some feedback.
Otherwise, we (people reviewing this patch) have to also do this research from scratch.
In this case, it seems bug was added in
commit 705c7091262d02b09eb686c24491de61bf42fdb2
Author: Jiri Pirko <[email protected]> Date: Fri Aug 4 14:29:14 2017 +0200
net: sched: cls_u32: no need to call tcf_exts_change for newly
allocated struct
A nice Fixes: tag would then be
tcf_exts_change for newly allocated struct")
Thanks.
Quoted 38 lines · Eric Dumazet
Quoted 13 lines · Lee Jones
Quoted 10 lines · Jamal Hadi Salim
Quoted 9 lines · Eric Dumazet
Quoted 7 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Cc [email protected] # v4.14+Please add a Fixes: tag.
Why?
How have you identified v4.14+ ?
Probably you did some research/"git archeology".
By adding the Fixes: tag, you allow us to double check immediately, and see if other bugs need to be fixed at the same time.
You can also CC blamed patch authors, to get some feedback.
Otherwise, we (people reviewing this patch) have to also do this research from scratch.
In this case, it seems bug was added in
commit
705c7091262d02b09eb686c24491de61bf42fdb2Author: Jiri Pirko <[email protected]> Date: Fri Aug 4 14:29:14 2017 +0200
Quoted 8 lines
net: sched:
cls_u32: no need to calltcf_exts_changefor newly
allocated structA nice Fixes: tag would then be
Fixes 705c7091262d ("net: sched: cls_u32: no need to call
tcf_exts_changefor newly allocated struct")
Thanks for digging this out. I will add it.
Signature · 1 line
Lee Jones [李琼斯]
Jamal Hadi Salim
maintainerQuoted 103 lines · Lee Jones
Quoted 10 lines · Jamal Hadi Salim
Quoted 9 lines · Eric Dumazet
Quoted 7 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Cc [email protected] # v4.14+Please add a Fixes: tag.
Why?
From memory, I couldn't identify a specific commit to fix, which is why I used a Cc tag as per the Stable documentation:
Option 1
********To have the patch automatically included in the stable tree, add the tag
.. code-block:: none
in the sign-off area. Once the patch is merged it will be applied to the stable tree without anything else needing to be done by the author or subsystem maintainer.
Quoted 39 lines
Quoted 39 lines
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct tcf_proto *tp, · line 764
Quoted 13 lines
Signed-off-by Lee Jones <[email protected]>--- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/sched/cls_u32.c +0
-0@@ -762,8 +762,11 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,if (tb[TCA_U32_INDEV ]) {int ret; ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack);This call should probably be done earlier in the function, next to
tcf_exts_validate_ex()Otherwise we might ask why the
tcf_bind_filter()does not need to be undone.Something like:
net/sched/cls_u32.c +0
-0100644
net/sched/cls_u32.c +1
-1@@ -718,13 +718,18 @@ static int u32_set_parms(struct net *net, structtcf_proto *tp, struct nlattr *est, u32 flags, u32 fl_flags, struct netlink_ext_ack *extack) { - int err; + int err, ifindex = -1; err =tcf_exts_validate_ex (net, tp, tb, est, &n->exts, flags, fl_flags, extack);if (err <0 )return err;Quoted 5 lines
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct · line 733
Quoted 5 lines
net/sched/cls_u32.c +5
-0+if (tb[TCA_U32_INDEV ]) { + ifindex =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); +if (ifindex <0 ) +return -EINVAL ; + }Thanks for the advice. Leave it with me.
Quoted 20 lines
Quoted 8 lines
net/sched/cls_u32.c +0
-0if (tb[TCA_U32_LINK ]) {u32 handle =nla_get_u32 (tb[TCA_U32_LINK ]);struct tc_u_hnode *ht_down = NULL, *ht_old;net/sched/cls_u32.c +0
-0@@ -759,13 +764,9 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,tcf_bind_filter (tp, &n->res, base); }net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct · line 770
Quoted 10 lines
net/sched/cls_u32.c +3
-7-if (tb[TCA_U32_INDEV ]) { -int ret; - ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); -if (ret <0 ) -return -EINVAL ; - n->ifindex = ret; - } +if (ifindex >=0 ) + n->ifindex = ifindex; +I guess we crossed paths ;->
Quoted 6 lines
Quoted 2 lines
Please, add a tdc test as well - it doesnt have to be in this patch, can be a followup.
I don't know how to do that, or even what a 'tdc' is. Is it trivial?
Can you point me towards the documentation please?
There is a README in tools/testing/selftests/tc-testing/README If you created the scenario by running some tc command line it should not be difficult to create such a test. Or just tell us what command line you used to create it and we can help do one for you this time. If you found the issue by just eyeballing the code or syzkaller then just say that in the commit.
cheers,
jamal
Quoted 2 lines
--
Lee Jones [李琼斯]
Jamal Hadi Salim
maintainerQuoted 20 lines · Lee Jones
In the event of a failure in
tcf_change_indev(),u32_set_parms()will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'.Cc [email protected] # v4.14+Signed-off-by Lee Jones <[email protected]>--- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/sched/cls_u32.c +0
-0@@ -762,8 +762,11 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp,if (tb[TCA_U32_INDEV ]) {int ret;
net/sched/cls_u32.c static int u32_set_parms(struct net *net, struct tcf_proto *tp, · line 772
Quoted 10 lines
net/sched/cls_u32.c +4
-1ret =tcf_change_indev (net, tb[TCA_U32_INDEV ], extack); -if (ret <0 ) +if (ret <0 ) { +if (tb[TCA_U32_LINK ]) + n->ht_down->refcnt--;return -EINVAL ; + } n->ifindex = ret; }return 0 ;
The spirit of the patch looks right I dont think this fully solves the issue you state.
My suggestion: Move the if (tb[TCA_U32_INDEV]) above the if (tb[TCA_U32_LINK]) {
Did you see this in practice or you found it by eyeballing the code? Can you also add a tdc test for it? There are simple ways to create the scenario.
cheers,
jamal
Quoted 2 lines
2.41.0.rc0.172.g3f132b7071-goog