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

Fix more UB in windows/system refresh_processes_specifics #1014

Merged

Conversation

LunNova
Copy link
Contributor

@LunNova LunNova commented Jul 16, 2023

For #1009

@@ -239,7 +240,6 @@ impl SystemExt for System {
if ntstatus == STATUS_INFO_LENGTH_MISMATCH {
// GetNewBufferSize
if cb_needed == 0 {
process_information.reserve(buffer_size);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Putting the reserve call here failed to reserve if cb_needed was >0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also would do nothing because the Vec already had capacity for buffer_size additional elements, it hasn't been doubled yet

@LunNova LunNova marked this pull request as draft July 16, 2023 21:09
@LunNova LunNova force-pushed the lunnova/refresh-process-ub branch 2 times, most recently from ce50358 to 9410c73 Compare July 16, 2023 21:30
@LunNova LunNova force-pushed the lunnova/refresh-process-ub branch from 9410c73 to 95bd5c5 Compare July 16, 2023 21:31
@@ -227,6 +228,9 @@ impl SystemExt for System {

loop {
let mut cb_needed = 0;
// reserve(n) ensures the Vec has capacity for n additional elements
// so we should reserve buffer_size - len
process_information.reserve(buffer_size - process_information.len());
Copy link
Owner

Choose a reason for hiding this comment

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

Unless I missed something, process_information's length is always 0 until we use set_len below. So why subtracting the length?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is true, but it would be pretty easy for a future change to this function to allow reaching this point with a length set as it's pretty long and hard to follow.

I'd feel more comfortable removing the len part if this function broke out of the loop immediately before the set_len. Would you be okay with a refactor like that?

Copy link
Owner

Choose a reason for hiding this comment

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

Sure, go ahead!

@GuillaumeGomez
Copy link
Owner

Thanks for working on this! It looks good to me. Do you plan to add more things or can I merge once CI pass?

@LunNova
Copy link
Contributor Author

LunNova commented Jul 18, 2023

I've tested that it doesn't cause "misaligned pointer dereference: address must be a multiple of 0x8" under wine but haven't tested it on native windows yet, can you confirm that's still good before merging?

let mut process_information_offset = 0;
loop {
let p = unsafe {
process_information
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could assert! that process_information_offset isn't too large here.

Copy link
Owner

@GuillaumeGomez GuillaumeGomez Jul 18, 2023

Choose a reason for hiding this comment

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

Yes, good idea.

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'll make a followup PR later with some more hardening.

Copy link
Owner

Choose a reason for hiding this comment

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

Fine by me!

@GuillaumeGomez
Copy link
Owner

I've tested that it doesn't cause "misaligned pointer dereference: address must be a multiple of 0x8" under wine but haven't tested it on native windows yet, can you confirm that's still good before merging?

Gonna do that in a few then.

@GuillaumeGomez
Copy link
Owner

Tested locally on Windows and worked as expected. 👍

@LunNova LunNova marked this pull request as ready for review July 18, 2023 19:17
@LunNova LunNova changed the title Fix more UB in windows/system Fix more UB in windows/system refresh_processes_specifics Jul 18, 2023
@GuillaumeGomez
Copy link
Owner

Thanks for fixing this bug!

@GuillaumeGomez GuillaumeGomez merged commit d340256 into GuillaumeGomez:master Jul 18, 2023
51 of 60 checks passed
@CryZe
Copy link
Contributor

CryZe commented Jul 18, 2023

Can we have a release with this? (and maybe the current version yanked?)

@GuillaumeGomez
Copy link
Owner

I'm planning to. Just waiting to hear from @granitDev about their bug. If it's not fixed by this PR, I'll need to make another patch.

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.

3 participants