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

Ensure BPF read is 4-byte aligned #655

Merged
merged 3 commits into from
May 30, 2024
Merged

Conversation

frankplow
Copy link
Contributor

@frankplow frankplow commented Oct 21, 2023

Closes #641 and closes #645.

For pointer alignment, buffer_offset must be a multiple of 4. If it is not, the program will panic like in #641 and #645. This PR rounds buffer_offset up to the nearest multiple of 4 to avoid this issue. This also requires increasing the size of the buffer to ensure there is enough room to read into. I have made buffer_offset a member of DataLinkReceiverImpl as the allocation and use of the read buffer both require it, and previously it was derived twice in these two locations. By making it a member, it only needs to be derived once.

This is pretty hacky code. As the FIXME in the file indicates, there are much better ways of getting around this. I think this would require breaking API changes however, and I am not familiar with the codebase. For now I think this hotfix is sufficient.

@frankplow frankplow changed the title Ensure BPF read buffer is 4-byte aligned Ensure BPF read is 4-byte aligned Oct 21, 2023
@frankplow
Copy link
Contributor Author

next_multiple_of was stabilised in 1.73.0. The MacOS runner is using an old stable version causing a build failure. It doesn't look like a strict MSRV is specified with a rust-version anywhere, so I'm not sure whether this is okay or not? I can move back to the old (x + align - 1) & !(align - 1) but that's pretty evil-looking code imo.

@tgross35
Copy link
Contributor

Is it possible to run that arp_packet example in CI as part of the test suite?

@mrmonday
Copy link
Contributor

Thanks!

@mrmonday mrmonday merged commit 2d32460 into libpnet:main May 30, 2024
2 of 4 checks passed
@frankplow frankplow deleted the fix-bpf-alignment branch May 30, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crashed in bpf.rs:416:44 MacOS misaligned pointer dereference in DataLinkReceiver with invalid interface
3 participants