-
Notifications
You must be signed in to change notification settings - Fork 172
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
fix(userspace/libscap): check bound before reading past socket buffer #2271
base: master
Are you sure you want to change the base?
fix(userspace/libscap): check bound before reading past socket buffer #2271
Conversation
Welcome @shane-lawrence! It looks like this is your first PR to falcosecurity/libs 🎉 |
Thanks for this contribution; it makes sense to me. |
Perf diff from master - unit tests
Heap diff from master - unit tests
Heap diff from master - scap file
Benchmarks diff from master
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2271 +/- ##
=======================================
Coverage 75.32% 75.32%
=======================================
Files 280 280
Lines 34556 34556
Branches 5902 5902
=======================================
Hits 26031 26031
Misses 8525 8525
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
dd4443c
to
1f409df
Compare
I added tests and confirmed that it triggers a segfault without the fix but succeeds with the fix. |
1f409df
to
e0878bf
Compare
Just rebased on master to pick up the API changes @ekoops made recently. |
Hey @shane-lawrence Thank you for this PR! Just noticed 👇 May you fix the code formatting, please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Catch! Thank you!
LGTM label has been added. Git tree hash: 431f76b8d8276d05cd2821e4cd7cc62d0faf0a32
|
I'm restarting the CI, if it passes it's good for me |
Oh that's it! When I ran the new test without the changes to libscap, CI skipped libscap_test. I understand now that's because it didn't see any changes to userspace/libscap.
Great! I added this in 824363c. |
Oh nice, enabling asan found another issue in the test-scap 😆
EDIT: of course, evt->n_params is 6bit aligned because we pack the struct:
This thread is interesting: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51628 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this committed by error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's mock data to allow testing. I couldn't use real data from a production host but we need input that meets the specific conditions for triggering the bug, so I generated this. It should mimic a valid sockets table and must have a line with consecutive spaces at the 1MB buffer boundary and the inode number just beyond that.
The mock data is read for the test in scap_fds_impl.c:31-43.
About the test-scap tests failing, cc @LucaGuerra any idea? |
Most of the tests that trigger this can avoid it by copying the field before evaluating them like this:
That won't work for test_scap_create_event though, since it's asserting that each field is in the correct location in the packed struct. I'm a bit out of my depth here, but it looks like allowing 2 bytes of padding would satisfy the alignment check on 4-byte boundaries, but it would also be a sweeping change and even a couple bytes can be significant when it's on every event. Would it be okay if I just suppress these for now and open a separate issue? |
Yep we cannot change the struct padding :/ I am ok with your solution, but at that point it would be better to expose helper functions in scap_event.h like
If you agree, i'd just suppress Or, you can revert the enablement of ASAN in the libscap test CI and we will re-enable it in a follow up PR trying also to fix these failures :) That works too! |
007c4f2
to
86611fb
Compare
I added a getter to safely check nparams, and changed the test to use memcmp with a uint8_t* so we can safely compare bytes in the event without memory alignment issues. |
Next I ran into this (unsigned int -1) value triggering an error for undefined behaviour:
I started to fix this in 6e103ba, but the test fails because it expects -1 for each error condition. We can revert that if UNKNOWN isn't a suitable reply for an error condition. One possible solution would be to add an error value to the enum and return something like |
Since the test below (
ie: a wrong syscall ID lead to Again, thanks you are doing a terrific job in this PR (and sorry if it is growing a little bit over your expectations!) EDIT: oh, but looking at some usage of those APIs, we explicitly check for |
6e103ba
to
b29c16c
Compare
Thanks for all of your help so far! I would like to get the undefined behaviours fixed but they're out of scope for this PR. All of the address sanitizer issues detected by the tests have been resolved and it will help to ensure that future changes to libscap don't reintroduce a similar tricky segfault. I'm leaving asan enabled but disabling ubsan and moving the alignment and enum fixes to a separate branch for a future PR. While we can see that some potential bugs are ignored here, I think this leaves us with a resolution to my issue and detection in place for a whole class of bugs, while keeping the PR to a reasonable scope. Let me know if you want me to rebase to clean up the commit history a bit, but hopefully the overall changes are now ready for a final review :). |
I fully agree, and am really sorry that what seemed to be a simple patch became much larger :/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, left a small comment.
Also, do you mind squashing the changes in like 2 commits (the fix + the new test)?
Thanks, then we are good to go for real ahah
Signed-off-by: Shane Lawrence <[email protected]>
Signed-off-by: Shane Lawrence <[email protected]>
f772f58
to
d4391a8
Compare
I rebased on master with two clean commits and CI is running now. Let me know if you have any other suggestions. |
Everything 🟢 on my side. Thank you very much once again! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
LGTM label has been added. Git tree hash: 5f821cb6085f2838efb01fc7576c398fa9df2fb7
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: FedeDP, shane-lawrence The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Signed-off-by: Shane Lawrence [email protected]
What type of PR is this?
/kind bug
Any specific area of the project related to this PR?
/area libscap
Does this PR require a change in the driver versions?
I don't think so.
What this PR does / why we need it:
This PR corrects a bug in libscap where the next character in a buffer is read before checking if it's out of bounds. This can cause a segfault when the 1 MB buffer ends with a TIME_WAIT socket.
Which issue(s) this PR fixes:
Fixes #2272 and #2276.
Special notes for your reviewer:
I had trouble getting the C++ test suite to work with the older C code in scap_fds.c, so I put them in separate files. Please let me know if there's a better way to handle it.
Does this PR introduce a user-facing change?:
no