Skip to content

eBPF-based security platform

build: warn on format strings, writable literals and undefined macros

Daniel Borkmann696d825ddcdc
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 66a506d1..94fee9a7 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -80,7 +80,8 @@ WERROR ?= 1
WERROR_CFLAGS := $(if $(filter 1,$(WERROR)),-Werror)
WARNINGS := -Wall -Wextra -Wno-unused-parameter -Wstrict-prototypes -Wvla \
-Wmissing-prototypes -Wpointer-arith -Wshadow \
- -Wdeclaration-after-statement -Wformat-security \
+ -Wdeclaration-after-statement -Wformat-security -Wformat=2 \
+ -Wwrite-strings -Wundef \
$(WERROR_CFLAGS)
SHIRUDO_DEBUG ?= 1
@@ -891,7 +892,8 @@ unit-tests: $(UNIT_TESTS)
# and the compiler runs both checks. sparse has no __has_include either,
# which glibc 2.43 asks to find <linux/openat2.h> before declaring a
# struct open_how of its own; __glibc_has_open_how gives the answer the
-# compiler would.
+# compiler would. sparse predefines no __FLT_EVAL_METHOD__, which
+# glibc's <math.h> tests, so -Wundef is the compiler's to check.
SPARSE_TYPES_H ?= /usr/include/linux/types.h
SPARSE_SRCS := shirudo.c $(BUILTIN_SRCS) $(UTIL_SRCS) $(BUILD_SRCS)
@@ -899,7 +901,8 @@ SPARSE_SRCS := shirudo.c $(BUILTIN_SRCS) $(UTIL_SRCS) $(BUILD_SRCS)
sparse:
$(Q)for f in $(SPARSE_SRCS); do \
printf ' SPARSE %s\n' "$$f"; \
- cgcc -no-compile -Wsparse-error $(CFLAGS) -U_FORTIFY_SOURCE \
+ cgcc -no-compile -Wsparse-error $(CFLAGS) -Wno-undef \
+ -U_FORTIFY_SOURCE \
-DCURL_DISABLE_TYPECHECK -D__glibc_has_open_how=1 \
-include $(SPARSE_TYPES_H) -D_UAPI_LINUX_TYPES_H \
"$$f" || exit 1; \
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 7cebdf0c..3e1a7234 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -2385,7 +2385,7 @@ static int bpm_exec_operator(const char *sub, int argc, const char **argv)
helper_argv[2] = sub;
for (int i = 3; i < argc + 1; i++)
helper_argv[i] = argv[i - 1];
- ret = build_exec(label, (char **)helper_argv);
+ ret = build_exec(label, helper_argv);
xfree(helper_argv, ((size_t)argc + 2) * sizeof(*helper_argv));
return ret;
}
diff --git a/builtin-build.c b/builtin-build.c
index 33c65321..1ce06bfc 100644
--- a/builtin-build.c
+++ b/builtin-build.c
@@ -88,7 +88,7 @@ bool build_available(void)
return use_path ? on_path(helper) : access(helper, X_OK) == 0;
}
-int build_exec(const char *label, char **argv)
+int build_exec(const char *label, const char **argv)
{
bool use_path;
const char *path = resolve_helper(&use_path);
@@ -104,11 +104,11 @@ int build_exec(const char *label, char **argv)
"PATH.", label, path);
return 127;
}
- argv[0] = (char *)path;
+ argv[0] = path;
if (use_path)
- execvp(path, argv);
+ execvp(path, (char *const *)argv);
else
- execv(path, argv);
+ execv(path, (char *const *)argv);
pr_err("exec %s: %s", path, strerror(errno));
return 127;
}
@@ -119,5 +119,5 @@ int cmd_build(int argc, const char **argv)
print_help(&build_help);
return 0;
}
- return build_exec("build", (char **)argv);
+ return build_exec("build", argv);
}
diff --git a/builtin-image.c b/builtin-image.c
index b65fbd81..6e61e5a8 100644
--- a/builtin-image.c
+++ b/builtin-image.c
@@ -33,7 +33,7 @@ int cmd_image(int argc, const char **argv)
return argc < 2 ? 2 : 0;
}
if (!strcmp(argv[1], "build") || !strcmp(argv[1], "verify")) {
- char **helper;
+ const char **helper;
size_t bytes;
int i;
@@ -43,7 +43,7 @@ int cmd_image(int argc, const char **argv)
return 1;
helper[1] = "image";
for (i = 1; i < argc; i++)
- helper[i + 1] = (char *)argv[i];
+ helper[i + 1] = argv[i];
ret = build_exec(!strcmp(argv[1], "build") ?
"image build" : "image verify", helper);
xfree(helper, bytes);
diff --git a/builtin-keys.c b/builtin-keys.c
index 2ab06e34..1ed5107f 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -878,7 +878,7 @@ static int keys_vouch(int argc, const char **argv)
helper_argv[2] = "vouch";
for (int i = 1; i < argc; i++)
helper_argv[i + 2] = argv[i];
- ret = build_exec("keys vouch", (char **)helper_argv);
+ ret = build_exec("keys vouch", helper_argv);
xfree(helper_argv, sz);
return ret;
}
diff --git a/builtin.h b/builtin.h
index 1cebc8c6..8b5e54a0 100644
--- a/builtin.h
+++ b/builtin.h
@@ -19,7 +19,7 @@ int cmd_build(int argc, const char **argv);
* site, which returns only on failure.
*/
bool build_available(void);
-int build_exec(const char *label, char **argv);
+int build_exec(const char *label, const char **argv);
/* Target-side. */
int cmd_init(int argc, const char **argv);
diff --git a/testing/fuzz/log.c b/testing/fuzz/log.c
index dde57238..613c7768 100644
--- a/testing/fuzz/log.c
+++ b/testing/fuzz/log.c
@@ -19,7 +19,7 @@ void log_init(void)
* not to the journal. Formatting still runs, so a message that reads
* past what it prints is caught.
*/
-static void drop(const char *fmt, va_list ap)
+static __printf(1, 0) void drop(const char *fmt, va_list ap)
{
char msg[8192];
diff --git a/tools/shirudo-build/image.c b/tools/shirudo-build/image.c
index 925d2311..de3b67a2 100644
--- a/tools/shirudo-build/image.c
+++ b/tools/shirudo-build/image.c
@@ -43,7 +43,7 @@ static void image_usage(FILE *fp)
static int __build_image(int argc, char **argv)
{
char executable[PATH_MAX], script[PATH_MAX], directory[PATH_MAX];
- char **command;
+ const char **command;
ssize_t size;
char *slash;
int i;
@@ -72,7 +72,7 @@ static int __build_image(int argc, char **argv)
command[2] = executable;
for (i = 2; i < argc; i++)
command[i + 1] = argv[i];
- execv(script, command);
+ execv(script, (char *const *)command);
pr_err("image build: cannot run %s: %s", script, strerror(errno));
xfree(command, ((size_t)argc + 2) * sizeof(*command));
return 1;
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index eeaea961..c0220dbc 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -1214,7 +1214,8 @@ int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
{
struct bpm_http_response r = {};
char hex[65], digest[72], url[PATH_MAX], upload[PATH_MAX];
- char location[PATH_MAX], *sep;
+ char location[PATH_MAX];
+ const char *sep;
int ret;
if (bpm_sha256_hex(body, body_len, hex, sizeof(hex)))
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 9fc1b3a3..2976f17a 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -251,7 +251,7 @@ static int selector_config_line(const char *line,
char values[][TREE_STRING_MAX],
bool found[])
{
- char copy[1024], *start, *eq, *end, *value;
+ char copy[1024], not_set[] = "n", *start, *eq, *end, *value;
int index;
if (strscpy(copy, line, sizeof(copy)) < 0)
@@ -268,7 +268,7 @@ static int selector_config_line(const char *line,
if (!eq)
return 0;
*eq = '\0';
- value = "n";
+ value = not_set;
} else {
if (*start == '#')
return 0;
diff --git a/util/sysinfo.c b/util/sysinfo.c
index 989bac49..93a25612 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -190,7 +190,7 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
if (!f)
return -1;
while (fgets(line, sizeof(line), f)) {
- char *key, *value, *eq, *not_set;
+ char *key, *value, *eq, *not_set, unset[] = "n";
line[strcspn(line, "\r\n")] = '\0';
key = trim_config_text(line);
@@ -203,7 +203,7 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
if (!not_set || trim_config_text(not_set + 11)[0] != '\0')
continue;
*not_set = '\0';
- value = "n";
+ value = unset;
} else {
eq = strchr(key, '=');
if (!eq)
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 2dcc3b95..417f31c1 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -1138,6 +1138,7 @@ static int add_configured_headers(struct otel_state *st, const char *input)
pair = strtok_r(NULL, ",", &save)) {
char *eq = strchr(pair, '=');
char *name, *value, *line;
+ const char *header;
bool tenant;
size_t n;
@@ -1170,15 +1171,15 @@ static int add_configured_headers(struct otel_state *st, const char *input)
break;
}
tenant_seen = true;
- name = "X-Tenant-ID";
}
- n = strlen(name) + strlen(value) + 3;
+ header = tenant ? "X-Tenant-ID" : name;
+ n = strlen(header) + strlen(value) + 3;
line = xmalloc(n);
if (!line) {
ret = -ENOMEM;
break;
}
- ret = strsprintf(line, n, "%s: %s", name, value) < 0 ? -E2BIG :
+ ret = strsprintf(line, n, "%s: %s", header, value) < 0 ? -E2BIG :
append_header(st, line);
memzero_explicit(line, n);
xfree(line, n);