Skip to content

eBPF-based security platform

treewide: use ARRAY_SIZE(), min() and max()

Daniel Borkmann5e46f92749f2
diff --git a/bpf/lib/identity.h b/bpf/lib/identity.h
index 46cf22d0..015c6ca9 100644
--- a/bpf/lib/identity.h
+++ b/bpf/lib/identity.h
@@ -7,7 +7,7 @@
#include <shirudo/tpm.h>
/* Frozen ABI shared with struct identity in util/identity.h. */
-#define IDENTITY_KEYS_MAX 16
+#define IDENTITY_KEYS_MAX 16u
struct sb_id {
__u32 sb_dev;
diff --git a/builtin-init.c b/builtin-init.c
index 873b3bc5..9b1f37e7 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -1256,8 +1256,7 @@ static const struct help init_help = {
.summary = "Bootstrap enforcement on this target. Idempotent; safe "
"to re-run.\nThe pinned link survives the process exit.",
.sections = init_help_sections,
- .n_sections = sizeof(init_help_sections) /
- sizeof(init_help_sections[0]),
+ .n_sections = ARRAY_SIZE(init_help_sections),
};
static void usage(void)
diff --git a/builtin-keys.c b/builtin-keys.c
index 799e863e..2ab06e34 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -846,7 +846,7 @@ int keys_revoke_image(const __u8 *der, size_t size, bool boot)
}
}
ret = purge_signed_by(der, size, &unloaded);
- return ret < 0 ? ret : 0;
+ return min(ret, 0);
}
static int keys_revoke(int argc, const char **argv)
@@ -983,8 +983,7 @@ static int keys_show(int argc, const char **argv)
const struct keys_anchor anchors[] = {
{ ".bpf", keyring_bpf_serial, id.keyring_serial,
id.key_serials,
- id.n_keys < IDENTITY_KEYS_MAX ?
- id.n_keys : IDENTITY_KEYS_MAX },
+ min(id.n_keys, IDENTITY_KEYS_MAX) },
{ ".dm-verity", keyring_dm_verity_serial,
id.dm_verity_ring, &id.dm_verity_key,
id.dm_verity_key > 0 },
diff --git a/builtin-status.c b/builtin-status.c
index 0365adac..afbe442a 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -370,8 +370,7 @@ static const struct help status_help = {
.summary = "Local readiness + state report. Auto-detects target "
"vs operator role.\nSafe to run before `shirudo init`.",
.sections = status_help_sections,
- .n_sections = sizeof(status_help_sections) /
- sizeof(status_help_sections[0]),
+ .n_sections = ARRAY_SIZE(status_help_sections),
};
static void usage(void)
@@ -608,8 +607,7 @@ static void keyring_view_bpf(struct status_keyring *k,
size_t i, n_frozen = 0;
if (k->pinned >= 1)
- n_frozen = ident->n_keys < IDENTITY_KEYS_MAX ?
- ident->n_keys : IDENTITY_KEYS_MAX;
+ n_frozen = min(ident->n_keys, IDENTITY_KEYS_MAX);
keyring_view_links(k, ident->key_serials, n_frozen);
k->certs_rc = certs_rc;
for (i = 0; certs_rc == 0 && i < certs->n; i++) {
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index f38e585e..4c37bcee 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -893,7 +893,7 @@ static int bpm_oci_request(struct bpm_oci_client *c, const char *method,
if (r->status < 200 || r->status >= 300) {
if (r->status != 404) {
if (r->body.data && r->body.len) {
- size_t n = r->body.len > 512 ? 512 : r->body.len;
+ size_t n = min(r->body.len, 512U);
pr_err("bpm OCI request %s: HTTP %ld: %.*s", url,
r->status, (int)n, r->body.data);
diff --git a/util/identity.h b/util/identity.h
index 5fbb8851..8c89e658 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -15,7 +15,7 @@
#define SIG_KEYRING_BPF 5
/* The most keys the anchor keyring holds; util/keys.h bounds the set. */
-#define IDENTITY_KEYS_MAX 16
+#define IDENTITY_KEYS_MAX 16u
/* The frozen ABI shared with bpf/lib/identity.h. */
struct identity {
diff --git a/util/tpm_wire.c b/util/tpm_wire.c
index fb1f965d..d1007661 100644
--- a/util/tpm_wire.c
+++ b/util/tpm_wire.c
@@ -483,7 +483,7 @@ int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,
ret = -EIO;
break;
}
- n = out_len - done < 32 ? out_len - done : 32;
+ n = min(out_len - done, 32U);
memcpy(out + done, block, n);
done += n;
}
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index 0ec8e9e0..ff30bafe 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -663,7 +663,7 @@ static char *format_value(const void *value, __u32 value_size,
/* Hex fallback (cap at 32 bytes shown for readability). */
{
const __u8 *p = value;
- __u32 cap = value_size < 32 ? value_size : 32;
+ __u32 cap = min(value_size, 32U);
size_t buf_sz = (size_t)cap * 2 + 8;
char *out = xmalloc(buf_sz);
size_t off = 0;
@@ -1115,8 +1115,7 @@ static int read_entries(int fd, struct map_view *v,
xfree(value, value_alloc);
return -ENOMEM;
}
- memcpy(key, &i, sizeof(i) <= v->key_size ?
- sizeof(i) : v->key_size);
+ memcpy(key, &i, min(v->key_size, sizeof(i)));
if (v->key_size > sizeof(i))
memset((char *)key + sizeof(i), 0,
v->key_size - sizeof(i));
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index bb382fab..8fda9625 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -1383,7 +1383,7 @@ int otel_retry_after(const char *value, time_t now, __u64 *seconds)
delay = delay * 10 + (__u64)(*p - '0');
}
if (!*p) {
- *seconds = delay > 30 ? 30 : delay;
+ *seconds = min(delay, 30U);
return 0;
}
parsed = 0;
@@ -1412,7 +1412,7 @@ int otel_retry_after(const char *value, time_t now, __u64 *seconds)
return 0;
}
delay = (__u64)(retry_time - now);
- *seconds = delay > 30 ? 30 : delay;
+ *seconds = min(delay, 30U);
return 0;
}
@@ -1483,7 +1483,7 @@ static void retry_wait(__u32 retry, __u64 retry_after,
bool retry_after_valid)
{
struct timespec ts;
- __u32 cap_ms = 250U << (retry > 4 ? 4 : retry);
+ __u32 cap_ms = 250U << min(retry, 4U);
__u32 delay_ms;
if (cap_ms > 5000)