All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] netlink: do not proceed if dump's start() errs
@ 2017-09-27 12:39 Jason A. Donenfeld
  2017-09-27 12:50 ` Jason A. Donenfeld
  0 siblings, 1 reply; 9+ messages in thread
From: Jason A. Donenfeld @ 2017-09-27 12:39 UTC (permalink / raw)
  To: davem, johannes.berg, netdev, linux-kernel; +Cc: Jason A. Donenfeld, stable

Drivers that use the start method for netlink dumping rely on dumpit not
being called if start fails. For example, ila_xlat.c allocates memory
and assigns it to cb->args[0] in its start() function. It might fail to
do that and return -ENOMEM instead. However, even when returning an
error, dumpit will be called, which, in the example above, quickly
dereferences the memory in cb->args[0], which will OOPS the kernel. This
is but one example of how this goes wrong.

Since start() has always been a function with an int return type, it
therefore makes sense to use it properly, rather than ignoring it. This
patch thus returns early and does not call dumpit() when start() fails.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: stable@vger.kernel.org
---
 net/netlink/af_netlink.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 327807731b44..be179876227d 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2270,8 +2270,11 @@ int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 
 	mutex_unlock(nlk->cb_mutex);
 
-	if (cb->start)
-		cb->start(cb);
+	if (cb->start) {
+		ret = cb->start(cb);
+		if (ret)
+			return ret;
+	}
 
 	ret = netlink_dump(sk);
 	sock_put(sk);
-- 
2.14.1

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

* Re: [PATCH] netlink: do not proceed if dump's start() errs
  2017-09-27 12:39 [PATCH] netlink: do not proceed if dump's start() errs Jason A. Donenfeld
@ 2017-09-27 12:50 ` Jason A. Donenfeld
  2017-09-27 13:05   ` Johannes Berg
  0 siblings, 1 reply; 9+ messages in thread
From: Jason A. Donenfeld @ 2017-09-27 12:50 UTC (permalink / raw)
  To: David Miller, johannes.berg, Netdev, LKML; +Cc: Jason A. Donenfeld, stable

On Wed, Sep 27, 2017 at 2:39 PM, Jason A. Donenfeld <Jason@zx2c4.com> wrote:
> -       if (cb->start)
> -               cb->start(cb);
> +       if (cb->start) {
> +               ret = cb->start(cb);
> +               if (ret)

I need to sock_put(sk); before returning. I'll fix this for v2, but
will for additional comments in case anybody has some.

> +                       return ret;
> +       }
>
>         ret = netlink_dump(sk);
>         sock_put(sk);

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

* Re: [PATCH] netlink: do not proceed if dump's start() errs
  2017-09-27 12:50 ` Jason A. Donenfeld
@ 2017-09-27 13:05   ` Johannes Berg
  2017-09-27 13:06     ` Jason A. Donenfeld
  0 siblings, 1 reply; 9+ messages in thread
From: Johannes Berg @ 2017-09-27 13:05 UTC (permalink / raw)
  To: Jason A. Donenfeld, David Miller, Netdev, LKML; +Cc: stable

On Wed, 2017-09-27 at 14:50 +0200, Jason A. Donenfeld wrote:
> On Wed, Sep 27, 2017 at 2:39 PM, Jason A. Donenfeld <Jason@zx2c4.com>
> wrote:
> > -       if (cb->start)
> > -               cb->start(cb);
> > +       if (cb->start) {
> > +               ret = cb->start(cb);
> > +               if (ret)
> 
> I need to sock_put(sk); before returning. I'll fix this for v2, but
> will for additional comments in case anybody has some.

I guess you could change it to

if (cb->start)
	ret = cb->start(cb);
if (!ret)
	ret = netlink_dump(sk);

johannes

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

* Re: [PATCH] netlink: do not proceed if dump's start() errs
  2017-09-27 13:05   ` Johannes Berg
@ 2017-09-27 13:06     ` Jason A. Donenfeld
  2017-09-27 22:41       ` [PATCH v2] " Jason A. Donenfeld
  0 siblings, 1 reply; 9+ messages in thread
From: Jason A. Donenfeld @ 2017-09-27 13:06 UTC (permalink / raw)
  To: Johannes Berg; +Cc: David Miller, Netdev, LKML, stable

On Wed, Sep 27, 2017 at 3:05 PM, Johannes Berg
<johannes@sipsolutions.net> wrote:
> I guess you could change it to
>
> if (cb->start)
>         ret = cb->start(cb);
> if (!ret)
>         ret = netlink_dump(sk);

Very clean. I'll do it like that. I'll wait a bit longer before
submitting v2, but beyond that, seems sane to you?

Jason

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

* [PATCH v2] netlink: do not proceed if dump's start() errs
  2017-09-27 13:06     ` Jason A. Donenfeld
@ 2017-09-27 22:41       ` Jason A. Donenfeld
  2017-09-28 10:40         ` Jason A. Donenfeld
                           ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Jason A. Donenfeld @ 2017-09-27 22:41 UTC (permalink / raw)
  To: davem, johannes.berg, netdev, linux-kernel
  Cc: Jason A. Donenfeld, Johannes Berg

Drivers that use the start method for netlink dumping rely on dumpit not
being called if start fails. For example, ila_xlat.c allocates memory
and assigns it to cb->args[0] in its start() function. It might fail to
do that and return -ENOMEM instead. However, even when returning an
error, dumpit will be called, which, in the example above, quickly
dereferences the memory in cb->args[0], which will OOPS the kernel. This
is but one example of how this goes wrong.

Since start() has always been a function with an int return type, it
therefore makes sense to use it properly, rather than ignoring it. This
patch thus returns early and does not call dumpit() when start() fails.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
---
 net/netlink/af_netlink.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 327807731b44..94c11cf0459d 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2270,10 +2270,13 @@ int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 
 	mutex_unlock(nlk->cb_mutex);
 
+	ret = 0;
 	if (cb->start)
-		cb->start(cb);
+		ret = cb->start(cb);
+
+	if (!ret)
+		ret = netlink_dump(sk);
 
-	ret = netlink_dump(sk);
 	sock_put(sk);
 
 	if (ret)
-- 
2.14.1

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

* Re: [PATCH v2] netlink: do not proceed if dump's start() errs
  2017-09-27 22:41       ` [PATCH v2] " Jason A. Donenfeld
@ 2017-09-28 10:40         ` Jason A. Donenfeld
  2017-09-30  6:27         ` David Miller
  2017-09-30  6:56         ` Johannes Berg
  2 siblings, 0 replies; 9+ messages in thread
From: Jason A. Donenfeld @ 2017-09-28 10:40 UTC (permalink / raw)
  To: davem, johannes.berg, netdev, linux-kernel; +Cc: Johannes Berg

On Thu, Sep 28, 2017 at 12:41:44AM +0200, Jason A. Donenfeld wrote:
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index 327807731b44..94c11cf0459d 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -2270,10 +2270,13 @@ int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
>  
>  	mutex_unlock(nlk->cb_mutex);
>  
> +	ret = 0;
>  	if (cb->start)
> -		cb->start(cb);
> +		ret = cb->start(cb);
> +
> +	if (!ret)
> +		ret = netlink_dump(sk);
>  
> -	ret = netlink_dump(sk);
>  	sock_put(sk);
>  
>  	if (ret)


FYI, using this horrific hack to currently work around bug:

    #define KERNEL_VERSION_THAT_HAS_NETLINK_START_FIX KERNEL_VERSION(4, 14, 0) /* Hopefully! */

    static int get(struct sk_buff *skb, struct netlink_callback *cb)
    {
    #if LINUX_VERSION_CODE < KERNEL_VERSION_THAT_HAS_NETLINK_START_FIX
        /* https://marc.info/?l=linux-netdev&m=150655213004221&w=2 */
        if (!cb->args[0]) {
            ret = get_start(cb);
            if (ret)
                return ret;
        }
    #endif
        ...
    }

    static const struct genl_ops genl_ops[] = {
        {
            .cmd = CMD_GET,
    #if LINUX_VERSION_CODE >= KERNEL_VERSION_THAT_HAS_NETLINK_START_FIX
            /* https://marc.info/?l=linux-netdev&m=150655213004221&w=2 */
            .start = get_start,
    #endif
            .dumpit = get,
            ...
        }
    }

Gross.

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

* Re: [PATCH v2] netlink: do not proceed if dump's start() errs
  2017-09-27 22:41       ` [PATCH v2] " Jason A. Donenfeld
  2017-09-28 10:40         ` Jason A. Donenfeld
@ 2017-09-30  6:27         ` David Miller
  2017-09-30  6:56         ` Johannes Berg
  2 siblings, 0 replies; 9+ messages in thread
From: David Miller @ 2017-09-30  6:27 UTC (permalink / raw)
  To: Jason; +Cc: johannes.berg, netdev, linux-kernel, johannes

From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Thu, 28 Sep 2017 00:41:44 +0200

> Drivers that use the start method for netlink dumping rely on dumpit not
> being called if start fails. For example, ila_xlat.c allocates memory
> and assigns it to cb->args[0] in its start() function. It might fail to
> do that and return -ENOMEM instead. However, even when returning an
> error, dumpit will be called, which, in the example above, quickly
> dereferences the memory in cb->args[0], which will OOPS the kernel. This
> is but one example of how this goes wrong.
> 
> Since start() has always been a function with an int return type, it
> therefore makes sense to use it properly, rather than ignoring it. This
> patch thus returns early and does not call dumpit() when start() fails.
> 
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> Cc: Johannes Berg <johannes@sipsolutions.net>

Johannes, this looks straightforward to me, but please would you give
it a quick review?

Thank you.

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

* Re: [PATCH v2] netlink: do not proceed if dump's start() errs
  2017-09-27 22:41       ` [PATCH v2] " Jason A. Donenfeld
  2017-09-28 10:40         ` Jason A. Donenfeld
  2017-09-30  6:27         ` David Miller
@ 2017-09-30  6:56         ` Johannes Berg
  2017-09-30 15:14           ` David Miller
  2 siblings, 1 reply; 9+ messages in thread
From: Johannes Berg @ 2017-09-30  6:56 UTC (permalink / raw)
  To: Jason A. Donenfeld, davem, netdev, linux-kernel

On Thu, 2017-09-28 at 00:41 +0200, Jason A. Donenfeld wrote:
> Drivers that use the start method for netlink dumping rely on dumpit
> not
> being called if start fails. For example, ila_xlat.c allocates memory
> and assigns it to cb->args[0] in its start() function. It might fail
> to
> do that and return -ENOMEM instead. However, even when returning an
> error, dumpit will be called, which, in the example above, quickly
> dereferences the memory in cb->args[0], which will OOPS the kernel.
> This
> is but one example of how this goes wrong.
> 
> Since start() has always been a function with an int return type, it
> therefore makes sense to use it properly, rather than ignoring it.
> This
> patch thus returns early and does not call dumpit() when start()
> fails.
> 
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

Reviewed-by: Johannes Berg <johannes@sipsolutions.net>


FWIW, I found another (indirect, via genetlink, like ila_xlat.c) in-
tree user that cares and expects the correct failure behaviour:

net/ipv6/seg6.c
	.start  = seg6_genl_dumphmac_start,

which can also have memory allocation failures. No others appear to
exist, afaict.

Either way, perhaps it's worth sending this to stable for that reason.

johannes

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

* Re: [PATCH v2] netlink: do not proceed if dump's start() errs
  2017-09-30  6:56         ` Johannes Berg
@ 2017-09-30 15:14           ` David Miller
  0 siblings, 0 replies; 9+ messages in thread
From: David Miller @ 2017-09-30 15:14 UTC (permalink / raw)
  To: johannes; +Cc: Jason, netdev, linux-kernel

From: Johannes Berg <johannes@sipsolutions.net>
Date: Sat, 30 Sep 2017 08:56:10 +0200

> On Thu, 2017-09-28 at 00:41 +0200, Jason A. Donenfeld wrote:
>> Drivers that use the start method for netlink dumping rely on dumpit
>> not
>> being called if start fails. For example, ila_xlat.c allocates memory
>> and assigns it to cb->args[0] in its start() function. It might fail
>> to
>> do that and return -ENOMEM instead. However, even when returning an
>> error, dumpit will be called, which, in the example above, quickly
>> dereferences the memory in cb->args[0], which will OOPS the kernel.
>> This
>> is but one example of how this goes wrong.
>> 
>> Since start() has always been a function with an int return type, it
>> therefore makes sense to use it properly, rather than ignoring it.
>> This
>> patch thus returns early and does not call dumpit() when start()
>> fails.
>> 
>> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> 
> Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
 ...
> Either way, perhaps it's worth sending this to stable for that reason.

Agreed, applied and queued up for -stable, thanks.

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

end of thread, other threads:[~2017-09-30 15:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-27 12:39 [PATCH] netlink: do not proceed if dump's start() errs Jason A. Donenfeld
2017-09-27 12:50 ` Jason A. Donenfeld
2017-09-27 13:05   ` Johannes Berg
2017-09-27 13:06     ` Jason A. Donenfeld
2017-09-27 22:41       ` [PATCH v2] " Jason A. Donenfeld
2017-09-28 10:40         ` Jason A. Donenfeld
2017-09-30  6:27         ` David Miller
2017-09-30  6:56         ` Johannes Berg
2017-09-30 15:14           ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.