• Eric Biggers's avatar
    crypto: rsa - fix buffer overread when stripping leading zeroes · 80dbdc5a
    Eric Biggers authored
    commit d2890c37 upstream.
    
    In rsa_get_n(), if the buffer contained all 0's and "FIPS mode" is
    enabled, we would read one byte past the end of the buffer while
    scanning the leading zeroes.  Fix it by checking 'n_sz' before '!*ptr'.
    
    This bug was reachable by adding a specially crafted key of type
    "asymmetric" (requires CONFIG_RSA and CONFIG_X509_CERTIFICATE_PARSER).
    
    KASAN report:
    
        BUG: KASAN: slab-out-of-bounds in rsa_get_n+0x19e/0x1d0 crypto/rsa_helper.c:33
        Read of size 1 at addr ffff88003501a708 by task keyctl/196
    
        CPU: 1 PID: 196 Comm: keyctl Not tainted 4.14.0-09238-g1d3b78bb #26
        Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-20171110_100015-anatol 04/01/2014
        Call Trace:
         rsa_get_n+0x19e/0x1d0 crypto/rsa_helper.c:33
         asn1_ber_decoder+0x82a/0x1fd0 lib/asn1_decoder.c:328
         rsa_set_pub_key+0xd3/0x320 crypto/rsa.c:278
         crypto_akcipher_set_pub_key ./incl...
    80dbdc5a
rsa_helper.c 4.24 KB