Skip to content

eBPF-based security platform

build: take the kernel's warnings where the compiler has them

Daniel Borkmann961d394e1ce5
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 2d4574dd..c646c180 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -84,6 +84,21 @@ WARNINGS := -Wall -Wextra -Wno-unused-parameter -Wstrict-prototypes -Wvla \
-Wwrite-strings -Wundef \
$(WERROR_CFLAGS)
+# The warnings that not every compiler shirudo builds with has, each
+# kept where the compiler takes it: a __DATE__ that would make the build
+# unreproducible, a switch without a default, an include directory that
+# does not exist, and gcc's test of a logical operator on equal
+# operands. gcc's
+# -Wmaybe-uninitialized is off, as in the kernel: what it reports
+# changes with the gcc version and the optimization level, so it fails a
+# build on one host and not on the next, and the uninit Coccinelle rule
+# and -ftrivial-auto-var-init=zero cover what it would.
+cc-option = $(foreach f,$(2),$(shell $(1) -Werror $(f) -E -x c /dev/null \
+ -o /dev/null >/dev/null 2>&1 && echo $(f)))
+PROBED_WARNINGS := -Wdate-time -Wswitch-default -Wmissing-include-dirs \
+ -Wlogical-op -Wno-maybe-uninitialized
+CC_WARNINGS := $(call cc-option,$(CC),$(PROBED_WARNINGS))
+
SHIRUDO_DEBUG ?= 1
DEBUG_CFLAGS := $(if $(filter 1 yes y true,$(SHIRUDO_DEBUG)),-g,)
@@ -114,8 +129,8 @@ HARDEN_STATIC_PIE_LDFLAGS := $(filter-out -pie,$(HARDEN_LDFLAGS))
LANG_CFLAGS := -fno-strict-overflow -fno-delete-null-pointer-checks \
-fno-strict-aliasing -funsigned-char
-CFLAGS_BASE := -O2 $(DEBUG_CFLAGS) -std=gnu11 $(WARNINGS) $(HARDEN_CFLAGS) \
- $(LANG_CFLAGS) \
+CFLAGS_BASE := -O2 $(DEBUG_CFLAGS) -std=gnu11 $(WARNINGS) $(CC_WARNINGS) \
+ $(HARDEN_CFLAGS) $(LANG_CFLAGS) \
-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE \
-DLIBEXEC_DIR='"$(SHIRUDO_LIBEXEC_DIR)"' \
-DBPF_INCDIR='"$(SHIRUDO_BPF_INCDIR)"' \
@@ -923,9 +938,10 @@ sparse:
# with clang from the util sources under ASan and UBSan; make -C testing
# fuzz builds and runs them.
FUZZ_CC ?= clang
+FUZZ_WARNINGS := $(call cc-option,$(FUZZ_CC),$(PROBED_WARNINGS))
FUZZ_CFLAGS := -O1 -g -std=gnu11 -fno-omit-frame-pointer $(LANG_CFLAGS) \
-fsanitize=fuzzer,address,undefined \
- -fno-sanitize-recover=all $(WARNINGS) \
+ -fno-sanitize-recover=all $(WARNINGS) $(FUZZ_WARNINGS) \
-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(INCLUDES) \
$(LIBCRYPTO_CFLAGS) $(LIBCURL_CFLAGS) $(ZLIB_CFLAGS)
FUZZ_LIBS := $(LIBCRYPTO_LIBS) $(LIBSSL_LIBS) $(LIBCURL_LIBS) $(ZLIB_LIBS)
diff --git a/util/kernel_abi.c b/util/kernel_abi.c
index 2ce5e5a0..f4b86486 100644
--- a/util/kernel_abi.c
+++ b/util/kernel_abi.c
@@ -28,7 +28,8 @@
#define MAP_SHA_SIZE 32U
#define BTF_MAX_UNWRAP 32
-static int abi_fail(char *detail, size_t detail_sz, const char *fmt, ...)
+static __printf(3, 4) int abi_fail(char *detail, size_t detail_sz,
+ const char *fmt, ...)
{
va_list ap;
diff --git a/util/log.c b/util/log.c
index 05451bea..3fdd2ee0 100644
--- a/util/log.c
+++ b/util/log.c
@@ -55,7 +55,7 @@ static void escape(char *dst, size_t cap, const char *src)
* may change errno on the way (no /dev/log in an early initrd), so it
* is kept for them.
*/
-static void emit(int priority, const char *fmt, va_list ap)
+static __printf(2, 0) void emit(int priority, const char *fmt, va_list ap)
{
char msg[LOG_MSG_MAX], out[4 * LOG_MSG_MAX + 1];
int saved_errno = errno;
diff --git a/util/serializer.c b/util/serializer.c
index 5084f561..48c2d49d 100644
--- a/util/serializer.c
+++ b/util/serializer.c
@@ -57,7 +57,7 @@ int serializer_lock_acquire_at(const char *dir, __u32 map_id)
}
/* The open fd owns the lock; callers release it with close(). */
if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
- ret = (errno == EWOULDBLOCK || errno == EAGAIN) ? -EBUSY : -errno;
+ ret = errno == EWOULDBLOCK ? -EBUSY : -errno;
close(fd);
return ret;
}
diff --git a/util/upatch_rodata.c b/util/upatch_rodata.c
index fa24ab7f..48d30db4 100644
--- a/util/upatch_rodata.c
+++ b/util/upatch_rodata.c
@@ -488,6 +488,8 @@ static int append_type_name(const struct btf *btf, __u32 id, char *buf,
case BTF_KIND_FWD:
fallback = "forward declaration";
break;
+ default:
+ break;
}
n = strsprintf(buf + *off, cap - *off, "%s", fallback);
}