Skip to content

DSA_sign hangs with invalid parameters #20268

Closed

Description

@guidovranken

This hangs:

#include <openssl/dsa.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <string.h>

#define CF_CHECK_NE(expr, res) if ( (expr) == (res) ) { goto end; }

int main(int argc, char *argv[])
{
    DSA *dsa;
    BIGNUM *p, *q, *g, *priv_key, *pub_key;
    unsigned char *signature;
    const unsigned char msg[] = {0x00};
    unsigned int signature_len;

    p = BN_new();
    g = BN_new();
    q = BN_new();
    priv_key = BN_new();
    pub_key = BN_new();

    BN_dec2bn(&p, "26959946667150639794667015087019640346410327083120074548994958668279");
    BN_dec2bn(&q, "1");
    BN_dec2bn(&g, "11031159150393236145358169167399899033873710076688195872983140529812576");
    BN_dec2bn(&pub_key, "0");
    BN_dec2bn(&priv_key, "44072239980705620047479035580457605556085320000");

    dsa = DSA_new();
    CF_CHECK_NE(DSA_set0_pqg(dsa, p, q, g), 0);
    CF_CHECK_NE(DSA_set0_key(dsa, pub_key, priv_key), 0);

    signature = malloc(DSA_size(dsa));

    DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa);

end:
    DSA_free(dsa);
    free(signature);

    return 0;
}

If I instruct my fuzzer to not call DSA_sign if these constraints are violated:

  • P > Q
  • Q > priv
  • priv != 0

Then no hang occurs (as far as I can tell now).

So adding these checks in DSA_sign will presumably resolve the issue.

BoringSSL does not hang except when priv == 0. @davidben

I can't test LibreSSL right now because the build is broken on Linux. @botovq

Activity

  1. added
    issue: bug report
    on Feb 11, 2023
  2. davidben commented on Feb 11, 2023

    @davidben
    Contributor

    BoringSSL does not hang except when priv == 0. @davidben

    Hmm, I'm not able to repro this. To make sure I'm trying the right thing, this is if I use the test program, but replacing the priv_key line with BN_dec2bn(&priv_key, "0");?

  3. davidben commented on Feb 11, 2023

    @davidben
    Contributor

    Oh, it's not doing anything because we reject any q values that aren't exactly 160-, 224-, or 256-bit, as of https://boringssl-review.googlesource.com/c/boringssl/+/42504

    If I adjust q to be in size and then put g in bounds, I can repro. Looking...

  4. botovq commented on Feb 11, 2023

    @botovq
    Contributor
  5. davidben commented on Feb 13, 2023

    @davidben
    Contributor

    Fixes for BoringSSL:

    This reminded me that @hannob had reported something similar way back, but it slipped through, so that series also fixes that report. On the BoringSSL side, I think there were a few things going on:

    First, we allowed the private key to be zero on import. Even though the "domain parameters" are valid, we still need to do this check because, as you found, zero private key + zero hash will never converge. So that series fixes that in our DSA and ECDSA implementations.

    Second, there's deeper problem with how the DSA was specificied. DSA and ECDSA have important preconditions in what FIPS 186-4 and 186-5 calls "domain parameters". When those preconditions aren't met, all assumptions in the algorithms go out the window, including whether the retry loop converges. Unfortunately, the 186 documents were catastrophic failures in how to define cryptography, particularly in DSA. By failing to pick parameters, they force every system account for arbitrary domain parameters, despite the important differences they have from key material.

    This notion empirically did not get through to folks. X.509 encodes DSA domain parameters by coefficients, and has a mode where it does the same for ECDSA. OpenSSL's APIs treat domain parameters as just one of many things you can parse or manually configure. This was all incorrect. As a result, it is very easy for untrusted domain parameters flow into a system. And thus we have this infinite loop, and previously OpenSSL had CVE-2022-0778.

    The right way to do this would have been to do everything a la ECDSA named curves: named, well-known DSA parameters, with no mechanism for arbitrary domain parameters at all. But that's not what happened, and the specification made it very hard to do this.

    While priv == 0 isn't a loop from invalid domain parameters, there are other infinite loop cases BoringSSL which are. As @botovq suggests, there's no real reason to retry indefinitely. (Really there's no real reason to retry at all. For valid groups, needing to retry even once happens with negligible probability.) So while the root cause is, IMO, elsewhere, it's too late to fix the root cause, so we're going to cap the loop in (EC)DSA.

  6. added
    triaged: bug
    branch: master
    branch: 3.0
    branch: 3.1
    and removed
    issue: bug report
    on Feb 20, 2023
  7. guidovranken commented on Feb 23, 2023

    @guidovranken
    ContributorAuthor

    Now that the patches have been merged, I have confirmed that there are no more issues with DSA in BoringSSL.

  8. added a commit that references this issue on Feb 27, 2023
    dab3b3b
  9. 47 remaining items

  10. added 2 commits that reference this issue on May 30, 2023
    ed436cf
    93f0be7
  11. added 2 commits that reference this issue on Jul 19, 2023
    7b1f02e
    e5465ad
  12. added 2 commits that reference this issue on Aug 1, 2023
    32c3c3f
    52ac4f8
  13. added 2 commits that reference this issue on Aug 19, 2023
    fc83ad9
    55638b2
  14. added 2 commits that reference this issue on Sep 12, 2023
    6a84eb7
    499496f
  15. added a commit that references this issue on Aug 1, 2024
    3a4e09a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    branch: 3.0branch: 3.1branch: mastertriaged: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions