23ac29
diff -urNp c/agent/snmp_agent.c d/agent/snmp_agent.c
23ac29
--- c/agent/snmp_agent.c	2019-09-18 08:44:53.833601845 +0200
23ac29
+++ d/agent/snmp_agent.c	2019-09-18 08:46:38.176595597 +0200
23ac29
@@ -1604,6 +1604,13 @@ free_agent_snmp_session(netsnmp_agent_se
23ac29
     
23ac29
     DEBUGMSGTL(("verbose:asp", "asp %p reqinfo %p freed\n",
23ac29
                 asp, asp->reqinfo));
23ac29
+
23ac29
+    /* Clean up securityStateRef here to prevent a double free */
23ac29
+    if (asp->orig_pdu && asp->orig_pdu->securityStateRef)
23ac29
+	snmp_free_securityStateRef(asp->orig_pdu);
23ac29
+    if (asp->pdu && asp->pdu->securityStateRef)
23ac29
+	snmp_free_securityStateRef(asp->pdu);
23ac29
+
23ac29
     if (asp->orig_pdu)
23ac29
         snmp_free_pdu(asp->orig_pdu);
23ac29
     if (asp->pdu)
23ac29
diff -urNp c/include/net-snmp/pdu_api.h d/include/net-snmp/pdu_api.h
23ac29
--- c/include/net-snmp/pdu_api.h	2019-09-18 08:44:53.822601740 +0200
23ac29
+++ d/include/net-snmp/pdu_api.h	2019-09-18 08:47:03.620838212 +0200
23ac29
@@ -19,6 +19,8 @@ NETSNMP_IMPORT
23ac29
 netsnmp_pdu    *snmp_fix_pdu(  netsnmp_pdu *pdu, int idx);
23ac29
 NETSNMP_IMPORT
23ac29
 void            snmp_free_pdu( netsnmp_pdu *pdu);
23ac29
+NETSNMP_IMPORT
23ac29
+void            snmp_free_securityStateRef( netsnmp_pdu *pdu);
23ac29
 
23ac29
 #ifdef __cplusplus
23ac29
 }
23ac29
diff -urNp c/snmplib/snmp_api.c d/snmplib/snmp_api.c
23ac29
--- c/snmplib/snmp_api.c	2019-09-18 08:44:53.807601597 +0200
23ac29
+++ d/snmplib/snmp_api.c	2019-09-18 08:53:19.937435576 +0200
23ac29
@@ -4012,7 +4012,12 @@ snmpv3_parse(netsnmp_pdu *pdu,
23ac29
 static void
23ac29
 free_securityStateRef(netsnmp_pdu* pdu)
23ac29
 {
23ac29
-    struct snmp_secmod_def *sptr = find_sec_mod(pdu->securityModel);
23ac29
+    struct snmp_secmod_def *sptr;
23ac29
+
23ac29
+    if(!pdu->securityStateRef)
23ac29
+       return;
23ac29
+
23ac29
+    sptr = find_sec_mod(pdu->securityModel);
23ac29
     if (sptr) {
23ac29
         if (sptr->pdu_free_state_ref) {
23ac29
             (*sptr->pdu_free_state_ref) (pdu->securityStateRef);
23ac29
@@ -4029,6 +4034,17 @@ free_securityStateRef(netsnmp_pdu* pdu)
23ac29
     pdu->securityStateRef = NULL;
23ac29
 }
23ac29
 
23ac29
+/*
23ac29
+ * This function is here to provide a separate call to
23ac29
+ * free the securityStateRef memory. This is needed to prevent
23ac29
+ * a double free if this memory is freed in snmp_free_pdu.
23ac29
+ */
23ac29
+void
23ac29
+snmp_free_securityStateRef(netsnmp_pdu* pdu)
23ac29
+{
23ac29
+   free_securityStateRef(pdu);
23ac29
+}
23ac29
+
23ac29
 #define ERROR_STAT_LENGTH 11
23ac29
 
23ac29
 int
23ac29
diff -urNp c/snmplib/snmpusm.c d/snmplib/snmpusm.c
23ac29
--- c/snmplib/snmpusm.c	2019-09-18 08:44:53.802601550 +0200
23ac29
+++ d/snmplib/snmpusm.c	2019-09-18 08:57:35.696872662 +0200
23ac29
@@ -299,16 +299,20 @@ usm_free_usmStateReference(void *old)
23ac29
 
23ac29
     if (old_ref) {
23ac29
 
23ac29
-        SNMP_FREE(old_ref->usr_name);
23ac29
-        SNMP_FREE(old_ref->usr_engine_id);
23ac29
-        SNMP_FREE(old_ref->usr_auth_protocol);
23ac29
-        SNMP_FREE(old_ref->usr_priv_protocol);
23ac29
+        if (old_ref->usr_name_length)
23ac29
+            SNMP_FREE(old_ref->usr_name);
23ac29
+        if (old_ref->usr_engine_id_length)
23ac29
+            SNMP_FREE(old_ref->usr_engine_id);
23ac29
+        if (old_ref->usr_auth_protocol_length)
23ac29
+            SNMP_FREE(old_ref->usr_auth_protocol);
23ac29
+        if (old_ref->usr_priv_protocol_length)
23ac29
+            SNMP_FREE(old_ref->usr_priv_protocol);
23ac29
 
23ac29
-        if (old_ref->usr_auth_key) {
23ac29
+        if (old_ref->usr_auth_key_length && old_ref->usr_auth_key) {
23ac29
             SNMP_ZERO(old_ref->usr_auth_key, old_ref->usr_auth_key_length);
23ac29
             SNMP_FREE(old_ref->usr_auth_key);
23ac29
         }
23ac29
-        if (old_ref->usr_priv_key) {
23ac29
+        if (old_ref->usr_priv_key_length && old_ref->usr_priv_key) {
23ac29
             SNMP_ZERO(old_ref->usr_priv_key, old_ref->usr_priv_key_length);
23ac29
             SNMP_FREE(old_ref->usr_priv_key);
23ac29
         }
23ac29
@@ -1039,7 +1043,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
         if ((user = usm_get_user(secEngineID, secEngineIDLen, secName))
23ac29
             == NULL && secLevel != SNMP_SEC_LEVEL_NOAUTH) {
23ac29
             DEBUGMSGTL(("usm", "Unknown User(%s)\n", secName));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_UNKNOWNSECURITYNAME;
23ac29
         }
23ac29
 
23ac29
@@ -1091,7 +1094,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
                                         thePrivProtocolLength) == 1) {
23ac29
         DEBUGMSGTL(("usm", "Unsupported Security Level (%d)\n",
23ac29
                     theSecLevel));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_USM_UNSUPPORTEDSECURITYLEVEL;
23ac29
     }
23ac29
 
23ac29
@@ -1121,7 +1123,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
                          &msgAuthParmLen, &msgPrivParmLen, &otstlen,
23ac29
                          &seq_len, &msgSecParmLen) == -1) {
23ac29
         DEBUGMSGTL(("usm", "Failed calculating offsets.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_USM_GENERICERROR;
23ac29
     }
23ac29
 
23ac29
@@ -1143,7 +1144,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
     ptr = *wholeMsg = globalData;
23ac29
     if (theTotalLength > *wholeMsgLen) {
23ac29
         DEBUGMSGTL(("usm", "Message won't fit in buffer.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_USM_GENERICERROR;
23ac29
     }
23ac29
 
23ac29
@@ -1169,7 +1169,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
                                htonl(boots_uint), htonl(time_uint),
23ac29
                                &ptr[privParamsOffset]) == -1) {
23ac29
                 DEBUGMSGTL(("usm", "Can't set AES iv.\n"));
23ac29
-                usm_free_usmStateReference(secStateRef);
23ac29
                 return SNMPERR_USM_GENERICERROR;
23ac29
             }
23ac29
         }
23ac29
@@ -1185,7 +1184,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
                               &ptr[privParamsOffset])
23ac29
                  == -1)) {
23ac29
                 DEBUGMSGTL(("usm", "Can't set DES-CBC salt.\n"));
23ac29
-                usm_free_usmStateReference(secStateRef);
23ac29
                 return SNMPERR_USM_GENERICERROR;
23ac29
             }
23ac29
         }
23ac29
@@ -1198,7 +1196,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
                        &ptr[dataOffset], &encrypted_length)
23ac29
             != SNMP_ERR_NOERROR) {
23ac29
             DEBUGMSGTL(("usm", "encryption error.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_ENCRYPTIONERROR;
23ac29
         }
23ac29
 #ifdef NETSNMP_ENABLE_TESTING_CODE
23ac29
@@ -1226,7 +1223,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
         if ((encrypted_length != (theTotalLength - dataOffset))
23ac29
             || (salt_length != msgPrivParmLen)) {
23ac29
             DEBUGMSGTL(("usm", "encryption length error.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_ENCRYPTIONERROR;
23ac29
         }
23ac29
 
23ac29
@@ -1362,7 +1358,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
 
23ac29
         if (temp_sig == NULL) {
23ac29
             DEBUGMSGTL(("usm", "Out of memory.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_GENERICERROR;
23ac29
         }
23ac29
 
23ac29
@@ -1376,7 +1371,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
             SNMP_ZERO(temp_sig, temp_sig_len);
23ac29
             SNMP_FREE(temp_sig);
23ac29
             DEBUGMSGTL(("usm", "Signing failed.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_AUTHENTICATIONFAILURE;
23ac29
         }
23ac29
 
23ac29
@@ -1384,7 +1378,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
             SNMP_ZERO(temp_sig, temp_sig_len);
23ac29
             SNMP_FREE(temp_sig);
23ac29
             DEBUGMSGTL(("usm", "Signing lengths failed.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_AUTHENTICATIONFAILURE;
23ac29
         }
23ac29
 
23ac29
@@ -1398,7 +1391,6 @@ usm_generate_out_msg(int msgProcModel,
23ac29
     /*
23ac29
      * endif -- create keyed hash 
23ac29
      */
23ac29
-    usm_free_usmStateReference(secStateRef);
23ac29
 
23ac29
     DEBUGMSGTL(("usm", "USM processing completed.\n"));
23ac29
 
23ac29
@@ -1548,7 +1540,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
         if ((user = usm_get_user(secEngineID, secEngineIDLen, secName))
23ac29
             == NULL && secLevel != SNMP_SEC_LEVEL_NOAUTH) {
23ac29
             DEBUGMSGTL(("usm", "Unknown User\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_UNKNOWNSECURITYNAME;
23ac29
         }
23ac29
 
23ac29
@@ -1601,7 +1592,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
         DEBUGMSGTL(("usm", "Unsupported Security Level or type (%d)\n",
23ac29
                     theSecLevel));
23ac29
 
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_USM_UNSUPPORTEDSECURITYLEVEL;
23ac29
     }
23ac29
 
23ac29
@@ -1636,7 +1626,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
             DEBUGMSGTL(("usm",
23ac29
                         "couldn't malloc %d bytes for encrypted PDU\n",
23ac29
                         (int)ciphertextlen));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_MALLOC;
23ac29
         }
23ac29
 
23ac29
@@ -1652,7 +1641,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
                                htonl(boots_uint), htonl(time_uint),
23ac29
                                iv) == -1) {
23ac29
                 DEBUGMSGTL(("usm", "Can't set AES iv.\n"));
23ac29
-                usm_free_usmStateReference(secStateRef);
23ac29
                 SNMP_FREE(ciphertext);
23ac29
                 return SNMPERR_USM_GENERICERROR;
23ac29
             }
23ac29
@@ -1667,7 +1655,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
                                              thePrivKeyLength - 8,
23ac29
                                              iv) == -1)) {
23ac29
                 DEBUGMSGTL(("usm", "Can't set DES-CBC salt.\n"));
23ac29
-                usm_free_usmStateReference(secStateRef);
23ac29
                 SNMP_FREE(ciphertext);
23ac29
                 return SNMPERR_USM_GENERICERROR;
23ac29
             }
23ac29
@@ -1686,7 +1673,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
                        scopedPdu, scopedPduLen,
23ac29
                        ciphertext, &ciphertextlen) != SNMP_ERR_NOERROR) {
23ac29
             DEBUGMSGTL(("usm", "encryption error.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             SNMP_FREE(ciphertext);
23ac29
             return SNMPERR_USM_ENCRYPTIONERROR;
23ac29
         }
23ac29
@@ -1703,7 +1689,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
                                        ciphertext, ciphertextlen);
23ac29
         if (rc == 0) {
23ac29
             DEBUGMSGTL(("usm", "Encryption failed.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             SNMP_FREE(ciphertext);
23ac29
             return SNMPERR_USM_ENCRYPTIONERROR;
23ac29
         }
23ac29
@@ -1743,7 +1728,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     DEBUGINDENTLESS();
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building privParams failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1766,7 +1750,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     DEBUGINDENTLESS();
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building authParams failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1789,7 +1772,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     DEBUGINDENTLESS();
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building authParams failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1805,7 +1787,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm",
23ac29
                     "building msgAuthoritativeEngineTime failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1821,7 +1802,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm",
23ac29
                     "building msgAuthoritativeEngineBoots failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1833,7 +1813,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     DEBUGINDENTLESS();
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building msgAuthoritativeEngineID failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1846,7 +1825,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
                                      *offset - sp_offset);
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building usm security parameters failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1860,7 +1838,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
 
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building msgSecurityParameters failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1870,7 +1847,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     while ((*wholeMsgLen - *offset) < globalDataLen) {
23ac29
         if (!asn_realloc(wholeMsg, wholeMsgLen)) {
23ac29
             DEBUGMSGTL(("usm", "building global data failed.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_TOO_LONG;
23ac29
         }
23ac29
     }
23ac29
@@ -1886,7 +1862,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
                                                ASN_CONSTRUCTOR), *offset);
23ac29
     if (rc == 0) {
23ac29
         DEBUGMSGTL(("usm", "building master packet sequence failed.\n"));
23ac29
-        usm_free_usmStateReference(secStateRef);
23ac29
         return SNMPERR_TOO_LONG;
23ac29
     }
23ac29
 
23ac29
@@ -1904,7 +1879,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
 
23ac29
         if (temp_sig == NULL) {
23ac29
             DEBUGMSGTL(("usm", "Out of memory.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_GENERICERROR;
23ac29
         }
23ac29
 
23ac29
@@ -1915,14 +1889,12 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
             != SNMP_ERR_NOERROR) {
23ac29
             SNMP_FREE(temp_sig);
23ac29
             DEBUGMSGTL(("usm", "Signing failed.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_AUTHENTICATIONFAILURE;
23ac29
         }
23ac29
 
23ac29
         if (temp_sig_len != msgAuthParmLen) {
23ac29
             SNMP_FREE(temp_sig);
23ac29
             DEBUGMSGTL(("usm", "Signing lengths failed.\n"));
23ac29
-            usm_free_usmStateReference(secStateRef);
23ac29
             return SNMPERR_USM_AUTHENTICATIONFAILURE;
23ac29
         }
23ac29
 
23ac29
@@ -1933,7 +1905,6 @@ usm_rgenerate_out_msg(int msgProcModel,
23ac29
     /*
23ac29
      * endif -- create keyed hash 
23ac29
      */
23ac29
-    usm_free_usmStateReference(secStateRef);
23ac29
     DEBUGMSGTL(("usm", "USM processing completed.\n"));
23ac29
     return SNMPERR_SUCCESS;
23ac29
 }                               /* end usm_rgenerate_out_msg() */