git: 18a5823dc565 - stable/15 - bsnmp: do not act on an SNMPv3 discovery message that carries bindings

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:06:10 UTC
The branch stable/15 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=18a5823dc5655caab3ac3a660d5079dd388c2132

commit 18a5823dc5655caab3ac3a660d5079dd388c2132
Author:     Maxim Konovalov <maxim@FreeBSD.org>
AuthorDate: 2026-11-13 03:00:02 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 16:57:23 +0000

    bsnmp: do not act on an SNMPv3 discovery message that carries bindings
    
    An SNMPv3 message with an empty engine id and empty user name is handled
    as snmpEngineID discovery: snmp_pdu_auth_user() sets SNMP_MSG_AUTODISCOVER
    and returns without a USM user, and snmp_pdu_auth_access() then grants the
    message access.  A genuine discovery request carries no variable bindings
    and only elicits a Report, but a discovery PDU that did carry bindings was
    executed anyway -- so a GET or SET ran with no authentication and without
    the VACM access check.
    
    This affects two cases.  With an empty user name the message is wholly
    unauthenticated, so anyone able to reach the agent could issue a SET.  It
    also affects a configured noAuthNoPriv user: such a user has no digest to
    verify, so it too can set the AUTODISCOVER flag (an authenticated request
    before time synchronisation) and have a bindings-carrying PDU bypass VACM
    and operate outside its views.
    
    Refuse any AUTODISCOVER message that arrives with variable bindings
    instead of acting on it.  Empty-binding discovery, the authenticated
    time-window exchange, and ordinary authenticated SNMPv3 are unaffected.
    
    While here, free the PDU on the SNMPD_INPUT_FAILED path of snmpd_input().
    Every other path releases it, but the failed path consumed the datagram
    and returned without freeing the decoded bindings -- so a rejected PDU
    leaked any heap they held, such as the buffer for an OCTET STRING in a
    SET.  Rejecting a bindings-carrying discovery message now reaches that
    path, so an unauthenticated client could otherwise grow the agent's
    memory without bound.
    
    For that free to be safe, snmp_input_start() must leave the PDU in a
    defined state on every return.  It zeroed the PDU only after the initial
    snmp_pdu_snoop() check, so a malformed datagram that failed the snoop
    returned SNMPD_INPUT_FAILED with the caller's PDU uninitialised; freeing
    it would then walk a garbage binding count.  Zero the PDU up front,
    before the snoop, so all of these paths leave an empty PDU that frees
    cleanly.
    
    Approved by:    so
    Security:       FreeBSD-SA-26:89.bsnmp
    Security:       CVE-2026-127742
    Reviewed by:    harti
    Fixes:          135f7de5ddb6
---
 contrib/bsnmp/snmpd/main.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/contrib/bsnmp/snmpd/main.c b/contrib/bsnmp/snmpd/main.c
index 933ab7aa655a..add9ec680eb1 100644
--- a/contrib/bsnmp/snmpd/main.c
+++ b/contrib/bsnmp/snmpd/main.c
@@ -496,6 +496,10 @@ snmp_input_start(const u_char *buf, size_t len, const char *source,
 
 	ret = SNMPD_INPUT_OK;
 
+	/* Zero the PDU before anything can return, so a failure path that
+	 * runs before the header is decoded still leaves it safe to free. */
+	memset(pdu, 0, sizeof(*pdu));
+
 	/* look whether we have enough bytes for the entire PDU. */
 	switch (sret = snmp_pdu_snoop(&b)) {
 
@@ -508,7 +512,6 @@ snmp_input_start(const u_char *buf, size_t len, const char *source,
 	}
 	b.asn_len = *pdulen = (size_t)sret;
 
-	memset(pdu, 0, sizeof(*pdu));
 	if ((code = snmp_pdu_decode_header(&b, pdu)) != SNMP_CODE_OK)
 		goto decoded;
 
@@ -645,6 +648,17 @@ decoded:
 	     (pdu->engine.engine_boots == 0 || pdu->engine.engine_time == 0)) {
 		snmpd_usmstats.not_in_time_windows++;
 		ret = SNMPD_INPUT_FAILED;
+	} else if ((pdu->flags & SNMP_MSG_AUTODISCOVER) != 0) {
+		/*
+		 * A discovery message (RFC 3414) may only elicit a Report and
+		 * carries no variable bindings of its own.  Reaching here with
+		 * bindings means the message was not authenticated as a user
+		 * (empty security name, or a noAuthNoPriv user): acting on it
+		 * would run a GET or SET that bypasses USM authentication and
+		 * the VACM access check.  Refuse it.
+		 */
+		snmpd_usmstats.unknown_users++;
+		ret = SNMPD_INPUT_FAILED;
 	}
 
 	if ((code = snmp_pdu_auth_access(pdu, ip)) != SNMP_CODE_OK)
@@ -1088,6 +1102,7 @@ snmpd_input(struct port_input *pi, struct tport *tport)
 	 * check the access first. We don't want to return an error response
 	 * to a proxy PDU with a wrong community */
 	if (ierr == SNMPD_INPUT_FAILED) {
+		snmp_pdu_free(&pdu);
 		/* for streaming transports this is fatal */
 		if (pi->stream)
 			return (-1);