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

Make TLV Logging cache threadsafe #16299

Merged

Conversation

sjanusz-r7
Copy link
Contributor

@sjanusz-r7 sjanusz-r7 commented Mar 7, 2022

This PR fixes a potential issue of the same TLV types being defined multiple times when multiple threads want to get a list of TLV types.

Example:
image

Verification

  • Start msfconsole
  • use payload/python/meterpreter_reverse_tcp
  • set lhost ...
  • generate -f raw -o shell.py
  • Attempt to open multiple sessions: "for i in seq 5; do python3 shell.py; done"
  • Confirm that all types are defined only once. E.g. oneOf(COMMAND_ID,COMMAND_ID) is not present.

@sjanusz-r7 sjanusz-r7 force-pushed the make-tlv-logging-cache-threadsafe branch from 69746cf to b7006a4 Compare March 7, 2022 13:25
@@ -108,25 +109,31 @@ def self.get_commands(*extensions)
# @param Integer value The value of the TLV type to retrieve the TLV type names for.
# @return [Array<Symbol>] An array of symbols of all TLV types that are defined with the value. Can be empty.
def self.get_tlv_names(value)
return @@cached_tlv_types[value] unless @@cached_tlv_types[value].nil? || @@cached_tlv_types[value].empty?
@@tlv_cache_mutex.synchronize_read do
Copy link
Contributor

Choose a reason for hiding this comment

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

What impacts could this have on throughput?

Copy link
Contributor

Choose a reason for hiding this comment

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

Performance shouldn't be an issue here, as exclusive write access will only be needed once to hydrate the cache at the start, from that point onwards it's purely reader access - which is the optimised codepath

That aside; I've created a separate issue to fix the thread safety issue in the reader/writer lock itself: rapid7/rex-core#27

@sjanusz-r7 sjanusz-r7 force-pushed the make-tlv-logging-cache-threadsafe branch from a092c45 to 558d8bb Compare March 23, 2022 10:35
@adfoster-r7 adfoster-r7 merged commit bef0c9b into rapid7:master Apr 1, 2022
@adfoster-r7 adfoster-r7 added the rn-no-release-notes no release notes label Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants