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

Fix memory leaks related to the cache of securityStateReference. #143

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pysnmp/proto/mpmod/rfc3412.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ def prepare_response_message(
elif securityLevel == 3:
msgFlags |= 0x03
else:
if securityModel in snmpEngine.security_models:
Copy link

Choose a reason for hiding this comment

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

def prepare_data_elements in rfc3412.py already blocked invalid msgFlags from being passed on, so it is impossible to have a securityLevel value here.

smHandler = snmpEngine.security_models[securityModel]
smHandler.release_state_information(securityStateReference)
raise error.ProtocolError("Unknown securityLevel %s" % securityLevel)

if pdu.tagSet in rfc3411.CONFIRMED_CLASS_PDUS: # XXX not needed?
Expand Down
10 changes: 10 additions & 0 deletions pysnmp/proto/secmod/rfc3414/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ def process_incoming_message(
"processIncomingMsg: scopedPduData not plaintext %s"
% scopedPduData.prettyPrint()
)
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.unknownEngineID
)
Expand All @@ -1073,6 +1074,7 @@ def process_incoming_message(
"processIncomingMsg: will not discover EngineID"
)
# free securityStateReference XXX
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.unknownEngineID
)
Expand Down Expand Up @@ -1154,6 +1156,7 @@ def process_incoming_message(
"__SNMPv2-MIB", "snmpInGenErrs"
)
snmpInGenErrs.syntax += 1
self._cache.pop(securityStateReference)
raise error.StatusInformation(errorIndication=errind.invalidMsg)
else:
# empty username used for engineID discovery
Expand Down Expand Up @@ -1255,6 +1258,7 @@ def process_incoming_message(
if usmUserAuthProtocol in self.AUTH_SERVICES:
authHandler = self.AUTH_SERVICES[usmUserAuthProtocol]
else:
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.authenticationFailure
)
Expand Down Expand Up @@ -1346,6 +1350,7 @@ def process_incoming_message(
)
)
else:
self._cache.pop(securityStateReference)
raise error.ProtocolError("Peer SNMP engine info missing")

# 3.2.7a
Expand Down Expand Up @@ -1417,6 +1422,7 @@ def process_incoming_message(
)
> 150
):
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.notInTimeWindow, msgUserName=msgUserName
)
Expand All @@ -1426,11 +1432,13 @@ def process_incoming_message(
if usmUserPrivProtocol in self.PRIV_SERVICES:
privHandler = self.PRIV_SERVICES[usmUserPrivProtocol]
else:
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.decryptionError, msgUserName=msgUserName
)
encryptedPDU = scopedPduData.getComponentByPosition(1)
if encryptedPDU is None: # no ciphertext
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.decryptionError, msgUserName=msgUserName
)
Expand Down Expand Up @@ -1494,13 +1502,15 @@ def process_incoming_message(
)

if eoo.endOfOctets.isSameTypeWith(scopedPDU):
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.decryptionError, msgUserName=msgUserName
)
else:
# 3.2.8b
scopedPDU = scopedPduData.getComponentByPosition(0)
if scopedPDU is None: # no plaintext
self._cache.pop(securityStateReference)
raise error.StatusInformation(
errorIndication=errind.decryptionError, msgUserName=msgUserName
)
Expand Down