Skip to content

eBPF-based security platform

log: escape control characters in messages

Daniel Borkmann26aa3b5f216a
diff --git a/util/log.c b/util/log.c
index 5cec34cb..55478a6e 100644
--- a/util/log.c
+++ b/util/log.c
@@ -9,22 +9,53 @@
#include <stdio.h>
#include <syslog.h>
+#include <shirudo/misc.h>
+
#include "log.h"
+#define LOG_MSG_MAX 8192
+
void shirudo_log_init(void)
{
openlog("shirudo", LOG_PID | LOG_NDELAY, LOG_AUTHPRIV);
}
+/*
+ * A message can carry text from outside, a registry's error body or a
+ * name from a package, and with it terminal escape sequences or line
+ * breaks that forge records. Keep printable ASCII, double a backslash and
+ * write every other byte as a backslash and three octal digits.
+ */
+static void escape(char *dst, size_t cap, const char *src)
+{
+ size_t n = 0;
+
+ for (; *src && n + 5 <= cap; src++) {
+ __u8 c = *src;
+
+ if (c == '\\') {
+ dst[n++] = '\\';
+ dst[n++] = '\\';
+ } else if (c >= 0x20 && c < 0x7f) {
+ dst[n++] = c;
+ } else {
+ dst[n++] = '\\';
+ dst[n++] = '0' + (c >> 6);
+ dst[n++] = '0' + ((c >> 3) & 7);
+ dst[n++] = '0' + (c & 7);
+ }
+ }
+ dst[n] = '\0';
+}
+
static void emit(int priority, const char *fmt, va_list ap)
{
- va_list ap2;
+ char msg[LOG_MSG_MAX], out[4 * LOG_MSG_MAX + 1];
- va_copy(ap2, ap);
- vsyslog(priority, fmt, ap);
- vfprintf(stderr, fmt, ap2);
- fputc('\n', stderr);
- va_end(ap2);
+ vsnprintf(msg, sizeof(msg), fmt, ap);
+ escape(out, sizeof(out), msg);
+ syslog(priority, "%s", out);
+ fprintf(stderr, "%s\n", out);
}
#define PR_DEFINE(name, prio) \
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index a47e1925..7961c03e 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -34,6 +34,7 @@
#include "crypto.h"
#include "hex.h"
#include "json.h"
+#include "log.h"
#include "config.h"
#include "macros.h"
#include "malloc.h"
@@ -1618,10 +1619,9 @@ static int check_partial_success(enum shirudo_otel_signal signal,
sizeof(error), 0, NULL, 0 };
if (shirudo_json_parse(body, len, root, ARRAY_SIZE(root)))
error[0] = '\0';
- fprintf(stderr, "otel: Collector partially rejected %lld"
- " %s%s%s\n", rejected,
- signal == SHIRUDO_OTEL_LOG ? "log record(s)" :
- "data point(s)", error[0] ? ": " : "", error);
+ pr_err("otel: Collector partially rejected %lld %s%s%s",
+ rejected, signal == SHIRUDO_OTEL_LOG ? "log record(s)" :
+ "data point(s)", error[0] ? ": " : "", error);
return -EREMOTEIO;
}
return 0;