Skip to content

SNMP: Fix parsing of truncated ASN.1 length fields#2450

Open
rousskov wants to merge 2 commits into
squid-cache:masterfrom
measurement-factory:SQUID-111-asn_parse_length-lookahead
Open

SNMP: Fix parsing of truncated ASN.1 length fields#2450
rousskov wants to merge 2 commits into
squid-cache:masterfrom
measurement-factory:SQUID-111-asn_parse_length-lookahead

Conversation

@rousskov

Copy link
Copy Markdown
Contributor

Also improved parsing of truncated input for a few other ASN.1 fields.

Also encapsulated heavily duplicated ASN.1 type field parsing code.

rousskov added 2 commits June 26, 2026 15:47
XXX: This implementation assumes that asn_parse_length() receives input
length in `*length`. It does not. `*length` is undefined in current
callers, making reasonable asn_parse_length() implementation impossible.
Also encapsulated duplicated ASN `type` parsing code into a local helper
function.

@rousskov rousskov left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: My confidence in this ugly code is low. It passes an SNMP walk test, but some buggy variations of this code passed that test as well, so that test alone is not a good indication that these changes did not make things worse somewhere!

If you can test this code, please do.

Comment thread lib/snmplib/asn1.c
return (NULL);
}

u_char lengthbyte = *data;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of input access that lacks length checks in official code.

To add the missing check, I had to add a dataLength parameter to asn_parse_length(). Most (if not all) ASN parsing functions update their data length parameter, and such updates are a good idea, so new PR code follows that pattern. Naturally, all that required caller adjustments. While adjusting caller code, I added a few other "obviously" missing checks. I did not perform a full audit though!

This comment does not request any changes.

@rousskov rousskov added S-waiting-for-more-reviewers needs a reviewer and/or a second opinion S-could-use-an-approval An approval may speed this PR merger (but is not required) labels Jun 27, 2026
Comment thread include/asn1.h
u_char *asn_build_header_with_truth(u_char *, int *, u_char, int, int);

u_char *asn_parse_length(u_char *, u_int *);
u_char *asn_parse_length(u_char *, int *, u_int *);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This being an ABI breaking change in a bundled third-party library please just remove the dropped function and make the custom one a static like the new type parser (shuffle it up higher in the file as needed).

Suggested change
u_char *asn_parse_length(u_char *, int *, u_int *);

Comment thread lib/snmplib/asn1.c
* field bytes or all of the expected variable-length object bytes). OUT
* values documented below are only meaningful for successful outcomes.
*/
u_char *

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
u_char *
static u_char *

and shuffle the whole changed function up above first caller.

@yadij

yadij commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

FTR; The current official code for this library is published as the "ucd-snmp" legacy API by the NetSNMP project (libsnmp or net-snmp in most OS distributors). I am working on several followup PRs to drop this ancient Squid bundled snmplib code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-could-use-an-approval An approval may speed this PR merger (but is not required) S-waiting-for-more-reviewers needs a reviewer and/or a second opinion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants