Blame SOURCES/net-snmp-5.8-v3-forward.patch

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