diff mbox series

[net-next] ethtool: plca: fix plca enable data type while parsing the value

Message ID 20230831104523.7178-1-Parthiban.Veerasooran@microchip.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] ethtool: plca: fix plca enable data type while parsing the value | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1332 this patch: 1332
netdev/cc_maintainers fail 1 blamed authors not CCed: andrew@lunn.ch; 2 maintainers not CCed: pabeni@redhat.com andrew@lunn.ch
netdev/build_clang success Errors and warnings before: 1353 this patch: 1353
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: 1355 this patch: 1355
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 52 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Parthiban Veerasooran Aug. 31, 2023, 10:45 a.m. UTC
The ETHTOOL_A_PLCA_ENABLED data type is u8. But while parsing the
value from the attribute, nla_get_u32() is used in the plca_update_sint()
function instead of nla_get_u8(). So plca_cfg.enabled variable is updated
with some garbage value instead of 0 or 1 and always enables plca even
though plca is disabled through ethtool application. This bug has been
fixed by implementing plca_update_sint_from_u8() function which uses
nla_get_u8() function to extract the plca_cfg.enabled value and the
function plca_update_sint_from_u32() is used for extracting the other
values using nla_get_u32() function.

Fixes: 8580e16c28f3 ("net/ethtool: add netlink interface for the PLCA RS")
Signed-off-by: Parthiban Veerasooran <Parthiban.Veerasooran@microchip.com>
---
 net/ethtool/plca.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

Comments

Jakub Kicinski Sept. 5, 2023, 10:15 p.m. UTC | #1
On Thu, 31 Aug 2023 16:15:23 +0530 Parthiban Veerasooran wrote:
> Subject: [PATCH net-next] ethtool: plca: fix plca enable data type while parsing the value

Since this is a fix the subject should say net instead of net-next.
[PATCH net v2] for the next revision.

> The ETHTOOL_A_PLCA_ENABLED data type is u8. But while parsing the
> value from the attribute, nla_get_u32() is used in the plca_update_sint()
> function instead of nla_get_u8(). So plca_cfg.enabled variable is updated
> with some garbage value instead of 0 or 1 and always enables plca even
> though plca is disabled through ethtool application. This bug has been
> fixed by implementing plca_update_sint_from_u8() function which uses
> nla_get_u8() function to extract the plca_cfg.enabled value and the
> function plca_update_sint_from_u32() is used for extracting the other
> values using nla_get_u32() function.

Hm, yes, that seems like the best of the available options.

> Fixes: 8580e16c28f3 ("net/ethtool: add netlink interface for the PLCA RS")

Please make sure you CC all the people who signed off the commit under
Fixes. You missed pabeni@redhat.com and andrew@lunn.ch 

> Signed-off-by: Parthiban Veerasooran <Parthiban.Veerasooran@microchip.com>
> ---
>  net/ethtool/plca.c | 34 ++++++++++++++++++++++++----------
>  1 file changed, 24 insertions(+), 10 deletions(-)
> 
> diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c
> index b238a1afe9ae..b4e80dd33590 100644
> --- a/net/ethtool/plca.c
> +++ b/net/ethtool/plca.c
> @@ -21,8 +21,8 @@ struct plca_reply_data {
>  #define PLCA_REPDATA(__reply_base) \
>  	container_of(__reply_base, struct plca_reply_data, base)
>  
> -static void plca_update_sint(int *dst, const struct nlattr *attr,
> -			     bool *mod)
> +static void plca_update_sint_from_u32(int *dst, const struct nlattr *attr,
> +				      bool *mod)
>  {
>  	if (!attr)
>  		return;
> @@ -31,6 +31,16 @@ static void plca_update_sint(int *dst, const struct nlattr *attr,
>  	*mod = true;
>  }
>  
> +static void plca_update_sint_from_u8(int *dst, const struct nlattr *attr,
> +				     bool *mod)
> +{
> +	if (!attr)
> +		return;
> +
> +	*dst = nla_get_u8(attr);
> +	*mod = true;
> +}
> +
>  // PLCA get configuration message ------------------------------------------- //
>  
>  const struct nla_policy ethnl_plca_get_cfg_policy[] = {
> @@ -144,14 +154,18 @@ ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
>  		return -EOPNOTSUPP;
>  
>  	memset(&plca_cfg, 0xff, sizeof(plca_cfg));
> -	plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod);
> -	plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod);
> -	plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
> -	plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod);
> -	plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT],
> -			 &mod);
> -	plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR],
> -			 &mod);
> +	plca_update_sint_from_u8(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED],
> +				 &mod);
> +	plca_update_sint_from_u32(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID],
> +				  &mod);
> +	plca_update_sint_from_u32(&plca_cfg.node_cnt,
> +				  tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
> +	plca_update_sint_from_u32(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR],
> +				  &mod);
> +	plca_update_sint_from_u32(&plca_cfg.burst_cnt,
> +				  tb[ETHTOOL_A_PLCA_BURST_CNT], &mod);
> +	plca_update_sint_from_u32(&plca_cfg.burst_tmr,
> +				  tb[ETHTOOL_A_PLCA_BURST_TMR], &mod);

This looks error prone. We still need to maintain the types in multiple
places. How about we use the policy to decide the type? Something along
the lines of (untested):

diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c
index b238a1afe9ae..aed30665e9c0 100644
--- a/net/ethtool/plca.c
+++ b/net/ethtool/plca.c
@@ -21,16 +21,6 @@ struct plca_reply_data {
 #define PLCA_REPDATA(__reply_base) \
 	container_of(__reply_base, struct plca_reply_data, base)
 
-static void plca_update_sint(int *dst, const struct nlattr *attr,
-			     bool *mod)
-{
-	if (!attr)
-		return;
-
-	*dst = nla_get_u32(attr);
-	*mod = true;
-}
-
 // PLCA get configuration message ------------------------------------------- //
 
 const struct nla_policy ethnl_plca_get_cfg_policy[] = {
@@ -125,6 +115,29 @@ const struct nla_policy ethnl_plca_set_cfg_policy[] = {
 	[ETHTOOL_A_PLCA_BURST_TMR]	= NLA_POLICY_MAX(NLA_U32, 255),
 };
 
+static void
+plca_update_sint(int *dst, const struct nlattr **tb, u32 attrid, bool *mod)
+{
+	const struct nlattr *attr = tb[attrid];
+
+	if (!attr ||
+	    WARN_ON_ONCE(attrid >= ARRAY_SIZE(ethnl_plca_set_cfg_policy)))
+		return;
+
+	switch (ethnl_plca_set_cfg_policy[attrid].type) {
+	case NLA_U8:
+		*dst = nla_get_u8(attr);
+		break;
+	case NLA_U32:
+		*dst = nla_get_u32(attr);
+		break;
+	default:
+		WARN_ON_ONCE(1);
+	}
+
+	*mod = true;
+}
+
 static int
 ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
 {
@@ -144,13 +157,13 @@ ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
 		return -EOPNOTSUPP;
 
 	memset(&plca_cfg, 0xff, sizeof(plca_cfg));
-	plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod);
-	plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod);
-	plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
-	plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod);
-	plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT],
+	plca_update_sint(&plca_cfg.enabled, tb, ETHTOOL_A_PLCA_ENABLED, &mod);
+	plca_update_sint(&plca_cfg.node_id, tb, ETHTOOL_A_PLCA_NODE_ID, &mod);
+	plca_update_sint(&plca_cfg.node_cnt, tb, ETHTOOL_A_PLCA_NODE_CNT, &mod);
+	plca_update_sint(&plca_cfg.to_tmr, tb, ETHTOOL_A_PLCA_TO_TMR, &mod);
+	plca_update_sint(&plca_cfg.burst_cnt, tb, ETHTOOL_A_PLCA_BURST_CNT,
 			 &mod);
-	plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR],
+	plca_update_sint(&plca_cfg.burst_tmr, tb, ETHTOOL_A_PLCA_BURST_TMR,
 			 &mod);
 	if (!mod)
 		return 0;
Parthiban Veerasooran Sept. 7, 2023, 1:38 p.m. UTC | #2
Hi Jakub,

On 06/09/23 3:45 am, Jakub Kicinski wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> On Thu, 31 Aug 2023 16:15:23 +0530 Parthiban Veerasooran wrote:
>> Subject: [PATCH net-next] ethtool: plca: fix plca enable data type while parsing the value
> 
> Since this is a fix the subject should say net instead of net-next.
> [PATCH net v2] for the next revision.
Sure, will correct it in the v2.
> 
>> The ETHTOOL_A_PLCA_ENABLED data type is u8. But while parsing the
>> value from the attribute, nla_get_u32() is used in the plca_update_sint()
>> function instead of nla_get_u8(). So plca_cfg.enabled variable is updated
>> with some garbage value instead of 0 or 1 and always enables plca even
>> though plca is disabled through ethtool application. This bug has been
>> fixed by implementing plca_update_sint_from_u8() function which uses
>> nla_get_u8() function to extract the plca_cfg.enabled value and the
>> function plca_update_sint_from_u32() is used for extracting the other
>> values using nla_get_u32() function.
> 
> Hm, yes, that seems like the best of the available options.
> 
>> Fixes: 8580e16c28f3 ("net/ethtool: add netlink interface for the PLCA RS")
> 
> Please make sure you CC all the people who signed off the commit under
> Fixes. You missed pabeni@redhat.com and andrew@lunn.ch
Ah yes, will include them in the v2.
> 
>> Signed-off-by: Parthiban Veerasooran <Parthiban.Veerasooran@microchip.com>
>> ---
>>   net/ethtool/plca.c | 34 ++++++++++++++++++++++++----------
>>   1 file changed, 24 insertions(+), 10 deletions(-)
>>
>> diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c
>> index b238a1afe9ae..b4e80dd33590 100644
>> --- a/net/ethtool/plca.c
>> +++ b/net/ethtool/plca.c
>> @@ -21,8 +21,8 @@ struct plca_reply_data {
>>   #define PLCA_REPDATA(__reply_base) \
>>        container_of(__reply_base, struct plca_reply_data, base)
>>
>> -static void plca_update_sint(int *dst, const struct nlattr *attr,
>> -                          bool *mod)
>> +static void plca_update_sint_from_u32(int *dst, const struct nlattr *attr,
>> +                                   bool *mod)
>>   {
>>        if (!attr)
>>                return;
>> @@ -31,6 +31,16 @@ static void plca_update_sint(int *dst, const struct nlattr *attr,
>>        *mod = true;
>>   }
>>
>> +static void plca_update_sint_from_u8(int *dst, const struct nlattr *attr,
>> +                                  bool *mod)
>> +{
>> +     if (!attr)
>> +             return;
>> +
>> +     *dst = nla_get_u8(attr);
>> +     *mod = true;
>> +}
>> +
>>   // PLCA get configuration message ------------------------------------------- //
>>
>>   const struct nla_policy ethnl_plca_get_cfg_policy[] = {
>> @@ -144,14 +154,18 @@ ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
>>                return -EOPNOTSUPP;
>>
>>        memset(&plca_cfg, 0xff, sizeof(plca_cfg));
>> -     plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod);
>> -     plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod);
>> -     plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
>> -     plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod);
>> -     plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT],
>> -                      &mod);
>> -     plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR],
>> -                      &mod);
>> +     plca_update_sint_from_u8(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED],
>> +                              &mod);
>> +     plca_update_sint_from_u32(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID],
>> +                               &mod);
>> +     plca_update_sint_from_u32(&plca_cfg.node_cnt,
>> +                               tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
>> +     plca_update_sint_from_u32(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR],
>> +                               &mod);
>> +     plca_update_sint_from_u32(&plca_cfg.burst_cnt,
>> +                               tb[ETHTOOL_A_PLCA_BURST_CNT], &mod);
>> +     plca_update_sint_from_u32(&plca_cfg.burst_tmr,
>> +                               tb[ETHTOOL_A_PLCA_BURST_TMR], &mod);
> 
> This looks error prone. We still need to maintain the types in multiple
> places. How about we use the policy to decide the type? Something along
> the lines of (untested):
Hmm ok, I compiled and tested your proposal. Below is the minor change 
oberved during compilation.
> 
> diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c
> index b238a1afe9ae..aed30665e9c0 100644
> --- a/net/ethtool/plca.c
> +++ b/net/ethtool/plca.c
> @@ -21,16 +21,6 @@ struct plca_reply_data {
>   #define PLCA_REPDATA(__reply_base) \
>          container_of(__reply_base, struct plca_reply_data, base)
> 
> -static void plca_update_sint(int *dst, const struct nlattr *attr,
> -                            bool *mod)
> -{
> -       if (!attr)
> -               return;
> -
> -       *dst = nla_get_u32(attr);
> -       *mod = true;
> -}
> -
>   // PLCA get configuration message ------------------------------------------- //
> 
>   const struct nla_policy ethnl_plca_get_cfg_policy[] = {
> @@ -125,6 +115,29 @@ const struct nla_policy ethnl_plca_set_cfg_policy[] = {
>          [ETHTOOL_A_PLCA_BURST_TMR]      = NLA_POLICY_MAX(NLA_U32, 255),
>   };
> 
> +static void
> +plca_update_sint(int *dst, const struct nlattr **tb, u32 attrid, bool *mod)
plca_update_sint(int *dst, struct nlattr **tb, u32 attrid, bool *mod)

Will prepare and send the v2 with this proposal.

Best Regards,
Parthiban V

> +{
> +       const struct nlattr *attr = tb[attrid];
> +
> +       if (!attr ||
> +           WARN_ON_ONCE(attrid >= ARRAY_SIZE(ethnl_plca_set_cfg_policy)))
> +               return;
> +
> +       switch (ethnl_plca_set_cfg_policy[attrid].type) {
> +       case NLA_U8:
> +               *dst = nla_get_u8(attr);
> +               break;
> +       case NLA_U32:
> +               *dst = nla_get_u32(attr);
> +               break;
> +       default:
> +               WARN_ON_ONCE(1);
> +       }
> +
> +       *mod = true;
> +}
> +
>   static int
>   ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
>   {
> @@ -144,13 +157,13 @@ ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
>                  return -EOPNOTSUPP;
> 
>          memset(&plca_cfg, 0xff, sizeof(plca_cfg));
> -       plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod);
> -       plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod);
> -       plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
> -       plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod);
> -       plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT],
> +       plca_update_sint(&plca_cfg.enabled, tb, ETHTOOL_A_PLCA_ENABLED, &mod);
> +       plca_update_sint(&plca_cfg.node_id, tb, ETHTOOL_A_PLCA_NODE_ID, &mod);
> +       plca_update_sint(&plca_cfg.node_cnt, tb, ETHTOOL_A_PLCA_NODE_CNT, &mod);
> +       plca_update_sint(&plca_cfg.to_tmr, tb, ETHTOOL_A_PLCA_TO_TMR, &mod);
> +       plca_update_sint(&plca_cfg.burst_cnt, tb, ETHTOOL_A_PLCA_BURST_CNT,
>                           &mod);
> -       plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR],
> +       plca_update_sint(&plca_cfg.burst_tmr, tb, ETHTOOL_A_PLCA_BURST_TMR,
>                           &mod);
>          if (!mod)
>                  return 0;
>
diff mbox series

Patch

diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c
index b238a1afe9ae..b4e80dd33590 100644
--- a/net/ethtool/plca.c
+++ b/net/ethtool/plca.c
@@ -21,8 +21,8 @@  struct plca_reply_data {
 #define PLCA_REPDATA(__reply_base) \
 	container_of(__reply_base, struct plca_reply_data, base)
 
-static void plca_update_sint(int *dst, const struct nlattr *attr,
-			     bool *mod)
+static void plca_update_sint_from_u32(int *dst, const struct nlattr *attr,
+				      bool *mod)
 {
 	if (!attr)
 		return;
@@ -31,6 +31,16 @@  static void plca_update_sint(int *dst, const struct nlattr *attr,
 	*mod = true;
 }
 
+static void plca_update_sint_from_u8(int *dst, const struct nlattr *attr,
+				     bool *mod)
+{
+	if (!attr)
+		return;
+
+	*dst = nla_get_u8(attr);
+	*mod = true;
+}
+
 // PLCA get configuration message ------------------------------------------- //
 
 const struct nla_policy ethnl_plca_get_cfg_policy[] = {
@@ -144,14 +154,18 @@  ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
 		return -EOPNOTSUPP;
 
 	memset(&plca_cfg, 0xff, sizeof(plca_cfg));
-	plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod);
-	plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod);
-	plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
-	plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod);
-	plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT],
-			 &mod);
-	plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR],
-			 &mod);
+	plca_update_sint_from_u8(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED],
+				 &mod);
+	plca_update_sint_from_u32(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID],
+				  &mod);
+	plca_update_sint_from_u32(&plca_cfg.node_cnt,
+				  tb[ETHTOOL_A_PLCA_NODE_CNT], &mod);
+	plca_update_sint_from_u32(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR],
+				  &mod);
+	plca_update_sint_from_u32(&plca_cfg.burst_cnt,
+				  tb[ETHTOOL_A_PLCA_BURST_CNT], &mod);
+	plca_update_sint_from_u32(&plca_cfg.burst_tmr,
+				  tb[ETHTOOL_A_PLCA_BURST_TMR], &mod);
 	if (!mod)
 		return 0;