Skip to content

wincng: validate Diffie-Hellman public value in ssh2_dh_secret - #2645

Closed
abdul-khaliq-khalid wants to merge 1 commit into
libssh2:masterfrom
abdul-khaliq-khalid:wincng-dh-validate-f
Closed

abdul-khaliq-khalid wants to merge 1 commit into
libssh2:masterfrom
abdul-khaliq-khalid:wincng-dh-validate-f

Conversation

@abdul-khaliq-khalid

@abdul-khaliq-khalid abdul-khaliq-khalid commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

ssh2_dh_secret() in the Windows CNG backend uses the server's Diffie-Hellman public value f without the range check the other four backends apply through ssh2_dh_validate(), so a malicious server can send f of 0, 1 or p-1 and push the shared secret into a trivial subgroup that openssl, libgcrypt, mbedtls and os400qc3 all reject with LIBSSH2_ERROR_PROTO. This adds a WinCNG ssh2_dh_validate() that enforces 1 < f < p-1 and the same minimum-set-bits check on the big-endian value, and calls it at the top of ssh2_dh_secret() like the other backends do. The shared test_ssh2_dh_validate() in tests/test_simple.c was stubbed out on WinCNG only because the function did not exist, so it now runs against the real implementation too.

Reported-by: yaotushaozhu on github
Ref: GHSA-m9rg-h9jv-9gvh
Follow-up to fcf8307 #2239

Comment thread src/wincng.c Resolved
@vszakats

vszakats commented Oct 2, 2026

Copy link
Copy Markdown
Member

This triggered a clang-tidy report:

/home/runner/work/libssh2/libssh2/src/wincng.c:157:14: error: Call to 'realloc' has an allocation size of 0 bytes [clang-analyzer-optin.portability.UnixAPI,-warnings-as-errors]
  157 |     bignum = realloc(bn->bignum, length);
      |              ^

details: https://github.com/libssh2/libssh2/actions/runs/37005655840/job/110833185040?pr=2645#step:8:62

@vszakats

vszakats commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

This triggered a clang-tidy report:

/home/runner/work/libssh2/libssh2/src/wincng.c:157:14: error: Call to 'realloc' has an allocation size of 0 bytes [clang-analyzer-optin.portability.UnixAPI,-warnings-as-errors]
  157 |     bignum = realloc(bn->bignum, length);
      |              ^

details: https://github.com/libssh2/libssh2/actions/runs/37005655840/job/110833185040?pr=2645#step:8:62

Since the code isn't touched by this PR, it may be that the touched part indirectly may cause a zero length passed here, or there is new code that checks for a zero length, which makes the analyzer think the same may happen in this other function. Maybe a separate patch is necessary before this one, to tackle it?

→ PR #2649

vszakats added a commit that referenced this pull request Oct 4, 2026
- check `wcng_bn_resize()` success. The only call where this check was
  missing.

- check if bignum length is non-zero before calling resize.
  To fix this clang-tidy warning after applying pending PR #2645:
  ```
  src/wincng.c:157:14: error: Call to 'realloc' has an allocation size of 0 bytes [clang-analyzer-optin.portability.UnixAPI,-warnings-as-errors]
    157 |     bignum = realloc(bn->bignum, length);
        |              ^
  [...]
  src/wincng.c:255:27: note: Value assigned to 'offset'
    255 |                     ret = BCryptEncrypt(hKey, bignum, length, NULL, NULL, 0,
        |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    256 |                                         r->bignum, (ULONG)r->length, &offset,
        |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    257 |                                         BCRYPT_PAD_NONE);
        |                                         ~~~~~~~~~~~~~~~~
  [...]
  src/wincng.c:259:21: note: Taking true branch
    259 |                     if(BCRYPT_SUCCESS(ret))
        |                     ^
  src/wincng.c:260:43: note: Passing value via 2nd parameter 'length'
    260 |                         wcng_bn_resize(r, offset);
        |                                           ^~~~~~
  src/wincng.c:260:25: note: Calling 'wcng_bn_resize'
    260 |                         wcng_bn_resize(r, offset);
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
  [...]
  src/wincng.c:157:14: note: Call to 'realloc' has an allocation size of 0 bytes
    157 |     bignum = realloc(bn->bignum, length);
        |              ^                   ~~~~~~
  ```
  Ref: https://github.com/libssh2/libssh2/actions/runs/37005655840/job/110833185040?pr=2645#step:8:62
  Bug: #2645 (comment)

Closes #2649
@vszakats
vszakats force-pushed the wincng-dh-validate-f branch from cfd8a85 to 01c2ea9 Compare October 4, 2026 18:41
@vszakats vszakats closed this in 7f28c29 Oct 4, 2026
@vszakats

vszakats commented Oct 4, 2026

Copy link
Copy Markdown
Member

Thanks!

(merged the casing nit and fixed the clang-tidy issue via #2649). Tests pass after rebasing to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants