diff mbox series

[net] ipv4: Check attribute length for RTA_GATEWAY

Message ID 20211211162148.74404-1-dsahern@kernel.org (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net] ipv4: Check attribute length for RTA_GATEWAY | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers fail 1 blamed authors not CCed: davem@davemloft.net; 3 maintainers not CCed: yoshfuji@linux-ipv6.org kuba@kernel.org davem@davemloft.net
netdev/build_clang success Errors and warnings before: 20 this patch: 20
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 4 this patch: 4
netdev/checkpatch warning WARNING: line length of 102 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

David Ahern Dec. 11, 2021, 4:21 p.m. UTC
syzbot reported uninit-value:
============================================================
  BUG: KMSAN: uninit-value in fib_get_nhs+0xac4/0x1f80
  net/ipv4/fib_semantics.c:708
   fib_get_nhs+0xac4/0x1f80 net/ipv4/fib_semantics.c:708
   fib_create_info+0x2411/0x4870 net/ipv4/fib_semantics.c:1453
   fib_table_insert+0x45c/0x3a10 net/ipv4/fib_trie.c:1224
   inet_rtm_newroute+0x289/0x420 net/ipv4/fib_frontend.c:886

Add length checking before using the attribute.

Fixes: 4e902c57417c ("[IPv4]: FIB configuration using struct fib_config")
Reported-by: syzbot+d4b9a2851cc3ce998741@syzkaller.appspotmail.com
Signed-off-by: David Ahern <dsahern@kernel.org>
Cc: Thomas Graf <tgraf@suug.ch>
---
I do not have KMSAN setup, so this is based on a code analysis. Before
4e902c57417c fib_get_attr32 was checking the attribute length; the
switch to nla_get_u32 does not.

 net/ipv4/fib_semantics.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Ido Schimmel Dec. 11, 2021, 7:13 p.m. UTC | #1
On Sat, Dec 11, 2021 at 09:21:48AM -0700, David Ahern wrote:
> syzbot reported uninit-value:
> ============================================================
>   BUG: KMSAN: uninit-value in fib_get_nhs+0xac4/0x1f80
>   net/ipv4/fib_semantics.c:708
>    fib_get_nhs+0xac4/0x1f80 net/ipv4/fib_semantics.c:708
>    fib_create_info+0x2411/0x4870 net/ipv4/fib_semantics.c:1453
>    fib_table_insert+0x45c/0x3a10 net/ipv4/fib_trie.c:1224
>    inet_rtm_newroute+0x289/0x420 net/ipv4/fib_frontend.c:886
> 
> Add length checking before using the attribute.
> 
> Fixes: 4e902c57417c ("[IPv4]: FIB configuration using struct fib_config")
> Reported-by: syzbot+d4b9a2851cc3ce998741@syzkaller.appspotmail.com
> Signed-off-by: David Ahern <dsahern@kernel.org>
> Cc: Thomas Graf <tgraf@suug.ch>
> ---
> I do not have KMSAN setup, so this is based on a code analysis. Before

Was using this in the past:
https://github.com/google/syzkaller/blob/master/docs/syzbot.md#kmsan-bugs

> 4e902c57417c fib_get_attr32 was checking the attribute length; the
> switch to nla_get_u32 does not.
> 
>  net/ipv4/fib_semantics.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
> index 3cad543dc747..930843ba3b17 100644
> --- a/net/ipv4/fib_semantics.c
> +++ b/net/ipv4/fib_semantics.c
> @@ -704,6 +704,10 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
>  				return -EINVAL;
>  			}
>  			if (nla) {
> +				if (nla_len(nla) < sizeof(__be32)) {
> +					NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
> +					return -EINVAL;
> +				}

Isn't the problem more general than that? It seems that there is no
minimum length validation to any of the attributes inside RTA_MULTIPATH.
Except maybe RTA_VIA

>  				fib_cfg.fc_gw4 = nla_get_in_addr(nla);
>  				if (fib_cfg.fc_gw4)
>  					fib_cfg.fc_gw_family = AF_INET;
> -- 
> 2.24.3 (Apple Git-128)
>
David Ahern Dec. 11, 2021, 10:13 p.m. UTC | #2
On 12/11/21 12:13 PM, Ido Schimmel wrote:
> On Sat, Dec 11, 2021 at 09:21:48AM -0700, David Ahern wrote:
>> syzbot reported uninit-value:
>> ============================================================
>>   BUG: KMSAN: uninit-value in fib_get_nhs+0xac4/0x1f80
>>   net/ipv4/fib_semantics.c:708
>>    fib_get_nhs+0xac4/0x1f80 net/ipv4/fib_semantics.c:708
>>    fib_create_info+0x2411/0x4870 net/ipv4/fib_semantics.c:1453
>>    fib_table_insert+0x45c/0x3a10 net/ipv4/fib_trie.c:1224
>>    inet_rtm_newroute+0x289/0x420 net/ipv4/fib_frontend.c:886
>>
>> Add length checking before using the attribute.
>>
>> Fixes: 4e902c57417c ("[IPv4]: FIB configuration using struct fib_config")
>> Reported-by: syzbot+d4b9a2851cc3ce998741@syzkaller.appspotmail.com
>> Signed-off-by: David Ahern <dsahern@kernel.org>
>> Cc: Thomas Graf <tgraf@suug.ch>
>> ---
>> I do not have KMSAN setup, so this is based on a code analysis. Before
> 
> Was using this in the past:
> https://github.com/google/syzkaller/blob/master/docs/syzbot.md#kmsan-bugs

thanks for the pointer. I am a bit hardware challenged at the moment for
out of tree features.

> 
>> 4e902c57417c fib_get_attr32 was checking the attribute length; the
>> switch to nla_get_u32 does not.
>>
>>  net/ipv4/fib_semantics.c | 4 ++++
>>  1 file changed, 4 insertions(+)
>>
>> diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
>> index 3cad543dc747..930843ba3b17 100644
>> --- a/net/ipv4/fib_semantics.c
>> +++ b/net/ipv4/fib_semantics.c
>> @@ -704,6 +704,10 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
>>  				return -EINVAL;
>>  			}
>>  			if (nla) {
>> +				if (nla_len(nla) < sizeof(__be32)) {
>> +					NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
>> +					return -EINVAL;
>> +				}
> 
> Isn't the problem more general than that? It seems that there is no
> minimum length validation to any of the attributes inside RTA_MULTIPATH.
> Except maybe RTA_VIA
> 

A follow up commit is needed for RTA_FLOW.
Ido Schimmel Dec. 12, 2021, 8:09 a.m. UTC | #3
On Sat, Dec 11, 2021 at 03:13:02PM -0700, David Ahern wrote:
> On 12/11/21 12:13 PM, Ido Schimmel wrote:
> > On Sat, Dec 11, 2021 at 09:21:48AM -0700, David Ahern wrote:
> >> syzbot reported uninit-value:
> >> ============================================================
> >>   BUG: KMSAN: uninit-value in fib_get_nhs+0xac4/0x1f80
> >>   net/ipv4/fib_semantics.c:708
> >>    fib_get_nhs+0xac4/0x1f80 net/ipv4/fib_semantics.c:708
> >>    fib_create_info+0x2411/0x4870 net/ipv4/fib_semantics.c:1453
> >>    fib_table_insert+0x45c/0x3a10 net/ipv4/fib_trie.c:1224
> >>    inet_rtm_newroute+0x289/0x420 net/ipv4/fib_frontend.c:886
> >>
> >> Add length checking before using the attribute.
> >>
> >> Fixes: 4e902c57417c ("[IPv4]: FIB configuration using struct fib_config")
> >> Reported-by: syzbot+d4b9a2851cc3ce998741@syzkaller.appspotmail.com
> >> Signed-off-by: David Ahern <dsahern@kernel.org>
> >> Cc: Thomas Graf <tgraf@suug.ch>
> >> ---
> >> I do not have KMSAN setup, so this is based on a code analysis. Before
> > 
> > Was using this in the past:
> > https://github.com/google/syzkaller/blob/master/docs/syzbot.md#kmsan-bugs
> 
> thanks for the pointer. I am a bit hardware challenged at the moment for
> out of tree features.
> 
> > 
> >> 4e902c57417c fib_get_attr32 was checking the attribute length; the
> >> switch to nla_get_u32 does not.
> >>
> >>  net/ipv4/fib_semantics.c | 4 ++++
> >>  1 file changed, 4 insertions(+)
> >>
> >> diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
> >> index 3cad543dc747..930843ba3b17 100644
> >> --- a/net/ipv4/fib_semantics.c
> >> +++ b/net/ipv4/fib_semantics.c
> >> @@ -704,6 +704,10 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
> >>  				return -EINVAL;
> >>  			}
> >>  			if (nla) {
> >> +				if (nla_len(nla) < sizeof(__be32)) {
> >> +					NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
> >> +					return -EINVAL;
> >> +				}
> > 
> > Isn't the problem more general than that? It seems that there is no
> > minimum length validation to any of the attributes inside RTA_MULTIPATH.
> > Except maybe RTA_VIA
> > 
> 
> A follow up commit is needed for RTA_FLOW.

The same problem seems to exists in the delete path and IPv6. See
fib_nh_match() and ip6_route_multipath_add(), for example.
diff mbox series

Patch

diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index 3cad543dc747..930843ba3b17 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -704,6 +704,10 @@  static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
 				return -EINVAL;
 			}
 			if (nla) {
+				if (nla_len(nla) < sizeof(__be32)) {
+					NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
+					return -EINVAL;
+				}
 				fib_cfg.fc_gw4 = nla_get_in_addr(nla);
 				if (fib_cfg.fc_gw4)
 					fib_cfg.fc_gw_family = AF_INET;