Skip to content

Commit 2d5f089

Browse files
committed
Land rapid7#9646, fix stale module cache issue
2 parents 425f949 + 0949e0a commit 2d5f089

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

lib/msf/core/modules/metadata/cache.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Cache
2424
# Refreshes cached module metadata as well as updating the store
2525
#
2626
def refresh_metadata_instance(module_instance)
27+
dlog "Refreshing #{module_instance.refname} of type: #{module_instance.type}"
2728
refresh_metadata_instance_internal(module_instance)
2829
update_store
2930
end
@@ -48,8 +49,22 @@ def refresh_metadata(module_sets)
4849

4950
mt[1].keys.sort.each do |mn|
5051
next if unchanged_reference_name_set.include? mn
51-
module_instance = mt[1].create(mn)
52-
next if not module_instance
52+
53+
begin
54+
module_instance = mt[1].create(mn)
55+
rescue Exception => e
56+
elog "Unable to create module: #{mn}. #{e.message}"
57+
end
58+
59+
unless module_instance
60+
wlog "Removing invalid module reference from cache: #{mn}"
61+
existed = remove_from_cache(mn)
62+
if existed
63+
has_changes = true
64+
end
65+
next
66+
end
67+
5368
begin
5469
refresh_metadata_instance_internal(module_instance)
5570
has_changes = true
@@ -72,7 +87,7 @@ def get_unchanged_module_references
7287

7388
@module_metadata_cache.each_value do |module_metadata|
7489

75-
unless module_metadata.path and ::File.exist?(module_metadata.path)
90+
unless module_metadata.path && ::File.exist?(module_metadata.path)
7691
next
7792
end
7893

@@ -91,12 +106,28 @@ def get_unchanged_module_references
91106
private
92107
#######
93108

109+
def remove_from_cache(module_name)
110+
old_cache_size = @module_metadata_cache.size
111+
@module_metadata_cache.delete_if {|_, module_metadata|
112+
module_metadata.ref_name.eql? module_name
113+
}
114+
115+
return old_cache_size != @module_metadata_cache.size
116+
end
117+
94118
def wait_for_load
95119
@load_thread.join unless @store_loaded
96120
end
97121

98122
def refresh_metadata_instance_internal(module_instance)
99123
metadata_obj = Obj.new(module_instance)
124+
125+
# Remove all instances of modules pointing to the same path. This prevents stale data hanging
126+
# around when modules are incorrectly typed (eg: Auxilary that should be Exploit)
127+
@module_metadata_cache.delete_if {|_, module_metadata|
128+
module_metadata.path.eql? metadata_obj.path
129+
}
130+
100131
@module_metadata_cache[get_cache_key(module_instance)] = metadata_obj
101132
end
102133

0 commit comments

Comments
 (0)