The two attached files show that libksba can read form uninitalized memory for invalid ASN.1 inputs. One danger is that
uninitialized memory might contain valuable information (keys, nonces, or simply addresses of functions, defeating ASLR) that will
be incorporated in the dataflow of the program using libksba and eventually sent back to the attacker.
In order to make the use of uninitialized memory obvious, please apply the following patch:
~/instrumented/libksba-1.3.4$ diff -u src/util.c{~,}
- src/util.c~ 2013-03-15 20:26:38.000000000 +0100
+++ src/util.c 2016-05-11 02:18:39.685533973 +0200
@@ -121,13 +121,13 @@
/* Wrapper for the common memory allocation functions. These are here
so that we can add hooks. The corresponding macros should be used. These macros are not named xfoo() because this name is commonly
- used for function which die on errror. We use macronames like
+ used for function which die on error. We use macronames like
xtryfoo() instead. */
void *
ksba_malloc (size_t n )
{
- return alloc_func (n);
+ void *p = alloc_func (n); if (p && n) memset(p,0x3e,n); return p;
}
void *
This fills memory that would otherwise have been uninitialized (coming from malloc()) with the character 3E. This is not supposed
to influence the behavior of a well-behaved program, as the contents of a block allocated by malloc() are indeterminate and
shouldn't be used, in C standard parlance.
~/instrumented/libksba-1.3.4$ ./tests/cert-basic uninitialized1.crt
Certificate in `uninitialized1.crt':
serial....: (#00#) issuer....:
`0.23.13.48.51.48.51.51.48.49=#39343E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3
E,0.0.0.0=#43412043657274205369676E696E6720417574686F00800000,OU=http://www.cacert.org,O=Root CA'
...
Uninitialized memory is read here and is shown by the 3E3E3E... sequence.
~/instrumented/libksba-1.3.4$ ./tests/cert-basic uninitialized2.crt
Certificate in `uninitialized2.crt':
serial....: (#00EE04D9636253D675#) issuer....: `CN=a' subject...: `CN=a' notBefore.: none notAfter..: 2042-08-30 23:09:33 hash algo.: (null)
cert-basic.c:464: public key not found
Extn: 2.5.29.14 at 206 with length 22
SubjectKeyIdentifier: (#3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E#)
...
Uninitialized memory is read here and is shown by the 3E3E3E... sequence.
tis-interpreter, applied to the non-instrumented version of libksba 1.3.4, provides the following information:
For uninitialized1.crt:
Certificate in `t.crt':
serial....:
00
src/dn.c:628:[kernel] warning: accessing uninitialized left-value:
assert \initialized(image+((node->off+node->nhdr)+i)); stack: append_atv :: src/dn.c:667 <- dn_to_str :: src/dn.c:692 <- _ksba_dn_to_str :: src/cert.c:609 <- get_name :: src/cert.c:744 <- _ksba_cert_get_issuer :: src/visibility.c:190 <- ksba_cert_get_issuer :: tests/cert-basic.c:424 <- one_file :: tests/cert-basic.c:593 <- main
src/dn.c:628:[kernel] warning: completely indeterminate value in mallocksba_malloc_l130_935 with offset 1104 bits.
For uninitialized2.crt:
Certificate in `t.crt':
serial....:
00
EE
04
63
62
53
75
issuer....:
`CN=a'
subject...:
`CN=a'
notBefore.:
none
notAfter..:
2042-08-30 23:09:33
hash algo.: (null)
fprintf(__fc_stderr,...)
tests/cert-basic.c:464: public key not found
Extn: 2.5.29.14 at 206 with length 22
SubjectKeyIdentifier:
tests/t-common.h:137:[kernel] warning: accessing uninitialized left-value: assert \initialized(s);
stack: print_sexp :: tests/cert-basic.c:182 <- list_extensions :: tests/cert-basic.c:546 <- one_file :: tests/cert-basic.c:593 <- main
tests/t-common.h:137:[kernel] warning: completely indeterminate value in mallocksba_malloc_l130_944 with offset 32 bits.