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

sys/net/grnc: fix NULL ptr dereferencing #20660

Merged

Conversation

maribu
Copy link
Member

@maribu maribu commented May 8, 2024

Contribution description

This bug was spotted by GCC 14.1.0:

In file included from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-internal.h:27,
                 from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-arsm.h:33,
                 from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-6ln.h:30,
                 from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:28:
In function 'bf_isset',
    inlined from '_build_ext_opts' at /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:256:17,
    inlined from '_snd_ra' at /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:368:20,
    inlined from '_snd_rtr_advs' at /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:99:9:
/home/maribu/Repos/software/RIOT/master/sys/include/bitfield.h:130:18: error: array subscript 0 is outside array bounds of 'uint8_t[0]' {aka 'unsigned char[]'} [-Werror=array-bounds=]
  130 |     return (field[idx / 8] & (1u << (7 - (idx % 8))));
      |             ~~~~~^~~~~~~~~
In function '_snd_rtr_advs':
cc1: note: source object is likely at address zero
cc1: all warnings being treated as errors

Testing procedure

Apps that use GNRC and send router advertisements should still work.

Issues/PRs references

None

@maribu maribu added the Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) label May 8, 2024
@maribu maribu requested a review from benpicco May 8, 2024 11:23
@github-actions github-actions bot added Area: network Area: Networking Area: sys Area: System labels May 8, 2024
@@ -95,8 +95,6 @@ void _snd_rtr_advs(gnrc_netif_t *netif, const ipv6_addr_t *dst, bool final)
sizeof(addr_str)));
_snd_ra(netif, dst, final, abr);
}
} else {
_snd_ra(netif, dst, final, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

I think this is the wrong line to remove / to fix. As you pointed out in chat: This can still be reached by a 6LR that does not have multihop prefix and 6LoWPAN context dissemination (MULTIHOP_P6C) activated.

Copy link
Member

Choose a reason for hiding this comment

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

I am a little bit confused, why GCC then comes to this conclusion though. Maybe it helps to make the IS_ACTIVE() check here

if (gnrc_netif_is_6ln(netif)) {
#if IS_ACTIVE(CONFIG_GNRC_IPV6_NIB_MULTIHOP_P6C)

a C-conditional to help GCC figure this out correctly?

Copy link
Member Author

Choose a reason for hiding this comment

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

GCC did figure this out correctly: If CONFIG_GNRC_IPV6_NIB_MULTIHOP_P6C is used and a netif is 6ln but not 6lr, the NULL pointer dereferencing would very much happen.

Copy link
Member

@miri64 miri64 May 8, 2024

Choose a reason for hiding this comment

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

GCC did figure this out correctly: If CONFIG_GNRC_IPV6_NIB_MULTIHOP_P6C is used and a netif is 6ln but not 6lr, the NULL pointer dereferencing would very much happen.

Ok, but your line removal also removes the “netif is a 6lr but does not use the CONFIG_GNRC_IPV6_NIB_MULTIHOP_P6C” case, which is a correct scenario. Then I guess, the check for CONFIG_GNRC_IPV6_NIB_MULTIHOP_P6C check either needs a check for the netif also being a 6lr (which would be the right check spec-wise) or if abr is NULL (which would be the safer check code-wise).

Copy link
Member Author

Choose a reason for hiding this comment

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

So, maybe this is the correct fix? Checking for 6lr in both cases?

GCC's static analysis does indeed notice that the check then is the same in both cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

check for the netif also being a 6lr (which would be the right check spec-wise) or if abr is NULL (which would be the safer check code-wise).

OK, I did both now with the check for abr being an assert().

@maribu maribu force-pushed the sys/net/grnc/fix-compilation-with-gcc-14-1-0 branch 2 times, most recently from 8c9cb2e to 15a84eb Compare May 8, 2024 17:41
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label May 8, 2024
@riot-ci
Copy link

riot-ci commented May 8, 2024

Murdock results

✔️ PASSED

abd7205 sys/net/grnc: fix NULL ptr dereferencing

Success Failures Total Runtime
10082 0 10083 14m:46s

Artifacts

@maribu maribu marked this pull request as ready for review May 8, 2024 18:04
@maribu maribu requested a review from PeterKietzmann as a code owner May 8, 2024 18:04
This bug was spotted by GCC 14.1.0:

    In file included from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-internal.h:27,
                     from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-arsm.h:33,
                     from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-6ln.h:30,
                     from /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:28:
    In function 'bf_isset',
        inlined from '_build_ext_opts' at /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:256:17,
        inlined from '_snd_ra' at /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:368:20,
        inlined from '_snd_rtr_advs' at /home/maribu/Repos/software/RIOT/master/sys/net/gnrc/network_layer/ipv6/nib/_nib-router.c:99:9:
    /home/maribu/Repos/software/RIOT/master/sys/include/bitfield.h:130:18: error: array subscript 0 is outside array bounds of 'uint8_t[0]' {aka 'unsigned char[]'} [-Werror=array-bounds=]
      130 |     return (field[idx / 8] & (1u << (7 - (idx % 8))));
          |             ~~~~~^~~~~~~~~
    In function '_snd_rtr_advs':
    cc1: note: source object is likely at address zero
    cc1: all warnings being treated as errors
@maribu maribu force-pushed the sys/net/grnc/fix-compilation-with-gcc-14-1-0 branch from 15a84eb to abd7205 Compare May 9, 2024 10:04
@miri64 miri64 added this pull request to the merge queue May 9, 2024
Merged via the queue into RIOT-OS:master with commit 3927a4f May 9, 2024
27 checks passed
@miri64
Copy link
Member

miri64 commented May 9, 2024

Thanks for the fix!

@maribu maribu deleted the sys/net/grnc/fix-compilation-with-gcc-14-1-0 branch May 10, 2024 07:01
@mguetschow mguetschow added this to the Release 2024.07 milestone Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: network Area: Networking Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants