netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length
@ 2023-07-25  5:57 Lin Ma
  2023-07-25  7:30 ` Hangbin Liu
  2023-07-25 14:30 ` Nikolay Aleksandrov
  0 siblings, 2 replies; 6+ messages in thread
From: Lin Ma @ 2023-07-25  5:57 UTC (permalink / raw)
  To: davem, edumazet, kuba, pabeni, razor, idosch, lucien.xin,
	liuhangbin, edwin.peer, jiri, md.fahad.iqbal.polash,
	anirudh.venkataramanan, jeffrey.t.kirsher, neerav.parikh, netdev,
	linux-kernel
  Cc: Lin Ma

There are totally 9 ndo_bridge_setlink handlers in the current kernel,
which are 1) bnxt_bridge_setlink, 2) be_ndo_bridge_setlink 3)
i40e_ndo_bridge_setlink 4) ice_bridge_setlink 5)
ixgbe_ndo_bridge_setlink 6) mlx5e_bridge_setlink 7)
nfp_net_bridge_setlink 8) qeth_l2_bridge_setlink 9) br_setlink.

By investigating the code, we find that 1-7 parse and use nlattr
IFLA_BRIDGE_MODE but 3 and 4 forget to do the nla_len check. This can
lead to an out-of-attribute read and allow a malformed nlattr (e.g.,
length 0) to be viewed as a 2 byte integer.

To avoid such issues, also for other ndo_bridge_setlink handlers in the
future. This patch adds the nla_len check in rtnl_bridge_setlink and
does an early error return if length mismatches. To make it works, the
break is removed from the parsing for IFLA_BRIDGE_FLAGS to make sure
this nla_for_each_nested iterates every attribute.

Fixes: b1edc14a3fbf ("ice: Implement ice_bridge_getlink and ice_bridge_setlink")
Fixes: 51616018dd1b ("i40e: Add support for getlink, setlink ndo ops")
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Lin Ma <linma@zju.edu.cn>
---
V1 -> V2: removes the break in parsing for IFLA_BRIDGE_FLAGS suggested
          by Hangbin Liu <liuhangbin@gmail.com>

 net/core/rtnetlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 3ad4e030846d..aef25aa5cf1d 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -5140,13 +5140,17 @@ static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
 	if (br_spec) {
 		nla_for_each_nested(attr, br_spec, rem) {
-			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
+			if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) {
 				if (nla_len(attr) < sizeof(flags))
 					return -EINVAL;
 
 				have_flags = true;
 				flags = nla_get_u16(attr);
-				break;
+			}
+
+			if (nla_type(attr) == IFLA_BRIDGE_MODE) {
+				if (nla_len(attr) < sizeof(u16))
+					return -EINVAL;
 			}
 		}
 	}
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length
  2023-07-25  5:57 [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length Lin Ma
@ 2023-07-25  7:30 ` Hangbin Liu
  2023-07-25 14:30 ` Nikolay Aleksandrov
  1 sibling, 0 replies; 6+ messages in thread
From: Hangbin Liu @ 2023-07-25  7:30 UTC (permalink / raw)
  To: Lin Ma
  Cc: davem, edumazet, kuba, pabeni, razor, idosch, lucien.xin, jiri,
	anirudh.venkataramanan, netdev, linux-kernel

Hi Ma Lin,

Please add the target branch in your subject. e.g. [PATCHv2 net]

On Tue, Jul 25, 2023 at 01:57:06PM +0800, Lin Ma wrote:
> There are totally 9 ndo_bridge_setlink handlers in the current kernel,
> which are 1) bnxt_bridge_setlink, 2) be_ndo_bridge_setlink 3)
> i40e_ndo_bridge_setlink 4) ice_bridge_setlink 5)
> ixgbe_ndo_bridge_setlink 6) mlx5e_bridge_setlink 7)
> nfp_net_bridge_setlink 8) qeth_l2_bridge_setlink 9) br_setlink.
> 
> By investigating the code, we find that 1-7 parse and use nlattr
> IFLA_BRIDGE_MODE but 3 and 4 forget to do the nla_len check. This can
> lead to an out-of-attribute read and allow a malformed nlattr (e.g.,
> length 0) to be viewed as a 2 byte integer.
> 
> To avoid such issues, also for other ndo_bridge_setlink handlers in the
> future. This patch adds the nla_len check in rtnl_bridge_setlink and
> does an early error return if length mismatches. To make it works, the
> break is removed from the parsing for IFLA_BRIDGE_FLAGS to make sure
> this nla_for_each_nested iterates every attribute.

Since you have checked the length in rtnl_bridge_setlink(). Can we remove
the check in the driver handlers to avoid duplicate code? You can hold on
this update and see if others have different opinion.

Thanks
Hangbin

> 
> Fixes: b1edc14a3fbf ("ice: Implement ice_bridge_getlink and ice_bridge_setlink")
> Fixes: 51616018dd1b ("i40e: Add support for getlink, setlink ndo ops")
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Lin Ma <linma@zju.edu.cn>
> ---
> V1 -> V2: removes the break in parsing for IFLA_BRIDGE_FLAGS suggested
>           by Hangbin Liu <liuhangbin@gmail.com>
> 
>  net/core/rtnetlink.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
> index 3ad4e030846d..aef25aa5cf1d 100644
> --- a/net/core/rtnetlink.c
> +++ b/net/core/rtnetlink.c
> @@ -5140,13 +5140,17 @@ static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
>  	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
>  	if (br_spec) {
>  		nla_for_each_nested(attr, br_spec, rem) {
> -			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
> +			if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) {
>  				if (nla_len(attr) < sizeof(flags))
>  					return -EINVAL;
>  
>  				have_flags = true;
>  				flags = nla_get_u16(attr);
> -				break;
> +			}
> +
> +			if (nla_type(attr) == IFLA_BRIDGE_MODE) {
> +				if (nla_len(attr) < sizeof(u16))
> +					return -EINVAL;
>  			}
>  		}
>  	}
> -- 
> 2.17.1
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length
  2023-07-25  5:57 [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length Lin Ma
  2023-07-25  7:30 ` Hangbin Liu
@ 2023-07-25 14:30 ` Nikolay Aleksandrov
  2023-07-26  7:49   ` Lin Ma
  1 sibling, 1 reply; 6+ messages in thread
From: Nikolay Aleksandrov @ 2023-07-25 14:30 UTC (permalink / raw)
  To: Lin Ma, davem, edumazet, kuba, pabeni, idosch, lucien.xin,
	liuhangbin, edwin.peer, jiri, md.fahad.iqbal.polash,
	anirudh.venkataramanan, jeffrey.t.kirsher, neerav.parikh, netdev,
	linux-kernel

On 7/25/23 08:57, Lin Ma wrote:
> There are totally 9 ndo_bridge_setlink handlers in the current kernel,
> which are 1) bnxt_bridge_setlink, 2) be_ndo_bridge_setlink 3)
> i40e_ndo_bridge_setlink 4) ice_bridge_setlink 5)
> ixgbe_ndo_bridge_setlink 6) mlx5e_bridge_setlink 7)
> nfp_net_bridge_setlink 8) qeth_l2_bridge_setlink 9) br_setlink.
> 
> By investigating the code, we find that 1-7 parse and use nlattr
> IFLA_BRIDGE_MODE but 3 and 4 forget to do the nla_len check. This can
> lead to an out-of-attribute read and allow a malformed nlattr (e.g.,
> length 0) to be viewed as a 2 byte integer.
> 
> To avoid such issues, also for other ndo_bridge_setlink handlers in the
> future. This patch adds the nla_len check in rtnl_bridge_setlink and
> does an early error return if length mismatches. To make it works, the
> break is removed from the parsing for IFLA_BRIDGE_FLAGS to make sure
> this nla_for_each_nested iterates every attribute.
> 
> Fixes: b1edc14a3fbf ("ice: Implement ice_bridge_getlink and ice_bridge_setlink")
> Fixes: 51616018dd1b ("i40e: Add support for getlink, setlink ndo ops")
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Lin Ma <linma@zju.edu.cn>
> ---
> V1 -> V2: removes the break in parsing for IFLA_BRIDGE_FLAGS suggested
>            by Hangbin Liu <liuhangbin@gmail.com>
> 
>   net/core/rtnetlink.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
> index 3ad4e030846d..aef25aa5cf1d 100644
> --- a/net/core/rtnetlink.c
> +++ b/net/core/rtnetlink.c
> @@ -5140,13 +5140,17 @@ static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
>   	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
>   	if (br_spec) {
>   		nla_for_each_nested(attr, br_spec, rem) {
> -			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
> +			if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) {
>   				if (nla_len(attr) < sizeof(flags))
>   					return -EINVAL;
>   
>   				have_flags = true;
>   				flags = nla_get_u16(attr);
> -				break;
> +			}
> +
> +			if (nla_type(attr) == IFLA_BRIDGE_MODE) {
> +				if (nla_len(attr) < sizeof(u16))
> +					return -EINVAL;
>   			}
>   		}
>   	}

Patch looks good now, you should probably remove the extra checks done
by each driver that are now unnecessary (net-next material). As Hangbin
commented you should target this fix at -net, with that:

Acked-by: Nikolay Aleksandrov <razor@blackwall.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length
  2023-07-25 14:30 ` Nikolay Aleksandrov
@ 2023-07-26  7:49   ` Lin Ma
  2023-07-26 15:44     ` Jakub Kicinski
  0 siblings, 1 reply; 6+ messages in thread
From: Lin Ma @ 2023-07-26  7:49 UTC (permalink / raw)
  To: Nikolay Aleksandrov
  Cc: davem, edumazet, kuba, pabeni, idosch, lucien.xin, liuhangbin,
	edwin.peer, jiri, md.fahad.iqbal.polash, anirudh.venkataramanan,
	jeffrey.t.kirsher, neerav.parikh, netdev, linux-kernel

Hi Nikolay,

> 
> Patch looks good now, you should probably remove the extra checks done
> by each driver that are now unnecessary (net-next material). As Hangbin
> commented you should target this fix at -net, with that:
> 
> Acked-by: Nikolay Aleksandrov <razor@blackwall.org>

Cool, I agree with Hangbin that another patch which removes the redundant
checks in driver is needed.

But I have a simple question. I will send this patch to net one and another
to net-next one. How can I ensure the latter one depends on the former one?
Or should I send a patch series to net-next that contains the former one :)
(I currently choose the method 2 and please let me know if I do this wrong)

Regards
Lin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length
  2023-07-26  7:49   ` Lin Ma
@ 2023-07-26 15:44     ` Jakub Kicinski
  2023-07-27  0:03       ` Lin Ma
  0 siblings, 1 reply; 6+ messages in thread
From: Jakub Kicinski @ 2023-07-26 15:44 UTC (permalink / raw)
  To: Lin Ma
  Cc: Nikolay Aleksandrov, davem, edumazet, pabeni, idosch, lucien.xin,
	liuhangbin, edwin.peer, jiri, md.fahad.iqbal.polash,
	anirudh.venkataramanan, jeffrey.t.kirsher, neerav.parikh, netdev,
	linux-kernel

On Wed, 26 Jul 2023 15:49:02 +0800 (GMT+08:00) Lin Ma wrote:
> Cool, I agree with Hangbin that another patch which removes the redundant
> checks in driver is needed.
> 
> But I have a simple question. I will send this patch to net one and another
> to net-next one. How can I ensure the latter one depends on the former one?
> Or should I send a patch series to net-next that contains the former one :)
> (I currently choose the method 2 and please let me know if I do this wrong)

You'll need to wait for the patch to propagate before posting.
Our trees merge each Thursday, so if you post on Friday the fix
should be in net-next.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length
  2023-07-26 15:44     ` Jakub Kicinski
@ 2023-07-27  0:03       ` Lin Ma
  0 siblings, 0 replies; 6+ messages in thread
From: Lin Ma @ 2023-07-27  0:03 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: Nikolay Aleksandrov, davem, edumazet, pabeni, idosch, lucien.xin,
	liuhangbin, edwin.peer, jiri, md.fahad.iqbal.polash,
	anirudh.venkataramanan, jeffrey.t.kirsher, neerav.parikh, netdev,
	linux-kernel

Hi Jakub,

> 
> You'll need to wait for the patch to propagate before posting.
> Our trees merge each Thursday, so if you post on Friday the fix
> should be in net-next.

Cool, I understand now. Thanks!

Regards
Lin

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-07-27  0:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-25  5:57 [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length Lin Ma
2023-07-25  7:30 ` Hangbin Liu
2023-07-25 14:30 ` Nikolay Aleksandrov
2023-07-26  7:49   ` Lin Ma
2023-07-26 15:44     ` Jakub Kicinski
2023-07-27  0:03       ` Lin Ma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).