Skip to content

wincng: check bignum length and resize result in wcng_bn_mod_exp() - #2649

Closed
vszakats wants to merge 17 commits into
libssh2:masterfrom
vszakats:group-order-min
Closed

vszakats wants to merge 17 commits into
libssh2:masterfrom
vszakats:group-order-min

Conversation

@vszakats

@vszakats vszakats commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

/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);
      |              ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3242:8: note: Calling 'ssh2_dh_validate'
 3242 |     if(ssh2_dh_validate(f, p))  /* Verify if parameters are valid */
      |        ^~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:8: note: Assuming 'f' is non-null
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |        ^~
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:8: note: Left side of '||' is false
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:14: note: Assuming field 'bignum' is non-null
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |              ^~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:8: note: Left side of '||' is false
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |        ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:28: note: Assuming 'p' is non-null
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |                            ^~
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:8: note: Left side of '||' is false
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |        ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:34: note: Assuming field 'bignum' is non-null
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |                                  ^~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3175:5: note: Taking false branch
 3175 |     if(!f || !f->bignum || !p || !p->bignum)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3181:11: note: Assuming 'flen' is not equal to 0
 3181 |     while(flen && !*fb) {
      |           ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3181:11: note: Left side of '&&' is true
/home/runner/work/libssh2/libssh2/src/wincng.c:3181:19: note: Assuming the condition is false
 3181 |     while(flen && !*fb) {
      |                   ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3181:5: note: Loop condition is false. Execution continues on line 3185
 3181 |     while(flen && !*fb) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3187:11: note: Assuming 'plen' is not equal to 0
 3187 |     while(plen && !*pb) {
      |           ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3187:11: note: Left side of '&&' is true
/home/runner/work/libssh2/libssh2/src/wincng.c:3187:19: note: Assuming the condition is false
 3187 |     while(plen && !*pb) {
      |                   ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3187:5: note: Loop condition is false. Execution continues on line 3193
 3187 |     while(plen && !*pb) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3193:8: note: 'flen' is not equal to 0
 3193 |     if(flen == 0 || (flen == 1 && fb[0] <= 1))
      |        ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3193:8: note: Left side of '||' is false
/home/runner/work/libssh2/libssh2/src/wincng.c:3193:22: note: Assuming 'flen' is not equal to 1
 3193 |     if(flen == 0 || (flen == 1 && fb[0] <= 1))
      |                      ^~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3193:32: note: Left side of '&&' is false
 3193 |     if(flen == 0 || (flen == 1 && fb[0] <= 1))
      |                                ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3197:8: note: 'plen' is not equal to 0
 3197 |     if(plen == 0)
      |        ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3197:5: note: Taking false branch
 3197 |     if(plen == 0)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3200:8: note: Assuming 'pm1' is non-null
 3200 |     if(!pm1)
      |        ^~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3200:5: note: Taking false branch
 3200 |     if(!pm1)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3203:5: note: Loop condition is true.  Entering loop body
 3203 |     for(i = plen; i-- > 0;) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3204:12: note: Assuming the condition is true
 3204 |         if(pm1[i]--)  /* subtract one with borrow, big-endian */
      |            ^~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3204:9: note: Taking true branch
 3204 |         if(pm1[i]--)  /* subtract one with borrow, big-endian */
      |         ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3205:13: note:  Execution continues on line 3207
 3205 |             break;
      |             ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3209:11: note: 'pm1len' is not equal to 0
 3209 |     while(pm1len && !*q) {
      |           ^~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3209:11: note: Left side of '&&' is true
/home/runner/work/libssh2/libssh2/src/wincng.c:3209:5: note: Loop condition is false. Execution continues on line 3213
 3209 |     while(pm1len && !*q) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3213:12: note: Assuming 'flen' is <= 'pm1len'
 3213 |     ret = (flen > pm1len ||
      |            ^~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3213:12: note: Left side of '||' is false
/home/runner/work/libssh2/libssh2/src/wincng.c:3214:13: note: Assuming 'flen' is not equal to 'pm1len'
 3214 |            (flen == pm1len && memcmp(fb, q, flen) >= 0)) ? -2 : 0;
      |             ^~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3214:28: note: Left side of '&&' is false
 3214 |            (flen == pm1len && memcmp(fb, q, flen) >= 0)) ? -2 : 0;
      |                            ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3216:8: note: 'ret' is 0
 3216 |     if(ret)
      |        ^~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3216:5: note: Taking false branch
 3216 |     if(ret)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3220:16: note: 'i' is < 'flen'
 3220 |     for(i = 0; i < flen; i++) {
      |                ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3220:5: note: Loop condition is true.  Entering loop body
 3220 |     for(i = 0; i < flen; i++) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3222:9: note: Loop condition is true.  Entering loop body
 3222 |         while(b) {
      |         ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3222:9: note: Loop condition is true.  Entering loop body
/home/runner/work/libssh2/libssh2/src/wincng.c:3222:9: note: Loop condition is false. Execution continues on line 3220
/home/runner/work/libssh2/libssh2/src/wincng.c:3220:16: note: 'i' is < 'flen'
 3220 |     for(i = 0; i < flen; i++) {
      |                ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3220:5: note: Loop condition is true.  Entering loop body
 3220 |     for(i = 0; i < flen; i++) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3222:9: note: Loop condition is false. Execution continues on line 3220
 3222 |         while(b) {
      |         ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3220:16: note: Assuming 'i' is >= 'flen'
 3220 |     for(i = 0; i < flen; i++) {
      |                ^~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3220:5: note: Loop condition is false. Execution continues on line 3227
 3220 |     for(i = 0; i < flen; i++) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3227:8: note: Assuming 'bits_set' is >= 4
 3227 |     if(bits_set < 4)
      |        ^~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3227:5: note: Taking false branch
 3227 |     if(bits_set < 4)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3242:8: note: Returning from 'ssh2_dh_validate'
 3242 |     if(ssh2_dh_validate(f, p))  /* Verify if parameters are valid */
      |        ^~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3242:5: note: Taking false branch
 3242 |     if(ssh2_dh_validate(f, p))  /* Verify if parameters are valid */
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3245:8: note: Assuming field 'hAlgDH' is null
 3245 |     if(ssh2_wcng.hAlgDH && ssh2_wcng.hasAlgDHwithKDF != -1 &&
      |        ^~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:3245:25: note: Left side of '&&' is false
 3245 |     if(ssh2_wcng.hAlgDH && ssh2_wcng.hasAlgDHwithKDF != -1 &&
      |                         ^
/home/runner/work/libssh2/libssh2/src/wincng.c:3356:8: note: Calling 'wcng_bn_mod_exp'
 3356 |     if(wcng_bn_mod_exp(secret, f, dhctx->dh_privbn, p))
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:218:8: note: Assuming 'r' is non-null
  218 |     if(!r || !a || !p || !m)
      |        ^~
/home/runner/work/libssh2/libssh2/src/wincng.c:218:8: note: Left side of '||' is false
/home/runner/work/libssh2/libssh2/src/wincng.c:218:15: note: 'a' is non-null
  218 |     if(!r || !a || !p || !m)
      |               ^
/home/runner/work/libssh2/libssh2/src/wincng.c:218:8: note: Left side of '||' is false
  218 |     if(!r || !a || !p || !m)
      |        ^
/home/runner/work/libssh2/libssh2/src/wincng.c:218:20: note: Assuming 'p' is non-null
  218 |     if(!r || !a || !p || !m)
      |                    ^~
/home/runner/work/libssh2/libssh2/src/wincng.c:218:8: note: Left side of '||' is false
  218 |     if(!r || !a || !p || !m)
      |        ^
/home/runner/work/libssh2/libssh2/src/wincng.c:218:27: note: 'm' is non-null
  218 |     if(!r || !a || !p || !m)
      |                           ^
/home/runner/work/libssh2/libssh2/src/wincng.c:218:5: note: Taking false branch
  218 |     if(!r || !a || !p || !m)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:225:8: note: Assuming 'rsakey' is non-null
  225 |     if(!rsakey)
      |        ^~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:225:5: note: Taking false branch
  225 |     if(!rsakey)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:244:8: note: Assuming 'ret' is >= 0
  244 |     if(BCRYPT_SUCCESS(ret)) {
      |        ^
/usr/x86_64-w64-mingw32/include/bcrypt.h:31:33: note: expanded from macro 'BCRYPT_SUCCESS'
   31 | #define BCRYPT_SUCCESS(Status) (((NTSTATUS) (Status)) >= 0)
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:244:5: note: Taking true branch
  244 |     if(BCRYPT_SUCCESS(ret)) {
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:247:12: note: Assuming 'ret' is >= 0
  247 |         if(BCRYPT_SUCCESS(ret)) {
      |            ^
/usr/x86_64-w64-mingw32/include/bcrypt.h:31:33: note: expanded from macro 'BCRYPT_SUCCESS'
   31 | #define BCRYPT_SUCCESS(Status) (((NTSTATUS) (Status)) >= 0)
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:247:9: note: Taking true branch
  247 |         if(BCRYPT_SUCCESS(ret)) {
      |         ^
/home/runner/work/libssh2/libssh2/src/wincng.c:248:13: note: Taking true branch
  248 |             if(!wcng_bn_resize(r, length)) {
      |             ^
/home/runner/work/libssh2/libssh2/src/wincng.c:249:26: note: Assuming 'length' is < field 'length'
  249 |                 length = max((ULONG)a->length, length);
      |                          ^
/usr/x86_64-w64-mingw32/include/minwindef.h:173:21: note: expanded from macro 'max'
  173 | #define max(a, b) (((a) > (b)) ? (a) : (b))
      |                     ^~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:249:26: note: '?' condition is true
  249 |                 length = max((ULONG)a->length, length);
      |                          ^
/usr/x86_64-w64-mingw32/include/minwindef.h:173:20: note: expanded from macro 'max'
  173 | #define max(a, b) (((a) > (b)) ? (a) : (b))
      |                    ^
/home/runner/work/libssh2/libssh2/src/wincng.c:251:20: note: Assuming 'bignum' is non-null
  251 |                 if(bignum) {
      |                    ^~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:251:17: note: Taking true branch
  251 |                 if(bignum) {
      |                 ^
/home/runner/work/libssh2/libssh2/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);
      |                                         ~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:259:24: note: Assuming 'ret' is >= 0
  259 |                     if(BCRYPT_SUCCESS(ret))
      |                        ^
/usr/x86_64-w64-mingw32/include/bcrypt.h:31:33: note: expanded from macro 'BCRYPT_SUCCESS'
   31 | #define BCRYPT_SUCCESS(Status) (((NTSTATUS) (Status)) >= 0)
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:259:21: note: Taking true branch
  259 |                     if(BCRYPT_SUCCESS(ret))
      |                     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:260:43: note: Passing value via 2nd parameter 'length'
  260 |                         wcng_bn_resize(r, offset);
      |                                           ^~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:260:25: note: Calling 'wcng_bn_resize'
  260 |                         wcng_bn_resize(r, offset);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:148:9: note: 'bn' is non-null
  148 |     if(!bn)
      |         ^~
/home/runner/work/libssh2/libssh2/src/wincng.c:148:5: note: Taking false branch
  148 |     if(!bn)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:151:8: note: Assuming 'length' is not equal to field 'length'
  151 |     if(length == bn->length)
      |        ^~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:151:5: note: Taking false branch
  151 |     if(length == bn->length)
      |     ^
/home/runner/work/libssh2/libssh2/src/wincng.c:154:12: note: Field 'bignum' is non-null
  154 |     if(bn->bignum && bn->length > 0 && length < bn->length)
      |            ^
/home/runner/work/libssh2/libssh2/src/wincng.c:154:8: note: Left side of '&&' is true
  154 |     if(bn->bignum && bn->length > 0 && length < bn->length)
      |        ^
/home/runner/work/libssh2/libssh2/src/wincng.c:154:26: note: Field 'length' is > 0
  154 |     if(bn->bignum && bn->length > 0 && length < bn->length)
      |                          ^
/home/runner/work/libssh2/libssh2/src/wincng.c:154:8: note: Left side of '&&' is true
  154 |     if(bn->bignum && bn->length > 0 && length < bn->length)
      |        ^
/home/runner/work/libssh2/libssh2/src/wincng.c:154:40: note: Assuming 'length' is < field 'length'
  154 |     if(bn->bignum && bn->length > 0 && length < bn->length)
      |                                        ^~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/wincng.c:154:5: note: Taking true branch
  154 |     if(bn->bignum && bn->length > 0 && length < bn->length)
      |     ^
/home/runner/work/libssh2/libssh2/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

To avoid corner cases ending up with zero allocations in some codepaths
(e.g. WinCNG).
@vszakats
vszakats marked this pull request as draft October 4, 2026 13:56
@vszakats vszakats changed the title crypto: bump minimum required group_order crypto: bump minimum required group_order to 512 (was: 0) Oct 4, 2026
@vszakats
vszakats marked this pull request as ready for review October 4, 2026 15:14
@vszakats vszakats changed the title crypto: bump minimum required group_order to 512 (was: 0) crypto: bump minimum required group_order to 512 (was: 0) in ssh2_dh_key_pair() Oct 4, 2026
Comment thread src/kex.c Fixed
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.
@vszakats
vszakats marked this pull request as draft October 4, 2026 16:05
@vszakats vszakats changed the title crypto: bump minimum required group_order to 512 (was: 0) in ssh2_dh_key_pair() wincng: check bignum length and resize result in wcng_bn_mod_exp() Oct 4, 2026
@vszakats
vszakats marked this pull request as ready for review October 4, 2026 17:39
@vszakats
vszakats requested a balanced review from Copilot October 4, 2026 17:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused change correctly handles both zero-length output and resize failure.

Review effort: Balanced
Findings: None

What changed in this PR

Updates WinCNG modular exponentiation error handling.

Changes:

  • Avoids zero-length bignum resizing.
  • Handles resize allocation failures.
File Description
src/​wincng.c Validates output length and resize success.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@vszakats vszakats closed this in 10c374c Oct 4, 2026
vszakats added a commit that referenced this pull request Oct 4, 2026
To silence a warning, in rare failure cases:
```
srclimit_penalise: ipv4: new 172.17.0.1/32 deferred penalty of 1.000
  seconds for penalty: connections without attempting authentication
```
Ref: https://github.com/libssh2/libssh2/actions/runs/37207427612/job/111451438515?pr=2649#step:25:537

Also:
- seize the opportunity and bump sshd debug level to 2.
  Level 1 is too low to see clearly in some cases.
  Ref: #2649
  Follow-up to 45aa5c5 #2621

Refs:
https://man.openbsd.org/sshd_config#PerSourcePenalties
https://www.openssh.org/txt/release-9.8

Closes #2650
@vszakats
vszakats deleted the group-order-min branch October 4, 2026 18:08
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.

4 participants