Skip to contentSkip to conversation

net: sched: fix possible refcount leak in tc_chain_tmplt_add()

PATCH Superseded Hangyu Hua 4 participants 7 messages Jun 5, 2023 – Jun 7, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH 1/1 net: sched: fix possible refcount leak in tc_chain_tmplt_add()

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Fixes 9f407f1768d3 ("net: sched: introduce chain templates")
---
 net/sched/cls_api.c | 1 +
 1 file changed, 1 insertion(+)
net/sched/cls_api.c +1 -0
index 2621550bfddc..92bfb892e638 100644
@@ -2952,6 +2952,7 @@ static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
 		return PTR_ERR(ops);
 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
+		module_put(ops->owner);
 		return -EOPNOTSUPP;
 	}
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Hangyu Hua

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Hi Hangyu Hua,

Is this correct even if try_module_get() is
not called via tcf_proto_lookup_ops() ?

Quoted 22 lines
Fixes 9f407f1768d3 ("net: sched: introduce chain templates")
---
 net/sched/cls_api.c | 1 +
 1 file changed, 1 insertion(+)
net/sched/cls_api.c +1 -0
index 2621550bfddc..92bfb892e638 100644
@@ -2952,6 +2952,7 @@ static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
 		return PTR_ERR(ops);
 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
+		module_put(ops->owner);
 		return -EOPNOTSUPP;
 	}
 
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 10 lines · Simon Horman
Quoted 3 lines · Hangyu Hua

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Hi Hangyu Hua,

Is this correct even if try_module_get() is
not called via tcf_proto_lookup_ops() ?

tcf_proto_lookup_ops will return error if try_module_get() is not called in tcf_proto_lookup_ops(). I am not sure what you mean?

Thanks,
Hangyu

Quoted 22 lines
Quoted 22 lines
Fixes 9f407f1768d3 ("net: sched: introduce chain templates")
---
  net/sched/cls_api.c | 1 +
  1 file changed, 1 insertion(+)
net/sched/cls_api.c +1 -0
index 2621550bfddc..92bfb892e638 100644
@@ -2952,6 +2952,7 @@ static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
  		return PTR_ERR(ops);
  	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
  		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
+		module_put(ops->owner);
  		return -EOPNOTSUPP;
  	}
 
Signature · 1 line
2.34.1
#4 lore raw
In reply to Hangyu Hua
Quoted 4 lines · Hangyu Hua

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Code seems reasonable. But commit message is pretty hard to understand. Please, replace "corresponding" with "required".
Also change the first sentence, do not use "can". From what I see, successful execution of tcf_proto_lookup_ops always means we now hold reference to module.

CC me in v2, I'll give you Reviewed-by.

Quoted 22 lines
Fixes 9f407f1768d3 ("net: sched: introduce chain templates")
---
 net/sched/cls_api.c | 1 +
 1 file changed, 1 insertion(+)
net/sched/cls_api.c +1 -0
index 2621550bfddc..92bfb892e638 100644
@@ -2952,6 +2952,7 @@ static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
 		return PTR_ERR(ops);
 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
+		module_put(ops->owner);
 		return -EOPNOTSUPP;
 	}
 
Signature · 1 line
2.34.1
#3 lore raw
In reply to Hangyu Hua
Quoted 6 lines · Hangyu Hua
Quoted 5 lines · Simon Horman
Quoted 3 lines · Hangyu Hua

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Quoted 8 lines
Quoted 5 lines

Hi Hangyu Hua,

Is this correct even if try_module_get() is
not called via tcf_proto_lookup_ops() ?

tcf_proto_lookup_ops will return error if try_module_get() is not called in tcf_proto_lookup_ops(). I am not sure what you mean?

Thanks,

I think that answers my question.

My concern was if there is a situation where module_put() is now called, but there was not in fact a reference that should be released.

...

maintainer
#6 lore raw
In reply to Hangyu Hua
Quoted 3 lines · Mon, Jun 05, 2023 at 09:01:58AM CEST, [email protected] wrote:

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Who's "we"? Use imperative mood. Tell the codebase what to do, what to change, etc.

Code-wise, this is fine. Please fix the patch description.

Quoted 22 lines
Fixes 9f407f1768d3 ("net: sched: introduce chain templates")

---
net/sched/cls_api.c | 1 +

1 file changed, 1 insertion(+)
net/sched/cls_api.c +1 -0
index 2621550bfddc..92bfb892e638 100644
@@ -2952,6 +2952,7 @@ static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
 		return PTR_ERR(ops);
 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
+		module_put(ops->owner);
 		return -EOPNOTSUPP;
 	}
 
Signature · 1 line
2.34.1
author
#5 lore raw
In reply to Larysa Zaremba
Quoted 6 lines · Larysa Zaremba
Quoted 4 lines · Hangyu Hua

try_module_get can be called in tcf_proto_lookup_ops. So if ops don't implement the corresponding function we should call module_put to drop the refcount.

Quoted 7 lines

Code seems reasonable. But commit message is pretty hard to understand. Please, replace "corresponding" with "required".
Also change the first sentence, do not use "can". From what I see, successful execution of tcf_proto_lookup_ops always means we now hold reference to module.

CC me in v2, I'll give you Reviewed-by.

I apologize for my incorrect English expression. I will send a v2 later.

Thanks,
Hangyu

Quoted 22 lines
Quoted 22 lines
Fixes 9f407f1768d3 ("net: sched: introduce chain templates")
---
  net/sched/cls_api.c | 1 +
  1 file changed, 1 insertion(+)
net/sched/cls_api.c +1 -0
index 2621550bfddc..92bfb892e638 100644
@@ -2952,6 +2952,7 @@ static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
  		return PTR_ERR(ops);
  	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
  		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
+		module_put(ops->owner);
  		return -EOPNOTSUPP;
  	}
 
Signature · 1 line
2.34.1