Blame SOURCES/net-snmp-5.7.2-sec-counter.patch

e7c6a6
diff -up net-snmp-5.7.2/include/net-snmp/library/snmpusm.h.bz1809076 net-snmp-5.7.2/include/net-snmp/library/snmpusm.h
e7c6a6
--- net-snmp-5.7.2/include/net-snmp/library/snmpusm.h.bz1809076	2020-03-02 14:11:34.000000000 +0100
e7c6a6
+++ net-snmp-5.7.2/include/net-snmp/library/snmpusm.h	2020-03-02 14:05:12.000000000 +0100
e7c6a6
@@ -34,6 +34,7 @@ extern          "C" {
e7c6a6
      * Structures.
e7c6a6
      */
e7c6a6
     struct usmStateReference {
e7c6a6
+        int             refcnt;
e7c6a6
         char           *usr_name;
e7c6a6
         size_t          usr_name_length;
e7c6a6
         u_char         *usr_engine_id;
e7c6a6
diff -up net-snmp-5.7.2/snmplib/snmp_client.c.bz1809076 net-snmp-5.7.2/snmplib/snmp_client.c
e7c6a6
--- net-snmp-5.7.2/snmplib/snmp_client.c.bz1809076	2020-03-02 14:11:27.000000000 +0100
e7c6a6
+++ net-snmp-5.7.2/snmplib/snmp_client.c	2020-03-02 14:03:40.000000000 +0100
e7c6a6
@@ -391,27 +391,16 @@ _clone_pdu_header(netsnmp_pdu *pdu)
e7c6a6
         return NULL;
e7c6a6
     }
e7c6a6
 
e7c6a6
-    if (pdu != NULL && pdu->securityStateRef &&
e7c6a6
-        pdu->command == SNMP_MSG_TRAP2) {
e7c6a6
-
e7c6a6
-        ret = usm_clone_usmStateReference((struct usmStateReference *) pdu->securityStateRef,
e7c6a6
-                (struct usmStateReference **) &newpdu->securityStateRef );
e7c6a6
-
e7c6a6
-        if (ret)
e7c6a6
-        {
e7c6a6
+    sptr = find_sec_mod(newpdu->securityModel);
e7c6a6
+    if (sptr && sptr->pdu_clone) {
e7c6a6
+        /* call security model if it needs to know about this */
e7c6a6
+        ret = sptr->pdu_clone(pdu, newpdu);
e7c6a6
+        if (ret) {
e7c6a6
             snmp_free_pdu(newpdu);
e7c6a6
             return 0;
e7c6a6
         }
e7c6a6
     }
e7c6a6
 
e7c6a6
-    if ((sptr = find_sec_mod(newpdu->securityModel)) != NULL &&
e7c6a6
-        sptr->pdu_clone != NULL) {
e7c6a6
-        /*
e7c6a6
-         * call security model if it needs to know about this 
e7c6a6
-         */
e7c6a6
-        (*sptr->pdu_clone) (pdu, newpdu);
e7c6a6
-    }
e7c6a6
-
e7c6a6
     return newpdu;
e7c6a6
 }
e7c6a6
 
e7c6a6
diff -up net-snmp-5.7.2/snmplib/snmpusm.c.bz1809076 net-snmp-5.7.2/snmplib/snmpusm.c
e7c6a6
--- net-snmp-5.7.2/snmplib/snmpusm.c.bz1809076	2020-03-02 14:11:20.000000000 +0100
e7c6a6
+++ net-snmp-5.7.2/snmplib/snmpusm.c	2020-03-02 14:08:30.000000000 +0100
e7c6a6
@@ -192,43 +192,63 @@ free_enginetime_on_shutdown(int majorid,
e7c6a6
 struct usmStateReference *
e7c6a6
 usm_malloc_usmStateReference(void)
e7c6a6
 {
e7c6a6
-    struct usmStateReference *retval = (struct usmStateReference *)
e7c6a6
-        calloc(1, sizeof(struct usmStateReference));
e7c6a6
+    struct usmStateReference *retval;
e7c6a6
+
e7c6a6
+    retval = calloc(1, sizeof(struct usmStateReference));
e7c6a6
+    if (retval)
e7c6a6
+        retval->refcnt = 1;
e7c6a6
 
e7c6a6
     return retval;
e7c6a6
 }                               /* end usm_malloc_usmStateReference() */
e7c6a6
 
e7c6a6
+static int
e7c6a6
+usm_clone(netsnmp_pdu *pdu, netsnmp_pdu *new_pdu)
e7c6a6
+{
e7c6a6
+    struct usmStateReference *ref = pdu->securityStateRef;
e7c6a6
+    struct usmStateReference **new_ref =
e7c6a6
+        (struct usmStateReference **)&new_pdu->securityStateRef;
e7c6a6
+    int ret = 0;
e7c6a6
+
e7c6a6
+    if (!ref)
e7c6a6
+        return ret;
e7c6a6
+
e7c6a6
+    if (pdu->command == SNMP_MSG_TRAP2) {
e7c6a6
+        netsnmp_assert(pdu->securityModel == SNMP_DEFAULT_SECMODEL);
e7c6a6
+        ret = usm_clone_usmStateReference(ref, new_ref);
e7c6a6
+    } else {
e7c6a6
+        netsnmp_assert(ref == *new_ref);
e7c6a6
+        ref->refcnt++;
e7c6a6
+    }
e7c6a6
+
e7c6a6
+    return ret;
e7c6a6
+}
e7c6a6
 
e7c6a6
 void
e7c6a6
 usm_free_usmStateReference(void *old)
e7c6a6
 {
e7c6a6
-    struct usmStateReference *old_ref = (struct usmStateReference *) old;
e7c6a6
+    struct usmStateReference *ref = old;
e7c6a6
 
e7c6a6
-    if (old_ref) {
e7c6a6
+    if (!ref)
e7c6a6
+        return;
e7c6a6
 
e7c6a6
-        if (old_ref->usr_name_length)
e7c6a6
-            SNMP_FREE(old_ref->usr_name);
e7c6a6
-        if (old_ref->usr_engine_id_length)
e7c6a6
-            SNMP_FREE(old_ref->usr_engine_id);
e7c6a6
-        if (old_ref->usr_auth_protocol_length)
e7c6a6
-            SNMP_FREE(old_ref->usr_auth_protocol);
e7c6a6
-        if (old_ref->usr_priv_protocol_length)
e7c6a6
-            SNMP_FREE(old_ref->usr_priv_protocol);
e7c6a6
-
e7c6a6
-        if (old_ref->usr_auth_key_length && old_ref->usr_auth_key) {
e7c6a6
-            SNMP_ZERO(old_ref->usr_auth_key, old_ref->usr_auth_key_length);
e7c6a6
-            SNMP_FREE(old_ref->usr_auth_key);
e7c6a6
-        }
e7c6a6
-        if (old_ref->usr_priv_key_length && old_ref->usr_priv_key) {
e7c6a6
-            SNMP_ZERO(old_ref->usr_priv_key, old_ref->usr_priv_key_length);
e7c6a6
-            SNMP_FREE(old_ref->usr_priv_key);
e7c6a6
-        }
e7c6a6
+    if (--ref->refcnt > 0)
e7c6a6
+        return;
e7c6a6
 
e7c6a6
-        SNMP_ZERO(old_ref, sizeof(*old_ref));
e7c6a6
-        SNMP_FREE(old_ref);
e7c6a6
+    SNMP_FREE(ref->usr_name);
e7c6a6
+    SNMP_FREE(ref->usr_engine_id);
e7c6a6
+    SNMP_FREE(ref->usr_auth_protocol);
e7c6a6
+    SNMP_FREE(ref->usr_priv_protocol);
e7c6a6
 
e7c6a6
+    if (ref->usr_auth_key_length && ref->usr_auth_key) {
e7c6a6
+        SNMP_ZERO(ref->usr_auth_key, ref->usr_auth_key_length);
e7c6a6
+        SNMP_FREE(ref->usr_auth_key);
e7c6a6
+    }
e7c6a6
+    if (ref->usr_priv_key_length && ref->usr_priv_key) {
e7c6a6
+        SNMP_ZERO(ref->usr_priv_key, ref->usr_priv_key_length);
e7c6a6
+        SNMP_FREE(ref->usr_priv_key);
e7c6a6
     }
e7c6a6
 
e7c6a6
+    SNMP_FREE(ref);
e7c6a6
 }                               /* end usm_free_usmStateReference() */
e7c6a6
 
e7c6a6
 struct usmUser *
e7c6a6
@@ -3184,6 +3204,7 @@ init_usm(void)
e7c6a6
     def->encode_reverse = usm_secmod_rgenerate_out_msg;
e7c6a6
     def->encode_forward = usm_secmod_generate_out_msg;
e7c6a6
     def->decode = usm_secmod_process_in_msg;
e7c6a6
+    def->pdu_clone = usm_clone;
e7c6a6
     def->pdu_free_state_ref = usm_free_usmStateReference;
e7c6a6
     def->session_setup = usm_session_init;
e7c6a6
     def->handle_report = usm_handle_report;