diff options
author | ngie <ngie@FreeBSD.org> | 2017-01-07 08:28:51 +0000 |
---|---|---|
committer | ngie <ngie@FreeBSD.org> | 2017-01-07 08:28:51 +0000 |
commit | b6903072edd16b3e4bdb742fb6249a7c2430b4c0 (patch) | |
tree | c332a3535ac09b8ef9e9174fcfaf79bc3310bc5e /usr.sbin | |
parent | 733659d4511f6c512213b585fa20b5fb34c10761 (diff) | |
download | FreeBSD-src-b6903072edd16b3e4bdb742fb6249a7c2430b4c0.zip FreeBSD-src-b6903072edd16b3e4bdb742fb6249a7c2430b4c0.tar.gz |
MFC r310954,r310987,r311222:
r310954:
Set value->v.octetstring.len to a correct value on malloc success/failure
The previous code always set value->v.octetstring.len to len, regardless
of the result from the malloc call. This misleads the caller on malloc
failure. Set .len to len on success and 0 on failure.
CID: 1007590
r310987:
snmp_output_err_resp, snmp_output_resp: allocate `object` using calloc, not
on the stack
Some of the callers try to determine whether or not `object` is valid by
testing the value for NULL, which will never be true if it's a stack value,
so in order to be clear and correct down the call stack, use a heap
allocated object.
This also addresses a Coverity issue by initializing all of `object` via
calloc
CID: 1006392
r311222:
Fix logical inversion when checking result from calloc
in snmp_output_err_resp(..)
CID: 1368195
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c | 6 | ||||
-rw-r--r-- | usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c | 31 |
2 files changed, 26 insertions, 11 deletions
diff --git a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c index cd7316f..32ba216 100644 --- a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c +++ b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c @@ -266,13 +266,13 @@ parse_octetstring(struct snmp_value *value, char *val) return (-1); } - value->v.octetstring.len = len; - - if((value->v.octetstring.octets = malloc(len)) == NULL) { + if ((value->v.octetstring.octets = malloc(len)) == NULL) { + value->v.octetstring.len = 0; syslog(LOG_ERR,"malloc failed: %s", strerror(errno)); return (-1); } + value->v.octetstring.len = len; memcpy(value->v.octetstring.octets, val, len); value->syntax = SNMP_SYNTAX_OCTETSTRING; diff --git a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c index 8731287..3ad5a63 100644 --- a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c +++ b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c @@ -2008,20 +2008,25 @@ snmp_output_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *o) void snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu) { + struct snmp_object *object; char buf[ASN_OIDSTRLEN]; - struct snmp_object object; if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) { - fprintf(stdout,"Invalid error index in PDU\n"); + fprintf(stdout, "Invalid error index in PDU\n"); + return; + } + + if ((object = calloc(1, sizeof(struct snmp_object))) == NULL) { + fprintf(stdout, "calloc: %s", strerror(errno)); return; } fprintf(stdout, "Agent %s:%s returned error \n", snmp_client.chost, snmp_client.cport); - if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, &object, + if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, object, &(pdu->bindings[pdu->error_index - 1])) > 0)) - snmp_output_object(snmptoolctx, &object); + snmp_output_object(snmptoolctx, object); else { asn_oid2str_r(&(pdu->bindings[pdu->error_index - 1].var), buf); fprintf(stdout,"%s", buf); @@ -2033,17 +2038,23 @@ snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu) fprintf(stdout, "%s\n", error_strings[pdu->error_status].str); else fprintf(stdout,"%s\n", error_strings[SNMP_ERR_UNKNOWN].str); + + free(object); + object = NULL; } int32_t snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu, struct asn_oid *root) { - struct snmp_object object; + struct snmp_object *object; char p[ASN_OIDSTRLEN]; int32_t error; uint32_t i; + if ((object = calloc(1, sizeof(struct snmp_object))) == NULL) + return (-1); + i = error = 0; while (i < pdu->nbindings) { if (root != NULL && !(asn_is_suboid(root, @@ -2052,18 +2063,22 @@ snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu, if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) { if (!ISSET_NUMERIC(snmptoolctx) && - (snmp_fill_object(snmptoolctx, &object, + (snmp_fill_object(snmptoolctx, object, &(pdu->bindings[i])) > 0)) - snmp_output_object(snmptoolctx, &object); + snmp_output_object(snmptoolctx, object); else { asn_oid2str_r(&(pdu->bindings[i].var), p); fprintf(stdout, "%s", p); } } - error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), object.info); + error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), + object->info); i++; } + free(object); + object = NULL; + if (error) return (-1); |