1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-01-27 00:18:12 +03:00
Commit Graph

1649 Commits

Author SHA1 Message Date
Viktor Szakats
cbe7654d72 windows: drop support for Visual Studio 2008 and older
Closes #1769
2025-12-25 23:42:25 +01:00
Viktor Szakats
6e56a8bdca tidy-up: drop duplicate newlines, fix non-ASCII chars, other formatting, check in CI
Closes #1766
2025-12-25 22:24:13 +01:00
Viktor Szakats
35e1c653bd cmake: rename libssh2-config.cmake.in to libssh2-config.in.cmake
To:
- match filename suffix with file content.
- simplify recognizing CMake sources.
- ensure syntax highlighters use the correct file type.

Also:
- cmakelint.sh: simplify, alpha sort the filelist.

Closes #1765
2025-12-25 20:37:50 +01:00
Michael Buckley
631e2f82a3 Fix a NULL dereference of kex->exchange_keys during key exchange. (#1763)
Credit:
Aldo Ristori
2025-12-19 15:41:45 -08:00
Joshua Rogers
c10cb2449c agent: do not truncate socket path; reject (#1709)
Credit:
Joshua Rogers
2025-12-03 10:42:31 -08:00
Viktor Szakats
96e21c2e25 cmake: verify minimum CMake version in libssh2-config.cmake
This is the same minimum version required to build libssh2 itself (3.7).

Closes #1754
2025-12-01 20:20:56 +01:00
Lars Nordin
81f0203c1c userauth: fix error message for EAGAIN in public-key response (#1747) 2025-11-28 11:58:05 +01:00
Joshua Rogers
8ddf3b317f agent: pageant backend, bound reply copy, handle missing reply (#1711)
The Pageant transact path trusted the 32-bit length in the shared memory
mapping and could memcpy past the mapped view. It also treated
a non-positive SendMessage(WM_COPYDATA) result as success.

Changes:

Reject replies when SendMessage returns ≤ 0 and report
LIBSSH2_ERROR_AGENT_PROTOCOL.

Bound the copy by validating response_len <= PAGEANT_MAX_MSGLEN - 4
(accounting for the length prefix) to avoid OOB reads.

Impact: prevents potential out-of-bounds read and use of uninitialized
mapping contents when Pageant misbehaves or is malicious.
2025-11-17 11:25:26 +01:00
Will Cosgrove
4ed26f5740 Fix sftp_symlink when getting SSH_FXP_STATUS response (#1731)
Move advancing past packet ID before reading the FXP_STATUS response.
2025-10-20 14:04:52 -07:00
Viktor Szakats
16ce0ec89b mbedtls: fix using garbage value (reported by clang-tidy)
In `_libssh2_mbedtls_pub_priv_key()` on a NON-error code path, a stack
variable was checked without initializing it first.

I found it interesting that clang-tidy did not find this when building
against the system mbedtls (2.x) with 2.x compatibility code still in.
Then it did find it when using a manual build of mbedtls 3.1.0 with
2.x compatibility code deleted from libssh2. Being such a trivial error
I wonder why no compiler ever detected it as a regular warning.

linux (clang-tidy, amd64, mbedTLS-prev [3.1.0], cmake, ON):
```
src/mbedtls.c:744:8: error: Branch condition evaluates to a garbage value [clang-analyzer-core.uninitialized.Branch,-warnings-as-errors]
  744 |     if(ret) {
      |        ^
```
Ref: https://github.com/libssh2/libssh2/actions/runs/18620615649/job/53091295760#step:22:44

Follow-up to 186f1a2d75 #132
Cherry-picked from #1727
Closes #1729
2025-10-18 23:59:26 +02:00
Viktor Szakats
557611f92b checksrc: update, update local config, fix toctou in examples
Closes #1719
2025-10-14 03:29:05 +02:00
Viktor Szakats
b807dac921 wolfssl: fix detecting ECDSA support
Use `HAVE_ECC` as an indicator for ECDSA when building with wolfSSL.

Before this patch the OpenSSL macros were used, in particular
`OPENSSL_NO_EC`, which made ECDSA support disabled with certain
wolfSSL build configurations, e.g. the Ubuntu 24.04 one.

ECDSA is necessary to run tests with OpenSSH v10, e.g. on Debian Trixie.

Follow-up to b95e758239 #666
Ref: #1720
Closes #1723
2025-10-14 01:04:13 +02:00
Viktor Szakats
4f9e6e0bfb REUSE: add copyright headers to more files, and .gitignore updates
Also:
- rename a spellcheck file to match curl.
- editorconfig: fix line width.
- editorconfig: make it use UTF-8.
- editroconfig: apply some rules to all files.
- .gitignore: drop dupe, drop `.DS_Store` (not created by this repo),
   sort.
- .gitignore: add for tests executables.

Closes #1718
2025-10-13 13:35:25 +02:00
Will Cosgrove
2dae302489 Update sftp_symlink to avoid out of bounds read on malformed packet #1705 (#1717)
Use buffer struct to guard against out of bounds reads and invalid packets.

Discovery Credit:
Joshua Rogers
2025-10-10 08:26:20 -07:00
Joshua Rogers
31ec5a8b55 packet: authagent_open: fix failure packet length (#1701)
Compute packet_len using strlen(AuthAgentUnavail) (not X11FwdUnAvil).
The mismatch could send 1 uninitialized byte on the wire.

Credit:
Joshua Rogers
2025-10-09 10:18:53 -07:00
Joshua Rogers
13a7145184 Fix type mismatch in "reason code" (#1706)
Credit:
Joshua Rogers
2025-10-09 10:03:39 -07:00
Joshua Rogers
4266904092 Fix error code on agent recv error (#1710)
Credit:
Joshua Rogers
2025-10-09 10:01:10 -07:00
Viktor Szakats
cea796fc2d openssl: drop support for OpenSSL <1.1.1, LibreSSL <2.7 (2018-03-21)
- OpenSSL 1.0.2 is officially not supported for open source,
  OpenSSL 1.1.0 is officially not supported at all.
  https://openssl-library.org/policies/releasestrat/index.html

- LibreSSL 2.6.x and older are just old. 2.7.0 was released on 2018-03-21.

Closes #1668
2025-10-09 03:41:33 +02:00
Joshua Rogers
c3d8976c03 Remove unused kex state variable exchange_hash (#1713)
kex: completely remove unused exchange_hash it's unused.
2025-10-07 10:18:00 -07:00
Will Cosgrove
c097c6811c Downcast packet length checks #1707 (#1714)
Downcast size_t values to uint32_t before comparing so the addition isn't upcast to size_t. 

Credit: 
Joshua Rogers
2025-10-07 10:16:38 -07:00
Viktor Szakats
61d51cb449 cmake: tidy up passing compiler and linker options
- prefer `set_property()` over `target_compile_options()`, to use
  the modern style.

  Follow-up to 8017592163 #939

- prefer to append to compiler/linker options in examples/tests.
  To prevent accidentally overriding a previously set option.

  Follow-up to 14a709a72f #1686
  Follow-up to 854cfa8292 #1524

- replace `LINK_FLAGS` with `LINK_OPTIONS` that superceded it, for
  CMake 3.13+.

  Follow-up to 854cfa8292 #1524
  https://cmake.org/cmake/help/v4.1/prop_tgt/LINK_OPTIONS.html

Closes #1690
2025-09-29 13:32:46 +02:00
Viktor Szakats
14a709a72f cmake: pass picky C options to C targets only (and not to C++ ossfuzz)
This also means that we no longer pass any picky warning option to
ossfuzz. It's probably not worth maintaining picky C++ options for this
single, small target.

Silencing:
```
cc1plus: warning: command-line option '-Wbad-function-cast' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wdeclaration-after-statement' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wenum-int-mismatch' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wjump-misses-init' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wmissing-parameter-type' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wmissing-prototypes' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wnested-externs' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wold-style-declaration' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wold-style-definition' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
```
Ref: https://github.com/libssh2/libssh2/actions/runs/18063134305/job/51402236388#step:7:15

Closes #1686
2025-09-27 21:50:34 +02:00
Will Cosgrove
ff50682c23 Add input bounds checking to SFTP API (#1679)
Add bounds checking to public SFTP API input to avoid possible heap corruption when passing in invalid values.

Credit:
Oblivionsage
2025-09-26 08:03:28 -07:00
Viktor Szakats
af679ecc79 cmake: use modern alternative for get_filename_component()
- use `cmake_path()` to query filenames, with CMake 3.20 or upper.
  https://cmake.org/cmake/help/v4.1/command/cmake_path.html#query

- also quote the value passed to `get_filename_component()` where
  missing. (Could not cause an actual issue as used in the code.)

Closes #1673
2025-09-22 20:00:57 +02:00
Viktor Szakats
51b35d1b7a fix two NULL checks
- userauth: fix NULL dereference when out-of-memory.
  Also fix indentation.
  Follow-up to 3a6ab70dcf #1314

- openssl: drop redundant NULL check and logic.
  Follow-up to ed439a29bb #698

Pointed out by CodeQL

Closes #1656
2025-09-15 01:16:36 +02:00
Viktor Szakats
199526f941 tidy-up: Makefile.am comments and copyright header
Closes #1662
2025-09-14 23:44:40 +02:00
Viktor Szakats
c3e6dbd469 tidy-up: fix typos found by typos-cli 1.36.2
Closes #1654
2025-09-13 22:58:06 +02:00
Will Cosgrove
d1c0e14d2a Improve known_hosts parsing and harden _libssh2_base64_encode() (#1641)
Notes:
Added additional base64 decoding validation when parsing known_hosts and no longer assume what is going into _libssh2_base64_encode() is a null terminated C string, input now must have a length and buffer.

Reported by:
Dhiraj Mishra mishra.dhiraj95@gmail.com

Credit:
Will Cosgrove

Reviewed by:
Michael Buckley
2025-08-25 09:36:40 -07:00
trustytrojan
dd50a41152 cmake: add LIBSSH2_DISABLE_INSTALL option
To disable all installation targets.

Closes #1638
2025-08-24 18:15:10 +02:00
Josh Brobst
30befffe04 kex: fix DH-GEX-sha256 bignum initialization
In `kex_method_diffie_hellman_group_exchange_sha256_key_exchange`,
`p` and `g` are later initialized with `_libssh2_bn_from_bin`, so they
should be initially created using `_libssh2_bn_init_from_bin` rather
than `_libssh2_bn_init`, as is done in
`kex_method_diffie_hellman_group_exchange_sha1_key_exchange`.

Fixing memory leaks when using the libgcrypt backend.

Follow-up to 09c5e59933
Ref: https://web.archive.org/web/trac.libssh2.org/ticket/168

Closes #1599
2025-07-21 21:26:32 +02:00
Will Cosgrove
cea8783ed8 Fix small leak in base64_encode() with empty data #1627 (#1630)
Fix small memory leak when trying to encode base64 data with no data.

Credit:
Liu Xing Yu
2025-07-21 11:48:25 -07:00
Will Cosgrove
aa4c7b5b96 Handle malformed comments in known_hosts parsing #1628 (#1629)
Notes:
Handle malformed comment in known_hosts parsing to avoid buffer overflow.

Credit:
Liu Xing Yu
2025-07-21 11:46:58 -07:00
Ryan Kelley
992dafbc7f scp: fix NULL dereference in path arg of send/recv (#1625)
Notes:
* Error handling if path for scp is NULL

Reported-by:
Liu Xing Yu

Credit:
Ryan Kelley
2025-07-21 10:02:42 -07:00
Josh Brobst
f46422223f libgcrypt: add support for RSA SHA-256/SHA-512
Closes #1600
2025-07-21 02:01:02 +02:00
Viktor Szakats
ad8e924583 cmake: fixup Libs.private lib order
- Reworking the `Libs.private` collector logic for INTERFACE targets,
  broke the original lib order. Fix it by going back a single loop
  to retain order.
  Follow-up to df0563a857 #1535

- Implement the above with one change: move implicit CMake libs
  to the end of the list (was: the beginning).
  I expect these to be libs that any custom libs may depend on,
  like system libs, C runtime, compiler runtime lib.
  Follow-up to c87f129630 #1466

Closes #1623
2025-07-14 01:34:40 +02:00
Viktor Szakats
e1da7b2cb8 cmake: de-duplicate LIBSSH2_PC_LIBS_PRIVATE more carefully
`LIBSSH2_PC_LIBS_PRIVATE` ends up in `Libs.private` in `libssh2.pc`.

The order and duplication may be significant for linkers that rely on
strict lib order and unable to resolve symbols without it. Such linker
is binutils `ld`. De-duplication can break it.

As of now there is no purposeful duplication in libs in libssh2, thus
the de-duplication most likely did not affect actual builds.

It was originally introduced to avoid a repeat `-lz` (with
a zlib-enabled OpenSSL or wolfSSL build.) To keep this feature, this
patch makes sure to only delete duplicates that are next to each other.

Follow-up to 6464301820 #1131

Closes #1621
2025-07-14 01:34:40 +02:00
Viktor Szakats
4e08dac73d cmake: tidy up add_feature_info() formatting
Closes #1622
2025-07-13 21:24:10 +02:00
Viktor Szakats
22784a56e2 cmake: drop redundant -lws2_32 meant for libssh2.pc
The `libssh2.pc` generator logic automatically adds `-lws2_32` while
parsing `LIBSSH2_LIBS`, which contains this lib already. Then discard
the duplicate.

This patch introduces a change in the position of `ws2_32` within
the lib list advertised via `libssh2.pc` for static builds.

This order might in cases by significant, but:

- libssh2 no longer links against `libssl`, which was the library
  also referencing `ws2_32` and breaking picky binutils `ld` linker
  when not passed in strict dependency order.
  Ref: c84745e34e #1128

- since switching to INTERFACE targets, cmake messes up the lib order
  anyway, adding `OpenSSL:Crypto` last, instead of `ws2_32`. This did
  not seem to cause an issue so far.
  Ref: df0563a857 #1535

For these reasons it seems unlikely this position change could break
Windows OpenSSL static gcc/ld builds relying on `libssh2.pc` for their
lib list. It least no more than it was before this patch.

Turns out these theories don't stand in practice and the order is broken
possibly by introducing INTERFACE targets, with or without this patch.
`libcrypto` (tested with LibreSSL) is in fact depending on `ws2_32`, and
the `bcrypt` dependency is also causing breakage with picky binutils ld.
I may try addressing it in a separate PR.

Ref: 33b6d5f89d #827
Ref: 31fb8860db #811

Closes #1619
2025-07-13 14:34:17 +02:00
Viktor Szakats
723b701715 build: tidy up comments on Makefile.inc variables
Closes #1615
2025-07-02 12:02:09 +02:00
Viktor Szakats
694b9d964b cmake: add linter, fix issues
Ref: https://cmake-format.readthedocs.io/en/latest/cmake-lint.html
Ref: https://github.com/cheshirekow/cmake_format

Closes #1610
2025-06-11 07:09:10 +02:00
chrisgch
d8ae40bad0 Fixes when server/client uses encryption method with integrated MAC and the other doesn't (#1582)
Notes:
Connection fails when server/client uses one encryption method with integrated MAC and one without.

Credit:
Christian Ghisler
2025-05-21 13:38:11 -07:00
Anders Borum
9136cc763b agent: fix byte amount copied for method in libssh2_agent_sign
It looks like the incorrect length is used to copy the public key method
into the session in `libssh2_agent_sign()` and while the public key type
at the start of a identity blob is often identical to the public key
method it might not always be such as when method is `rsa-sha2-256` for
`ssh-rsa` keys.

Closes #1603
2025-05-17 23:46:45 +02:00
Viktor Szakats
72982d1589 build: sync up picky warnings with curl, fix fallouts
- cmake: sync `-ftree-vrp` behavior with autotools.
- build: enable `-Wjump-misses-init` for GCC 4.5+.
  Credits-to: Marcel Raad
- packet: fix `-Wjump-misses-init` warnings.
  ```
  src/packet.c: In function ‘_libssh2_packet_add’:
  src/packet.c:671:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:920:31: note: ‘want_reply’ declared here
  src/packet.c:671:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:919:26: note: ‘len’ declared here
  src/packet.c:669:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:1121:31: note: ‘want_reply’ declared here
  src/packet.c:669:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:1120:26: note: ‘len’ declared here
  src/packet.c:669:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:1119:26: note: ‘channel’ declared here
  ```
- build: enable gcc-12/13+, clang-10+ picky warnings
- acinclude.m4: sync formatting/comments with curl.
- autotools: fix `-Wtrampolines` picky warning for gcc 4.x versions.
  Follow-up to 854cfa8292 #1524
- cmake: enable `-Wall` for MSVC when `PICKY_COMPILER=ON`.
- MSVC: fix `-Wall` warnings.
  Seen on VS2015. Not seen on VS2022. Unknown for other versions.
  ```
  tests\test_simple.c(60): warning C4777: 'fprintf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::size_t'
  tests\test_simple.c(60): warning C4777: 'fprintf' : format string '%.*s' requires an argument of type 'int', but variadic argument 2 has type 'std::size_t'
  ```
- mbedtls: stop silencing warnings in 3rd-party header.
  Follow-up to a3aa6b4ca8 #1525
- cmake: stop deleting `-W<n>` from `CMAKE_C_FLAGS` (MSVC)
  1. `CMAKE_C_FLAGS` may apply to other projects, and deleting/altering it
     may be unexpected.
  2. We pass `-W4`/`-Wall` internally now, which do override custom
     `-W<n>` options as tested with VS2008 and newer VS generators.

Closes #1588
2025-05-07 23:24:41 +02:00
Viktor Szakats
ac80041852 cmake: IMPORTED target improvements and fixes
- fix `add_subdirectory` builds for old CMake versions.
- libssh2-config.cmake: fix to set CMP0099 for CMake 3.17+ only.
- libssh2-config.cmake: generalize code to support any number of deps.
  (mainly to sync with curl.)
- libssh2-config.cmake: bind dependencies to the static libssh2 only.

Follow-up to a0d8529b08 #1571
Follow-up to df0563a857 #1535

Closes #1581
2025-04-27 13:46:30 +02:00
Viktor Szakats
7b4f821e7a cmake: fix declspec for MSVC consuming static libssh2
Ref, same fix in curl (2018-08-15):
ab66a80488
https://github.com/curl/curl/issues/2817
https://github.com/curl/curl/pull/2823

Reported-by: geral-victor on github
Fixes #1578
Closes #1579
2025-04-22 08:56:11 +02:00
Viktor Szakats
a8e4456b05 cmake: avoid passing options via CMAKE_*_FLAGS
- replace `CMAKE_C_FLAGS*` and `CMAKE_CXX_FLAGS` with `COMPILE_OPTIONS`.
- replace `CMAKE_SHARED_LINKER_FLAGS_DEBUG` with
  `LINK_OPTIONS`/`LINK_FLAGS`.
- make it explicit to pass these C flags to feature checks.
- enable `-pedantic-errors` picky option for GCC with CMake <3.23.
- drop redundant condition when stripping existing MSVC `/Wn` options.

CMake passes `CMAKE_C_FLAGS` to targets, feature checks and raw
`try_compile()` calls. With `COMPILE_OPTIONS`, this is limited to
targets, and we must explicitly pass them to feature checks. This
makes the build logic clearer, and offers more control. It also
reduces log noise by omitting these options from linker commands,
and from `CMAKE_C_FLAGS` dumps in feature checks.

Closes #1575
2025-04-15 01:47:31 +02:00
Viktor Szakats
a0d8529b08 cmake: fix target interfaces for old CMake versions
- drop `VERSION` target property for cmake <3.19 compatibility
  ```
  CMake Error at CMake/Find*.cmake:90 (set_target_properties):
    INTERFACE_LIBRARY targets may only have whitelisted properties.
    The property "VERSION" is not allowed.
  ```
- move custom target property to the `INTERFACE_` namespace
  for cmake <3.19 compatibility. (To avoid same error as above)
- fix forwarding multiple `CFLAGS`, when detected via `pkg-config`.
- restore support for `-framework` and raw libs when processing
  the internal lib list for generating `libssh2.pc`. For good measure,
  at the moment libssh2 doesn't depend on a Framework.
- limit `libssh2_dumptargetprops()` to cmake 3.19+. It doesn't work with
  older versions.

Issues found while applying this change to curl. They did not surface in
libssh2 CI.

Follow-up to df0563a857 #1535

Closes #1571
2025-04-14 02:13:37 +02:00
Viktor Szakats
71179930e1 cmake: use VERSION_GREATER_EQUAL
Available since 3.7, which is the minimum required for libssh2:
https://cmake.org/cmake/help/latest/command/if.html#version-greater-equal

Closes #1573
2025-04-10 08:59:47 +02:00
Viktor Szakats
a1a28ac943 clang-tidy: fix and/or silence issues found, and more
- kex: drop unused assigment.
- knownhost: error when salt is NULL.
- mbedtls: avoid unnecessary inline assigments, that were ignored for
  the second block and replaceable with a `ret = 0` initialization for
  the first one.
- mbedtls: fix ignoring an API failure and ending up calling
  `mbedtls_rsa_check_privkey()` unconditionally.
- misc: initialize datalen on error in `_libssh2_base64_decode()`.
- openssl: drop unused assigments.
- openssl: fix unused static function.
- packet: avoid NULL deref.
- packet: avoid NULL in `memcpy` src.
- publickey: optimize struct layout to avoid padding.
- sftp: replace ignored `rc` error assigment with `_libssh2_error()` call.
- transport: fix potential NULL ptr dereferences.
- transport: silence uninitialized value warnings.
- userauth: drop unused assigment.
- userauth: possible use of unitialized pointer.
- userauth: replace `rewind()` with `fseek()`.
  `rewind()` returns an error condition in `errno`. `errno` is
  problematic and reduces portability. Use `fseek()` to avoid it.
- userauth: replace potential NULL deref by returning error from
  `sign_frommemory()`. Possible false positive. `rc` should be set
  upstream if the callback is NULL.
- userauth: replace potential NULL deref by returning error from
  `sign_fromfile()`. clang-tidy did not warn about this one, but
  let's match `sign_frommemory()` anyway.
- wincng: fix potentially unused macros.
- wincng: make sure bignum is not NULL before use.

tests:
- openssh_fixture: drop unused assignment.
- session_fixture: exit if `username` not set, to avoid `strlen(NULL)`.
- session_fixture: replace `rewind()` with `fseek()`.
  `rewind()` returns an error condition in `errno`. `errno` is
  problematic and reduces portability. Use `fseek()` to avoid it.
- test_read: exit if `username` not set, to avoid `strlen(NULL)`.

examples:
- scp_write_nonblock: fix file handle leak.
- sftp_write_nonblock: file handle leak on error.
- sftp_write_sliding: file handle leak on error.
- ssh2_agent_forwarding: fix unused error codes.

Details in the subcommits under the PR.

Thanks-to: Michael Buckley
Thanks-to: Will Cosgrove

Closes #1561
2025-03-24 14:37:08 +01:00
Viktor Szakats
64fafc78b0 transport: fix formatting
Cherry-picked from #1561
2025-03-24 14:06:28 +01:00