1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-31 00:03:08 +03:00
Commit Graph

2997 Commits

Author SHA1 Message Date
afa6b86560 build: enable missing OpenSSF-recommended warnings, with fixes
Ref:
https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
(2023-11-29)

Enable new warnings:

- replace `-Wno-sign-conversion` with `-Wsign-conversion`.

  Fix them in example, tests and wincng. There remain about 360 of these
  warnings in `src`. Add a TODO item for those and disable `-Werror` for
  this particular warning.

- enable `-Wformat=2` for clang (in both cmake and autotools).

- enable `__attribute__((format))` for `_libssh2_debug()`,
  `_libssh2_snprintf()` and in tests for `run_command()`.

  `LIBSSH2_PRINTF()` copied from `CURL_TEMP_PRINTF()` in curl.

- enable `-Wimplicit-fallthrough`.

- enable `-Wtrampolines`.

Fix them:

- src: replace obsolete fall-through-comments with
  `__attribute__((fallthrough))`.

- wincng: fix `-Wsign-conversion` warnings.

- tests: fix `-Wsign-conversion` warnings.

- example: fix `-Wsign-conversion` warnings.

- src: fix `-Wformat` issues in trace calls.

  Also, where necessary fix `int` and `unsigned char` casts to
  `unsigned int` and adjust printf format strings. These were not
  causing compiler warnings.

  Cast large types to `long` to avoid dealing with printf masks for
  `size_t` and other C99 types. Existing code often used `int` for this.
  I'll update them to `long` in an upcoming commit.

- tests: fix `-Wformat` warning.

- silence `-Wformat-nonliteral` warnings.

- mbedtls: silence `-Wsign-conversion`/`-Warith-conversion`
  in external header.

Closes #1257
2023-12-03 01:32:20 +00:00
e0a0466490 packet: whitespace fix
Tested via #1257
2023-12-02 22:31:12 +00:00
b136c37998 tidy-up: unsigned -> unsigned int
In the `interval` argument of public `libssh2_keepalive_config()`.

Tested via #1257
2023-12-02 09:53:56 +00:00
eb996af851 tests: sync port number type with the rest of codebase
Tested via #1257
2023-12-02 09:53:55 +00:00
ecdf519911 autotools: enable -Wunused-macros with gcc
It works with gcc without the libtool warnings seen with clang
on Windows in 96682bd5e1 #1227.

Sync usage of of this macro with CMake and
autotools + clang + non-Windows. Making it enabled everywhere except
autotools + clang + Windows due to the libtool stub issue.

Follow-up to 7ecc309cd1 #1224

Closes #1262
2023-12-02 09:51:59 +00:00
0b4bdc856f TODO: disable or drop weak algos [ci skip]
Closes #1261
2023-12-02 08:29:49 +00:00
744e059f31 example, tests: fix/silence -Wformat-truncation=2 gcc warnings
Then sync this warning option with curl.

Seems like a false positive and/or couldn't figure how to fix it, so silence:
```
example/ssh2.c:227:38: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size 0 [-Werror=format-truncation=]
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |                                      ^~
example/ssh2.c:227:34: note: assuming directive output of 1 byte
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |                                  ^~~~~~~
example/ssh2.c:227:13: note: 'snprintf' output 3 or more bytes (assuming 4) into a destination of size 2
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
example/ssh2.c:228:38: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size 0 [-Werror=format-truncation=]
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |                                      ^~
example/ssh2.c:228:34: note: assuming directive output of 1 byte
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |                                  ^~~~~~~
example/ssh2.c:228:13: note: 'snprintf' output 3 or more bytes (assuming 4) into a destination of size 2
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Ref: https://github.com/libssh2/libssh2/actions/runs/7055480458/job/19205970397#step:10:98

Fix:
```
tests/openssh_fixture.c:116:38: error: ' 2>&1' directive output may be truncated writing 5 bytes into a region of size between 1 and 1024 [-Werror=format-truncation=]
tests/openssh_fixture.c:116:11: note: 'snprintf' output between 6 and 1029 bytes into a destination of size 1024
```
Ref: https://github.com/libssh2/libssh2/actions/runs/7055480458/job/19205969221#step:10:51

Tested via #1257
2023-12-01 13:31:47 +00:00
2e57dcb9d2 example: fix indentation follow-up
Fix long line and fix more indentations.

Follow-up to 9e896e1b80
2023-12-01 13:02:11 +00:00
9e896e1b80 example: fix indentation
Tested via #1257
2023-12-01 12:18:01 +00:00
6c53815aa7 autotools: fix missed -pedantic and -Wall options for gcc
Follow-up to 5996fefe2b #1223

Tested via #1257
2023-12-01 05:50:12 +00:00
c912408809 ci: show compiler in cross/cygwin job names
Tested via #1257
2023-12-01 05:50:12 +00:00
4be5e33ae1 mbedtls: further improve disabling -Wredundant-decls
Move warning option suppression to `src/mbedtls.h` to surround the actual
external header #includes that need it.

Follow-up to ecec68a2c1 #1226
Follow-up to 7ecc309cd1 #1224

Tested via #1257
2023-12-01 05:49:25 +00:00
8d69e63db9 example: replace remaining libssh2_scp_recv with libssh2_scp_recv2 in output messages (#1258)
libssh2_scp_recv is deprecated and has been replaced by libssh2_scp_recv2
in prior commit.

Follow-up to 6c84a426be
2023-12-01 04:52:12 +01:00
363dcbf449 openssl: use OpenSSL 3 HMAC API, add no-deprecated CI job
- use OpenSSL 3 API when available for HMAC.
  This fixes building with OpenSSL 3 `no-deprecated` builds.

- ensure we support pure OpenSSL 3 API by adding a CI job using
  OpenSSL 3 custom-built with `no-deprecated`.

Follow-up to b0ab005fe7 #1207

Fixes #1235
Closes #1243
2023-11-27 09:41:05 +00:00
66108f02f0 ci: restore lost comment for FreeBSD [ci skip]
Follow-up to eee4e8055a
2023-11-26 12:00:23 +00:00
0c9a8e3590 ci: add OpenBSD (v7.4) job + fix build error in example
- Use CMake, LibreSSL and clang from the base install.

- This uncovered a build error in `example/subsystem_netconf.c`, caused
  by using the `%n` printf mask. This is a security risk and some
  systems (notably OpenBSD) disable this feature.

  Fix it by applying this patch from OpenBSD ports (from 2021-09-11):
  https://cvsweb.openbsd.org/ports/security/libssh2/patches/patch-example_subsystem_netconf_c?rev=1.1&content-type=text/x-cvsweb-markup
  2c5b2f3e94
  "The old code is also broken, as it passes a pointer to a variable
  of a different size (on LP64).  There is no check for truncation,
  but buf[] is 1MB in size."
  Patch-by: naddy

  ```
  /home/runner/work/libssh2/libssh2/example/subsystem_netconf.c:252:17: error: '%n' format specifier support is deactivated and will call abort(3) [-Werror]
        "]]>]]>\n%n", (int *)&len);
                 ~^
  /home/runner/work/libssh2/libssh2/example/subsystem_netconf.c:270:17: error: '%n' format specifier support is deactivated and will call abort(3) [-Werror]
        "]]>]]>\n%n", (int *)&len);
                 ~^
  2 errors generated.
  ```
  Ref: https://github.com/libssh2/libssh2/actions/runs/6991449778/job/19022024280#step:3:420

Also made tests with arm64, but it takes consistently almost 14m to
finish the job, vs. 2-3m for the native amd64:
https://github.com/libssh2/libssh2/actions/runs/6991648984/job/19022440525
https://github.com/libssh2/libssh2/actions/runs/6991551220/job/19022233651

Cherry-picked from #1250
Closes #1250
2023-11-26 10:16:56 +00:00
65c7a7a55a ci: add NetBSD (v9.3) job
Use CMake, OpenSSL (v1.1) and clang from the base install.

Cherry-picked from #1250
2023-11-26 10:16:01 +00:00
eee4e8055a ci: update and speed up FreeBSD job
- switch to an alternate GitHub action. This one seems (more) actively
  maintained, and runs faster:
  https://github.com/cross-platform-actions/action

- use clang instead of gcc. clang is already present in the base
  install, saving install time and bandwidth.

- stop installing `openssl-quictls` and use the OpenSSL (v1.1) from
  the base system.
  (I'm suspecting that quictls before this patch wasn't detected by
  the build.)
  https://wiki.freebsd.org/OpenSSL

Cherry-picked from #1250
2023-11-26 10:16:01 +00:00
c6589b8823 stop using leading underscores in macro names
Underscored macros are reserved for the compiler / standard lib / etc.
Stop using them in user code.

We used them as header guards in `src` and in `__FILESIZE` in `example`.

Closes #1248
2023-11-25 13:09:27 +00:00
7494881617 ci: use absolute path in CMAKE_INSTALL_PREFIX
To make the installed locations unambiguous in the build logs.

Closes #1247
2023-11-25 11:25:52 +00:00
efee91333b openssl: make a function static, add #ifdef comments
Follow-up to 0309229259 #248
where the function was added.

Also add comments to make `#ifdef` branches easier to follow in
`openssl.h`.

Closes #1246
2023-11-25 11:01:01 +00:00
236e79a115 ci: boost mbedTLS build speed
Build times down to 4 seconds (from 18-20).

Closes #1245
2023-11-25 11:01:00 +00:00
82581941d6 openssl: fix DSA code to use OpenSSL 3 API
- fix missing `DSA` type when building for OpenSSL 3 `no-deprecated`.
- fix fallouts after fixing the above by switching away from `DSA`
  with OpenSSL 3.

Follow-up to b0ab005fe7 #1207

Closes #1244
2023-11-25 04:44:15 +00:00
d2ef66473c openssl: formatting (delete empty lines) [ci skip] 2023-11-25 01:08:25 +00:00
5326a5ce26 tests: fall back to $LOGNAME for username
If the `$USER` variable is empty, fall back to using `$LOGNAME` to
retrieve the logged-in username.

In POSIX, `$LOGNAME` is a mandatory variable, while `$USER` isn't, and
on some systems it may not be set. Without this value, tests were unable
to provide the correct username when logging into the SSH server running
under the active user's session.

Reported-by: Nicolas Mora
Suggested-by: Nicolas Mora
Ref: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1056348
Fixes #1240
Closes #1241
2023-11-24 15:14:00 +00:00
631e7734c8 libssh2.h: use _WIN32 for Windows detection instead of rolling our own
Sync up `libssh2.h` Windows detection with the libssh2 source code.

`libssh2.h` was using `WIN32` and `LIBSSH2_WIN32` for Windows detection,
next to the official `_WIN32`. After this patch it only uses `_WIN32`
for this. Also, make it stop defining `LIBSSH2_WIN32`.

There is a slight chance these break compatibility with Windows
compilers that fail to define `_WIN32`. I'm not aware of any obsolete
or modern compiler affected, but in case there is one, one possible
solution is to define this macro manually.

Closes #1238
2023-11-22 13:10:54 +00:00
487152f4fa openssl: fix EC_KEY reference with OpenSSL 3 no-deprecated build
Fixes:
```
src/openssl.c:650:5: error: use of undeclared identifier 'EC_KEY'
    EC_KEY *ec_key = EC_KEY_new_by_curve_name(curve);
    ^
src/openssl.c:650:13: error: use of undeclared identifier 'ec_key'
    EC_KEY *ec_key = EC_KEY_new_by_curve_name(curve);
            ^
src/openssl.c:650:22: error: implicit declaration of function 'EC_KEY_new_by_curve_name' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    EC_KEY *ec_key = EC_KEY_new_by_curve_name(curve);
                     ^
src/openssl.c:650:22: note: did you mean 'EC_GROUP_new_by_curve_name'?
./quictls/_a64-mac-sys/usr/include/openssl/ec.h:483:11: note: 'EC_GROUP_new_by_curve_name' declared here
EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
          ^
In file included from ./_a64-mac-sys-bld/src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c:19:
In file included from src/crypto.c:10:
src/openssl.c:652:8: error: use of undeclared identifier 'ec_key'
    if(ec_key) {
       ^
```
Ref: https://github.com/curl/curl-for-win/actions/runs/6950001225/job/18909297867#step:3:4341

Follow-up to b0ab005fe7 #1207

Bug #1235
Closes #1236
2023-11-22 11:20:57 +00:00
910ed9b035 openssl: formatting
Sync up these lines with the other two similar occurrences in the code.

Cherry-picked from #1236
2023-11-22 11:19:57 +00:00
b0ab005fe7 openssl: use non-deprecated APIs with OpenSSL 3.x (#1207)
Assisted-by: Viktor Szakats
2023-11-21 18:33:01 +01:00
c9dd3566b2 ci: add BoringSSL job (cmake, gcc, amd64)
Closes #1233
2023-11-21 01:42:38 +00:00
89ccc83c7d autotools: fix dotless gcc and Apple clang version detections
- fix parsing dotless (major-only) gcc versions.
  Follow-up to 00a3b88c51 #1187

- sync gcc detection variable names with curl.

- fix Apple clang version detection for releases between
  'Apple LLVM version 7.3.0' and 'Apple LLVM version 10.0.1' where the
  version was under-detected as 3.7 llvm/clang equivalent.

- fix Apple clang version detection for 'Apple clang version 11.0.0'
  and newer where the Apple clang version was detected, instead of its
  llvm/clang equivalent.

- revert to show `clang` instead of `Apple clang`, because we follow it
  with an llvm/clang version number. (Apple-ness still visible in raw
  version.)

Used this collection for Apple clang / llvm/clang translation and test
inputs: https://gist.github.com/yamaya/2924292

Closes #1232
2023-11-20 20:04:56 +00:00
e9017b1046 acinclude.m4: revert accidental edit [ci skip]
Follow-up to 8c320a93a4
2023-11-20 01:48:33 +00:00
fb58016180 autotools: show more clang/gcc version details
Also:
- show if we detected Apple clang.
- delete duplicate version detection for clang.

Closes #1230
2023-11-19 22:42:31 +00:00
8c320a93a4 acinclude.m4: re-sync with curl [ci skip] 2023-11-18 16:04:53 +00:00
96682bd5e1 autotools: avoid warnings in libtool stub code
Seen on Windows with clang64, in libtool-generated stub code for
examples and tests.

The error didn't break the CI job for some reason.

msys2 (autotools, clang64, clang-x86_64:
```
[...]
2023-11-17T20:14:17.8639574Z ./.libs/lt-test_read.c:91:10: error: macro is not used [-Werror,-Wunused-macros]
[...]
2023-11-17T20:14:39.8729255Z ./.libs/lt-sftp_write_nonblock.c:91:10: error: macro is not used [-Werror,-Wunused-macros]
[...]
```
Ref: https://github.com/libssh2/libssh2/actions/runs/6908585056/job/18798193405?pr=1226#step:8:474

Follow-up to 7ecc309cd1 #1224

Closes #1227
2023-11-17 22:24:34 +00:00
ecec68a2c1 mbedtls: improve disabling -Wredundant-decls
Disable these warnings specifically for the mbedTLS public headers
and leave it on for the the rest of the code. This also fixes this
issue for autotools. Previous solution was globally disabling this
warning for the whole code when using mbedTLS and only with CMake.

Follow-up to 7ecc309cd1 #1224

Closes #1226
2023-11-17 20:17:19 +00:00
64d6789f71 cmake: rename picky warnings script
To match the camel-case style used in other CMake scripts and also
to match the name used in curl.

Closes #1225
2023-11-16 17:08:46 +00:00
7ecc309cd1 build: enable more compiler warnings and fix them
Enable more picky compiler warnings. I've found these options in the
nghttp3 project when implementing the CMake quick picky warning
functionality for it.

Fix issues found along the way:

- wincng, mbedtls: delete duplicate function declarations.
  Most of this was due to re-#defining crypto functions to
  crypto-backend specific implementations These redefines also remapped
  the declarations in `crypto.h`, making the backend-specific
  declarations duplicates.
  This patch deletes the backend-specific declarations.

- wincng mapped two crypto functions to the same local function.
  Also causing double declarations.
  Fix this by adding two disctinct wrappers and moving
  the common function to a static one.

- delete unreachable `break;` statements.

- kex: disable macros when unused.

- agent: disable unused constants.

- mbedtls: disable double declaration warnings because public mbedTLS
  headers trigger it. (with function `psa_set_key_domain_parameters`)

- crypto.h: formatting.

Ref: a70edb08e9/cmake/PickyWarningsC.cmake

Closes #1224
2023-11-16 14:35:23 +00:00
5996fefe2b autotools: sync warning enabler code with curl
Tiny changes and minor updates to bring this code closer
to curl's `m4/curl-compilers.m4`.

Closes #1223
2023-11-16 00:51:32 +00:00
e645fa95f3 acinclude.m4: fix indentation [ci skip]
Also match indentation of curl's `m4/curl-compilers.m4` for
easier syncing.
2023-11-15 23:13:51 +00:00
ce5f208a55 autotool: rename variable
`WARN` -> `tmp_CFLAGS`

To match curl and make syncing this code easier.

Ref: d1820768cc/m4/curl-compilers.m4 (L479)

Closes #1222
2023-11-15 22:40:55 +00:00
cdca8cff88 autotools: picky warning options tidy-up
- sync clang warning version limits with CMake.
- make `WARN=` vs. `CURL_ADD_COMPILER_WARNINGS()` consistent with curl
  and between clang and gcc (`WARN=` is for `no-` options in general).

Closes #1221
2023-11-15 20:25:31 +00:00
328a96b3da build: picky warning updates
- cmake, autotools: sync picky gcc warnings with curl.
- cmake, autotools: add `-Wold-style-definition` for clang too.
- cmake, autotools: add comment for `-Wformat-truncation=1`.
- cmake: more precise version info for old clang options.

Closes #1219
2023-11-15 11:02:04 +00:00
fea6664e1b ci: fixup FreeBSD version, bump mbedtls
We haven't been using the FreeBSD version. Also it turns out,
the single version supported is 13.2 at the moment:
  https://github.com/vmactions/freebsd-vm/tree/main/conf

Stop trying to set the version and instead rely on the action
providing the latest supported one automatically.

Follow-up to a7d2a573be

Also:
- add more details to the FreeBSD job description.
- bump mbedtls version while here.

Closes #1217
2023-11-10 16:58:19 +00:00
932d6a3277 cmake: fix multiple include of libssh2 package
Also extend our integration test double inclusion. It will still not
catch this case, because that requires
`cmake_minimum_required(VERSION 3.18)` or higher.

Fixes:
```
CMake Error at .../lib/cmake/libssh2/libssh2-config.cmake:8 (add_library):
  add_library cannot create ALIAS target "libssh2::libssh2" because another
  target with the same name already exists.
Call Stack (most recent call first):
  CMakeLists.txt:24 (find_package)

CMake Error at .../lib/cmake/libssh2/libssh2-config.cmake:13 (add_library):
  add_library cannot create ALIAS target "Libssh2::libssh2" because another
  target with the same name already exists.
Call Stack (most recent call first):
  CMakeLists.txt:24 (find_package)
```

Test to reproduce:
```cmake
cmake_minimum_required(VERSION 3.18)  # must be 3.18 or higher

project(test)

find_package(libssh2 CONFIG)
find_package(libssh2 CONFIG)  # fails

add_executable(test main.c)
target_link_libraries(test libssh2::libssh2)
```

Ref: https://cmake.org/cmake/help/latest/release/3.18.html#other-changes
Ref: https://cmake.org/cmake/help/v3.18/policy/CMP0107.html

Assisted-by: Kai Pastor
Assisted-by: Harry Mallon
Ref: https://github.com/curl/curl/pull/11913

Closes #1216
2023-11-09 22:38:07 +00:00
a7d2a573be ci: add FreeBSD 13.2 job
It runs over Linux via qemu. First two runs were (very) slow, then it
became (much) more performant at just 2x slower than a native Linux
build. Then got slow again, then fast again. Still seems acceptable
for the value this adds.

The build uses autotools and quictls.

Successful builds:
1. https://github.com/libssh2/libssh2/actions/runs/6802676786/job/18496286419 (13m59s, -j3)
2. https://github.com/libssh2/libssh2/actions/runs/6802976375/job/18497243225 (11m5s, -j2)
3. https://github.com/libssh2/libssh2/actions/runs/6803142201/job/18497785049 (3m6s, -j1)
4. https://github.com/libssh2/libssh2/actions/runs/6803194839/job/18497962766 (3m10s, -j2)
5. https://github.com/libssh2/libssh2/actions/runs/6803267201/job/18498208501 (3m13s)
6. https://github.com/libssh2/libssh2/actions/runs/6803510333/job/18498993698 (15m25s)
7. https://github.com/libssh2/libssh2/actions/runs/6813602863/job/18528571057 (3m13s)

Similar solution exists for Solaris (over macOS via VirtualBox), but it
hangs forever at `Waiting for text: solaris console login`:
https://github.com/libssh2/libssh2/actions/runs/6802388128/job/18495391869#step:4:185

Idea taken from LibreSSL.

FIXME: Unrelated, the `distcheck` job became flaky in recent days:
https://github.com/libssh2/libssh2/actions/runs/6802976375/job/18497256437#step:10:536
```
FAIL: test_auth_pubkey_ok_rsa_aes256gcm
```
https://github.com/libssh2/libssh2/actions/runs/6813602863/job/18528588933#step:10:533
```
FAIL: test_read
```

Closes #1215
2023-11-09 15:32:45 +00:00
b9a4ed83b4 reuse: fix duplicate copyright warning
```
PendingDeprecationWarning:
Copyright and licensing information for 'tests/openssh_server/Dockerfile'
has been found in both 'tests/openssh_server/Dockerfile' and in the DEP5
file located at '.reuse/dep5'. The information for these two sources has
been aggregated. In the future this behaviour will change, and you will
need to explicitly enable aggregation. [...]
```
Ref: https://github.com/libssh2/libssh2/actions/runs/6789274955/job/18456085964#step:4:4
2023-11-08 03:52:45 +00:00
4348557960 Makefile.mk: delete Windows-focused raw GNU Make build
We recommend using CMake instead. Especially in unity mode, it's faster
and probably more familiar for most. It's also easily portable.

(`Makefile.mk` was also portable, but in practice only usable for
Windows. Other platforms required a manual config header.)

Also:
- migrate `LIBSSH2_NO_*` option CI tests to CMake.
- make MSYS2 CMake builds verbose to show compilation options.

Closes #1204
2023-11-06 21:41:12 +00:00
bfa00f1bd5 tidy-up: around stdint.h
- os400: delete unused `HAVE_STDINT_H`.

- fuzz: delete redundant `stdint.h` use.
  `inttypes.h` is already included via `testinput.h`.

- docs/TODO: adjust type in planned function.

Closes #1212
2023-11-06 15:00:08 +00:00
20387285d3 cmake: show crypto backend in feature summary
This was visible as an enabled package before this patch, but it missed
to show WinCNG.

Closes #1211
2023-11-06 03:41:22 +00:00