asn1: CVE-2022-44640 invalid free in ASN.1 codec

Heimdal's ASN.1 compiler generates code that allows specially
crafted DER encodings of CHOICEs to invoke the wrong free function
on the decoded structure upon decode error.  This is known to impact
the Heimdal KDC, leading to an invalid free() of an address partly
or wholly under the control of the attacker, in turn leading to a
potential remote code execution (RCE) vulnerability.

This error affects the DER codec for all CHOICE types used in
Heimdal, though not all cases will be exploitable.  We have not
completed a thorough analysis of all the Heimdal components
affected, thus the Kerberos client, the X.509 library, and other
parts, may be affected as well.

This bug has been in Heimdal since 2005.  It was first reported by
Douglas Bagnall, though it had been found independently by the
Heimdal maintainers via fuzzing a few weeks earlier.
This commit is contained in:
Nicolas Williams
2021-03-10 16:49:04 -06:00
parent 2a4210b7e9
commit 9c9dac2b16
5 changed files with 15 additions and 6 deletions

Binary file not shown.

View File

@@ -694,14 +694,14 @@ decode_type(const char *name, const Type *t, int optional, struct value *defval,
classname(cl), classname(cl),
ty ? "CONS" : "PRIM", ty ? "CONS" : "PRIM",
valuename(cl, tag)); valuename(cl, tag));
fprintf(codefile,
"(%s)->element = %s;\n",
name, m->label);
if (asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&", if (asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&",
name, m->gen_name) < 0 || s == NULL) name, m->gen_name) < 0 || s == NULL)
errx(1, "malloc"); errx(1, "malloc");
decode_type(s, m->type, m->optional, NULL, forwstr, m->gen_name, decode_type(s, m->type, m->optional, NULL, forwstr, m->gen_name,
NULL, depth + 1); NULL, depth + 1);
fprintf(codefile,
"(%s)->element = %s;\n",
name, m->label);
free(s); free(s);
fprintf(codefile, fprintf(codefile,
"}\n"); "}\n");
@@ -710,23 +710,23 @@ decode_type(const char *name, const Type *t, int optional, struct value *defval,
if (have_ellipsis) { if (have_ellipsis) {
fprintf(codefile, fprintf(codefile,
"else {\n" "else {\n"
"(%s)->element = %s;\n"
"(%s)->u.%s.data = calloc(1, len);\n" "(%s)->u.%s.data = calloc(1, len);\n"
"if ((%s)->u.%s.data == NULL) {\n" "if ((%s)->u.%s.data == NULL) {\n"
"e = ENOMEM; %s;\n" "e = ENOMEM; %s;\n"
"}\n" "}\n"
"(%s)->u.%s.length = len;\n" "(%s)->u.%s.length = len;\n"
"memcpy((%s)->u.%s.data, p, len);\n" "memcpy((%s)->u.%s.data, p, len);\n"
"(%s)->element = %s;\n"
"p += len;\n" "p += len;\n"
"ret += len;\n" "ret += len;\n"
"len = 0;\n" "len = 0;\n"
"}\n", "}\n",
name, have_ellipsis->label,
name, have_ellipsis->gen_name, name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name, name, have_ellipsis->gen_name,
forwstr, forwstr,
name, have_ellipsis->gen_name, name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name, name, have_ellipsis->gen_name);
name, have_ellipsis->label);
} else { } else {
fprintf(codefile, fprintf(codefile,
"else {\n" "else {\n"

View File

@@ -62,6 +62,13 @@ free_type (const char *name, const Type *t, int preserve)
case TNull: case TNull:
case TGeneralizedTime: case TGeneralizedTime:
case TUTCTime: case TUTCTime:
/*
* This doesn't do much, but it leaves zeros where garbage might
* otherwise have been found. Gets us closer to having the equivalent
* of a memset()-to-zero data structure after calling the free
* functions.
*/
fprintf(codefile, "*%s = 0;\n", name);
break; break;
case TBitString: case TBitString:
if (HEIM_TAILQ_EMPTY(t->members)) if (HEIM_TAILQ_EMPTY(t->members))

View File

@@ -1600,6 +1600,7 @@ generate_template(const Symbol *s)
"int ASN1CALL\n" "int ASN1CALL\n"
"decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n" "decode_%s(const unsigned char *p, size_t len, %s *data, size_t *size)\n"
"{\n" "{\n"
" memset(data, 0, sizeof(*data));\n"
" return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n" " return _asn1_decode_top(asn1_%s, 0|%s, p, len, data, size);\n"
"}\n" "}\n"
"\n", "\n",

View File

@@ -81,6 +81,7 @@ EXPORTS
KrbFastFinished, KrbFastFinished,
KrbFastReq, KrbFastReq,
KrbFastArmor, KrbFastArmor,
KrbFastArmoredReq,
KDCFastState, KDCFastState,
KDCFastCookie, KDCFastCookie,
KDC-PROXY-MESSAGE, KDC-PROXY-MESSAGE,