Skip to content

Commit ee39637

Browse files
committed
Merge branch '2817-replace-agent-singleton' into develop
Issue #2817 PR #3068
2 parents a1978bd + 889fc66 commit ee39637

File tree

4 files changed

+12
-116
lines changed

4 files changed

+12
-116
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
1919
### Security
2020
- Fixed plaintext private key in SSHKey pair list in UI. #2950
2121
- MongoDB version from 4.x to 6.0.4. #2706
22+
- Replaced the `SystemSingleton` component, which could allow local users to
23+
execute a DoS attack against agents. #2817
2224

2325
## [2.0.0] - 2023-02-08
2426
### Added

monkey/infection_monkey/monkey.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
PluginSourceExtractor,
8484
)
8585
from infection_monkey.puppet.puppet import Puppet
86-
from infection_monkey.system_singleton import SystemSingleton
8786
from infection_monkey.utils import agent_process, environment
8887
from infection_monkey.utils.file_utils import mark_file_for_deletion_on_windows
8988
from infection_monkey.utils.ids import get_agent_id, get_machine_id
@@ -107,12 +106,9 @@ def __init__(self, args, ipc_logger_queue: multiprocessing.Queue, log_path: Path
107106
logger.info(f"Agent ID: {self._agent_id}")
108107
logger.info(f"Process ID: {os.getpid()}")
109108

110-
# Spawn the manager before the acquiring the singleton in case the file handle gets copied
111-
# over to the manager process
112109
context = multiprocessing.get_context("spawn")
113110
self._manager = context.Manager()
114111

115-
self._singleton = SystemSingleton()
116112
self._opts = self._get_arguments(args)
117113

118114
self._ipc_logger_queue = ipc_logger_queue
@@ -222,12 +218,10 @@ def start(self):
222218
self._agent_event_forwarder.start()
223219
self._plugin_event_forwarder.start()
224220

225-
if self._is_another_monkey_running():
226-
logger.info("Another instance of the monkey is already running")
227-
return
228-
229221
logger.info("Agent is starting...")
230222

223+
# This check must be done after the agent event forwarder is started, otherwise the agent
224+
# will be unable to send a shutdown event to the Island.
231225
should_stop = self._control_channel.should_agent_stop()
232226
if should_stop:
233227
logger.info("The Monkey Island has instructed this agent to stop")
@@ -435,9 +429,6 @@ def _subscribe_events(self):
435429
PropagationEvent, notify_relay_on_propagation(self._relay)
436430
)
437431

438-
def _is_another_monkey_running(self):
439-
return not self._singleton.try_lock()
440-
441432
def cleanup(self):
442433
logger.info("Agent cleanup started")
443434
deleted = None
@@ -459,7 +450,9 @@ def cleanup(self):
459450
self._publish_agent_shutdown_event()
460451

461452
self._plugin_event_forwarder.flush()
462-
self._agent_event_forwarder.flush()
453+
454+
if self._agent_event_forwarder:
455+
self._agent_event_forwarder.flush()
463456

464457
self._heart.stop()
465458

@@ -471,13 +464,10 @@ def cleanup(self):
471464
InfectionMonkey._self_delete()
472465
finally:
473466
self._plugin_event_forwarder.stop()
474-
self._agent_event_forwarder.stop()
467+
if self._agent_event_forwarder:
468+
self._agent_event_forwarder.stop()
475469
self._delete_plugin_dir()
476470
self._manager.shutdown()
477-
try:
478-
self._singleton.unlock()
479-
except AssertionError as err:
480-
logger.warning(f"Failed to release the singleton: {err}")
481471

482472
logger.info("Agent is shutting down")
483473

monkey/infection_monkey/plugin_event_forwarder.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def stop(self, timeout=None):
6464
:param timeout: The number of seconds to wait for the PluginEventForwarder to stop
6565
"""
6666
logger.info("Stopping plugin event forwarder")
67-
self._stop.set()
68-
self._thread.join(timeout)
67+
if self._thread.is_alive():
68+
self._stop.set()
69+
self._thread.join(timeout)
6970
self.flush()
7071

7172
def flush(self):

monkey/infection_monkey/system_singleton.py

-97
This file was deleted.

0 commit comments

Comments
 (0)