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

PE: parse thread local storage - TLS data #404

Merged
merged 2 commits into from
Apr 27, 2024
Merged

Conversation

kkent030315
Copy link
Contributor

Parses TLS (thread local storage) data directory in PE64/32.

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

some nits i would like to see fixed, but otherwise this lgtm; i almost merged but would prefer to see them fixed before merging, thank you for the PR!

})?;
let mut i = 0;
// Read the callbacks until we find a null terminator
loop {
Copy link
Owner

Choose a reason for hiding this comment

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

on a malformed binary could this cause an infinite loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I think it would make sense to have a RVA validation there and return the malformed error if invalid RVAs found.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Owner

Choose a reason for hiding this comment

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

so what i meant specifically is: is this loop guaranteed to terminate? this comes up when there are malformed binaries, and the loop only terminates in a well-formed case, but if the loop has no upper bound, it might cause the library to run forever, which is unacceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The RVA check I previously introduced should be sufficient to address the malformations. In general, it does not result in an infinite loop. However, there might be instances where it could raise slice bound errors. Typically, the TLS callback array is located within the data sections—a common characteristic across multiple PECOFF compilers—and the data section is well-aligned with zeros. In my experience handling over 10,000 to 20,000 unique PECOFF binaries, I have not encountered any infinite loops with this implementation. Nonetheless, I believe setting a reasonable limit would be prudent. Is this the solution you were seeking?

Copy link
Owner

Choose a reason for hiding this comment

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

Yea I was just making sure there wasn't some obvious termination condition we could add, e.g., end of file, etc., that would guarantee we are done looping; my general reflex when i see loop is to ask how it can terminate, as we've had bugs in past of infinite loops without proper termination condition, but your answer is sufficient, thank you!

src/pe/tls.rs Outdated Show resolved Hide resolved
src/pe/mod.rs Outdated Show resolved Hide resolved
src/pe/tls.rs Outdated Show resolved Hide resolved
Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

This is ready to go basically, but I need more clarification on whether that loop is guaranteed to terminate or not; after that can merge :) and thank you for this!

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

LGTM thank you!

@m4b m4b merged commit 6c5b3e8 into m4b:master Apr 27, 2024
6 checks passed
@m4b
Copy link
Owner

m4b commented Apr 27, 2024

note: breaking change

ideeockus pushed a commit to ideeockus/goblin that referenced this pull request Jul 29, 2024
* PE: parse thread local storage - TLS data
* Breaking change
@m4b
Copy link
Owner

m4b commented Oct 21, 2024

released in 0.9.0, thank you for your patience!

@kkent030315
Copy link
Contributor Author

@m4b Thanks! and somewhat unrelated, would you be able to take a closer look at #406?
I'm off for a day w/o my laptop, but will be able to contribute in a day I guess.

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.

2 participants