Skip to content

Commit f0d6a28

Browse files
hbgitclaude
andcommitted
fix: resolve remaining static-analysis security findings (OpenSSF)
Fixes the static_analysis_fixed findings still open in TODO.md: - map2check.cpp: replace the new[]+strcpy+putenv pattern (CWE-119, clang-analyzer-security.insecureAPI.strcpy) with setenv(), which copies its arguments — also removes the deliberately leaked buffers - NonDetGeneratorKlee.c and NonDetGeneratorLibFuzzy.c: map2check_non_det_pchar returned a pointer to a local VLA (cppcheck returnDanglingLifetime); now heap-allocates the string. The LibFuzzy copy of the pattern was not listed in TODO.md but was flagged by cppcheck during verification - NonDetGeneratorLibFuzzy.c: cast get_next_input_from_fuzzer() to uint64_t before shifting — uint8_t promotes to int, and shifting an int by up to 56 bits is undefined behavior - NonDetLog.c: zero-initialize NONDET_CALL in new_nondet_call (the id field was never set and the row is persisted to disk by the BTree container); fix %d vs unsigned printf specifiers found by cppcheck Verified in the CI Docker image (clang-16): full build with KLEE and LibFuzzer enabled compiles and map2check --version runs (exercising the setenv path); cppcheck exits 0 on the three C files; the 6-test suite passes under strict ASan/UBSan and under Valgrind memcheck with 0 defects. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 0a0e6c0 commit f0d6a28

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

‎modules/backend/library/lib/NonDetGeneratorKlee.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../header/Map2CheckFunctions.h"
1010
#include "../header/NonDetGenerator.h"
1111
#include "../header/NonDetLog.h"
12+
#include <stdlib.h>
1213

1314
extern int __map2check_main__();
1415

@@ -33,12 +34,15 @@ extern void klee_make_symbolic(void *addr, size_t nbytes, const char *name);
3334
return non_det; \
3435
}
3536

36-
// TODO: this should be dynamic
3737
char *map2check_non_det_pchar() {
3838
unsigned length = map2check_non_det_unsigned();
3939
if (length == 0)
4040
return NULL;
41-
char string[length];
41+
/* heap allocation: returning a local VLA would leave the caller with a
42+
* dangling pointer (cppcheck returnDanglingLifetime) */
43+
char *string = malloc(length);
44+
if (string == NULL)
45+
return NULL;
4246
unsigned i = 0;
4347
for (i = 0; i < (length - 1); i++) {
4448
string[i] = map2check_non_det_char();

‎modules/backend/library/lib/NonDetGeneratorLibFuzzy.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ int map2check_non_det_int() {
100100
uint64_t result = 0;
101101
int i = 0;
102102
for (; i < 8; i++)
103-
result |= get_next_input_from_fuzzer() << (8 * i);
103+
/* cast before the shift: uint8_t promotes to int, and shifting an int
104+
* by up to 56 bits is undefined behavior */
105+
result |= (uint64_t)get_next_input_from_fuzzer() << (8 * i);
104106

105107
return (int)result;
106108
}
@@ -115,7 +117,11 @@ char *map2check_non_det_pchar() {
115117
unsigned length = map2check_non_det_unsigned();
116118
if (length == 0)
117119
return NULL;
118-
char string[length];
120+
/* heap allocation: returning a local VLA would leave the caller with a
121+
* dangling pointer (cppcheck returnDanglingLifetime) */
122+
char *string = malloc(length);
123+
if (string == NULL)
124+
return NULL;
119125
unsigned i = 0;
120126
for (i = 0; i < (length - 1); i++) {
121127
string[i] = map2check_non_det_char();

‎modules/backend/library/lib/NonDetLog.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Bool nondet_log_to_file(MAP2CHECK_CONTAINER klee_container) {
2929
return FALSE;
3030
}
3131
fprintf(output, "%d;", i);
32-
fprintf(output, "%d;", call->line);
33-
fprintf(output, "%d;", call->scope);
32+
fprintf(output, "%u;", call->line);
33+
fprintf(output, "%u;", call->scope);
3434
fprintf(output, "%s;", call->function_name);
35-
fprintf(output, "%d;", call->step_on_execution);
35+
fprintf(output, "%u;", call->step_on_execution);
3636

3737
// The value generated from nondet function
3838
if (((int)call->type) == UNSIGNED) {
@@ -61,6 +61,9 @@ NONDET_CALL new_nondet_call(enum NONDET_TYPE type, unsigned line,
6161
unsigned scope, void *value,
6262
const char *function_name, unsigned step) {
6363
NONDET_CALL result;
64+
/* zero-initialize: id and padding are persisted when the row is written
65+
* to disk by the BTree container */
66+
memset(&result, 0, sizeof(result));
6467
result.type = type;
6568
strncpy(result.function_name, function_name, FUNCTION_MAX_LENGTH_NAME);
6669
result.line = line;

‎modules/frontend/map2check.cpp‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,19 @@ inline void fixPath(char *map2check_bin_string) {
8888
} else {
8989
}
9090

91-
std::string map2check_env_var("MAP2CHECK_PATH=");
92-
map2check_env_var += pBuf;
91+
// setenv copies its arguments, avoiding the strcpy/putenv pattern
92+
// (CWE-119) and the manually leaked buffers it required
93+
setenv("MAP2CHECK_PATH", pBuf, 1);
94+
// Map2Check::Log::Debug(pBuf);
9395

94-
char *map2check_env_array = new char[map2check_env_var.length() + 1];
95-
strcpy(map2check_env_array, map2check_env_var.c_str());
96-
putenv(map2check_env_array);
97-
// Map2Check::Log::Debug(map2check_env_var);
98-
99-
std::string klee_env_var("KLEE_RUNTIME_LIBRARY_PATH=");
100-
klee_env_var += pBuf;
96+
std::string klee_env_var(pBuf);
10197
klee_env_var += "/lib/klee/runtime";
98+
setenv("KLEE_RUNTIME_LIBRARY_PATH", klee_env_var.c_str(), 1);
10299

103-
char *klee_env_array = new char[klee_env_var.length() + 1];
104-
strcpy(klee_env_array, klee_env_var.c_str());
105-
putenv(klee_env_array);
106-
107-
std::string ld_env_var("LD_LIBRARY_PATH=");
108-
ld_env_var += "$LD_LIBRARY_PATH:";
100+
std::string ld_env_var("$LD_LIBRARY_PATH:");
109101
ld_env_var += pBuf;
110102
ld_env_var += "/lib/";
111-
112-
char *ld_env_array = new char[ld_env_var.length() + 1];
113-
strcpy(ld_env_array, ld_env_var.c_str());
114-
putenv(ld_env_array);
103+
setenv("LD_LIBRARY_PATH", ld_env_var.c_str(), 1);
115104
}
116105
} // namespace
117106

0 commit comments

Comments
 (0)