Skip to content
New issue

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

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

Already on GitHub? # to your account

sys/shell/ping: fix ping packet size overflow #19927

Merged
merged 4 commits into from
Sep 21, 2023

Conversation

krzysztof-cabaj
Copy link
Contributor

Contribution description

In #19829 @mchesser point out integer overflow in the ping command and API. This PR fix this issue in two ways:

  1. Add protection in the API.
  2. Add protection in the user command.

Testing procedure

Without this PR passing negative number to the ping -s option cause segmentation fault, for example in the
example/gnrc_networking:

> ping -s -7 ::1
ping -s -7 ::1
Segmentation fault

With this PR user shows appropriate warning test:

> ping -s -7 ::1
ping -s -7 ::1
ICMPv6 datagram size should be in range <0, 65527>.
> 

Issues/PRs references

Issue #19829

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

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

Soft ACK, but a small formatting issue.

@@ -150,6 +150,12 @@ int gnrc_icmpv6_echo_send(const gnrc_netif_t *netif, const ipv6_addr_t *addr,
ipv6_hdr_t *ipv6;
uint8_t *databuf;

/* max IPv6 payload 65535 minus 8 bytes of icmp header = 65527 */
if (len > 65527) {
Copy link
Member

Choose a reason for hiding this comment

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

Ah and can you please use e.g. UINT16_MAX - sizeof(icmpv6_hdr_t) instead of the magic number, then you don't need the comment ;-).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Fixed. If this is not a problem I would like to leave this comment.

data->datalen = atoi(argv[i]);
value = atoi(argv[i]);

if(value < 0 || value > 65527) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@krzysztof-cabaj
Copy link
Contributor Author

Fixed and squashed.

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

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

bors merge

@bors
Copy link
Contributor

bors bot commented Sep 18, 2023

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@miri64 miri64 added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Sep 18, 2023
@riot-ci
Copy link

riot-ci commented Sep 18, 2023

Murdock results

✔️ PASSED

a958fa2 sys/shell/ping: reduce text to save ROM

Success Failures Total Runtime
7937 0 7937 15m:39s

Artifacts

@miri64
Copy link
Member

miri64 commented Sep 19, 2023

bors merge

bors bot added a commit that referenced this pull request Sep 19, 2023
19927: sys/shell/ping: fix ping packet size overflow r=miri64 a=krzysztof-cabaj

### Contribution description

In #19829 `@mchesser` point out integer overflow in the ```ping``` command and API. This PR fix this issue in two ways:
1) Add protection in the API.
2) Add protection in the user command.

### Testing procedure

Without this PR passing negative number to the ```ping -s``` option cause segmentation fault, for example in the 
```example/gnrc_networking```:

```
> ping -s -7 ::1
ping -s -7 ::1
Segmentation fault
```

With this PR user shows appropriate warning test:

```
> ping -s -7 ::1
ping -s -7 ::1
ICMPv6 datagram size should be in range <0, 65527>.
> 
``` 

### Issues/PRs references

Issue #19829 

Co-authored-by: krzysztof-cabaj <kcabaj@gmail.com>
@bors
Copy link
Contributor

bors bot commented Sep 19, 2023

Build failed:

@miri64
Copy link
Member

miri64 commented Sep 19, 2023

bors merge

@miri64
Copy link
Member

miri64 commented Sep 19, 2023

bors cancel

@bors
Copy link
Contributor

bors bot commented Sep 19, 2023

Canceled.

@miri64
Copy link
Member

miri64 commented Sep 19, 2023

bors merge

bors bot added a commit that referenced this pull request Sep 19, 2023
19927: sys/shell/ping: fix ping packet size overflow r=miri64 a=krzysztof-cabaj

### Contribution description

In #19829 `@mchesser` point out integer overflow in the ```ping``` command and API. This PR fix this issue in two ways:
1) Add protection in the API.
2) Add protection in the user command.

### Testing procedure

Without this PR passing negative number to the ```ping -s``` option cause segmentation fault, for example in the 
```example/gnrc_networking```:

```
> ping -s -7 ::1
ping -s -7 ::1
Segmentation fault
```

With this PR user shows appropriate warning test:

```
> ping -s -7 ::1
ping -s -7 ::1
ICMPv6 datagram size should be in range <0, 65527>.
> 
``` 

### Issues/PRs references

Issue #19829 

Co-authored-by: krzysztof-cabaj <kcabaj@gmail.com>
@miri64
Copy link
Member

miri64 commented Sep 19, 2023

bors cancel

@bors
Copy link
Contributor

bors bot commented Sep 19, 2023

Canceled.

@github-actions github-actions bot added Area: drivers Area: Device drivers Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: Kconfig Area: Kconfig integration labels Sep 20, 2023
@benpicco
Copy link
Contributor

I'm not sure what you did there, but your PR now adds +389,749 new lines of code.

Please do a git rebase -i master, that should clean things up. (given your master branch is on a state that matches upstream)

value = atoi(argv[i]);

if ((value < 0) || ((unsigned)value > (UINT16_MAX - sizeof(icmpv6_hdr_t)))) {
printf("ICMPv6 datagram size should be in range 0-65527.\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

tbh I don't think you need to check that twice. You already have that check in gnrc_icmpv6_echo_send(), no need to pre-check - you can just let gnrc_icmpv6_echo_send() return error.

Sending ICMP messages larger than 65527 bytes is not such a common use-case that we need special handling for it in the shell command.

Copy link
Member

Choose a reason for hiding this comment

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

There is nothing speaking against pre-checking if you want to have user-friendly error messages, IMHO. At least the value < 0 should stay in any case, since value is int, but len is size_t (so unsigned). Casting could introduce some unexpected values here (especially if size_t is set to uint16_t by the architecture).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that now I have to checks. The one in the API - protects users which would like send icmp pings in the own code. The second check as @miri64 write gives more user friendly warning. Instead, user when gives wrong size see only cryptic text:

All up, running the shell now
> ping -s -7 ::1 
ping -s -7 ::1
error: -22
error: -22
error: -22

--- ::1 PING statistics ---
3 packets transmitted, 0 packets received, 100% packet loss
>

@krzysztof-cabaj
Copy link
Contributor Author

A while ago I think that I understand rebase ;)
Sorry for the mess - give me a (longer) while to fix everything.

@github-actions github-actions bot added Area: examples Area: Example Applications and removed Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: doc Area: Documentation Area: build system Area: Build system Area: drivers Area: Device drivers Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: Kconfig Area: Kconfig integration labels Sep 20, 2023
@miri64
Copy link
Member

miri64 commented Sep 20, 2023

bors merge

@bors
Copy link
Contributor

bors bot commented Sep 20, 2023

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@krzysztof-cabaj
Copy link
Contributor Author

And now everything is corrected (I hope) - I missed one, crucial commit, which save ROM.

@miri64
Copy link
Member

miri64 commented Sep 20, 2023

bors merge

@bors
Copy link
Contributor

bors bot commented Sep 21, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 8d1a93a into RIOT-OS:master Sep 21, 2023
@krzysztof-cabaj
Copy link
Contributor Author

Thanks all for support ... and patience ;).

@miri64
Copy link
Member

miri64 commented Sep 21, 2023

Thank you for your contribution and for fixing this ☺️

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Area: examples Area: Example Applications 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.

5 participants