From 509eac8b96314933e51ebff41fed525524a1d340 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Mon, 8 Jun 2026 15:50:45 +0200 Subject: [PATCH] checksrc: extend to ossfuzz, reduce scope of exceptions Replace a few calls in tests to reduce exceptions. Closes #1998 --- example/.checksrc | 7 +++++++ scripts/checksrc-all.pl | 22 +++++----------------- src/agent.c | 1 + src/blowfish.c | 4 ++++ src/misc.c | 2 ++ src/scp.c | 3 +++ src/sftp.c | 1 + src/userauth.c | 2 ++ tests/.checksrc | 3 +++ tests/cmake/test.c | 7 +++++-- tests/openssh_fixture.c | 4 ++-- tests/ossfuzz/.checksrc | 7 +++++++ tests/ossfuzz/Makefile.am | 2 +- tests/test_read.c | 4 ++-- vms/.checksrc | 4 ++++ 15 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 tests/ossfuzz/.checksrc diff --git a/example/.checksrc b/example/.checksrc index 3fa79734..917c3612 100644 --- a/example/.checksrc +++ b/example/.checksrc @@ -1,4 +1,11 @@ # Copyright (C) The libssh2 project and its contributors. # SPDX-License-Identifier: BSD-3-Clause +allowfunc accept +allowfunc atoi allowfunc fprintf +allowfunc fstat +allowfunc printf +allowfunc recv +allowfunc send +allowfunc strdup diff --git a/scripts/checksrc-all.pl b/scripts/checksrc-all.pl index a2661c6b..6cabac8d 100755 --- a/scripts/checksrc-all.pl +++ b/scripts/checksrc-all.pl @@ -13,40 +13,28 @@ use Cwd 'abs_path'; my @options = ( "-i4", "-m79", "-AFIXME", "-AERRNOVAR", - "-aaccept", - "-aatoi", "-acalloc", "-aclose", - "-aCreateFileA", "-afclose", "-afopen", "-afree", - "-afstat", "-amalloc", - "-aprintf", "-arealloc", - "-arecv", - "-asend", "-asnprintf", "-asocket", - "-asocketpair", - "-astrdup", - "-astrtok", - "-astrtol", - "-avsnprintf", ); my @files; my $is_git = 0; if(system('git rev-parse --is-inside-work-tree >/dev/null 2>&1') == 0) { - open(O, '-|', 'git', 'ls-files', '*.[ch]') || die; push @files, ; close(O); + open(O, '-|', 'git', 'ls-files', '*.[ch]', '*.cc') || die; push @files, ; close(O); $is_git = 1; } else { - find(sub { if(/\.[ch]$/) { push(@files, $File::Find::name) } }, ('.')); + find(sub { if(/\.([ch]|cc)$/) { push(@files, $File::Find::name) } }, ('.')); } if(@ARGV) { - find(sub { if(/\.[ch]$/) { push(@files, $File::Find::name) } }, @ARGV); + find(sub { if(/\.([ch]|cc)$/) { push(@files, $File::Find::name) } }, @ARGV); } @files = grep !/\/CMakeFiles\//, @files; @@ -59,11 +47,11 @@ my $anyfailed = 0; for my $dir (@dirs) { if($is_git) { @files = (); - open(O, '-|', 'git', 'ls-files', ":(glob)$dir/*.[ch]") || die; push @files, ; close(O); + open(O, '-|', 'git', 'ls-files', ":(glob)$dir/*.[ch]", ":(glob)$dir/*.cc") || die; push @files, ; close(O); chomp(@files); } else { - @files = glob("$dir/*.[ch]"); + @files = glob("$dir/*.[ch] $dir/*.cc"); } if(@files && system("$scripts_dir/checksrc.pl", @options, @files) != 0) { $anyfailed = 1; diff --git a/src/agent.c b/src/agent.c index eaa2b74a..ed4bcdd0 100644 --- a/src/agent.c +++ b/src/agent.c @@ -260,6 +260,7 @@ static int agent_connect_openssh(LIBSSH2_AGENT *agent) * uncomment the following line to enable support within the Win32 * OpenSSH code. */ + /* !checksrc! disable BANNEDFUNC 1 */ pipe = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, /* FILE_FLAG_OVERLAPPED | */ diff --git a/src/blowfish.c b/src/blowfish.c index 83c66c4d..d1c7cd90 100644 --- a/src/blowfish.c +++ b/src/blowfish.c @@ -570,6 +570,7 @@ static void report(uint32_t data[], uint16_t len) { int i; for(i = 0; i < len; i += 2) + /* !checksrc! disable BANNEDFUNC 1 */ printf("Block %d: 0x%08lx 0x%08lx.\n", i / 2, (unsigned long)data[i], (unsigned long)data[i + 1]); } @@ -593,15 +594,18 @@ int main(void) blf_enc(&c, data, 5); blf_dec(&c, data, 1); blf_dec(&c, data + 2, 4); + /* !checksrc! disable BANNEDFUNC 1 */ printf("Should read as 0 - 9.\n"); report(data, 10); /* Second test */ blf_key(&c, (uint8_t *)key2, (uint16_t)strlen(key2)); blf_enc(&c, data2, 1); + /* !checksrc! disable BANNEDFUNC 1 */ printf("\nShould read as: 0x324ed0fe 0xf413a203.\n"); report(data2, 2); blf_dec(&c, data2, 1); + /* !checksrc! disable BANNEDFUNC 1 */ printf("\nShould read as: 0x424c4f57 0x46495348.\n"); report(data2, 2); diff --git a/src/misc.c b/src/misc.c index fca6688c..f7be1df0 100644 --- a/src/misc.c +++ b/src/misc.c @@ -75,6 +75,7 @@ int ssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...) if(cp_max_len < 2) return 0; va_start(args, fmt); + /* !checksrc! disable BANNEDFUNC 1 */ n = vsnprintf(cp, cp_max_len, fmt, args); va_end(args); return (n < (int)cp_max_len) ? n : (int)(cp_max_len - 1); @@ -595,6 +596,7 @@ void ssh2_deb_low(LIBSSH2_SESSION *session, int context, #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif + /* !checksrc! disable BANNEDFUNC 1 */ len = vsnprintf(buffer + msglen, buflen, format, vargs); #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop diff --git a/src/scp.c b/src/scp.c index 1e0de0cc..2d75682a 100644 --- a/src/scp.c +++ b/src/scp.c @@ -528,6 +528,7 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, *(p++) = '\0'; /* Make sure we do not get fooled by leftover values */ + /* !checksrc! disable BANNEDFUNC 1 */ session->scpRecv_mtime = strtol((char *)s, NULL, 10); s = (unsigned char *)strchr((char *)p, ' '); @@ -552,6 +553,7 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, *p = '\0'; /* Make sure we do not get fooled by leftover values */ + /* !checksrc! disable BANNEDFUNC 1 */ session->scpRecv_atime = strtol((char *)s, NULL, 10); /* SCP ACK */ @@ -682,6 +684,7 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, *(p++) = '\0'; /* Make sure we do not get fooled by leftover values */ + /* !checksrc! disable BANNEDFUNC 1 */ session->scpRecv_mode = strtol(s, &e, 8); if(e && *e) { ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, diff --git a/src/sftp.c b/src/sftp.c index 06198929..3287b5e4 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -972,6 +972,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) } memcpy(extversion_str, extdata, extdata_len); extversion_str[extdata_len] = '\0'; + /* !checksrc! disable BANNEDFUNC 1 */ extversion = (uint32_t)strtol(extversion_str, NULL, 10); SSH2_FREE(session, extversion_str); } diff --git a/src/userauth.c b/src/userauth.c index b952c56c..00a8acb9 100644 --- a/src/userauth.c +++ b/src/userauth.c @@ -1348,10 +1348,12 @@ static int is_version_less_than_78(const char *version) if(!version) return 0; + /* !checksrc! disable BANNEDFUNC 1 */ major = strtol(version, &endptr_major, 10); if(!endptr_major || *endptr_major != '.') return 0; /* Not a valid number */ + /* !checksrc! disable BANNEDFUNC 1 */ minor = strtol(endptr_major + 1, &endptr_minor, 10); if(!endptr_minor || endptr_minor == endptr_major + 1) return 0; /* Not a valid number */ diff --git a/tests/.checksrc b/tests/.checksrc index 3fa79734..a4cd0d16 100644 --- a/tests/.checksrc +++ b/tests/.checksrc @@ -1,4 +1,7 @@ # Copyright (C) The libssh2 project and its contributors. # SPDX-License-Identifier: BSD-3-Clause +allowfunc atoi allowfunc fprintf +allowfunc printf +allowfunc vsnprintf diff --git a/tests/cmake/test.c b/tests/cmake/test.c index b00dcc3d..e39d850d 100644 --- a/tests/cmake/test.c +++ b/tests/cmake/test.c @@ -32,8 +32,11 @@ int main(int argc, char **argv) crypto_str = "(unrecognized)"; } - printf("libssh2 test: |%s|%s|%s|\n", argv[0], - libssh2_version(0), crypto_str); + puts("libssh2 test:"); + puts(argv[0]); + puts(libssh2_version(0)); + puts(crypto_str); + puts("---"); return 0; } diff --git a/tests/openssh_fixture.c b/tests/openssh_fixture.c index 7ed50323..23acbb07 100644 --- a/tests/openssh_fixture.c +++ b/tests/openssh_fixture.c @@ -54,7 +54,7 @@ #endif #include -#include +#include /* for atoi() */ #include #include @@ -413,7 +413,7 @@ static libssh2_socket_t open_socket_to_container(char *container_id) } sin.sin_family = AF_INET; - sin.sin_port = htons((unsigned short)strtol(port_string, NULL, 0)); + sin.sin_port = htons((unsigned short)atoi(port_string)); sin.sin_addr.s_addr = hostaddr; for(counter = 0; counter < 3; ++counter) { diff --git a/tests/ossfuzz/.checksrc b/tests/ossfuzz/.checksrc new file mode 100644 index 00000000..b45de2ad --- /dev/null +++ b/tests/ossfuzz/.checksrc @@ -0,0 +1,7 @@ +# Copyright (C) The libssh2 project and its contributors. +# SPDX-License-Identifier: BSD-3-Clause + +allowfunc fprintf +allowfunc printf +allowfunc send +allowfunc socketpair diff --git a/tests/ossfuzz/Makefile.am b/tests/ossfuzz/Makefile.am index e8a9b8fc..2c93b3c5 100644 --- a/tests/ossfuzz/Makefile.am +++ b/tests/ossfuzz/Makefile.am @@ -34,4 +34,4 @@ ssh2_client_fuzzer_LDFLAGS = $(AM_LDFLAGS) -static libstandaloneengine_a_SOURCES = standaloneengine.cc libstandaloneengine_a_CXXFLAGS = $(AM_CXXFLAGS) -EXTRA_DIST = CMakeLists.txt ossfuzz.sh +EXTRA_DIST = .checksrc CMakeLists.txt ossfuzz.sh diff --git a/tests/test_read.c b/tests/test_read.c index 7562bf25..42518351 100644 --- a/tests/test_read.c +++ b/tests/test_read.c @@ -8,7 +8,7 @@ #include "runner.h" #include "openssh_fixture.h" -#include /* for getenv() */ +#include /* for atoi(), getenv() */ /* set in Dockerfile */ static const char *username = "libssh2"; @@ -81,7 +81,7 @@ int test(LIBSSH2_SESSION *session) env = getenv("FIXTURE_XFER_COUNT"); if(env) { - xfer_count = (unsigned long)strtol(env, NULL, 0); + xfer_count = (unsigned long)atoi(env); fprintf(stderr, "Custom xfer_count: %lu\n", xfer_count); } diff --git a/vms/.checksrc b/vms/.checksrc index 3fa79734..0fd376c4 100644 --- a/vms/.checksrc +++ b/vms/.checksrc @@ -1,4 +1,8 @@ # Copyright (C) The libssh2 project and its contributors. # SPDX-License-Identifier: BSD-3-Clause +allowfunc atoi allowfunc fprintf +allowfunc printf +allowfunc strdup +allowfunc strtok