Skip to content

eBPF-based security platform

upatch: follow the kernel coding style in the upatch core

Daniel Borkmannecc6cd4d3ef4
diff --git a/util/elf_target.c b/util/elf_target.c
index c0cf538f..893f3548 100644
--- a/util/elf_target.c
+++ b/util/elf_target.c
@@ -27,9 +27,9 @@
* headers that are not part of Shirudo's userspace build).
*/
extern int elf_resolve_pattern_offsets(const char *binary_path,
- const char *pattern,
- unsigned long **poffsets,
- size_t *pcnt);
+ const char *pattern,
+ unsigned long **poffsets,
+ size_t *pcnt);
static int u64_cmp(const void *a, const void *b)
{
diff --git a/util/kernel_abi.c b/util/kernel_abi.c
index c23935b8..6519130c 100644
--- a/util/kernel_abi.c
+++ b/util/kernel_abi.c
@@ -55,9 +55,9 @@ static const struct btf_type *unwrap_type(const struct btf *btf, uint32_t id)
}
static const struct btf_member *find_member(const struct btf *btf,
- const struct btf_type *t,
- const char *name,
- uint32_t *index)
+ const struct btf_type *t,
+ const char *name,
+ uint32_t *index)
{
const struct btf_member *members = btf_members(t);
uint32_t i;
diff --git a/util/upatch.c b/util/upatch.c
index 0b45b437..9569ef80 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -25,15 +25,18 @@ static int parse_header(const unsigned char *m, size_t n,
if (n < SHIRUDO_UPATCH_HEADER_SIZE)
return -EINVAL;
- out->magic = rd_le64(p); p += 8;
+ out->magic = rd_le64(p);
+ p += 8;
if (out->magic != SHIRUDO_UPATCH_MAGIC)
return -EINVAL;
- out->version = rd_le32(p); p += 4;
+ out->version = rd_le32(p);
+ p += 4;
if (out->version != SHIRUDO_UPATCH_VERSION)
return -EINVAL;
- out->flags = rd_le32(p); p += 4;
+ out->flags = rd_le32(p);
+ p += 4;
memcpy(out->name, p, sizeof(out->name));
p += sizeof(out->name);
@@ -41,20 +44,34 @@ static int parse_header(const unsigned char *m, size_t n,
memcpy(out->payload_sha256, p, sizeof(out->payload_sha256));
p += sizeof(out->payload_sha256);
- out->loader_insns_off = rd_le32(p); p += 4;
- out->loader_insns_len = rd_le32(p); p += 4;
- out->signature_off = rd_le32(p); p += 4;
- out->signature_len = rd_le32(p); p += 4;
- out->metadata_off = rd_le32(p); p += 4;
- out->metadata_len = rd_le32(p); p += 4;
- out->data_off = rd_le32(p); p += 4;
- out->data_len = rd_le32(p); p += 4;
- out->log_buf_sz = rd_le32(p); p += 4;
- out->nr_maps = rd_le32(p); p += 4;
- out->nr_progs = rd_le32(p); p += 4;
- out->reserved0 = rd_le32(p); p += 4;
- out->type = rd_le32(p); p += 4;
- out->reserved1 = rd_le32(p); p += 4;
+ out->loader_insns_off = rd_le32(p);
+ p += 4;
+ out->loader_insns_len = rd_le32(p);
+ p += 4;
+ out->signature_off = rd_le32(p);
+ p += 4;
+ out->signature_len = rd_le32(p);
+ p += 4;
+ out->metadata_off = rd_le32(p);
+ p += 4;
+ out->metadata_len = rd_le32(p);
+ p += 4;
+ out->data_off = rd_le32(p);
+ p += 4;
+ out->data_len = rd_le32(p);
+ p += 4;
+ out->log_buf_sz = rd_le32(p);
+ p += 4;
+ out->nr_maps = rd_le32(p);
+ p += 4;
+ out->nr_progs = rd_le32(p);
+ p += 4;
+ out->reserved0 = rd_le32(p);
+ p += 4;
+ out->type = rd_le32(p);
+ p += 4;
+ out->reserved1 = rd_le32(p);
+ p += 4;
if (out->reserved0 != 0 || out->reserved1 != 0)
return -EINVAL;
return 0;
@@ -90,7 +107,7 @@ static int validate_bounds(const struct shirudo_upatch_header *h, size_t n)
static bool has_nul(const char *s, size_t n)
{
- return memchr(s, '\0', n) != NULL;
+ return memchr(s, '\0', n);
}
/*
diff --git a/util/upatch_decode.c b/util/upatch_decode.c
index 19abf900..7c31398d 100644
--- a/util/upatch_decode.c
+++ b/util/upatch_decode.c
@@ -44,9 +44,10 @@ int shirudo_upatch_decode_map_file_json(const char *path,
return shirudo_upatch_decode_map_json(v, out, out_len);
}
-int shirudo_upatch_decode_map_file_json_limit(
- const char *path, const struct shirudo_map_view *v,
- size_t max_bytes, char **out, size_t *out_len)
+int shirudo_upatch_decode_map_file_json_limit(const char *path,
+ const struct shirudo_map_view *v,
+ size_t max_bytes, char **out,
+ size_t *out_len)
{
(void)path;
(void)max_bytes;
@@ -81,6 +82,7 @@ int shirudo_upatch_decode_map_file_otel(const char *path,
#include "json.h"
#include "hex.h"
+#include "macros.h"
#include "malloc.h"
#include "paths.h"
#include "upatch.h"
@@ -252,13 +254,13 @@ static void json_put_string(struct json_out *out, const char *s, size_t n)
} else if (c >= 0x80) {
size_t width, j;
- if ((c & 0xe0) == 0xc0)
+ if ((c & 0xe0) == 0xc0) {
width = 2;
- else if ((c & 0xf0) == 0xe0)
+ } else if ((c & 0xf0) == 0xe0) {
width = 3;
- else if ((c & 0xf8) == 0xf0)
+ } else if ((c & 0xf8) == 0xf0) {
width = 4;
- else {
+ } else {
out->err = -EILSEQ;
break;
}
@@ -558,9 +560,10 @@ static int hex_decode_alloc(const char *hex, size_t len,
*out = NULL;
*out_len = 0;
- for (i = 0; i < len; i++)
+ for (i = 0; i < len; i++) {
if (!isspace((unsigned char)hex[i]))
digits++;
+ }
if (digits & 1)
return -EINVAL;
buf = xmalloc(digits / 2);
@@ -820,12 +823,12 @@ static int lua_check_u64_string(lua_State *L, int arg, uint64_t *out)
if (!s[0] || strlen(s) > 20)
return luaL_argerror(L, arg, "expected a decimal u64 string");
for (i = 0; s[i]; i++) {
- unsigned digit;
+ unsigned int digit;
if (s[i] < '0' || s[i] > '9')
return luaL_argerror(L, arg,
"expected a decimal u64 string");
- digit = (unsigned)(s[i] - '0');
+ digit = (unsigned int)(s[i] - '0');
if (v > (UINT64_MAX - digit) / 10)
return luaL_argerror(L, arg, "u64 string out of range");
v = v * 10 + digit;
@@ -969,14 +972,14 @@ static void lua_install_host_api(lua_State *L)
static void lua_disable_globals(lua_State *L)
{
- static const char *deny[] = {
+ static const char *const deny[] = {
"io", "os", "package", "require", "debug", "jit", "ffi",
"dofile", "loadfile", "load", "loadstring", "module",
"collectgarbage",
};
size_t i;
- for (i = 0; i < sizeof(deny) / sizeof(deny[0]); i++) {
+ for (i = 0; i < ARRAY_SIZE(deny); i++) {
lua_pushnil(L);
lua_setglobal(L, deny[i]);
}
@@ -1227,7 +1230,8 @@ static int run_lua_decoder(const struct shirudo_upatch_decoder_view *d,
*out = jo.buf;
*out_len = jo.len;
jo.buf = NULL;
- jo.len = jo.cap = 0;
+ jo.len = 0;
+ jo.cap = 0;
out_close:
if (jo.buf)
@@ -1287,7 +1291,7 @@ static int decode_map_file(const char *path, const struct shirudo_map_view *v,
}
int shirudo_upatch_decode_map_signal(const struct shirudo_map_view *v,
- uint32_t *signal_out)
+ uint32_t *signal_out)
{
struct shirudo_upatch u;
char path[PATH_MAX];
@@ -1318,7 +1322,7 @@ int shirudo_upatch_decode_map_signal(const struct shirudo_map_view *v,
}
static int decode_map(const struct shirudo_map_view *v, bool otel,
- size_t max_bytes, char **out, size_t *out_len)
+ size_t max_bytes, char **out, size_t *out_len)
{
char path[PATH_MAX];
@@ -1362,9 +1366,10 @@ int shirudo_upatch_decode_map_file_json(const char *path,
out, out_len);
}
-int shirudo_upatch_decode_map_file_json_limit(
- const char *path, const struct shirudo_map_view *v,
- size_t max_bytes, char **out, size_t *out_len)
+int shirudo_upatch_decode_map_file_json_limit(const char *path,
+ const struct shirudo_map_view *v,
+ size_t max_bytes, char **out,
+ size_t *out_len)
{
if (!max_bytes)
return -EINVAL;
diff --git a/util/upatch_decode.h b/util/upatch_decode.h
index 2985694a..5eada405 100644
--- a/util/upatch_decode.h
+++ b/util/upatch_decode.h
@@ -33,14 +33,15 @@ int shirudo_upatch_decode_map_otel(const struct shirudo_map_view *v,
int shirudo_upatch_decode_map_file_json(const char *path,
const struct shirudo_map_view *v,
char **out, size_t *out_len);
-int shirudo_upatch_decode_map_file_json_limit(
- const char *path, const struct shirudo_map_view *v,
- size_t max_bytes, char **out, size_t *out_len);
+int shirudo_upatch_decode_map_file_json_limit(const char *path,
+ const struct shirudo_map_view *v,
+ size_t max_bytes, char **out,
+ size_t *out_len);
int shirudo_upatch_decode_map_file_otel(const char *path,
const struct shirudo_map_view *v,
char **out, size_t *out_len);
int shirudo_upatch_decode_map_signal(const struct shirudo_map_view *v,
- uint32_t *signal_out);
+ uint32_t *signal_out);
#endif /* SHIRUDO_UTIL_UPATCH_DECODE_H */
diff --git a/util/upatch_map_types.h b/util/upatch_map_types.h
index caa74430..210c7311 100644
--- a/util/upatch_map_types.h
+++ b/util/upatch_map_types.h
@@ -14,20 +14,30 @@
static inline const char *shirudo_map_type_name(uint32_t type)
{
switch (type) {
- case BPF_MAP_TYPE_ARRAY: return "array";
- case BPF_MAP_TYPE_PERCPU_ARRAY: return "percpu_array";
- case BPF_MAP_TYPE_HASH: return "hash";
- case BPF_MAP_TYPE_PERCPU_HASH: return "percpu_hash";
- case BPF_MAP_TYPE_LRU_HASH: return "lru_hash";
+ case BPF_MAP_TYPE_ARRAY:
+ return "array";
+ case BPF_MAP_TYPE_PERCPU_ARRAY:
+ return "percpu_array";
+ case BPF_MAP_TYPE_HASH:
+ return "hash";
+ case BPF_MAP_TYPE_PERCPU_HASH:
+ return "percpu_hash";
+ case BPF_MAP_TYPE_LRU_HASH:
+ return "lru_hash";
case BPF_MAP_TYPE_LRU_PERCPU_HASH:
return "lru_percpu_hash";
- case BPF_MAP_TYPE_RINGBUF: return "ringbuf";
+ case BPF_MAP_TYPE_RINGBUF:
+ return "ringbuf";
case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
return "perf_event_array";
- case BPF_MAP_TYPE_STACK_TRACE: return "stack_trace";
- case BPF_MAP_TYPE_PROG_ARRAY: return "prog_array";
- case BPF_MAP_TYPE_ARENA: return "arena";
- default: return "unknown";
+ case BPF_MAP_TYPE_STACK_TRACE:
+ return "stack_trace";
+ case BPF_MAP_TYPE_PROG_ARRAY:
+ return "prog_array";
+ case BPF_MAP_TYPE_ARENA:
+ return "arena";
+ default:
+ return "unknown";
}
}
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index 79786641..2ec37903 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -56,6 +56,7 @@ static void resolve_map_location(struct shirudo_map_view *v)
if (vbtf)
btf__free(vbtf);
}
+
#define SHIRUDO_SERIAL_MAX_BYTES \
(SHIRUDO_SERIAL_MAX_CHUNKS * SHIRUDO_SERIAL_CHUNK_PAYLOAD)
#define SHIRUDO_SERIAL_MAX_TOTAL_BYTES \
@@ -135,7 +136,7 @@ static void btf_type_name(struct btf *btf, uint32_t type_id,
}
}
-static char *fmt_alloc(const char *fmt, ...) __attribute__((format(printf,1,2)));
+static char *fmt_alloc(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
static uint32_t btf_unwrap(const struct btf *btf, uint32_t id)
{
@@ -169,11 +170,16 @@ static uint32_t btf_unwrap(const struct btf *btf, uint32_t id)
static const char *btf_kind_prefix(uint16_t kind)
{
switch (kind) {
- case BTF_KIND_STRUCT: return "struct ";
- case BTF_KIND_UNION: return "union ";
- case BTF_KIND_ENUM: return "enum ";
- case BTF_KIND_ENUM64: return "enum ";
- default: return "";
+ case BTF_KIND_STRUCT:
+ return "struct ";
+ case BTF_KIND_UNION:
+ return "union ";
+ case BTF_KIND_ENUM:
+ return "enum ";
+ case BTF_KIND_ENUM64:
+ return "enum ";
+ default:
+ return "";
}
}
@@ -254,7 +260,7 @@ static void btf_type_signature_into(const struct btf *btf, uint32_t id,
const struct btf_type *mt = btf__type_by_id(btf, mid);
const char *mtype = mt ?
btf__name_by_offset(btf,
- mt->name_off) :
+ mt->name_off) :
NULL;
uint16_t mkind = mt ? BTF_INFO_KIND(mt->info)
: 0;
@@ -435,12 +441,13 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
if (BTF_INFO_KFLAG(t->info) && size < 4 &&
(v & (1u << (8 * size - 1))))
v |= ~((1u << (8 * size)) - 1);
- for (i = 0; i < (uint32_t)vlen; i++)
+ for (i = 0; i < (uint32_t)vlen; i++) {
if ((uint32_t)e[i].val == v) {
const char *nm = btf__name_by_offset(btf,
e[i].name_off);
return fmt_alloc("%s", nm && nm[0] ? nm : "?");
}
+ }
return BTF_INFO_KFLAG(t->info) ?
fmt_alloc("%" PRId32, (int32_t)v) :
fmt_alloc("%" PRIu32, v);
@@ -1555,25 +1562,27 @@ void shirudo_upatch_maps_print_detail(const struct shirudo_map_view *v)
printf("\nContents:\n");
for (i = 0; i < v->n_entries; i++) {
const struct shirudo_map_entry *en = &v->entries[i];
- char *key_s = shirudo_upatch_map_format_cell(v, v->key_type_id,
- en->key, v->key_size,
- v->btf_key_type);
+ char *key_s;
+ key_s = shirudo_upatch_map_format_cell(v, v->key_type_id,
+ en->key, v->key_size,
+ v->btf_key_type);
if (shirudo_upatch_map_is_percpu(v->type)) {
- char *sum_s = shirudo_upatch_map_format_percpu_sum(v,
- en->value, v->btf_value_type);
+ char *sum_s;
int j;
+ sum_s = shirudo_upatch_map_format_percpu_sum(v, en->value,
+ v->btf_value_type);
printf(" key=%s: sum=%s\n", key_s ? key_s : "?",
sum_s ? sum_s : "?");
for (j = 0; j < (int)v->n_cpus; j++) {
- char *one = shirudo_upatch_map_format_cell(v,
- v->value_type_id,
- (const char *)en->value
- + (size_t)j *
- shirudo_upatch_map_value_stride(v),
- v->value_size, v->btf_value_type);
+ const char *p = (const char *)en->value +
+ (size_t)j * shirudo_upatch_map_value_stride(v);
+ char *one;
+ one = shirudo_upatch_map_format_cell(v, v->value_type_id,
+ p, v->value_size,
+ v->btf_value_type);
if (j % 4 == 0)
printf(" ");
printf("cpu%d=%-7s ", j, one ? one : "?");
@@ -1585,10 +1594,11 @@ void shirudo_upatch_maps_print_detail(const struct shirudo_map_view *v)
printf("\n");
xstrfree(sum_s);
} else {
- char *val_s = shirudo_upatch_map_format_cell(v,
- v->value_type_id, en->value, v->value_size,
- v->btf_value_type);
+ char *val_s;
+ val_s = shirudo_upatch_map_format_cell(v, v->value_type_id,
+ en->value, v->value_size,
+ v->btf_value_type);
printf(" key = %s\n -> %s\n", key_s ? key_s : "?",
val_s ? val_s : "?");
xstrfree(val_s);
@@ -1672,25 +1682,27 @@ void shirudo_upatch_maps_emit_json_fields(struct shirudo_json_emit *e,
shirudo_json_arr_open(e, "entries");
for (i = 0; i < v->n_entries; i++) {
const struct shirudo_map_entry *en = &v->entries[i];
- char *key_s = shirudo_upatch_map_format_cell(v, v->key_type_id,
- en->key, v->key_size,
- v->btf_key_type);
+ char *key_s;
+ key_s = shirudo_upatch_map_format_cell(v, v->key_type_id,
+ en->key, v->key_size,
+ v->btf_key_type);
shirudo_json_obj_open(e);
shirudo_json_kv_string(e, "key", key_s ? key_s : "");
if (shirudo_upatch_map_is_percpu(v->type)) {
- char *sum_s = shirudo_upatch_map_format_percpu_sum(v,
- en->value, v->btf_value_type);
+ char *sum_s;
+ sum_s = shirudo_upatch_map_format_percpu_sum(v, en->value,
+ v->btf_value_type);
shirudo_json_arr_open(e, "values_per_cpu");
for (j = 0; j < v->n_cpus; j++) {
- char *one = shirudo_upatch_map_format_cell(v,
- v->value_type_id,
- (const char *)en->value
- + (size_t)j *
- shirudo_upatch_map_value_stride(v),
- v->value_size, v->btf_value_type);
+ const char *p = (const char *)en->value +
+ (size_t)j * shirudo_upatch_map_value_stride(v);
+ char *one;
+ one = shirudo_upatch_map_format_cell(v, v->value_type_id,
+ p, v->value_size,
+ v->btf_value_type);
shirudo_json_str(e, one ? one : "");
xstrfree(one);
}
@@ -1699,10 +1711,11 @@ void shirudo_upatch_maps_emit_json_fields(struct shirudo_json_emit *e,
sum_s ? sum_s : "");
xstrfree(sum_s);
} else {
- char *val_s = shirudo_upatch_map_format_cell(v,
- v->value_type_id, en->value, v->value_size,
- v->btf_value_type);
+ char *val_s;
+ val_s = shirudo_upatch_map_format_cell(v, v->value_type_id,
+ en->value, v->value_size,
+ v->btf_value_type);
shirudo_json_kv_string(e, "value",
val_s ? val_s : "");
xstrfree(val_s);
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 1a04b1ca..6bb20051 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -71,12 +71,18 @@ static const char *loc_prefix(uint32_t prog_type, uint32_t attach_type)
return "netfilter";
if (prog_type == BPF_PROG_TYPE_TRACING) {
switch (attach_type) {
- case BPF_TRACE_FENTRY: return "fentry";
- case BPF_TRACE_FEXIT: return "fexit";
- case BPF_MODIFY_RETURN: return "fmod_ret";
- case BPF_TRACE_RAW_TP: return "tp_btf";
- case BPF_TRACE_ITER: return "iter";
- default: return NULL;
+ case BPF_TRACE_FENTRY:
+ return "fentry";
+ case BPF_TRACE_FEXIT:
+ return "fexit";
+ case BPF_MODIFY_RETURN:
+ return "fmod_ret";
+ case BPF_TRACE_RAW_TP:
+ return "tp_btf";
+ case BPF_TRACE_ITER:
+ return "iter";
+ default:
+ return NULL;
}
}
return NULL;