2018-03-14 14:05:16

by Alexander Potapenko

[permalink] [raw]
Subject: [PATCH] netlink: make sure nladdr has correct size in netlink_connect()

KMSAN reports use of uninitialized memory in the case when |alen| is
smaller than sizeof(struct netlink_sock), and therefore |nladdr| isn't
fully copied from the userspace.

Signed-off-by: Alexander Potapenko <[email protected]>
Fixes: 1da177e4c3f41524 ("Linux-2.6.12-rc2")
---
net/netlink/af_netlink.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 07e8478068f0..5d49b39e81c3 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -1085,6 +1085,9 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
if (addr->sa_family != AF_NETLINK)
return -EINVAL;

+ if (alen < sizeof(struct netlink_sock))
+ return -EINVAL;
+
if ((nladdr->nl_groups || nladdr->nl_pid) &&
!netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
return -EPERM;
--
2.16.2.804.g6dcf76e118-goog



2018-03-14 14:13:08

by Eric Dumazet

[permalink] [raw]
Subject: Re: [PATCH] netlink: make sure nladdr has correct size in netlink_connect()

On Wed, Mar 14, 2018 at 7:03 AM, Alexander Potapenko <[email protected]> wrote:
> KMSAN reports use of uninitialized memory in the case when |alen| is
> smaller than sizeof(struct netlink_sock), and therefore |nladdr| isn't
> fully copied from the userspace.
>
> Signed-off-by: Alexander Potapenko <[email protected]>
> Fixes: 1da177e4c3f41524 ("Linux-2.6.12-rc2")
> ---
> net/netlink/af_netlink.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index 07e8478068f0..5d49b39e81c3 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -1085,6 +1085,9 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
> if (addr->sa_family != AF_NETLINK)
> return -EINVAL;
>
> + if (alen < sizeof(struct netlink_sock))
> + return -EINVAL;
> +
>

Hmmm. How was this patch tested exactly ?

Thanks.

2018-03-14 14:41:46

by Eric Dumazet

[permalink] [raw]
Subject: Re: [PATCH] netlink: make sure nladdr has correct size in netlink_connect()

On Wed, Mar 14, 2018 at 7:16 AM, Alexander Potapenko <[email protected]> wrote:
>
>
>
> On Wed, Mar 14, 2018 at 3:11 PM Eric Dumazet <[email protected]> wrote:
>>
>> On Wed, Mar 14, 2018 at 7:03 AM, Alexander Potapenko <[email protected]>
>> wrote:
>> > KMSAN reports use of uninitialized memory in the case when |alen| is
>> > smaller than sizeof(struct netlink_sock), and therefore |nladdr| isn't
>> > fully copied from the userspace.
>> >
>> > Signed-off-by: Alexander Potapenko <[email protected]>
>> > Fixes: 1da177e4c3f41524 ("Linux-2.6.12-rc2")
>> > ---
>> > net/netlink/af_netlink.c | 3 +++
>> > 1 file changed, 3 insertions(+)
>> >
>> > diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
>> > index 07e8478068f0..5d49b39e81c3 100644
>> > --- a/net/netlink/af_netlink.c
>> > +++ b/net/netlink/af_netlink.c
>> > @@ -1085,6 +1085,9 @@ static int netlink_connect(struct socket *sock,
>> > struct sockaddr *addr,
>> > if (addr->sa_family != AF_NETLINK)
>> > return -EINVAL;
>> >
>> > + if (alen < sizeof(struct netlink_sock))
>> > + return -EINVAL;
>> > +
>> >
>>
>> Hmmm. How was this patch tested exactly ?
>
> You're absolutely right, I should have been using sizeof(sockaddr_nl).
> The reproducer that I used to trigger the bug was passing alen=2, so the
> patch still worked despite being incorrect.
> Is there any generic set of networking tests that I can use for such bugs?

There are upstream tests in tools/testing/selftests/

In your case, making sure tools like iproute2 ss are still working
would have done the job.