-
Notifications
You must be signed in to change notification settings - Fork 692
address: add optional id field for unique tracking of recycled PID/TID lifecycles #2889
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |||||||||||||||||||||||||
| # limitations under the License. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import abc | ||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class Address(abc.ABC): | ||||||||||||||||||||||||||
|
|
@@ -50,53 +51,83 @@ def __hash__(self): | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class ProcessAddress(Address): | ||||||||||||||||||||||||||
| """an address of a process in a dynamic execution trace""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __init__(self, pid: int, ppid: int = 0): | ||||||||||||||||||||||||||
| """an address of a process in a dynamic execution trace | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| pid: process ID assigned by the OS | ||||||||||||||||||||||||||
| ppid: parent process ID assigned by the OS | ||||||||||||||||||||||||||
| id: optional sandbox-specific unique identifier to distinguish | ||||||||||||||||||||||||||
| processes whose OS-assigned PIDs collide due to reuse. | ||||||||||||||||||||||||||
| For VMRay this is the monitor_id; for other backends | ||||||||||||||||||||||||||
| it may be a sequential counter or timestamp. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __init__(self, pid: int, ppid: int = 0, id: Optional[int] = None): | ||||||||||||||||||||||||||
| assert ppid >= 0 | ||||||||||||||||||||||||||
| assert pid > 0 | ||||||||||||||||||||||||||
| self.ppid = ppid | ||||||||||||||||||||||||||
| self.pid = pid | ||||||||||||||||||||||||||
| self.id = id | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __repr__(self): | ||||||||||||||||||||||||||
| return "process(%s%s)" % ( | ||||||||||||||||||||||||||
| f"ppid: {self.ppid}, " if self.ppid > 0 else "", | ||||||||||||||||||||||||||
| f"pid: {self.pid}", | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| parts = [] | ||||||||||||||||||||||||||
| if self.ppid > 0: | ||||||||||||||||||||||||||
| parts.append(f"ppid: {self.ppid}") | ||||||||||||||||||||||||||
| parts.append(f"pid: {self.pid}") | ||||||||||||||||||||||||||
| if self.id is not None: | ||||||||||||||||||||||||||
| parts.append(f"id: {self.id}") | ||||||||||||||||||||||||||
| return "process(%s)" % ", ".join(parts) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __hash__(self): | ||||||||||||||||||||||||||
| return hash((self.ppid, self.pid)) | ||||||||||||||||||||||||||
| return hash((self.ppid, self.pid, self.id)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __eq__(self, other): | ||||||||||||||||||||||||||
| assert isinstance(other, ProcessAddress) | ||||||||||||||||||||||||||
| return (self.ppid, self.pid) == (other.ppid, other.pid) | ||||||||||||||||||||||||||
| return (self.ppid, self.pid, self.id) == (other.ppid, other.pid, other.id) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __lt__(self, other): | ||||||||||||||||||||||||||
| assert isinstance(other, ProcessAddress) | ||||||||||||||||||||||||||
| return (self.ppid, self.pid) < (other.ppid, other.pid) | ||||||||||||||||||||||||||
| # None sorts before any real id | ||||||||||||||||||||||||||
| self_id = self.id if self.id is not None else -1 | ||||||||||||||||||||||||||
| other_id = other.id if other.id is not None else -1 | ||||||||||||||||||||||||||
| return (self.ppid, self.pid, self_id) < (other.ppid, other.pid, other_id) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class ThreadAddress(Address): | ||||||||||||||||||||||||||
| """addresses a thread in a dynamic execution trace""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __init__(self, process: ProcessAddress, tid: int): | ||||||||||||||||||||||||||
| """addresses a thread in a dynamic execution trace | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| process: address of the containing process | ||||||||||||||||||||||||||
| tid: thread ID assigned by the OS | ||||||||||||||||||||||||||
| id: optional sandbox-specific unique identifier to distinguish | ||||||||||||||||||||||||||
| threads whose OS-assigned TIDs collide due to reuse. | ||||||||||||||||||||||||||
| For VMRay this is the monitor_id; for other backends | ||||||||||||||||||||||||||
| it may be a sequential counter or timestamp. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __init__(self, process: ProcessAddress, tid: int, id: Optional[int] = None): | ||||||||||||||||||||||||||
| assert tid >= 0 | ||||||||||||||||||||||||||
| self.process = process | ||||||||||||||||||||||||||
| self.tid = tid | ||||||||||||||||||||||||||
| self.id = id | ||||||||||||||||||||||||||
|
Comment on lines
+108
to
+112
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the same reasons outlined in the comment on
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __repr__(self): | ||||||||||||||||||||||||||
| return f"{self.process}, thread(tid: {self.tid})" | ||||||||||||||||||||||||||
| id_part = f", id: {self.id}" if self.id is not None else "" | ||||||||||||||||||||||||||
| return f"{self.process}, thread(tid: {self.tid}{id_part})" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __hash__(self): | ||||||||||||||||||||||||||
| return hash((self.process, self.tid)) | ||||||||||||||||||||||||||
| return hash((self.process, self.tid, self.id)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __eq__(self, other): | ||||||||||||||||||||||||||
| assert isinstance(other, ThreadAddress) | ||||||||||||||||||||||||||
| return (self.process, self.tid) == (other.process, other.tid) | ||||||||||||||||||||||||||
| return (self.process, self.tid, self.id) == (other.process, other.tid, other.id) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __lt__(self, other): | ||||||||||||||||||||||||||
| assert isinstance(other, ThreadAddress) | ||||||||||||||||||||||||||
| return (self.process, self.tid) < (other.process, other.tid) | ||||||||||||||||||||||||||
| # None sorts before any real id | ||||||||||||||||||||||||||
| self_id = self.id if self.id is not None else -1 | ||||||||||||||||||||||||||
| other_id = other.id if other.id is not None else -1 | ||||||||||||||||||||||||||
| return (self.process, self.tid, self_id) < (other.process, other.tid, other_id) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class DynamicCallAddress(Address): | ||||||||||||||||||||||||||
|
|
@@ -114,7 +145,10 @@ def __hash__(self): | |||||||||||||||||||||||||
| return hash((self.thread, self.id)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __eq__(self, other): | ||||||||||||||||||||||||||
| return isinstance(other, DynamicCallAddress) and (self.thread, self.id) == (other.thread, other.id) | ||||||||||||||||||||||||||
| return isinstance(other, DynamicCallAddress) and (self.thread, self.id) == ( | ||||||||||||||||||||||||||
| other.thread, | ||||||||||||||||||||||||||
| other.id, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __lt__(self, other): | ||||||||||||||||||||||||||
| assert isinstance(other, DynamicCallAddress) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The current implementation has two potential issues related to the
idfield:__lt__method uses-1as a sentinel forNone, which assumes all valid IDs are non-negative.freezelogic uses0as a sentinel forNone, which would incorrectly handle a legitimate ID of0(it would be deserialized asNone).While current extractors seem to use positive IDs, this is not enforced. A future backend could provide
id=0or negative IDs, triggering these bugs.To make the implementation more robust and prevent these potential data loss and sorting issues, I suggest enforcing that
idmust be a positive integer if it is notNone. A similar assertion should be added toThreadAddress.