2019-10-12 14:54:15

by Markus Elfring

[permalink] [raw]
Subject: tcp: Checking a kmemdup() call in tcp_time_wait()

Hello,

I tried another script for the semantic patch language out.
This source code analysis approach points out that the implementation
of the function “tcp_time_wait” contains also a call of the function “kmemdup”.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/tcp_minisocks.c?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n306
https://elixir.bootlin.com/linux/v5.4-rc2/source/net/ipv4/tcp_minisocks.c#L306

* Do you find the usage of the macro call “BUG_ON” still appropriate at this place?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/checkpatch.pl?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n4080

* Is there a need to adjust the error handling here?

Regards,
Markus


2019-10-13 19:58:39

by Eric Dumazet

[permalink] [raw]
Subject: Re: tcp: Checking a kmemdup() call in tcp_time_wait()



On 10/12/19 7:51 AM, Markus Elfring wrote:
> Hello,
>
> I tried another script for the semantic patch language out.
> This source code analysis approach points out that the implementation
> of the function “tcp_time_wait” contains also a call of the function “kmemdup”.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/tcp_minisocks.c?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n306
> https://elixir.bootlin.com/linux/v5.4-rc2/source/net/ipv4/tcp_minisocks.c#L306
>
> * Do you find the usage of the macro call “BUG_ON” still appropriate at this place?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/checkpatch.pl?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n4080
>
> * Is there a need to adjust the error handling here?

Presumably the BUG would trigger if a really disturbing bug happened.

There is no chance a timewait socket could be created with a MD5 key,
if the established socket that is the 'parent' of the timewait
has not a MD5 context itself.

The parent socket only could have MD5 context if tcp_md5sig_pool_populated
could have been set to true.

Once tcp_md5sig_pool_populated is true it can never go back to false.

So the bug here would be that a socket had a successful MD5 context,
and following tcp_alloc_md5sig_pool() would return false.

We can discuss of all BUG() in general, some people simply disable
all of them (cf CONFIG_BUG), but this particular one does not seem
specially bad to me, compared to others.

2019-10-14 06:53:01

by Markus Elfring

[permalink] [raw]
Subject: Re: tcp: Checking a kmemdup() call in tcp_time_wait()

>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/tcp_minisocks.c?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n306
>> https://elixir.bootlin.com/linux/v5.4-rc2/source/net/ipv4/tcp_minisocks.c#L306

> Presumably the BUG would trigger if a really disturbing bug happened.

How “buggy” is this place if the function call “kmemdup” failed?

Can an other error reporting approach be nicer here?

Regards,
Markus

2019-10-14 13:03:17

by Eric Dumazet

[permalink] [raw]
Subject: Re: tcp: Checking a kmemdup() call in tcp_time_wait()



On 10/13/19 11:51 PM, Markus Elfring wrote:
>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/tcp_minisocks.c?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n306
>>> https://elixir.bootlin.com/linux/v5.4-rc2/source/net/ipv4/tcp_minisocks.c#L306
> …
>> Presumably the BUG would trigger if a really disturbing bug happened.
>
> How “buggy” is this place if the function call “kmemdup” failed?

It is not buggy. The BUG will not trigger.

BUG_ON(tcptw->tw_md5_key && !tcp_alloc_md5sig_pool());

This would be different if we had instead :

BUG_ON(!tcptw->tw_md5_key && !tcp_alloc_md5sig_pool());

>
> Can an other error reporting approach be nicer here?

There is no error reported if kmemdup() has failed.

timewait is best effort.

2019-10-14 13:06:07

by Markus Elfring

[permalink] [raw]
Subject: Re: tcp: Checking a kmemdup() call in tcp_time_wait()

>>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/tcp_minisocks.c?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n306
>>>> https://elixir.bootlin.com/linux/v5.4-rc2/source/net/ipv4/tcp_minisocks.c#L306


>> Can an other error reporting approach be nicer here?
>
> There is no error reported if kmemdup() has failed.

How do data from the Linux allocation failure report fit to this information?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=4f5cafb5cb8471e54afdc9054d973535614f7675#n878


> timewait is best effort.

How do you think about to return an error code like “-ENOMEM” at this place?

Regards,
Markus

2019-10-14 13:17:16

by Eric Dumazet

[permalink] [raw]
Subject: Re: tcp: Checking a kmemdup() call in tcp_time_wait()

On Mon, Oct 14, 2019 at 5:51 AM Markus Elfring <[email protected]> wrote:
>
> >>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/tcp_minisocks.c?id=1c0cc5f1ae5ee5a6913704c0d75a6e99604ee30a#n306
> >>>> https://elixir.bootlin.com/linux/v5.4-rc2/source/net/ipv4/tcp_minisocks.c#L306
> …
>
> >> Can an other error reporting approach be nicer here?
> >
> > There is no error reported if kmemdup() has failed.
>
> How do data from the Linux allocation failure report fit to this information?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=4f5cafb5cb8471e54afdc9054d973535614f7675#n878
>

This is coding style for newly submitted code.

We do not refactor code to the latest coding style, this would cost a lot.

Especially TCP stack that is quite often changed.

>
> > timewait is best effort.
>
> How do you think about to return an error code like “-ENOMEM” at this place?

tcp_time_wait() is void, the caller won't care. I told you time_wait
is best effort.

What is the problem you want to solve _exactly_ ?

Have you seen a real issue, or should you augment your static analyser
to not complain on :

ptr = kmemdup();
BUG_ON(<any condition>);

(<any condition> being different than (ptr == NULL))

I believe we have enough real bugs to fix.
I would prefer to not spend time arguing for every single BUG() or BUG_ON().

Thank you.

2019-10-14 17:25:16

by Markus Elfring

[permalink] [raw]
Subject: Re: tcp: Checking a kmemdup() call in tcp_time_wait()

> This is coding style for newly submitted code.
>
> We do not refactor code to the latest coding style, this would cost a lot.

Were any update candidates left over also in this function implementation?


>> How do you think about to return an error code like “-ENOMEM” at this place?
>
> tcp_time_wait() is void,

Can the function return type be eventually changed?


> the caller won't care.

Will any other software developers (and source code reviewers) start to
care more for unchecked function calls?


> I told you time_wait is best effort.

Can this approach still be improved another bit?


> What is the problem you want to solve _exactly_ ?

I became curious if the software situation can be adjusted around
a possibly ignored return value from a call of a function like kmemdup().

Regards,
Markus