Compare commits

..
Author SHA1 Message Date
Alex Jaramillo 5211348bb6 telemetry.c: improvements
- tm_set_payload: check payload for NULL value before duplication, this
  is desirable due the use of strnlen and strndup.

- tm_set_payload: replace strlen with strnlen to use MAX_PAYLOAD_LENGTH
  during function call instead of checking after the fact.

Signed-off-by: Alex Jaramillo <alex.v.jaramillo@intel.com>
2025-03-19 15:33:50 -07:00
10 changed files with 0 additions and 267 deletions
-1
View File
@@ -21,7 +21,6 @@ include $(top_srcdir)/src/nica/local.mk
include $(top_srcdir)/src/probes/local.mk
include $(top_srcdir)/src/journal/local.mk
include $(top_srcdir)/tests/local.mk
include $(top_srcdir)/fuzz/local.mk
release:
@git rev-parse v$(PACKAGE_VERSION) &> /dev/null; \
-3
View File
@@ -1,3 +0,0 @@
fuzz_libtelem
fuzz_config
fuzz_iorec
-54
View File
@@ -1,54 +0,0 @@
bin_PROGRAMS = fuzz_libtelem fuzz_config fuzz_iorec
CC = clang
CFLAGS = \
-g \
-I../. \
-I../src/. \
-DLOCALSTATEDIR=\"/tmp\" \
-DDATADIR="" \
-DBACKEND_ADDR=\"abcde\"
# Define the source files for each binary
fuzz_libtelem_SOURCES = \
fuzz_libtelem.c
fuzz_config_SOURCES = \
../src/configuration.c \
../src/nica/hashmap.c \
../src/nica/inifile.c \
utils.c \
fuzz_config.c
fuzz_iorec_SOURCES = \
utils.c \
fuzz_iorec.c
fuzz_iorec_CFLAGS = \
$(CFLAGS) \
-fsanitize=address,fuzzer
fuzz_libtelem_CFLAGS = \
$(CFLAGS) \
-fsanitize=address,fuzzer \
-ltelemetry
fuzz_config_CFLAGS = \
$(CFLAGS) \
-fsanitize=fuzzer # There's a false positive in configuration.c:189:56
all: $(bin_PROGRAMS)
fuzz_libtelem: $(fuzz_libtelem_SOURCES)
$(CC) $(fuzz_libtelem_CFLAGS) -o $@ $^
fuzz_config: $(fuzz_config_SOURCES)
$(CC) $(fuzz_config_CFLAGS) -o $@ $^
fuzz_iorec: $(fuzz_iorec_SOURCES)
$(CC) $(fuzz_iorec_CFLAGS) -o $@ $^
clean:
rm -f $(bin_PROGRAMS) *.o
.PHONY: all clean
-21
View File
@@ -1,21 +0,0 @@
# Fuzzing telemetrics-client
## Build
```
make
```
## Run
```
#fuzz_config
fuzz_config -close_fd_mask=2
#fuzz_libtelem
fuzz_libtelem
```
# Link runs in background
```
sudo systemd-run --unit=fuzzing.service -r --uid=$(id -u) \
--working-directory=${PWD}/fuzz -E LD_LIBRARY_PATH=${PWD}/src/.libs/ fuzz/<fuzzer_> -max_total_time=86400 -runs=10000000 fuzz/corpus
```
-32
View File
@@ -1,32 +0,0 @@
#include <configuration.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"
void test_configuration(const uint8_t *data, size_t size) {
char content[16384];
char config_file[] = "/tmp/config_fuzzerXXXXXX";
static struct configuration config = { { 0 }, { 0 }, { 0 }, false, NULL };
// Get a null ended string
strncpy(content, (const char *)data, size);
save_data(config_file, content);
read_config_from_file(config_file, &config);
for (int i = 0; i < 6; i++) {
if (config.strValues[i] == NULL) {
continue;
}
}
unlink(config_file);
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
test_configuration(data, size);
return 0;
}
-28
View File
@@ -1,28 +0,0 @@
#include <configuration.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "utils.h"
void test_read_record(const uint8_t *data, size_t size) {
char content[16384];
char record_file[] = "/tmp/record_fuzzerXXXXXX";
static struct configuration config = { { 0 }, { 0 }, { 0 }, false, NULL };
// Get a null ended string
strncpy(content, (const char *)data, size);
save_data(record_file, content);
// read_record(char *fullpath, char *headers[], char **body, char **cfg_file)
unlink(record_file);
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
test_read_record(data, size);
return 0;
}
-73
View File
@@ -1,73 +0,0 @@
#include <telemetry.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int test_tm_set_payload(char *payload) {
int ret = 0;
static uint32_t severity = 1;
static uint32_t payload_version = 1;
static const char *opt_class = "clearlinux/debug/fuzzing";
struct telem_ref *t_ref = NULL;
if ((ret = tm_create_record(&t_ref, (uint32_t)severity,
(char *)opt_class, payload_version)) < 0) {
return ret;
}
ret = tm_set_payload(t_ref, payload);
tm_free_record(t_ref);
return ret;
}
void test_tm_set_config_file(char *path) {
/*
No need to check returning value because
the important part is not it fails, but
if this crashes.
*/
tm_set_config_file(path);
}
int test_tm_set_event_id(char *full_id) {
int ret = 0;
char *id = NULL;
static uint32_t severity = 1;
static uint32_t payload_version = 1;
static const char *opt_class = "clearlinux/debug/fuzzing";
struct telem_ref *t_ref = NULL;
if ((ret = tm_create_record(&t_ref, (uint32_t)severity,
(char *)opt_class, payload_version)) < 0) {
return ret;
}
id = strndup(full_id, 32);
ret = tm_set_event_id(t_ref, id);
free(id);
tm_free_record(t_ref);
return ret;
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
int ret = 0;
char *payload = NULL;
/*
tm_set_payload expects a null terminated string, this
step duplicates data into a null terminated string.
*/
payload = strndup((char *)data, size);
if (payload == NULL) {
goto end;
}
test_tm_set_payload(payload);
test_tm_set_config_file(payload);
test_tm_set_event_id(payload);
free(payload);
end:
return ret;
}
-30
View File
@@ -1,30 +0,0 @@
COMPILER = clang
CLANG_FLAGS = \
-g \
-fsanitize=address,fuzzer
SOURCES = fuzz/libtelem_fuzzer.c
BIN = fuzz/libtelem_fuzzer
HALF_DAY_SEC = 43200
FUZZ_FLAGS = \
-max_len=16384 \
-timeout=10 \
-only_ascii=1 \
-runs=1000000 \
-max_total_time=$(HALF_DAY_SEC)
build_fuzz_libtelem:
LD_LIBRARY_PATH=src/.libs $(COMPILER) $(CLANG_FLAGS) -ltelemetry $(SOURCES) -o $(BIN)
fuzz: build_fuzz_kibtelem
$(BIN) fuzz/corpus $(FUZZ_FLAGS)
# vim: filetype=automake tabstop=8 shiftwidth=8 noexpandtab
create a makefile that builds {3 binaries}
{4 binaries}: fuzz_libtelem fuzz_config fuzz_iorec
each one has different compiling flags and SOURCES
-24
View File
@@ -1,24 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int save_data(char *filename, const char *data) {
FILE *temp_file = NULL;
int fd;
fd = mkstemp(filename);
if (fd == -1) {
return 1;
}
temp_file = fdopen(fd, "w");
if (temp_file == NULL) {
close(fd);
return 1;
}
fprintf(temp_file, "%s", data);
fclose(temp_file);
return 0;
}
-1
View File
@@ -1 +0,0 @@
int save_data(char *filename, const char *data);