diff mbox series

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

Message ID 20230605070158.48403-1-hbh25y@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] net: sched: fix possible refcount leak in tc_chain_tmplt_add() | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 17 this patch: 17
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 17 this patch: 17
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 7 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Hangyu Hua June 5, 2023, 7:01 a.m. UTC
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")
Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
---
 net/sched/cls_api.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Simon Horman June 5, 2023, 12:31 p.m. UTC | #1
On Mon, Jun 05, 2023 at 03:01:58PM +0800, Hangyu Hua 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.

Hi Hangyu Hua,

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

> Fixes: 9f407f1768d3 ("net: sched: introduce chain templates")
> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> ---
>  net/sched/cls_api.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
> index 2621550bfddc..92bfb892e638 100644
> --- a/net/sched/cls_api.c
> +++ b/net/sched/cls_api.c
> @@ -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;
>  	}
>  
> -- 
> 2.34.1
> 
>
Hangyu Hua June 6, 2023, 2:13 a.m. UTC | #2
On 5/6/2023 20:31, Simon Horman wrote:
> On Mon, Jun 05, 2023 at 03:01:58PM +0800, Hangyu Hua 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.
> 
> 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

>> Fixes: 9f407f1768d3 ("net: sched: introduce chain templates")
>> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
>> ---
>>   net/sched/cls_api.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
>> index 2621550bfddc..92bfb892e638 100644
>> --- a/net/sched/cls_api.c
>> +++ b/net/sched/cls_api.c
>> @@ -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;
>>   	}
>>   
>> -- 
>> 2.34.1
>>
>>
Larysa Zaremba June 6, 2023, 8:53 a.m. UTC | #3
On Mon, Jun 05, 2023 at 03:01:58PM +0800, Hangyu Hua 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.
> 

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.

> Fixes: 9f407f1768d3 ("net: sched: introduce chain templates")
> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> ---
>  net/sched/cls_api.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
> index 2621550bfddc..92bfb892e638 100644
> --- a/net/sched/cls_api.c
> +++ b/net/sched/cls_api.c
> @@ -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;
>  	}
>  
> -- 
> 2.34.1
> 
>
Simon Horman June 6, 2023, 9:33 a.m. UTC | #4
On Tue, Jun 06, 2023 at 10:13:44AM +0800, Hangyu Hua wrote:
> On 5/6/2023 20:31, Simon Horman wrote:
> > On Mon, Jun 05, 2023 at 03:01:58PM +0800, Hangyu Hua 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.
> > 
> > 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.

...
Jiri Pirko June 6, 2023, 11:21 a.m. UTC | #5
Mon, Jun 05, 2023 at 09:01:58AM CEST, hbh25y@gmail.com 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.


>
>Fixes: 9f407f1768d3 ("net: sched: introduce chain templates")
>Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
>---
> net/sched/cls_api.c | 1 +
> 1 file changed, 1 insertion(+)
>
>diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
>index 2621550bfddc..92bfb892e638 100644
>--- a/net/sched/cls_api.c
>+++ b/net/sched/cls_api.c
>@@ -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;
> 	}
> 
>-- 
>2.34.1
>
Hangyu Hua June 7, 2023, 2:20 a.m. UTC | #6
On 6/6/2023 16:53, Larysa Zaremba wrote:
> On Mon, Jun 05, 2023 at 03:01:58PM +0800, Hangyu Hua 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.
>>
> 
> 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

>> Fixes: 9f407f1768d3 ("net: sched: introduce chain templates")
>> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
>> ---
>>   net/sched/cls_api.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
>> index 2621550bfddc..92bfb892e638 100644
>> --- a/net/sched/cls_api.c
>> +++ b/net/sched/cls_api.c
>> @@ -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;
>>   	}
>>   
>> -- 
>> 2.34.1
>>
>>
diff mbox series

Patch

diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
index 2621550bfddc..92bfb892e638 100644
--- a/net/sched/cls_api.c
+++ b/net/sched/cls_api.c
@@ -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;
 	}