Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Coverity fixes #34037

Merged
merged 4 commits into from
Apr 20, 2021
Merged

Network Coverity fixes #34037

merged 4 commits into from
Apr 20, 2021

Conversation

jukkar
Copy link
Member

@jukkar jukkar commented Apr 6, 2021

Misc Coverity fixes

Fixes #34000
Fixes #34003
Fixes #34006

jukkar added 4 commits April 6, 2021 13:55
The iface pointer might be null so check it before access.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Do not try to set or get the interface MTU if the interface
pointer is NULL.

Coverity-CID: 220541
Fixes zephyrproject-rtos#34000

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Simplifying the loop in order to remove dead code (ctx_up is
always NULL after the slist loop.

Coverity-CID: 220538
Fixes zephyrproject-rtos#34003

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
The virtual_iface is already NULL checked by net_if_get_by_iface()
at the beginning of the function so no need to do it here too.

Coverity-CID: 220535
Fixes zephyrproject-rtos#34006

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
@jukkar jukkar requested review from rlubos and mniestroj April 6, 2021 10:57
@github-actions github-actions bot added area: API Changes to public APIs area: Networking labels Apr 6, 2021
@zephyrbot zephyrbot requested a review from mengxianglinx April 6, 2021 13:24
Copy link
Member

@mniestroj mniestroj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that commits:

  • net: if: Check null pointer when settings flags
  • net: if: Check iface before setting/getting interface MTU

are a good idea. I think that we should not allow (not handle) NULL iface passed to those APIs.

Comment on lines 783 to 790
static inline uint16_t net_if_get_mtu(struct net_if *iface)
{
if (iface == NULL) {
return 0U;
}

return iface->if_dev->mtu;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this fixes #34000. It might silence Coverity warning for some reason, but I don't see direct connection between this change and reported Coverity warning.

Additionally I think that net_if_get_mtu() should be used only on valid (non NULL) iface pointers. I don't see the reason why this API should be NULL "friendly" while other APIs not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this fixes #34000. It might silence Coverity warning for some reason, but I don't see direct connection between this change and reported Coverity warning.

It fixes the issue because the mtu function now checks the NULL and does not crash.

The NULL check in net_if_flag_is_set() is a valid issue as I saw a crash when testing these. Adding the checks to MTU functions are useful to have as these could be called by the application and we are not calling these functions constantly so there is no performance concerns. IMHO we should add more NULL checks in various network functions as the asserts are not enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not getting back to this PR for so long.

The NULL check in net_if_flag_is_set() is a valid issue as I saw a crash when testing these.

That is one point of view. I see it in a bit different way, i.e. any crash is a result of bug, which is not checking for iface != NULL. Such crash is quite easy to debug and fix, compared to continuing execution with buggy logic.

IMHO we should add more NULL checks in various network functions as the asserts are not enough.

Why are asserts not enough? Because they are not compiled in by default?

Simply checking for NULL parameters and returning arbitrary values in case of NULL doesn't solve all issues. It solves crashes in net_if code (like in net_if_flag_is_set()), but it doesn't make higher level code that executes net_if functions any better. Functions like net_if_flag_is_set() are in most cases called with other net_if functions. Should we check iface argument in all net_if functions? Additionally net_if_flag_is_set(NULL, flag) == 0 logic doesn't have to be always correct. Take into account this fragment:

if (!net_if_flag_is_set(iface, NET_IF_UP) ||
net_if_flag_is_set(iface, NET_IF_SUSPENDED)) {
/* Drop packet if interface is not up */
NET_WARN("iface %p is down", iface);
verdict = NET_DROP;
status = -ENETDOWN;
goto done;
}

If someone will use if (!net_if_flag_is_set(iface, FLAG)) (notice negation !), then we need to make sure to not dereference iface later on, as it might still be NULL. I think that it is easier to make sure iface != NULL from the beginning and leave net_if_flag_is_set() API as is, without adding NULL check resulting in (slightly, but still) increased flash size and (small) performance penalty. IMO crashes because of NULL pointer dereferencing are often easier to debug than some issues in logic, which could happen after treating NULL in functions like net_if_flag_is_set() as a "valid" interface.

@galak galak requested a review from mniestroj April 9, 2021 10:50
@nashif nashif merged commit d732115 into zephyrproject-rtos:master Apr 20, 2021
@jukkar jukkar deleted the coverity branch April 26, 2021 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Networking
Projects
None yet
4 participants