Skip to content

eBPF-based security platform

util: read the kernel's numbers with the bounded parser

Daniel Borkmannc1c6a8c7a970
diff --git a/util/kmodules.c b/util/kmodules.c
index 4a4824ec..1c52834c 100644
--- a/util/kmodules.c
+++ b/util/kmodules.c
@@ -8,6 +8,7 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -17,6 +18,7 @@
#include <time.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "format.h"
#include "kmodules.h"
@@ -93,6 +95,21 @@ static int kmod_find(const struct shirudo_kmod_set *set, const char *name)
return -1;
}
+/*
+ * The number in the next column, which *p then points past, or 0 when
+ * there is none: the kernel prints "-" as the use count of a module it
+ * cannot unload.
+ */
+static __u64 next_column(char **p, __u64 max)
+{
+ char *s = *p + strspn(*p, " \t");
+ size_t n = digits(s, strlen(s));
+ __u64 v;
+
+ *p = s + n;
+ return parse_u64(s, n, max, &v) ? 0 : v;
+}
+
int kmodules_parse(char *buf, struct shirudo_kmod_set *set)
{
size_t mod_cap = 64, deps_cap = 256, edge_cap = 256, n_edges = 0;
@@ -150,8 +167,8 @@ int kmodules_parse(char *buf, struct shirudo_kmod_set *set)
m = &set->mods[set->n++];
memcpy(m->name, p, name_len);
m->name[name_len] = '\0';
- m->size = strtoull(e, &p, 10);
- m->refcount = (__u32)strtoul(p, NULL, 10);
+ m->size = next_column(&e, UINT64_MAX);
+ m->refcount = (__u32)next_column(&e, UINT32_MAX);
}
qsort(set->mods, set->n, sizeof(*set->mods), kmod_cmp);
diff --git a/util/kmsg.c b/util/kmsg.c
index b10e43b4..ef91f8c0 100644
--- a/util/kmsg.c
+++ b/util/kmsg.c
@@ -9,6 +9,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
@@ -16,6 +17,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "kmsg.h"
#include "timefmt.h"
@@ -56,14 +58,11 @@ static int parse_record(const char *rec, size_t len,
if (*p == ',') {
commas++;
if (commas == 2) {
- __u64 us = 0;
const char *q = p + 1;
- while (q < semi && *q >= '0' && *q <= '9') {
- us = us * 10 + (__u64)(*q - '0');
- q++;
- }
- *monotonic_us_out = us;
+ if (parse_u64(q, digits(q, (size_t)(semi - q)),
+ UINT64_MAX, monotonic_us_out))
+ return -EINVAL;
break;
}
}
diff --git a/util/sysinfo.c b/util/sysinfo.c
index d328894c..bd1c8670 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -16,6 +16,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "json.h"
#include "kernel_build_id.h"
@@ -69,11 +70,13 @@ static void read_os_pretty(char *buf, size_t cap)
fclose(f);
}
+/* /proc/uptime holds the seconds since boot, with a fraction. */
static long read_uptime_sec(void)
{
char buf[64];
- int fd;
ssize_t n;
+ __u64 sec;
+ int fd;
fd = open("/proc/uptime", O_RDONLY | O_CLOEXEC);
if (fd < 0)
@@ -82,8 +85,9 @@ static long read_uptime_sec(void)
close(fd);
if (n <= 0)
return -1;
- buf[n] = '\0';
- return (long)strtod(buf, NULL);
+ if (parse_u64(buf, digits(buf, (size_t)n), LONG_MAX, &sec))
+ return -1;
+ return (long)sec;
}
static void read_mem(long *total_kb, long *avail_kb)