Bug 298222 - corrupt response packet can cause bsnmpwalk to write outside of array
Summary: corrupt response packet can cause bsnmpwalk to write outside of array
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Maxim Konovalov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-09-05 20:05 UTC by Robert Morris
Modified: 2026-10-06 17:40 UTC (History)
4 users (show)

See Also:


Attachments
feed a negative error_index to snmpwalk (1.44 KB, text/plain)
2026-09-05 20:05 UTC, Robert Morris
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Morris 2026-09-05 20:05:48 UTC
Created attachment 274450 [details]
feed a negative error_index to snmpwalk

If the response packet to bsnmpwalk contains a negative error_index, it
is read into the signed int pdu->error_index by snmp_parse_pdus_hdr()
in contrib/bsnmp/lib/snmp.c:

                if (asn_get_integer(b, &pdu->error_index) != ASN_ERR_OK) {

Later snmp_output_err_resp() in
usr.sbin/bsnmpd/tools/libsnmptools/bsnmptools.c checks for too
large but not too small:

        if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
                fprintf(stdout, "Invalid error index in PDU\n");
                return;
        }

And can pass a bad pointer in the val argument to snmp_fill_object():

        if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, object,
            &(pdu->bindings[pdu->error_index - 1])) > 0))

asn_append_oid() fetches the length for memcpy() by dereferencing that
bad pointer, potentially causing a crash.

I've attached a demo that acts as a fake malicious snmp server, and
runs bsnmpwalk:

# uname -a
FreeBSD xxx 16.0-CURRENT FreeBSD 16.0-CURRENT #38 main-n275530-83854d0b37c2-dirty: Sat Aug 15 15:07:11 AST 2026     root@xxx:/usr/obj/usr/src/amd64.amd64/sys/GENERIC amd64
# cc snmpwalk1a.c
# ./a.out
SNMP: ignoring trailing junk in message
ASN.1: bad length for NULL (9) at 01 00 05 00 00 00 00 00 00 00 00
SNMP: ignoring junk at end of binding
SNMP: ignoring trailing junk after pdu
bsnmpwalk: Error 32 in response
Agent localhost:snmp returned error 
# ls -l *core*
-rw-------  1 root wheel 13295616 Sep  5 15:30 bsnmpwalk.core

#0  memcpy () at /usr/src/lib/libc/amd64/string/memmove.S:304
#1  0x000000080108369a in asn_append_oid (to=to@entry=0x801e14a10, 
    from=from@entry=0x7ffffffcac18) at /usr/src/contrib/bsnmp/lib/asn1.c:922
#2  0x000000000102fab6 in snmp_fill_object (
    snmptoolctx=snmptoolctx@entry=0x7fffffffe920, obj=obj@entry=0x801e14a00, 
    val=0x7ffffffcac18)
    at /usr/src/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c:1859
#3  0x000000000102f945 in snmp_output_err_resp (snmptoolctx=0x7fffffffe920, 
    pdu=0x7ffffffcb450)
    at /usr/src/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c:2009
#4  0x00000000010293db in snmptool_walk (snmptoolctx=0x7fffffffe920)
    at /usr/src/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c:490
#5  0x00000000010286d3 in main (argc=<optimized out>, argv=0x7fffffffe9f0)
    at /usr/src/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c:1288
Comment 1 Maxim Konovalov freebsd_committer freebsd_triage 2026-09-08 22:19:31 UTC
Hi Robert,

Thank you for the throughout report!

The most obvious fix would be the following (copy&paste, tabs are mangled)

diff --git a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
index d8fbb5529..16c4ea4ac 100644
--- a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
+++ b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
@@ -1993,7 +1993,8 @@ snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
        struct snmp_object *object;
        char buf[ASN_OIDSTRLEN];
 
-       if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
+       if (pdu == NULL || pdu->error_index < 1 ||
+          (pdu->error_index > (int32_t) pdu->nbindings)) {
                fprintf(stdout, "Invalid error index in PDU\n");
                return;
        }
%%%

Please let me know if this works for you.

I also checked the bugzilla history and noticed that you have submitted dozens, if not hundreds, bug reports.  Some of them are very sophisticated, e.g., for tcp fastopen.

Would you mind sharing a story behind them?

Thank you,

Maxim
Comment 2 Maxim Konovalov freebsd_committer freebsd_triage 2026-09-08 22:30:25 UTC
* thorough report
Comment 3 Robert Morris 2026-09-16 12:33:15 UTC
(In reply to Maxim Konovalov from comment #1)
I've built myself a fuzzer guided by "concolic
execution", similar to this paper:

  https://www.usenix.org/system/files/usenixsecurity24-qi.pdf

My tool is a RISC-V emulator (based on Fabrice Bellard's tinyemu)
that tracks how branch conditions are related to inputs, and
for each input-dependent branch tries to find an input (using z3)
that would send the branch the other way.

The kinds of bugs this technique can find are those that trip
over panics or crashes or assertion failures. It doesn't
know anything about correctness. It's at its best with
recently-written C code that parses complex binary input formats.

Most of the work has been in producing test harnesses that
exersize pieces of code in which I'm looking for failures, and
in attempting to find the underlying reason for the failure.
Comment 4 Maxim Konovalov freebsd_committer freebsd_triage 2026-09-16 16:05:35 UTC
Impressive. Thanks for sharing, Robert.  Appreciate it.

I guess, FreeBSD Magazine folks (https://freebsdfoundation.org/our-work/journal/) would be interested in such great content.

I'd also encourage you to drop a note to the freebsd-hackers@ mailing list.

Maxim
Comment 5 commit-hook freebsd_committer freebsd_triage 2026-09-22 21:50:21 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=296e3fd54ca8972fa6696974097a2f2705f8dfc4

commit 296e3fd54ca8972fa6696974097a2f2705f8dfc4
Author:     Maxim Konovalov <maxim@FreeBSD.org>
AuthorDate: 2026-09-22 21:32:43 +0000
Commit:     Maxim Konovalov <maxim@FreeBSD.org>
CommitDate: 2026-09-22 21:32:43 +0000

    bsnmp: validate the lower bound of error_index in responses

    Check if the response's error_index is within a sane interval.
    Otherwise, a rogue peer could crash us.

    PR:             298222
    Reported by:    Robert Morris
    Reviewed by:    markj
    Discussed with: secteam (markj)
    MFC after:      2 weeks
    Analyzed with:  Claude Code Opus 5

 usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c      | 8 ++++++--
 usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c | 3 ++-
 2 files changed, 8 insertions(+), 3 deletions(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-10-06 17:40:13 UTC
A commit in branch stable/15 references this bug:

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

commit de5689d1e6ea621249e1ef44db3c2d3ee8431ba5
Author:     Maxim Konovalov <maxim@FreeBSD.org>
AuthorDate: 2026-09-22 21:32:43 +0000
Commit:     Maxim Konovalov <maxim@FreeBSD.org>
CommitDate: 2026-10-06 17:37:11 +0000

    bsnmp: validate the lower bound of error_index in responses

    Check if the response's error_index is within a sane interval.
    Otherwise, a rogue peer could crash us.

    PR:             298222
    Reported by:    Robert Morris
    Reviewed by:    markj
    Discussed with: secteam (markj)
    MFC after:      2 weeks
    Analyzed with:  Claude Code Opus 5

    (cherry picked from commit 296e3fd54ca8972fa6696974097a2f2705f8dfc4)

 usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c      | 8 ++++++--
 usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c | 3 ++-
 2 files changed, 8 insertions(+), 3 deletions(-)