Skip to content

eBPF-based security platform

treewide: spell fixed-width integers the kernel's way

Daniel Borkmann8320819fa53d
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 0038a724..02177480 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -563,7 +563,7 @@ testing/helper/test-register: testing/helper/test-register.c util/attest.o \
testing/helper/tpmsim-cmd: testing/helper/tpmsim-cmd.c
@printf ' CC $@\n'
- $(Q)$(CC) $(CFLAGS_BASE) $(TPMSIM_CFLAGS) -o $@ $< $(TPMSIM_LIBS) -lcrypto
+ $(Q)$(CC) $(CFLAGS_BASE) -Iinclude $(TPMSIM_CFLAGS) -o $@ $< $(TPMSIM_LIBS) -lcrypto
testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o util/log.o util/hex.o util/malloc.o
@printf ' CC $@\n'
diff --git a/bpf/lib/shirudo_serializer.h b/bpf/lib/shirudo_serializer.h
index c07cfa57..061a4622 100644
--- a/bpf/lib/shirudo_serializer.h
+++ b/bpf/lib/shirudo_serializer.h
@@ -35,31 +35,31 @@
#define SHIRUDO_SERIAL_CHUNK_PAYLOAD 4032u
struct shirudo_serialize_ctx {
- uint32_t magic;
- uint32_t version;
- uint32_t flags;
- uint32_t max_chunks;
- uint32_t chunk_payload_size;
- uint32_t page_index;
- uint64_t cursor;
- uint64_t invocation_id;
+ __u32 magic;
+ __u32 version;
+ __u32 flags;
+ __u32 max_chunks;
+ __u32 chunk_payload_size;
+ __u32 page_index;
+ __u64 cursor;
+ __u64 invocation_id;
};
struct shirudo_serial_chunk {
- uint32_t magic;
- uint32_t version;
- uint32_t flags;
+ __u32 magic;
+ __u32 version;
+ __u32 flags;
int status;
- uint32_t chunk_index;
- uint32_t total_chunks;
- uint32_t bytes_total;
- uint32_t bytes_in_chunk;
- uint32_t page_index;
- uint32_t reserved;
- uint64_t cursor;
- uint64_t next_cursor;
- uint64_t invocation_id;
- unsigned char data[SHIRUDO_SERIAL_CHUNK_PAYLOAD];
+ __u32 chunk_index;
+ __u32 total_chunks;
+ __u32 bytes_total;
+ __u32 bytes_in_chunk;
+ __u32 page_index;
+ __u32 reserved;
+ __u64 cursor;
+ __u64 next_cursor;
+ __u64 invocation_id;
+ __u8 data[SHIRUDO_SERIAL_CHUNK_PAYLOAD];
};
BUILD_BUG_ON(sizeof(struct shirudo_serialize_ctx) !=
diff --git a/builtin-attest.c b/builtin-attest.c
index 02425d50..ae3bc7db 100644
--- a/builtin-attest.c
+++ b/builtin-attest.c
@@ -15,6 +15,8 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "builtin.h"
#include "util/attest.h"
#include "util/chain.h"
@@ -115,7 +117,7 @@ static const struct shirudo_help attest_help = {
.n_sections = ARRAY_SIZE(attest_help_sections),
};
-static int parse_hex(const char *hex, uint8_t *out, size_t cap, size_t *len)
+static int parse_hex(const char *hex, __u8 *out, size_t cap, size_t *len)
{
size_t n = strlen(hex);
@@ -125,7 +127,7 @@ static int parse_hex(const char *hex, uint8_t *out, size_t cap, size_t *len)
return shirudo_hex_decode(hex, out, n / 2);
}
-static int parse_hex32(const char *what, const char *hex, uint8_t out[32])
+static int parse_hex32(const char *what, const char *hex, __u8 out[32])
{
if (strlen(hex) != 64 || shirudo_hex_decode(hex, out, 32)) {
pr_err("attest: %s must be 64 hex characters", what);
@@ -134,14 +136,14 @@ static int parse_hex32(const char *what, const char *hex, uint8_t out[32])
return 0;
}
-static int parse_pcrs(const char *list, uint32_t *mask)
+static int parse_pcrs(const char *list, __u32 *mask)
{
const char *p = list;
*mask = 0;
while (*p) {
char *end;
- unsigned long n = strtoul(p, &end, 10);
+ __u64 n = strtoul(p, &end, 10);
if (end == p || n >= 24)
return -EINVAL;
@@ -186,7 +188,7 @@ static int member_add_spec(struct members *m, const char *spec)
{
struct shirudo_measured_upatch *e;
const char *eq = strchr(spec, '=');
- uint8_t payload[32];
+ __u8 payload[32];
if (!eq || eq == spec ||
(size_t)(eq - spec) > SHIRUDO_UPATCH_NAME_MAX ||
@@ -201,11 +203,11 @@ static int member_add_spec(struct members *m, const char *spec)
return 0;
}
-static int cert_digest(const char *path, uint8_t out[32])
+static int cert_digest(const char *path, __u8 out[32])
{
- unsigned char *buf;
struct stat st;
ssize_t got;
+ __u8 *buf;
int fd;
fd = open(path, O_RDONLY | O_CLOEXEC);
@@ -231,11 +233,11 @@ static int cert_digest(const char *path, uint8_t out[32])
return got == st.st_size ? 0 : -EIO;
}
-static int apply_event(const char *spec, uint8_t chain[32])
+static int apply_event(const char *spec, __u8 chain[32])
{
char name[SHIRUDO_UPATCH_NAME_MAX + 1];
const char *colon = strchr(spec, ':'), *eq;
- uint8_t digest[32], event[32];
+ __u8 digest[32], event[32];
size_t kind_len, name_len;
eq = colon ? strchr(colon, '=') : NULL;
@@ -256,7 +258,7 @@ static int apply_event(const char *spec, uint8_t chain[32])
event);
} else if (kind_len == 4 && !strncmp(spec, "tree", 4)) {
char *end;
- long long seq = strtoll(name, &end, 10);
+ __s64 seq = strtoll(name, &end, 10);
if (end == name || *end || seq <= 0)
return -EINVAL;
@@ -272,10 +274,10 @@ static int apply_event(const char *spec, uint8_t chain[32])
static int cmd_attest_predict(int argc, const char **argv)
{
- uint8_t cert_sha[32], tree_root[32] = {}, set_digest[32], init[32];
- uint8_t cert_shas[SHIRUDO_KEYS_MAX][32];
+ __u8 cert_sha[32], tree_root[32] = {}, set_digest[32], init[32];
+ __u8 cert_shas[SHIRUDO_KEYS_MAX][32];
size_t n_certs = 0;
- uint8_t bind[2][32], chain[32] = {};
+ __u8 bind[2][32], chain[32] = {};
const char *events[EVENTS_MAX];
struct members m = {};
char hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
@@ -316,7 +318,7 @@ static int cmd_attest_predict(int argc, const char **argv)
events[n_events++] = a + 8;
} else if (!strncmp(a, "--bind-pcr=", 11)) {
char *end;
- unsigned long n = strtoul(a + 11, &end, 10);
+ __u64 n = strtoul(a + 11, &end, 10);
int slot = n == SHIRUDO_CHAIN_PCR ? 0 :
n == SHIRUDO_CHAIN_PHASE_PCR ? 1 : -1;
@@ -363,8 +365,8 @@ static int cmd_attest_predict(int argc, const char **argv)
printf("register_after_events: %s\n", hex);
}
if (have_bind[0] && have_bind[1]) {
- uint8_t branch_written[32], branch_init[32], policy[32];
- uint8_t name[SHIRUDO_TPM_NAME_LEN], anchor_digest[32];
+ __u8 branch_written[32], branch_init[32], policy[32];
+ __u8 name[SHIRUDO_TPM_NAME_LEN], anchor_digest[32];
char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];
shirudo_tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, bind[0], 2,
@@ -430,10 +432,10 @@ static int read_evidence(const char *path, char **out, size_t *len)
static int cmd_attest_verify(int argc, const char **argv)
{
struct shirudo_attest_expect x = {};
- uint8_t nonce[SHIRUDO_ATTEST_NONCE_MAX], cert_sha[32], set_digest[32];
- uint8_t cert_shas[SHIRUDO_KEYS_MAX][32];
+ __u8 nonce[SHIRUDO_ATTEST_NONCE_MAX], cert_sha[32], set_digest[32];
+ __u8 cert_shas[SHIRUDO_KEYS_MAX][32];
size_t n_certs = 0;
- uint8_t tree_root[32] = {};
+ __u8 tree_root[32] = {};
const char *evidence_path = NULL;
struct members m = {};
char *evidence = NULL, *report;
@@ -499,7 +501,7 @@ static int cmd_attest_verify(int argc, const char **argv)
x.have_chain = true;
} else if (!strncmp(a, "--expect-pcr=", 13)) {
char *end;
- unsigned long n = strtoul(a + 13, &end, 10);
+ __u64 n = strtoul(a + 13, &end, 10);
if (end == a + 13 || *end != '=' || n >= 24 ||
parse_hex32("--expect-pcr value", end + 1,
@@ -528,7 +530,7 @@ static int cmd_attest_verify(int argc, const char **argv)
x.have_ek_name = true;
} else if (!strncmp(a, "--expect-bind-pcr=", 18)) {
char *end;
- unsigned long n = strtoul(a + 18, &end, 10);
+ __u64 n = strtoul(a + 18, &end, 10);
if (end == a + 18 || *end != '=' || n >= 24 ||
parse_hex32("--expect-bind-pcr value", end + 1,
@@ -592,8 +594,8 @@ out:
static int cmd_attest_challenge(int argc, const char **argv)
{
struct shirudo_attest_challenge_policy pol = {};
- uint8_t credential[SHIRUDO_TPM_CREDENTIAL_LEN];
- uint8_t ak_name[SHIRUDO_TPM_NAME_LEN], ek_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 credential[SHIRUDO_TPM_CREDENTIAL_LEN];
+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN], ek_name[SHIRUDO_TPM_NAME_LEN];
const char *evidence_path = NULL, *out_path = NULL;
char *evidence = NULL, *doc = NULL, *report;
char hex[2 * SHIRUDO_TPM_CREDENTIAL_LEN + 1];
@@ -687,8 +689,8 @@ out:
static int cmd_attest_evidence(int argc, const char **argv)
{
struct shirudo_attest_challenge challenge = {};
- uint8_t nonce[SHIRUDO_ATTEST_NONCE_MAX];
- uint32_t mask = SHIRUDO_ATTEST_PCR_DEFAULT;
+ __u8 nonce[SHIRUDO_ATTEST_NONCE_MAX];
+ __u32 mask = SHIRUDO_ATTEST_PCR_DEFAULT;
const char *out_path = NULL, *challenge_path = NULL;
char *doc = NULL;
size_t nonce_len = 0, len = 0, cap = 0, i;
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 25c8a31f..61ff3e90 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -13,7 +13,6 @@
#include <ftw.h>
#include <limits.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -29,6 +28,8 @@
#include <openssl/pem.h>
#include <openssl/x509.h>
+#include <shirudo/misc.h>
+
#include "builtin.h"
#include "util/bpm_oci.h"
#include "util/bpm_package.h"
@@ -219,8 +220,8 @@ static int copy_file_ex(const char *src, const char *dst, mode_t mode,
bool exclusive)
{
struct stat in_st, out_st;
- unsigned char buf[65536];
int in, out, rc = 0;
+ __u8 buf[65536];
ssize_t n;
in = open(src, O_RDONLY | O_CLOEXEC);
@@ -349,8 +350,8 @@ static int cache_path(const char *source, char *out, size_t sz)
static int sha256_file_hex(const char *path, char *hex, size_t hex_sz)
{
struct shirudo_sha256_ctx ctx;
- unsigned char digest[SHIRUDO_CRYPTO_SHA256_LEN];
- unsigned char buf[65536];
+ __u8 digest[SHIRUDO_CRYPTO_SHA256_LEN];
+ __u8 buf[65536];
ssize_t nr;
int fd, rc = -EIO;
@@ -378,8 +379,8 @@ done:
static int print_package_readme(const char *source)
{
char temporary[] = "/tmp/shirudo-bpm-info-XXXXXX";
- unsigned char *beefile = NULL, *readme = NULL;
size_t beefile_len = 0, readme_len = 0;
+ __u8 *beefile = NULL, *readme = NULL;
const char *archive = source;
char package_dir[101];
int fd, rc;
@@ -421,8 +422,8 @@ out:
static int extract_archive(const char *archive, const char *dest)
{
gzFile gz = gzopen(archive, "rb");
- unsigned char h[512];
char root[256] = {};
+ __u8 h[512];
int rc = 0;
if (!gz)
@@ -435,7 +436,7 @@ static int extract_archive(const char *archive, const char *dest)
for (;;) {
char name[PATH_MAX], rel[PATH_MAX], path[PATH_MAX], *slash;
int n = gzread(gz, h, sizeof(h));
- uint64_t size, remain, pad;
+ __u64 size, remain, pad;
bool zero = true;
int type;
@@ -510,10 +511,10 @@ static int extract_archive(const char *archive, const char *dest)
}
remain = size;
while (remain) {
- unsigned char buf[65536];
+ __u8 buf[65536];
size_t want = remain > sizeof(buf) ? sizeof(buf) : (size_t)remain;
- n = gzread(gz, buf, (unsigned int)want);
+ n = gzread(gz, buf, (__u32)want);
if (n <= 0 || fwrite(buf, 1, (size_t)n, fp) != (size_t)n) {
rc = -EIO;
break;
@@ -530,10 +531,10 @@ static int extract_archive(const char *archive, const char *dest)
}
pad = (512 - (size % 512)) % 512;
while (pad) {
- unsigned char buf[512];
+ __u8 buf[512];
size_t want = pad > sizeof(buf) ? sizeof(buf) : (size_t)pad;
- n = gzread(gz, buf, (unsigned int)want);
+ n = gzread(gz, buf, (__u32)want);
if (n <= 0) {
rc = -EINVAL;
break;
@@ -553,7 +554,7 @@ static int extract_archive(const char *archive, const char *dest)
static int cert_matches_active(const char *path)
{
struct shirudo_cert a = {}, b = {};
- unsigned char da[SHIRUDO_CRYPTO_SHA256_LEN], db[SHIRUDO_CRYPTO_SHA256_LEN];
+ __u8 da[SHIRUDO_CRYPTO_SHA256_LEN], db[SHIRUDO_CRYPTO_SHA256_LEN];
int rc = -EINVAL;
if (!shirudo_cert_read_file(path, &a) &&
@@ -570,7 +571,7 @@ static int cert_matches_active(const char *path)
static int key_file_fingerprint(const char *path, char hex[65])
{
struct shirudo_cert cert = {};
- unsigned char digest[SHIRUDO_CRYPTO_SHA256_LEN];
+ __u8 digest[SHIRUDO_CRYPTO_SHA256_LEN];
int rc;
rc = shirudo_cert_read_file(path, &cert);
@@ -631,7 +632,7 @@ static int install_key_file(const char *src)
static int extract_package_cert(const char *archive, char *out, size_t out_sz)
{
char tmp[] = "/tmp/shirudo-bpm-cert-XXXXXX";
- unsigned char *body = NULL;
+ __u8 *body = NULL;
size_t len = 0;
int fd, rc;
@@ -711,7 +712,7 @@ static int list_keys(void)
strcasecmp(entry->d_name + 64, ".der"))
continue;
for (size_t i = 0; i < 64; i++) {
- if (!isxdigit((unsigned char)entry->d_name[i]))
+ if (!isxdigit((__u8)entry->d_name[i]))
valid = false;
}
if (!valid || snprintf(path, sizeof(path), "%s/%s",
@@ -777,13 +778,13 @@ static int on_state_entry(void *ud)
static int state_read(struct bpm_state_entry *st, int *n)
{
- unsigned char *body;
+ __u8 *body;
size_t len;
int rc;
struct state_sink sink = { .items = st };
struct shirudo_json_array arr = { on_state_entry, &sink };
struct shirudo_json_field sub[4], root[1];
- int64_t unused = 0;
+ __s64 unused = 0;
rc = shirudo_read_file(SHIRUDO_BPM_STATE_PATH, 8u * 1024u * 1024u,
&body, &len);
@@ -855,7 +856,7 @@ static int state_write(const struct bpm_state_entry *st, int n)
static int load_cached_index(const char *source, struct bpm_index_entry *entries, int *n)
{
char path[PATH_MAX];
- unsigned char *body;
+ __u8 *body;
size_t len;
int rc;
@@ -921,7 +922,7 @@ static int semver_compare(const char *a, const char *b)
for (int part = 0; part < 3; part++) {
char *ea, *eb;
- unsigned long va, vb;
+ __u64 va, vb;
va = strtoul(pa, &ea, 10);
vb = strtoul(pb, &eb, 10);
@@ -1529,7 +1530,7 @@ static int parse_tree_repository(const char *raw, struct bpm_oci_client *c,
return bpm_oci_parse_endpoint(endpoint, c);
}
-static int tree_digest_hex(const char *digest, uint8_t out[32])
+static int tree_digest_hex(const char *digest, __u8 out[32])
{
if (!digest || strncmp(digest, "sha256:", 7))
return -EINVAL;
@@ -1538,11 +1539,18 @@ static int tree_digest_hex(const char *digest, uint8_t out[32])
static bool tree_digest_valid(const char *digest)
{
- uint8_t raw[32];
+ __u8 raw[32];
return !tree_digest_hex(digest, raw);
}
+static bool tree_tpm_available(void)
+{
+ __u8 value[32];
+
+ return !shirudo_chain_read(value);
+}
+
static int tree_fetch_publication(const struct bpm_tree_config *cfg,
struct shirudo_tree_publication *pub,
char *manifest_digest, size_t digest_sz)
@@ -1641,7 +1649,7 @@ static int tree_pull_artifact(const struct shirudo_tree_operation *op,
goto out;
if (bpm_sha256_hex(blob.data, blob.len, hex, sizeof(hex)) ||
!bpm_digest_matches(layers[i].digest, hex) ||
- (layers[i].size >= 0 && (int64_t)blob.len != layers[i].size))
+ (layers[i].size >= 0 && (__s64)blob.len != layers[i].size))
rc = -EKEYREJECTED;
if (rc)
goto out;
@@ -1741,16 +1749,16 @@ static void tree_status_clear(const char *id)
}
struct bpm_tree_pending {
- int64_t seq;
+ __s64 seq;
char id[SHIRUDO_TREE_STRING_MAX];
char operation_digest[128];
};
static int tree_read_pending(struct bpm_tree_pending *pending)
{
- unsigned char *body;
+ __u8 *body;
size_t len;
- int64_t schema = 0;
+ __s64 schema = 0;
struct shirudo_json_field fields[] = {
{ "schema", SHIRUDO_JSON_T_INT, &schema, 0, 0, NULL, 0 },
{ "seq", SHIRUDO_JSON_T_INT, &pending->seq, 0, 0, NULL, 0 },
@@ -1779,9 +1787,9 @@ static int tree_read_pending(struct bpm_tree_pending *pending)
* operator attests.
*/
static int tree_measure_operation(const struct shirudo_tree_operation *op,
- const uint8_t operation_digest[32])
+ const __u8 operation_digest[32])
{
- uint8_t event[32], chain[32];
+ __u8 event[32], chain[32];
struct shirudo_json_emit e;
char line[4096], ts[32], boot_id[64], nv_index[16];
char digest_hex[65], chain_hex[65];
@@ -1828,7 +1836,7 @@ static int tree_recover_pending(const struct shirudo_tree_publication *pub,
{
struct bpm_tree_pending pending = {};
const struct shirudo_tree_operation *op = NULL;
- uint8_t digest[32], previous[32], next[32], replay[32];
+ __u8 digest[32], previous[32], next[32], replay[32];
bool applied = false, event_present = false;
int rc;
@@ -1888,7 +1896,7 @@ static int tree_recover_pending(const struct shirudo_tree_publication *pub,
static int tree_validate_operation(const struct shirudo_tree_operation *op)
{
- uint8_t digest[32];
+ __u8 digest[32];
char expected_digest[72];
if (!op || op->seq <= 0 || !shirudo_upatch_name_valid(op->id) ||
@@ -1917,7 +1925,7 @@ static void tree_sync_report_error(const char *phase,
if (rc > 0) {
if (op)
pr_err("bpm tree sync: phase=%s seq=%lld id=%s rc=%d "
- "(command status)", phase, (long long)op->seq,
+ "(command status)", phase, (__s64)op->seq,
op->id, rc);
else
pr_err("bpm tree sync: phase=%s rc=%d (command status)",
@@ -1928,7 +1936,7 @@ static void tree_sync_report_error(const char *phase,
if (op)
pr_err("bpm tree sync: phase=%s seq=%lld id=%s rc=%d (%s)",
- phase, (long long)op->seq, op->id, rc,
+ phase, (__s64)op->seq, op->id, rc,
err ? strerror(err) : "success");
else
pr_err("bpm tree sync: phase=%s rc=%d (%s)", phase, rc,
@@ -2060,10 +2068,10 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)
tree_sync_report_error("pending_recovery", NULL, rc);
goto out;
}
- tpm_available = shirudo_chain_read((uint8_t[32]){}) == 0;
+ tpm_available = tree_tpm_available();
for (int i = 0; i < pub.n_operations; i++) {
struct shirudo_tree_operation *op = &pub.operations[i];
- uint8_t digest[32], previous[32], next[32];
+ __u8 digest[32], previous[32], next[32];
char artifact[PATH_MAX];
bool skipped_module = false;
diff --git a/builtin-config.c b/builtin-config.c
index 8c27c521..9ae2b0ee 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -69,7 +69,7 @@ int cmd_config(int argc, const char **argv)
printf("# Eventlog cap; 0 = unlimited. When the log would exceed this,\n"
"# the state-changing op refuses with category: eventlog_full.\n");
- printf("eventlog.max_bytes = %lu\n\n", cfg.eventlog_max_bytes);
+ printf("eventlog.max_bytes = %llu\n\n", cfg.eventlog_max_bytes);
printf("# TPM owner-hierarchy password used at NV-index define time.\n"
"# Empty by default (works on fresh TPMs, swtpm, most cloud VMs);\n"
diff --git a/builtin-genkey.c b/builtin-genkey.c
index 09dd8830..a194c664 100644
--- a/builtin-genkey.c
+++ b/builtin-genkey.c
@@ -23,6 +23,8 @@
#include <openssl/x509.h>
#include <openssl/x509v3.h>
+#include <shirudo/misc.h>
+
#include "builtin.h"
#include "util/help.h"
#include "util/log.h"
@@ -78,7 +80,7 @@ static int set_subject_and_issuer(X509 *c)
X509_NAME *name = X509_get_subject_name(c);
if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8,
- (const unsigned char *)CERT_CN,
+ (const __u8 *)CERT_CN,
-1, -1, 0) != 1) {
shirudo_openssl_complain("X509_NAME_add_entry_by_txt");
return -1;
diff --git a/builtin-init.c b/builtin-init.c
index 09f875b0..f8cf198f 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -11,7 +11,6 @@
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -24,6 +23,7 @@
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include "builtin.h"
@@ -67,10 +67,10 @@ static void print_preflight(const struct shirudo_preflight_result *r)
r->n - r->n_failed, r->n, r->n_failed);
}
-static int read_der(const char *path, unsigned char **out, size_t *out_sz)
+static int read_der(const char *path, __u8 **out, size_t *out_sz)
{
- unsigned char *buf;
struct stat st;
+ __u8 *buf;
ssize_t n;
int fd;
@@ -169,8 +169,8 @@ static int keyring_holds_set(key_serial_t ring,
size_t i, held = 0;
for (i = 0; i < set->n; i++) {
- unsigned char *der = NULL;
size_t der_sz = 0;
+ __u8 *der = NULL;
int rc;
rc = read_der(set->certs[i].path, &der, &der_sz);
@@ -202,8 +202,8 @@ static int init_bpf_keyring(key_serial_t ring,
size_t i;
for (i = 0; i < set->n; i++) {
- unsigned char *der = NULL;
size_t der_sz = 0;
+ __u8 *der = NULL;
key_serial_t key;
rc = read_der(set->certs[i].path, &der, &der_sz);
@@ -277,9 +277,9 @@ static int init_keyring(void)
const struct shirudo_trusted_cert *primary = NULL;
struct shirudo_trusted_set set;
struct shirudo_identity id;
- unsigned char *der = NULL;
key_serial_t ring, key;
size_t der_sz = 0, i;
+ __u8 *der = NULL;
bool frozen;
int rc;
@@ -500,9 +500,9 @@ static int init_bpffs(void)
}
/* The BPF side compares s_dev in the kernel's encoding, not glibc's. */
-static uint32_t kernel_dev_t(dev_t st_dev)
+static __u32 kernel_dev_t(dev_t st_dev)
{
- return ((uint32_t)major(st_dev) << 20) | (minor(st_dev) & 0xfffffu);
+ return ((__u32)major(st_dev) << 20) | (minor(st_dev) & 0xfffffu);
}
/*
@@ -512,9 +512,9 @@ static uint32_t kernel_dev_t(dev_t st_dev)
*/
static bool binaries_differ(const char *a, const char *b)
{
- uint8_t ida[SHIRUDO_UPATCH_BUILD_ID_MAX], idb[SHIRUDO_UPATCH_BUILD_ID_MAX];
- unsigned char ba[65536], bb[65536];
- uint32_t lena = 0, lenb = 0;
+ __u8 ida[SHIRUDO_UPATCH_BUILD_ID_MAX], idb[SHIRUDO_UPATCH_BUILD_ID_MAX];
+ __u8 ba[65536], bb[65536];
+ __u32 lena = 0, lenb = 0;
bool differ = true;
ssize_t na, nb;
int fa, fb;
@@ -566,11 +566,11 @@ static int identity_executables(struct shirudo_identity *id)
}
if (!initrd_mode) {
id->exe_dev = kernel_dev_t(st.st_dev);
- id->exe_ino = (uint64_t)st.st_ino;
+ id->exe_ino = (__u64)st.st_ino;
return 0;
}
id->alt_exe_dev = kernel_dev_t(st.st_dev);
- id->alt_exe_ino = (uint64_t)st.st_ino;
+ id->alt_exe_ino = (__u64)st.st_ino;
n = readlink("/proc/self/exe", self, sizeof(self) - 1);
if (n < 0) {
@@ -588,7 +588,7 @@ static int identity_executables(struct shirudo_identity *id)
return -errno;
}
id->exe_dev = kernel_dev_t(st.st_dev);
- id->exe_ino = (uint64_t)st.st_ino;
+ id->exe_ino = (__u64)st.st_ino;
if (binaries_differ("/proc/self/exe", root_exe))
pr_warn("initrd: %s differs from the initrd's copy of shirudo; "
"regenerate the initrd after upgrading", root_exe);
@@ -723,7 +723,7 @@ _Static_assert(SHIRUDO_IDENTITY_KEYS_MAX == SHIRUDO_KEYS_MAX,
"the identity map holds one serial per trusted certificate");
struct anchor_keys {
- int32_t serials[SHIRUDO_IDENTITY_KEYS_MAX];
+ __s32 serials[SHIRUDO_IDENTITY_KEYS_MAX];
size_t n;
bool overflow;
};
@@ -740,7 +740,7 @@ static void collect_asymmetric(key_serial_t serial, const char *type,
k->overflow = true;
return;
}
- k->serials[k->n++] = (int32_t)serial;
+ k->serials[k->n++] = (__s32)serial;
}
static int init_lsm(bool attach_filesystem_seal)
@@ -777,7 +777,7 @@ static int init_lsm(bool attach_filesystem_seal)
key_serial_t keyring;
struct bpf_map *m_sb, *m_tamper;
struct stat st;
- uint32_t zero = 0;
+ __u32 zero = 0;
int err, map_fd, tamper_fd, i;
if (access(SHIRUDO_LSM_LINK_PIN, F_OK) == 0) {
@@ -848,7 +848,7 @@ static int init_lsm(bool attach_filesystem_seal)
strerror(-err));
goto out;
}
- id.keyring_serial = (int32_t)keyring;
+ id.keyring_serial = (__s32)keyring;
/*
* The keys that keyring holds, for lsm/key_permission: with the
@@ -867,7 +867,7 @@ static int init_lsm(bool attach_filesystem_seal)
err = keys.overflow ? -E2BIG : -ENOKEY;
goto out;
}
- id.n_keys = (uint32_t)keys.n;
+ id.n_keys = (__u32)keys.n;
memcpy(id.key_serials, keys.serials, keys.n * sizeof(keys.serials[0]));
m_sb = bpf_object__find_map_by_name(obj, "shirudo_bpffs_sb");
@@ -1185,10 +1185,10 @@ next:
/* The binding a verifier needs to recompute the register's public area. */
static int append_init_event(const struct shirudo_measured_upatch *set,
size_t n,
- const uint8_t cert_sha[32],
- const uint8_t set_digest[32],
- const uint8_t tree_root[32],
- const uint8_t init_digest[32],
+ const __u8 cert_sha[32],
+ const __u8 set_digest[32],
+ const __u8 tree_root[32],
+ const __u8 init_digest[32],
const struct shirudo_chain_init_info *info)
{
char buf[64 * 1024];
@@ -1266,7 +1266,7 @@ static int append_init_event(const struct shirudo_measured_upatch *set,
shirudo_eventlog_path(), buf);
}
-static bool phase_unmeasured(const uint8_t pcr[32])
+static bool phase_unmeasured(const __u8 pcr[32])
{
int i;
@@ -1277,7 +1277,7 @@ static bool phase_unmeasured(const uint8_t pcr[32])
return true;
}
-static void tree_root_at_boot(uint8_t out[32])
+static void tree_root_at_boot(__u8 out[32])
{
struct shirudo_tree_state s = {};
@@ -1299,13 +1299,13 @@ static void tree_root_at_boot(uint8_t out[32])
*/
static int chain_extend_init(struct shirudo_measured_upatch *set, size_t n)
{
- uint8_t cert_sha[32], set_digest[32], tree_root[32], init_digest[32];
+ __u8 cert_sha[32], set_digest[32], tree_root[32], init_digest[32];
struct shirudo_chain_init_info info;
int rc;
{
struct shirudo_config cfg;
- unsigned long max_bytes;
+ __u64 max_bytes;
bool full;
shirudo_config_load(&cfg);
@@ -1313,7 +1313,7 @@ static int chain_extend_init(struct shirudo_measured_upatch *set, size_t n)
full = shirudo_eventlog_would_exceed(max_bytes);
explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */
if (full) {
- pr_err("eventlog %s would exceed eventlog.max_bytes=%lu; "
+ pr_err("eventlog %s would exceed eventlog.max_bytes=%llu; "
"refusing chain extend (eventlog_full)",
shirudo_eventlog_path(), max_bytes);
return -ENOSPC;
diff --git a/builtin-keys.c b/builtin-keys.c
index eb9430bd..9d2bd789 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -25,6 +25,8 @@
#include <openssl/sha.h>
#include <openssl/x509.h>
+#include <shirudo/misc.h>
+
#include "builtin.h"
#include "util/chain.h"
#include "util/crypto.h"
@@ -117,9 +119,9 @@ static void format_expires_in(long days, char *out, size_t out_sz)
snprintf(out, out_sz, "%ldd", days);
}
-static int compute_cert_fpr(X509 *c, unsigned char fpr[SHA256_DIGEST_LENGTH])
+static int compute_cert_fpr(X509 *c, __u8 fpr[SHA256_DIGEST_LENGTH])
{
- unsigned int n = SHA256_DIGEST_LENGTH;
+ __u32 n = SHA256_DIGEST_LENGTH;
if (X509_digest(c, EVP_sha256(), fpr, &n) != 1) {
shirudo_openssl_complain("X509_digest");
@@ -130,10 +132,10 @@ static int compute_cert_fpr(X509 *c, unsigned char fpr[SHA256_DIGEST_LENGTH])
/* SHA-256 over the SPKI; comparable across a cert and its key. */
static int compute_spki_fpr(EVP_PKEY *pkey,
- unsigned char fpr[SHA256_DIGEST_LENGTH])
+ __u8 fpr[SHA256_DIGEST_LENGTH])
{
- unsigned int n = SHA256_DIGEST_LENGTH;
- unsigned char *der = NULL;
+ __u32 n = SHA256_DIGEST_LENGTH;
+ __u8 *der = NULL;
int der_len;
int rc = -1;
@@ -153,7 +155,7 @@ out:
return rc;
}
-static void print_fpr(const char *label, const unsigned char *fpr)
+static void print_fpr(const char *label, const __u8 *fpr)
{
printf(" %-22s SHA256:", label);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
@@ -180,7 +182,7 @@ static void format_pkey_algorithm(EVP_PKEY *pkey, char *out, size_t out_sz)
}
static int dump_operator_cert(X509 *cert, const char *cert_path,
- const unsigned char cert_spki_fpr[SHA256_DIGEST_LENGTH])
+ const __u8 cert_spki_fpr[SHA256_DIGEST_LENGTH])
{
X509_NAME *subj = X509_get_subject_name(cert);
X509_NAME *iss = X509_get_issuer_name(cert);
@@ -188,7 +190,7 @@ static int dump_operator_cert(X509 *cert, const char *cert_path,
const ASN1_TIME *na = X509_get0_notAfter(cert);
EVP_PKEY *pub = X509_get0_pubkey(cert);
char subj_s[256], iss_s[256], nb_s[32], na_s[32], exp_s[48];
- unsigned char fpr[SHA256_DIGEST_LENGTH];
+ __u8 fpr[SHA256_DIGEST_LENGTH];
char alg_s[64];
int self_signed;
long left;
@@ -219,9 +221,9 @@ static int dump_operator_cert(X509 *cert, const char *cert_path,
}
static int dump_signing_key(const char *pkey_path,
- const unsigned char cert_spki_fpr[SHA256_DIGEST_LENGTH])
+ const __u8 cert_spki_fpr[SHA256_DIGEST_LENGTH])
{
- unsigned char fpr_priv[SHA256_DIGEST_LENGTH];
+ __u8 fpr_priv[SHA256_DIGEST_LENGTH];
char alg_s[64];
EVP_PKEY *priv;
int rc = 0;
@@ -267,7 +269,7 @@ static int keys_dump_disk_material(const char *cert_path,
{
X509 *cert;
EVP_PKEY *pub;
- unsigned char cert_spki_fpr[SHA256_DIGEST_LENGTH];
+ __u8 cert_spki_fpr[SHA256_DIGEST_LENGTH];
int rc;
cert = read_cert_der(cert_path);
@@ -454,7 +456,7 @@ static const struct shirudo_help keys_vouch_help = {
};
/* The public key must be one the kernel verifies loaders with. */
-static int check_cert_algorithm(const unsigned char *der, size_t der_sz,
+static int check_cert_algorithm(const __u8 *der, size_t der_sz,
const char *what)
{
struct shirudo_cert cert = {};
@@ -482,12 +484,12 @@ static int check_cert_algorithm(const unsigned char *der, size_t der_sz,
* one being extended.
*/
static int check_voucher_live(const struct shirudo_trusted_set *set,
- const uint8_t voucher[32])
+ const __u8 voucher[32])
{
struct shirudo_identity id;
- unsigned char *der = NULL;
key_serial_t ring;
size_t der_sz = 0;
+ __u8 *der = NULL;
int rc = 0;
size_t i;
@@ -516,8 +518,8 @@ static int check_voucher_live(const struct shirudo_trusted_set *set,
static int keys_add(int argc, const char **argv)
{
const char *keys_dir_arg = NULL, *vouch_path = NULL, *cert = NULL;
- unsigned char *der = NULL, *sig = NULL;
- uint8_t sha256[32], fp[32], by[32];
+ __u8 *der = NULL, *sig = NULL;
+ __u8 sha256[32], fp[32], by[32];
struct shirudo_trusted_set set;
char fp_hex[65], by_hex[65];
size_t der_sz = 0, sig_sz = 0;
@@ -666,7 +668,7 @@ static const struct shirudo_help keys_revoke_help = {
};
static int append_key_event(const char *type, const char *fp_hex,
- const char *sha_hex, const uint8_t event[32])
+ const char *sha_hex, const __u8 event[32])
{
char buf[1024];
struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
@@ -707,7 +709,7 @@ static int append_key_event(const char *type, const char *fp_hex,
static int measure_revoke(const struct shirudo_trusted_cert *c,
const char *fp_hex)
{
- uint8_t event[32], chain[32];
+ __u8 event[32], chain[32];
char sha_hex[65], chain_hex[65];
int rc;
@@ -742,7 +744,7 @@ static int measure_revoke(const struct shirudo_trusted_cert *c,
* would keep running what a key nobody trusts any more authorised. The
* rest are still tried when one fails.
*/
-static int purge_signed_by(const unsigned char *der, size_t der_sz,
+static int purge_signed_by(const __u8 *der, size_t der_sz,
size_t *unloaded)
{
struct shirudo_cert cert = {};
@@ -797,9 +799,9 @@ static int purge_signed_by(const unsigned char *der, size_t der_sz,
static int promote(const struct shirudo_trusted_cert *c)
{
- unsigned char *der = NULL;
char vouch[PATH_MAX];
size_t der_sz = 0;
+ __u8 *der = NULL;
int rc;
rc = shirudo_keys_read_der(c->path, &der, &der_sz);
@@ -817,7 +819,7 @@ static int promote(const struct shirudo_trusted_cert *c)
}
static const struct shirudo_trusted_cert *
-set_find(const struct shirudo_trusted_set *set, const uint8_t fp[32])
+set_find(const struct shirudo_trusted_set *set, const __u8 fp[32])
{
size_t i;
@@ -873,8 +875,8 @@ static bool revoke_unmeasured(const char *marker, const char *fp_hex)
}
/* The certificate a revoked marker holds, as a member it once was. */
-static int revoked_cert(const uint8_t fp[32], const char *marker,
- struct shirudo_trusted_cert *c, unsigned char **der,
+static int revoked_cert(const __u8 fp[32], const char *marker,
+ struct shirudo_trusted_cert *c, __u8 **der,
size_t *der_sz)
{
char trusted[PATH_MAX];
@@ -904,9 +906,9 @@ static int keys_revoke(int argc, const char **argv)
const struct shirudo_trusted_cert *victim, *heir = NULL;
const char *fp_arg = NULL, *promote_arg = NULL;
struct shirudo_trusted_cert gone;
- uint8_t fp[32], promote_fp[32];
+ __u8 fp[32], promote_fp[32];
struct shirudo_trusted_set set;
- unsigned char *der = NULL;
+ __u8 *der = NULL;
struct shirudo_identity id;
char revoked[PATH_MAX];
key_serial_t ring, key;
diff --git a/builtin-status.c b/builtin-status.c
index d02f6af1..5ddee0df 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -9,7 +9,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -23,6 +22,7 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include <time.h>
@@ -289,12 +289,12 @@ static void print_system_human(const struct shirudo_sysinfo *si)
if (si->mem_total_kb > 0) {
char tot[16], used[16];
- shirudo_human_size((uint64_t)si->mem_total_kb * 1024,
+ shirudo_human_size((__u64)si->mem_total_kb * 1024,
tot, sizeof(tot));
if (si->mem_avail_kb >= 0 &&
si->mem_avail_kb <= si->mem_total_kb) {
- shirudo_human_size((uint64_t)(si->mem_total_kb -
- si->mem_avail_kb) * 1024,
+ shirudo_human_size((__u64)(si->mem_total_kb -
+ si->mem_avail_kb) * 1024,
used, sizeof(used));
printf(" %-16s %s/%s\n", "Memory:", used, tot);
} else {
@@ -304,9 +304,9 @@ static void print_system_human(const struct shirudo_sysinfo *si)
if (si->disk_total_kb > 0) {
char tot[16], used[16];
- shirudo_human_size((uint64_t)si->disk_total_kb * 1024,
+ shirudo_human_size((__u64)si->disk_total_kb * 1024,
tot, sizeof(tot));
- shirudo_human_size((uint64_t)si->disk_used_kb * 1024,
+ shirudo_human_size((__u64)si->disk_used_kb * 1024,
used, sizeof(used));
printf(" %-16s %s/%s\n", "Disk:", used, tot);
}
@@ -417,7 +417,7 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,
r->payload_sha256,
sizeof(r->payload_sha256));
shirudo_upatch_maps_list_one(name, &list, &nm);
- r->n_maps = (uint32_t)nm;
+ r->n_maps = (__u32)nm;
shirudo_map_summary_free(list, nm);
r->age_sec = shirudo_upatch_age_sec(r->loaded_at);
}
@@ -430,8 +430,8 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,
/* Status runs on the host, so reachable and parsed are trivially true. */
static void local_cond_input(struct shirudo_conditions_input *in,
int lsm_attached, int keyring_present, int staging,
- int64_t keyring_serial,
- int64_t keyring_serial_pinned,
+ __s64 keyring_serial,
+ __s64 keyring_serial_pinned,
int ks_seen, int ksp_seen, int ksm_seen,
int ks_matches,
int chain_seen, int chain_matches, int cm_seen,
@@ -519,7 +519,7 @@ int cmd_status(int argc, const char **argv)
bool include_system = false, include_eventlog = false;
bool include_kernel_log = false, include_modules = false;
bool show_full = false;
- unsigned int kernel_log_lines = 10;
+ __u32 kernel_log_lines = 10;
bool lsm_attached, keyring_present, is_operator, cert_present, is_target;
key_serial_t keyring_serial, keyring_pinned;
const char *keyring_mode, *mode;
@@ -561,8 +561,8 @@ int cmd_status(int argc, const char **argv)
} else if (strcmp(argv[i], "--include-kernellog") == 0) {
include_kernel_log = true;
} else if (strncmp(argv[i], "--include-kernellog=", 20) == 0) {
- unsigned long v;
char *end;
+ __u64 v;
v = strtoul(argv[i] + 20, &end, 0);
if (*end != '\0' || v < 1 ||
@@ -573,7 +573,7 @@ int cmd_status(int argc, const char **argv)
return 2;
}
include_kernel_log = true;
- kernel_log_lines = (unsigned int)v;
+ kernel_log_lines = (__u32)v;
} else if (strcmp(argv[i], "--include-modules") == 0) {
include_modules = true;
} else if (strcmp(argv[i], "--show-full") == 0) {
@@ -707,7 +707,7 @@ int cmd_status(int argc, const char **argv)
* no init record while a register exists) is a mismatch.
*/
if (chain_rc == 0) {
- uint8_t replay[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 replay[SHIRUDO_CHAIN_DIGEST_LEN];
match = cst.anchored && !cst.rebound &&
shirudo_eventlog_replay(replay) == 0 &&
@@ -858,11 +858,11 @@ int cmd_status(int argc, const char **argv)
*/
if (tamper_rc == 0) {
shirudo_json_obj_open_key(&e, "tamper_attempts");
- shirudo_json_kv_int(&e, "total", (int64_t)tamper.total);
+ shirudo_json_kv_int(&e, "total", (__s64)tamper.total);
shirudo_json_obj_open_key(&e, "counters");
for (i = 0; i < SHIRUDO_TAMPER_MAX; i++)
shirudo_json_kv_int(&e, shirudo_tamper_name(i),
- (int64_t)tamper.count[i]);
+ (__s64)tamper.count[i]);
shirudo_json_obj_close(&e);
shirudo_json_obj_close(&e);
} else {
@@ -944,13 +944,13 @@ int cmd_status(int argc, const char **argv)
struct shirudo_kmod_set ks = {};
int mrc = shirudo_kmodules_read(&ks);
struct timespec rt = {}, bt = {};
- int64_t boot_sec = 0;
+ __s64 boot_sec = 0;
char boot_iso[32] = "";
size_t mi;
if (clock_gettime(CLOCK_REALTIME, &rt) == 0 &&
clock_gettime(CLOCK_BOOTTIME, &bt) == 0) {
- boot_sec = (int64_t)rt.tv_sec - bt.tv_sec;
+ boot_sec = (__s64)rt.tv_sec - bt.tv_sec;
shirudo_iso8601((time_t)boot_sec, boot_iso,
sizeof(boot_iso));
}
@@ -963,7 +963,7 @@ int cmd_status(int argc, const char **argv)
&ks.mods[mi];
time_t at = m->loaded_at_sec;
char iso[32] = "";
- uint16_t di;
+ __u16 di;
if (at > 0)
shirudo_iso8601(at, iso,
@@ -972,7 +972,7 @@ int cmd_status(int argc, const char **argv)
shirudo_json_kv_string(&e, "name",
m->name);
shirudo_json_kv_int(&e, "size",
- (int64_t)m->size);
+ (__s64)m->size);
shirudo_json_kv_int(&e, "refcount",
m->refcount);
shirudo_json_kv_string(&e, "loaded_at",
@@ -1006,8 +1006,8 @@ int cmd_status(int argc, const char **argv)
strerror(-e.err));
} else {
if (otel && otel_endpoint_configured) {
- int64_t refused = tamper_rc == 0 ?
- (int64_t)tamper.total : -1;
+ __s64 refused = tamper_rc == 0 ?
+ (__s64)tamper.total : -1;
const char *severity_text;
int severity_number;
int rc;
@@ -1091,14 +1091,14 @@ int cmd_status(int argc, const char **argv)
if (tamper.count[i])
printf(" %-20s %6llu %s\n",
shirudo_tamper_name(i),
- (unsigned long long)tamper.count[i],
+ (__u64)tamper.count[i],
shirudo_tamper_meaning(i));
}
printf("\nKeyring (%s):\n", SHIRUDO_KEYRING_NAME);
if (keyring_present)
printf(" %-22s %lld\n", "Serial:",
- (long long)keyring_serial);
+ (__s64)keyring_serial);
else
printf(" %-22s %s\n", "Serial:",
"absent (operator host, or `shirudo init` "
@@ -1108,7 +1108,7 @@ int cmd_status(int argc, const char **argv)
"(none this boot)");
else
printf(" %-22s %lld%s\n", "Frozen serial:",
- (long long)keyring_pinned, !keyring_present ? "" :
+ (__s64)keyring_pinned, !keyring_present ? "" :
keyring_serial == keyring_pinned ?
" (matches)" :
" (MISMATCH -- swapped?)");
@@ -1232,13 +1232,13 @@ int cmd_status(int argc, const char **argv)
struct shirudo_kmod_set ks = {};
int mrc = shirudo_kmodules_read(&ks);
struct timespec rt = {}, bt = {};
- int64_t boot_sec = 0;
+ __s64 boot_sec = 0;
size_t last = 10;
size_t shown = ks.n < last ? ks.n : last;
if (clock_gettime(CLOCK_REALTIME, &rt) == 0 &&
clock_gettime(CLOCK_BOOTTIME, &bt) == 0)
- boot_sec = (int64_t)rt.tv_sec - bt.tv_sec;
+ boot_sec = (__s64)rt.tv_sec - bt.tv_sec;
if (mrc != 0) {
printf("\nLoaded modules:\n");
diff --git a/builtin-upatch.c b/builtin-upatch.c
index fefafbf6..86323e89 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -31,6 +31,8 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
+
#include "builtin.h"
#include "util/chain.h"
#include "util/config.h"
@@ -109,25 +111,25 @@ static const char *flag_str(const struct shirudo_upatch *u)
return shirudo_upatch_is_unload(u) ? "UNLOAD-directive" : "LOAD";
}
-static const char *decoder_language_str(uint32_t language)
+static const char *decoder_language_str(__u32 language)
{
return language == SHIRUDO_UPATCH_DECODER_LANG_LUA ? "lua" : "unknown";
}
-static const char *decoder_input_str(uint32_t input_format)
+static const char *decoder_input_str(__u32 input_format)
{
return input_format == SHIRUDO_UPATCH_DECODER_INPUT_MAP_V1 ?
"shirudo.map.v1" : "unknown";
}
-static const char *decoder_signal_str(uint32_t signal)
+static const char *decoder_signal_str(__u32 signal)
{
return signal == SHIRUDO_UPATCH_DECODER_EXPORT_LOG ? "log" :
signal == SHIRUDO_UPATCH_DECODER_EXPORT_METRIC ? "metric" :
"unknown";
}
-static int print_sha256_hex(const unsigned char hash[32])
+static int print_sha256_hex(const __u8 hash[32])
{
char hex[65];
@@ -339,9 +341,9 @@ static int cmd_upatch_info(int argc, const char **argv)
shirudo_json_kv_int(&e, "metadata_len", u.hdr.metadata_len);
shirudo_json_kv_int(&e, "data_len", u.hdr.data_len);
shirudo_json_kv_int(&e, "log_buf_sz", u.hdr.log_buf_sz);
- shirudo_json_kv_int(&e, "file_size", (int64_t)u.map_size);
+ shirudo_json_kv_int(&e, "file_size", (__s64)u.map_size);
shirudo_json_arr_open(&e, "decoders");
- for (uint32_t j = 0; j < u.nr_decoders; j++) {
+ for (__u32 j = 0; j < u.nr_decoders; j++) {
struct shirudo_upatch_decoder_view d;
if (shirudo_upatch_decoder_view(&u, j, &d))
@@ -406,7 +408,7 @@ static int cmd_upatch_info(int argc, const char **argv)
if (!shirudo_upatch_has_decoders(&u)) {
printf(" (none)\n");
} else {
- for (uint32_t j = 0; j < u.nr_decoders; j++) {
+ for (__u32 j = 0; j < u.nr_decoders; j++) {
struct shirudo_upatch_decoder_view d;
if (shirudo_upatch_decoder_view(&u, j, &d))
@@ -618,26 +620,26 @@ out:
*/
struct shirudo_loader_ctx {
- uint32_t sz;
- uint32_t flags;
- uint32_t log_level;
- uint32_t log_size;
- uint64_t log_buf;
+ __u32 sz;
+ __u32 flags;
+ __u32 log_level;
+ __u32 log_size;
+ __u64 log_buf;
};
struct shirudo_map_desc {
int map_fd;
- uint32_t max_entries;
- uint64_t initial_value;
+ __u32 max_entries;
+ __u64 initial_value;
};
struct shirudo_prog_desc {
int prog_fd;
};
-static int loader_map_create(const char *name, uint32_t value_size,
+static int loader_map_create(const char *name, __u32 value_size,
const void *excl_prog_hash,
- uint32_t excl_prog_hash_sz)
+ __u32 excl_prog_hash_sz)
{
union bpf_attr attr = {};
long fd;
@@ -646,7 +648,7 @@ static int loader_map_create(const char *name, uint32_t value_size,
attr.key_size = 4;
attr.value_size = value_size ? value_size : 4;
attr.max_entries = 1;
- attr.excl_prog_hash = (uint64_t)(uintptr_t)excl_prog_hash;
+ attr.excl_prog_hash = (__u64)(uintptr_t)excl_prog_hash;
attr.excl_prog_hash_size = excl_prog_hash_sz;
memcpy(attr.map_name, name, strnlen(name, sizeof(attr.map_name)));
@@ -654,16 +656,16 @@ static int loader_map_create(const char *name, uint32_t value_size,
return (int)fd;
}
-static int loader_map_update(int map_fd, const void *value, uint32_t value_sz)
+static int loader_map_update(int map_fd, const void *value, __u32 value_sz)
{
union bpf_attr attr = {};
- uint8_t zero[4] = {};
- uint32_t key = 0;
+ __u8 zero[4] = {};
+ __u32 key = 0;
long ret;
attr.map_fd = map_fd;
- attr.key = (uint64_t)(uintptr_t)&key;
- attr.value = (uint64_t)(uintptr_t)(value_sz ? value : zero);
+ attr.key = (__u64)(uintptr_t)&key;
+ attr.value = (__u64)(uintptr_t)(value_sz ? value : zero);
attr.flags = 0;
ret = syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
return (int)ret;
@@ -681,8 +683,8 @@ static int loader_map_freeze(int map_fd)
static int prog_load_signed(const struct bpf_insn *insns, size_t insn_cnt,
const char *prog_name,
- const void *signature, uint32_t signature_sz,
- int32_t keyring_id, int loader_map_fd,
+ const void *signature, __u32 signature_sz,
+ __s32 keyring_id, int loader_map_fd,
char *log_buf, size_t log_sz)
{
union bpf_attr attr = {};
@@ -690,17 +692,17 @@ static int prog_load_signed(const struct bpf_insn *insns, size_t insn_cnt,
long fd;
attr.prog_type = BPF_PROG_TYPE_SYSCALL;
- attr.insn_cnt = (uint32_t)insn_cnt;
- attr.insns = (uint64_t)(uintptr_t)insns;
- attr.license = (uint64_t)(uintptr_t)"Dual BSD/GPL";
+ attr.insn_cnt = (__u32)insn_cnt;
+ attr.insns = (__u64)(uintptr_t)insns;
+ attr.license = (__u64)(uintptr_t)"Dual BSD/GPL";
attr.prog_flags = BPF_F_SLEEPABLE;
attr.log_level = log_buf ? 1 : 0;
- attr.log_size = (uint32_t)log_sz;
- attr.log_buf = (uint64_t)(uintptr_t)log_buf;
- attr.signature = (uint64_t)(uintptr_t)signature;
+ attr.log_size = (__u32)log_sz;
+ attr.log_buf = (__u64)(uintptr_t)log_buf;
+ attr.signature = (__u64)(uintptr_t)signature;
attr.signature_size = signature_sz;
attr.keyring_id = keyring_id;
- attr.fd_array = (uint64_t)(uintptr_t)fd_array;
+ attr.fd_array = (__u64)(uintptr_t)fd_array;
/*
* fd_array_cnt makes the verifier resolve the exclusive metadata
* map up front and fold its frozen contents into the signed buffer
@@ -722,7 +724,7 @@ static int prog_attach_raw_tp(int prog_fd)
long fd;
attr.raw_tracepoint.name = 0;
- attr.raw_tracepoint.prog_fd = (uint32_t)prog_fd;
+ attr.raw_tracepoint.prog_fd = (__u32)prog_fd;
fd = syscall(__NR_bpf, BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
return fd >= 0 ? (int)fd : -errno;
}
@@ -735,7 +737,7 @@ static int prog_attach_btf_link(int prog_fd, enum bpf_attach_type attach_type)
union bpf_attr attr = {};
long fd;
- attr.link_create.prog_fd = (uint32_t)prog_fd;
+ attr.link_create.prog_fd = (__u32)prog_fd;
attr.link_create.target_fd = 0;
attr.link_create.attach_type = attach_type;
fd = syscall(__NR_bpf, BPF_LINK_CREATE, &attr, sizeof(attr));
@@ -752,7 +754,7 @@ static int prog_func_name(int prog_fd, const struct bpf_prog_info *base,
const char *name;
void *recs;
size_t recs_sz;
- uint32_t len;
+ __u32 len;
int rc = 0;
if (!out || cap == 0)
@@ -770,7 +772,7 @@ static int prog_func_name(int prog_fd, const struct bpf_prog_info *base,
info.nr_func_info = base->nr_func_info;
info.func_info_rec_size = base->func_info_rec_size;
- info.func_info = (uint64_t)(uintptr_t)recs;
+ info.func_info = (__u64)(uintptr_t)recs;
len = sizeof(info);
if (bpf_prog_get_info_by_fd(prog_fd, &info, &len) != 0) {
rc = -errno;
@@ -853,7 +855,7 @@ static int prog_attach_cgroup_skb_fallback(int prog_fd,
static int prog_attach_default(int prog_fd)
{
struct bpf_prog_info info = {};
- uint32_t info_len = sizeof(info);
+ __u32 info_len = sizeof(info);
enum bpf_attach_type attach_type;
if (bpf_prog_get_info_by_fd(prog_fd, &info, &info_len) != 0)
@@ -882,8 +884,8 @@ static int prog_attach_cgroup_skb(int prog_fd, enum bpf_attach_type attach_type)
if (cgroup_fd < 0)
return -errno;
- attr.link_create.prog_fd = (uint32_t)prog_fd;
- attr.link_create.target_fd = (uint32_t)cgroup_fd;
+ attr.link_create.prog_fd = (__u32)prog_fd;
+ attr.link_create.target_fd = (__u32)cgroup_fd;
attr.link_create.attach_type = attach_type;
fd = syscall(__NR_bpf, BPF_LINK_CREATE, &attr, sizeof(attr));
rc = fd >= 0 ? (int)fd : -errno;
@@ -939,7 +941,7 @@ static int determine_kprobe_retprobe_bit(void)
"config:%d\n");
}
-static int perf_event_open_kprobe(const char *target, uint64_t offset,
+static int perf_event_open_kprobe(const char *target, __u64 offset,
bool retprobe)
{
struct perf_event_attr attr = {};
@@ -950,8 +952,8 @@ static int perf_event_open_kprobe(const char *target, uint64_t offset,
if (type < 0)
return type;
attr.size = sizeof(attr);
- attr.type = (uint32_t)type;
- attr.config1 = (uint64_t)(uintptr_t)target;
+ attr.type = (__u32)type;
+ attr.config1 = (__u64)(uintptr_t)target;
attr.config2 = offset;
if (retprobe) {
bit = determine_kprobe_retprobe_bit();
@@ -973,30 +975,30 @@ static int prog_attach_perf_event(int prog_fd, int perf_fd)
union bpf_attr attr = {};
long fd;
- attr.link_create.prog_fd = (uint32_t)prog_fd;
- attr.link_create.target_fd = (uint32_t)perf_fd;
+ attr.link_create.prog_fd = (__u32)prog_fd;
+ attr.link_create.target_fd = (__u32)perf_fd;
attr.link_create.attach_type = BPF_PERF_EVENT;
fd = syscall(__NR_bpf, BPF_LINK_CREATE, &attr, sizeof(attr));
return fd >= 0 ? (int)fd : -errno;
}
struct shirudo_prog_spec {
- uint32_t role;
- uint32_t kind;
- uint64_t offset;
+ __u32 role;
+ __u32 kind;
+ __u64 offset;
char target[SHIRUDO_UPATCH_ATTACH_TARGET_MAX];
char map[SHIRUDO_UPATCH_PROG_MAP_MAX];
char *uprobe_path;
- uint32_t uprobe_path_len;
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX];
- uint32_t build_id_len;
- uint64_t *uprobe_offsets;
- uint32_t nr_uprobe_offsets;
+ __u32 uprobe_path_len;
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX];
+ __u32 build_id_len;
+ __u64 *uprobe_offsets;
+ __u32 nr_uprobe_offsets;
int target_fd;
struct stat target_stat;
};
-static const char *attach_kind_name(uint32_t kind)
+static const char *attach_kind_name(__u32 kind)
{
switch (kind) {
case SHIRUDO_UPATCH_ATTACH_RAW_TRACEPOINT:
@@ -1021,9 +1023,9 @@ static const char *attach_kind_name(uint32_t kind)
}
static void prog_specs_free(struct shirudo_prog_spec *specs,
- uint32_t nr_progs)
+ __u32 nr_progs)
{
- uint32_t i;
+ __u32 i;
if (!specs)
return;
@@ -1038,19 +1040,19 @@ static void prog_specs_free(struct shirudo_prog_spec *specs,
xfree(specs, (size_t)nr_progs * sizeof(*specs));
}
-static int prog_specs_parse(const void *metadata, uint32_t metadata_len,
- uint32_t nr_progs,
+static int prog_specs_parse(const void *metadata, __u32 metadata_len,
+ __u32 nr_progs,
struct shirudo_prog_spec **out)
{
struct shirudo_upatch_prog_metadata md;
struct shirudo_prog_spec *specs = NULL;
- const unsigned char *cursor, *end;
+ const __u8 *cursor, *end;
size_t base_len;
- uint32_t magic, version, count, i, nr_uprobes = 0;
+ __u32 magic, version, count, i, nr_uprobes = 0;
int rc = -EINVAL;
*out = NULL;
- if (!metadata || metadata_len < sizeof(uint32_t))
+ if (!metadata || metadata_len < sizeof(__u32))
return -EINVAL;
memcpy(&magic, metadata, sizeof(magic));
magic = le32toh(magic);
@@ -1081,9 +1083,9 @@ static int prog_specs_parse(const void *metadata, uint32_t metadata_len,
for (i = 0; i < nr_progs; i++) {
struct shirudo_upatch_prog_entry prog;
struct shirudo_prog_spec *spec = &specs[i];
- uint32_t role, kind;
+ __u32 role, kind;
- memcpy(&prog, (const unsigned char *)metadata + sizeof(md) +
+ memcpy(&prog, (const __u8 *)metadata + sizeof(md) +
(size_t)i * sizeof(prog), sizeof(prog));
role = le32toh(prog.role);
switch (role) {
@@ -1162,14 +1164,14 @@ static int prog_specs_parse(const void *metadata, uint32_t metadata_len,
return 0;
}
- cursor = (const unsigned char *)metadata + base_len;
- end = (const unsigned char *)metadata + metadata_len;
+ cursor = (const __u8 *)metadata + base_len;
+ end = (const __u8 *)metadata + metadata_len;
if ((size_t)(end - cursor) <
sizeof(struct shirudo_upatch_uprobe_targets))
goto out;
{
struct shirudo_upatch_uprobe_targets targets;
- uint32_t nr_targets;
+ __u32 nr_targets;
memcpy(&targets, cursor, sizeof(targets));
cursor += sizeof(targets);
@@ -1184,7 +1186,7 @@ static int prog_specs_parse(const void *metadata, uint32_t metadata_len,
for (i = 0; i < nr_targets; i++) {
struct shirudo_upatch_uprobe_target record;
struct shirudo_prog_spec *spec;
- uint32_t prog_idx, path_len, build_id_len, nr_offsets, j;
+ __u32 prog_idx, path_len, build_id_len, nr_offsets, j;
size_t payload_len;
if ((size_t)(end - cursor) < sizeof(record))
@@ -1206,7 +1208,7 @@ static int prog_specs_parse(const void *metadata, uint32_t metadata_len,
nr_offsets > SHIRUDO_UPATCH_UPROBE_OFFSETS_MAX)
goto out;
payload_len = (size_t)path_len + build_id_len +
- (size_t)nr_offsets * sizeof(uint64_t);
+ (size_t)nr_offsets * sizeof(__u64);
if ((size_t)(end - cursor) < payload_len ||
cursor[0] != '/' || memchr(cursor, '\0', path_len))
goto out;
@@ -1230,7 +1232,7 @@ static int prog_specs_parse(const void *metadata, uint32_t metadata_len,
}
spec->nr_uprobe_offsets = nr_offsets;
for (j = 0; j < nr_offsets; j++) {
- uint64_t offset;
+ __u64 offset;
memcpy(&offset, cursor, sizeof(offset));
cursor += sizeof(offset);
@@ -1256,14 +1258,14 @@ out:
}
static int prepare_uprobe_targets(struct shirudo_prog_spec *specs,
- uint32_t nr_progs)
+ __u32 nr_progs)
{
- uint32_t i;
+ __u32 i;
for (i = 0; i < nr_progs; i++) {
struct shirudo_prog_spec *spec = &specs[i];
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX];
- uint32_t build_id_len = 0;
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX];
+ __u32 build_id_len = 0;
char proc_path[64];
struct stat opened_stat;
int path_fd, fd, rc;
@@ -1297,8 +1299,8 @@ static int prepare_uprobe_targets(struct shirudo_prog_spec *specs,
close(fd);
return -ESTALE;
}
- for (uint32_t j = 0; j < spec->nr_uprobe_offsets; j++) {
- if (spec->uprobe_offsets[j] >= (uint64_t)opened_stat.st_size) {
+ for (__u32 j = 0; j < spec->nr_uprobe_offsets; j++) {
+ if (spec->uprobe_offsets[j] >= (__u64)opened_stat.st_size) {
close(fd);
return -ERANGE;
}
@@ -1327,13 +1329,13 @@ static int prog_attach_uprobe_multi(int prog_fd,
if (spec->target_fd < 0 || !spec->uprobe_path ||
!spec->uprobe_offsets || !spec->nr_uprobe_offsets)
return -EINVAL;
- attr.link_create.prog_fd = (uint32_t)prog_fd;
+ attr.link_create.prog_fd = (__u32)prog_fd;
attr.link_create.attach_type = BPF_TRACE_UPROBE_MULTI;
attr.link_create.uprobe_multi.offsets =
- (uint64_t)(uintptr_t)spec->uprobe_offsets;
+ (__u64)(uintptr_t)spec->uprobe_offsets;
attr.link_create.uprobe_multi.cnt = spec->nr_uprobe_offsets;
attr.link_create.uprobe_multi.flags = BPF_F_UPROBE_MULTI_PATH_FD;
- attr.link_create.uprobe_multi.path_fd = (uint32_t)spec->target_fd;
+ attr.link_create.uprobe_multi.path_fd = (__u32)spec->target_fd;
fd = syscall(__NR_bpf, BPF_LINK_CREATE, &attr, sizeof(attr));
return fd >= 0 ? (int)fd : -errno;
}
@@ -1377,8 +1379,8 @@ static int obj_pin(int fd, const char *path)
union bpf_attr attr = {};
long ret;
- attr.pathname = (uint64_t)(uintptr_t)path;
- attr.bpf_fd = (uint32_t)fd;
+ attr.pathname = (__u64)(uintptr_t)path;
+ attr.bpf_fd = (__u32)fd;
ret = syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
return ret >= 0 ? 0 : -errno;
}
@@ -1394,9 +1396,9 @@ static int mkdir_if_absent(const char *path, mode_t mode, bool *made)
}
static void count_attach_progs(const struct shirudo_prog_spec *specs,
- uint32_t nr_progs, uint32_t *out)
+ __u32 nr_progs, __u32 *out)
{
- uint32_t i, n = 0;
+ __u32 i, n = 0;
for (i = 0; i < nr_progs; i++) {
if (specs[i].role == SHIRUDO_UPATCH_PROG_ROLE_ATTACH)
@@ -1408,7 +1410,7 @@ static void count_attach_progs(const struct shirudo_prog_spec *specs,
static int prog_pin_path(char *pin, size_t sz, const char *dir,
const char *serializers_dir,
const struct shirudo_prog_spec *spec,
- uint32_t attach_count, uint32_t attach_idx)
+ __u32 attach_count, __u32 attach_idx)
{
int n;
@@ -1429,13 +1431,13 @@ static int prog_pin_path(char *pin, size_t sz, const char *dir,
* archived nor measured, so none of its programs may stay attached.
*/
static int attach_and_pin_progs(const struct shirudo_loader_ctx *cx,
- size_t prog_desc_off, uint32_t nr_progs,
+ size_t prog_desc_off, __u32 nr_progs,
const char *name,
const struct shirudo_prog_spec *specs)
{
char dir[PATH_MAX], serializers_dir[PATH_MAX + 16];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
- uint32_t i, attach_count = 0, attach_seen = 0;
+ __u32 i, attach_count = 0, attach_seen = 0;
bool made_dir = false, made_serializers = false;
char pin[PATH_MAX + 32];
bool *made = NULL;
@@ -1557,17 +1559,17 @@ static int attach_and_pin_progs(const struct shirudo_loader_ctx *cx,
i, attach_kind_name(spec->kind), pin);
}
if (test_fd >= 0) {
- uint64_t init_ctx[2] = {};
- uint32_t retval = 0;
+ __u64 init_ctx[2] = {};
+ __u32 retval = 0;
/* A zero context is the load-time arena initialization call. */
rc = shirudo_prog_test_run(test_fd, init_ctx, sizeof(init_ctx),
&retval);
- if (rc || (int32_t)retval < 0) {
+ if (rc || (__s32)retval < 0) {
pr_err("initialize test-run arena: %s",
- rc ? strerror(-rc) : strerror(-(int32_t)retval));
+ rc ? strerror(-rc) : strerror(-(__s32)retval));
if (!rc)
- rc = (int32_t)retval;
+ rc = (__s32)retval;
goto out;
}
}
@@ -1598,12 +1600,12 @@ out:
* in path components, so libbpf's .bss, .data and .rodata are skipped.
*/
static int pin_upatch_maps(const struct shirudo_loader_ctx *cx,
- uint32_t nr_maps, const char *name)
+ __u32 nr_maps, const char *name)
{
char dir[PATH_MAX], maps_dir[PATH_MAX + 8];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
char pin[PATH_MAX + 32];
- uint32_t i;
+ __u32 i;
int rc;
if (nr_maps == 0)
@@ -1629,7 +1631,7 @@ static int pin_upatch_maps(const struct shirudo_loader_ctx *cx,
sizeof(struct shirudo_loader_ctx) +
(size_t)i * sizeof(*md));
struct bpf_map_info info = {};
- uint32_t info_len = sizeof(info);
+ __u32 info_len = sizeof(info);
struct stat st;
int err;
@@ -1659,7 +1661,7 @@ static int pin_upatch_maps(const struct shirudo_loader_ctx *cx,
continue;
}
pr_info("pinned map %u (%s, type=%u) -> %s",
- i, info.name, (unsigned int)info.type, pin);
+ i, info.name, (__u32)info.type, pin);
}
return 0;
}
@@ -1669,7 +1671,7 @@ static int pin_upatch_maps(const struct shirudo_loader_ctx *cx,
* guard. The load stands, so a failure is not fatal, but it leaves the
* upatch un-unloadable through the normal path: an error, not a warning.
*/
-static void set_pin_payload(const char *name, const uint8_t payload[32])
+static void set_pin_payload(const char *name, const __u8 payload[32])
{
char dir[PATH_MAX];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
@@ -1685,7 +1687,7 @@ static void set_pin_payload(const char *name, const uint8_t payload[32])
dir, strerror(errno));
}
-static int get_pin_payload(const char *name, uint8_t out[32])
+static int get_pin_payload(const char *name, __u8 out[32])
{
char dir[PATH_MAX];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
@@ -1766,11 +1768,11 @@ static int trust_anchor_keyring(key_serial_t *keyring,
* upatch in one process.
*/
static void close_loader_fds(const struct shirudo_loader_ctx *cx,
- uint32_t nr_maps, uint32_t nr_progs)
+ __u32 nr_maps, __u32 nr_progs)
{
const struct shirudo_map_desc *md = (const void *)(cx + 1);
const struct shirudo_prog_desc *pd = (const void *)(md + nr_maps);
- uint32_t i;
+ __u32 i;
for (i = 0; i < nr_maps; i++) {
if (md[i].map_fd > 0)
@@ -1797,7 +1799,7 @@ static int upatch_apply(const char *path, bool verify_only,
size_t insn_cnt, ctx_sz = 0, prog_desc_off;
int prog_fd = -1, map_fd = -1, rc;
struct shirudo_loader_ctx *cx = NULL;
- uint32_t retval = 0;
+ __u32 retval = 0;
vlog = xmalloc(SHIRUDO_VERIFIER_LOG_MAX);
if (!vlog) {
@@ -1885,7 +1887,7 @@ static int upatch_apply(const char *path, bool verify_only,
* value that is not SHA-256(insns || data) is refused even when signed.
*/
{
- uint8_t content_digest[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
+ __u8 content_digest[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
shirudo_sha256_2(insns, u.hdr.loader_insns_len,
data, u.hdr.data_len, content_digest);
@@ -1948,7 +1950,7 @@ static int upatch_apply(const char *path, bool verify_only,
* covers insns || data.
*/
{
- uint8_t loader_digest[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
+ __u8 loader_digest[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
shirudo_sha256(insns, u.hdr.loader_insns_len, loader_digest);
map_fd = loader_map_create("__loader.map", u.hdr.data_len,
@@ -1988,7 +1990,7 @@ static int upatch_apply(const char *path, bool verify_only,
vlog[0] = '\0';
prog_fd = prog_load_signed(insns, insn_cnt, prog_name,
sig, u.hdr.signature_len,
- (int32_t)keyring, map_fd,
+ (__s32)keyring, map_fd,
vlog, SHIRUDO_VERIFIER_LOG_MAX);
if (prog_fd < 0) {
int e = errno;
@@ -2037,7 +2039,7 @@ static int upatch_apply(const char *path, bool verify_only,
goto out;
}
vlog[0] = '\0';
- cx->sz = (uint32_t)ctx_sz;
+ cx->sz = (__u32)ctx_sz;
/*
* Keep inner verifier diagnostics enabled without bpftool's full
* accepted-program trace; the verbose mode can make valid payload
@@ -2045,9 +2047,9 @@ static int upatch_apply(const char *path, bool verify_only,
*/
cx->log_level = 1;
cx->log_size = SHIRUDO_VERIFIER_LOG_MAX;
- cx->log_buf = (uint64_t)(uintptr_t)vlog;
+ cx->log_buf = (__u64)(uintptr_t)vlog;
- rc = shirudo_prog_test_run(prog_fd, cx, (uint32_t)ctx_sz, &retval);
+ rc = shirudo_prog_test_run(prog_fd, cx, (__u32)ctx_sz, &retval);
if (rc) {
pr_err("BPF_PROG_TEST_RUN: %s", strerror(errno));
if (vlog[0])
@@ -2058,20 +2060,20 @@ static int upatch_apply(const char *path, bool verify_only,
rc = 1;
goto out;
}
- if ((int32_t)retval < 0) {
- pr_err("loader returned error: %d", (int32_t)retval);
+ if ((__s32)retval < 0) {
+ pr_err("loader returned error: %d", (__s32)retval);
fprintf(stderr, "loader runtime log (len=%zu):\n%.4096s\n",
strnlen(vlog, SHIRUDO_VERIFIER_LOG_MAX), vlog);
rep_fail(rep, "payload_load", "verifier_rejected",
- -(int32_t)retval, "inner loader returned error");
+ -(__s32)retval, "inner loader returned error");
if (rep)
- rep->loader_return = (int32_t)retval;
+ rep->loader_return = (__s32)retval;
shirudo_report_set_log(rep, vlog);
rc = 1;
goto out;
}
- pr_info("loader ok, retval=%d (maps=%u progs=%u)", (int32_t)retval,
+ pr_info("loader ok, retval=%d (maps=%u progs=%u)", (__s32)retval,
u.hdr.nr_maps, u.hdr.nr_progs);
/*
@@ -2108,7 +2110,7 @@ static int upatch_apply(const char *path, bool verify_only,
pr_audit("upatch load: name=%s keyring=%d progs=%u", upatch_name,
(int)keyring, u.hdr.nr_progs);
if (rep)
- rep->loader_return = (int32_t)retval;
+ rep->loader_return = (__s32)retval;
rc = 0;
out:
prog_specs_free(prog_specs, u.hdr.nr_progs);
@@ -2137,7 +2139,7 @@ int shirudo_upatch_apply(const char *path)
static int upatch_preflight(const char *expect_chain,
struct shirudo_op_report *rep)
{
- uint8_t want[SHIRUDO_CHAIN_DIGEST_LEN], have[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 want[SHIRUDO_CHAIN_DIGEST_LEN], have[SHIRUDO_CHAIN_DIGEST_LEN];
char have_hex[2 * SHIRUDO_CHAIN_DIGEST_LEN + 1];
char abi_detail[SHIRUDO_KERNEL_ABI_DETAIL_MAX];
key_serial_t keyring;
@@ -2205,7 +2207,7 @@ static int eventlog_room(struct shirudo_op_report *rep, const char *op)
shirudo_config_load(&cfg);
if (shirudo_eventlog_would_exceed(cfg.eventlog_max_bytes)) {
pr_err("upatch %s: eventlog %s would exceed "
- "eventlog.max_bytes=%lu; refusing -- no load, no extend",
+ "eventlog.max_bytes=%llu; refusing -- no load, no extend",
op, shirudo_eventlog_path(), cfg.eventlog_max_bytes);
rep_fail(rep, "pre_flight", "eventlog_full", ENOSPC,
"eventlog would exceed eventlog.max_bytes");
@@ -2236,8 +2238,8 @@ static int version_gate(const char *expect_version_id, bool no_version_check,
}
static int append_upatch_event(const char *type, const char *name,
- const uint8_t payload[32],
- const uint8_t event[32], const char *reason)
+ const __u8 payload[32],
+ const __u8 event[32], const char *reason)
{
char buf[4096];
struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
@@ -2280,10 +2282,10 @@ static int append_upatch_event(const char *type, const char *name,
*/
static int upatch_chain_event(const char *tag, const char *type,
const char *op, const char *name,
- const uint8_t payload[32], const char *reason,
+ const __u8 payload[32], const char *reason,
char out_chain[65])
{
- uint8_t event[32], chain[32];
+ __u8 event[32], chain[32];
char chain_hex[65];
int rc;
@@ -2598,7 +2600,7 @@ static int remove_pin_dir(const char *name)
*/
static int stage_upatch(const char *src, char *staged, size_t sz)
{
- unsigned char *buf = NULL;
+ __u8 *buf = NULL;
size_t len = 0;
int rc;
@@ -2622,7 +2624,7 @@ static int cmd_upatch_load(int argc, const char **argv)
const char *file, *expect_chain, *expect_version_id, *report;
char name[SHIRUDO_UPATCH_NAME_MAX + 1] = {};
char staged[PATH_MAX] = {};
- uint8_t payload[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
+ __u8 payload[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
struct shirudo_upatch u;
struct shirudo_op_report rep = { .op = "upatch_load", .phase = "done" };
bool no_precheck, no_version_check, have_id = false;
@@ -2735,7 +2737,7 @@ static int cmd_upatch_unload(int argc, const char **argv)
const char *file, *expect_chain, *expect_version_id, *report;
char name[SHIRUDO_UPATCH_NAME_MAX + 1] = {};
char persisted[PATH_MAX];
- uint8_t payload[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN] = {};
+ __u8 payload[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN] = {};
struct shirudo_upatch u;
struct shirudo_op_report rep = { .op = "upatch_unload", .phase = "done" };
bool no_precheck, no_version_check;
@@ -2820,8 +2822,8 @@ static int cmd_upatch_unload(int argc, const char **argv)
* case below.
*/
{
- uint8_t zero[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN] = {};
- uint8_t live[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
+ __u8 zero[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN] = {};
+ __u8 live[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
int xrc;
if (memcmp(payload, zero, sizeof(payload)) == 0) {
@@ -2905,7 +2907,7 @@ done:
int shirudo_upatch_purge(const char *name, const char *reason)
{
- uint8_t payload[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN] = {};
+ __u8 payload[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN] = {};
char persisted[PATH_MAX];
struct shirudo_upatch u;
bool loaded;
@@ -2965,7 +2967,7 @@ int shirudo_upatch_remove(const char *path)
struct li_entry {
const char *file; /* into the BTF string section */
const char *src;
- uint32_t line;
+ __u32 line;
};
static int li_cmp(const void *a, const void *b)
@@ -2982,11 +2984,11 @@ static int li_cmp(const void *a, const void *b)
* line_info is in instruction order, so a source line recurs and lines
* come out of order; a source view wants unique lines sorted by file.
*/
-static uint32_t li_collect(const struct btf *btf, const void *recs,
- uint32_t rec_size, uint32_t n, struct li_entry **out)
+static __u32 li_collect(const struct btf *btf, const void *recs,
+ __u32 rec_size, __u32 n, struct li_entry **out)
{
struct li_entry *e;
- uint32_t i, c = 0, k;
+ __u32 i, c = 0, k;
*out = NULL;
if (!btf || !recs || n == 0 || rec_size < sizeof(struct bpf_line_info))
@@ -3026,10 +3028,10 @@ static uint32_t li_collect(const struct btf *btf, const void *recs,
return k;
}
-static void dump_line_info_text(struct li_entry *ents, uint32_t k)
+static void dump_line_info_text(struct li_entry *ents, __u32 k)
{
const char *last = NULL;
- uint32_t i;
+ __u32 i;
if (k == 0) {
printf(" (no line info)\n");
@@ -3045,14 +3047,14 @@ static void dump_line_info_text(struct li_entry *ents, uint32_t k)
}
static void emit_line_info_json(struct shirudo_json_emit *e,
- struct li_entry *ents, uint32_t k)
+ struct li_entry *ents, __u32 k)
{
- uint32_t i = 0;
+ __u32 i = 0;
shirudo_json_arr_open(e, "files");
while (i < k) {
const char *path = ents[i].file;
- uint32_t j;
+ __u32 j;
shirudo_json_obj_open(e);
shirudo_json_kv_string(e, "path", path);
@@ -3095,7 +3097,7 @@ static int export_one_map(const char *upatch, const char *map,
bool show_full, bool peek, bool print_detail)
{
struct shirudo_map_view v = {};
- uint32_t cap = (show_full || json_output || otel) ? 0 :
+ __u32 cap = (show_full || json_output || otel) ? 0 :
SHIRUDO_MAP_ENTRIES_DEFAULT;
int rc, ret = 0;
@@ -3121,7 +3123,7 @@ static int export_one_map(const char *upatch, const char *map,
}
}
if (otel) {
- uint32_t decoder_signal;
+ __u32 decoder_signal;
rc = shirudo_upatch_decode_map_signal(&v, &decoder_signal);
if (!rc)
@@ -3219,7 +3221,7 @@ static int cmd_upatch_map(int argc, const char **argv)
otel_opts.allow_insecure = true;
} else if (strncmp(a, "--otel-retries=", 15) == 0) {
char *end;
- unsigned long value;
+ __u64 value;
if (retries_set || !a[15]) {
fprintf(stderr, "upatch map: invalid or duplicate "
@@ -3232,7 +3234,7 @@ static int cmd_upatch_map(int argc, const char **argv)
fprintf(stderr, "upatch map: --otel-retries must be 0..10\n");
return 2;
}
- otel_opts.retries = (unsigned int)value;
+ otel_opts.retries = (__u32)value;
retries_set = true;
} else if (strncmp(a, "--s3-uri=", 9) == 0) {
if (otel_opts.s3_uri || !a[9]) {
@@ -3404,13 +3406,13 @@ static int cmd_upatch_map(int argc, const char **argv)
struct upatch_get_ctx {
const char *upatch;
- uint32_t prog_id;
+ __u32 prog_id;
char location[160];
char loaded_at[32];
char payload[65];
struct li_entry *ents; /* sorted unique source lines */
- uint32_t n_ents;
- uint32_t n_recs; /* raw line_info record count, for free() */
+ __u32 n_ents;
+ __u32 n_recs; /* raw line_info record count, for free() */
struct btf *btf;
};
@@ -3432,7 +3434,7 @@ static int upatch_get_collect(const char *want, struct upatch_get_ctx *c)
struct btf *vbtf = NULL;
void *recs = NULL;
size_t recs_sz = 0;
- uint32_t len, nr, rec_size, btf_id;
+ __u32 len, nr, rec_size, btf_id;
int link_fd = -1, prog_fd = -1, rc = 0;
c->upatch = want;
@@ -3492,7 +3494,7 @@ static int upatch_get_collect(const char *want, struct upatch_get_ctx *c)
memset(&info, 0, sizeof(info));
info.nr_line_info = nr;
info.line_info_rec_size = rec_size;
- info.line_info = (uint64_t)(unsigned long)recs;
+ info.line_info = ptr_to_u64(recs);
len = sizeof(info);
if (bpf_prog_get_info_by_fd(prog_fd, &info, &len) != 0) {
rc = -errno;
@@ -3525,7 +3527,7 @@ static void age_from_ts(const char *ts, char *out, size_t cap)
}
static void render_one_map_text(const char *upatch, const char *map,
- uint32_t cap)
+ __u32 cap)
{
struct shirudo_map_view v = {};
int rc = shirudo_upatch_maps_read_flags(upatch, map, cap,
@@ -3543,7 +3545,7 @@ static void render_one_map_text(const char *upatch, const char *map,
static void render_get_text(const struct upatch_get_ctx *c,
const struct shirudo_map_summary *maps, size_t nm,
- uint32_t map_cap)
+ __u32 map_cap)
{
char age[16] = "";
size_t k;
@@ -3578,7 +3580,7 @@ static void render_get_text(const struct upatch_get_ctx *c,
static int render_get_json(const struct upatch_get_ctx *c,
const struct shirudo_map_summary *maps, size_t nm,
- uint32_t map_cap)
+ __u32 map_cap)
{
struct shirudo_json_emit e = {};
size_t cap = 64 * 1024 + 1024 * nm;
@@ -3655,7 +3657,7 @@ static int cmd_upatch_get(int argc, const char **argv)
const char *want = NULL;
bool json = false, show_full = false;
size_t nm = 0;
- uint32_t map_cap;
+ __u32 map_cap;
int rc, i;
for (i = 1; i < argc; i++) {
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index f4e87e49..4f44b300 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -9,9 +9,10 @@
#define SHIRUDO_UPATCH_H
#include <endian.h>
-#include <stdint.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#define SHIRUDO_UPATCH_MAGIC 0x53485244554e4150ULL /* "SHRDUNAP" */
/*
* The PKCS#7 signature covers insns || data (the gen_loader metadata
@@ -33,9 +34,9 @@ enum shirudo_upatch_type {
#define SHIRUDO_UPATCH_PAYLOAD_HASH_LEN 32 /* SHA-256 */
struct shirudo_upatch_header {
- uint64_t magic; /* SHIRUDO_UPATCH_MAGIC */
- uint32_t version; /* SHIRUDO_UPATCH_VERSION */
- uint32_t flags; /* SHIRUDO_DIRECTIVE_* */
+ __u64 magic; /* SHIRUDO_UPATCH_MAGIC */
+ __u32 version; /* SHIRUDO_UPATCH_VERSION */
+ __u32 flags; /* SHIRUDO_DIRECTIVE_* */
char name[SHIRUDO_UPATCH_NAME_MAX];
/*
* SHA-256 of the signed content, insns || data. Distinct from the
@@ -43,21 +44,21 @@ struct shirudo_upatch_header {
* recomputes at load. This is the value recorded as the pin xattr,
* the TPM register event, and the unload match target.
*/
- uint8_t payload_sha256[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
- uint32_t loader_insns_off; /* file offset */
- uint32_t loader_insns_len;
- uint32_t signature_off;
- uint32_t signature_len;
- uint32_t metadata_off;
- uint32_t metadata_len;
- uint32_t data_off; /* gen_loader_opts.data */
- uint32_t data_len;
- uint32_t log_buf_sz; /* verifier-log buf size */
- uint32_t nr_maps; /* loader ctx bpf_map_desc count */
- uint32_t nr_progs; /* loader ctx bpf_prog_desc count */
- uint32_t reserved0; /* zero */
- uint32_t type; /* enum shirudo_upatch_type */
- uint32_t reserved1; /* zero; keeps the v1 header 8-byte aligned */
+ __u8 payload_sha256[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
+ __u32 loader_insns_off; /* file offset */
+ __u32 loader_insns_len;
+ __u32 signature_off;
+ __u32 signature_len;
+ __u32 metadata_off;
+ __u32 metadata_len;
+ __u32 data_off; /* gen_loader_opts.data */
+ __u32 data_len;
+ __u32 log_buf_sz; /* verifier-log buf size */
+ __u32 nr_maps; /* loader ctx bpf_map_desc count */
+ __u32 nr_progs; /* loader ctx bpf_prog_desc count */
+ __u32 reserved0; /* zero */
+ __u32 type; /* enum shirudo_upatch_type */
+ __u32 reserved1; /* zero; keeps the v1 header 8-byte aligned */
};
_Static_assert(sizeof(struct shirudo_upatch_header) ==
@@ -81,13 +82,13 @@ enum shirudo_upatch_decoder_export_signal {
};
struct shirudo_decoder_manifest {
- uint32_t magic;
- uint16_t version;
- uint16_t language;
- uint16_t api_version;
- uint16_t input_format; /* SHIRUDO_UPATCH_DECODER_INPUT_MAP_V1 */
- uint16_t export_signal;
- uint16_t reserved;
+ __u32 magic;
+ __u16 version;
+ __u16 language;
+ __u16 api_version;
+ __u16 input_format; /* SHIRUDO_UPATCH_DECODER_INPUT_MAP_V1 */
+ __u16 export_signal;
+ __u16 reserved;
char upatch[64];
char map[16];
char source[64];
@@ -95,31 +96,31 @@ struct shirudo_decoder_manifest {
};
struct shirudo_upatch_decoder_entry {
- uint32_t language;
- uint32_t api_version;
- uint32_t input_format;
- uint32_t export_signal;
+ __u32 language;
+ __u32 api_version;
+ __u32 input_format;
+ __u32 export_signal;
char upatch[64];
char map[16];
char source[64];
char entrypoint[32];
- uint32_t source_off;
- uint32_t source_len;
+ __u32 source_off;
+ __u32 source_len;
};
struct shirudo_upatch_decoder_blob {
- uint32_t magic;
- uint32_t version;
- uint32_t nr_decoders;
- uint32_t reserved;
+ __u32 magic;
+ __u32 version;
+ __u32 nr_decoders;
+ __u32 reserved;
struct shirudo_upatch_decoder_entry decoders[];
};
struct shirudo_upatch_decoder_trailer {
- uint32_t magic;
- uint32_t version;
- uint32_t decoder_off;
- uint32_t decoder_len;
+ __u32 magic;
+ __u32 version;
+ __u32 decoder_off;
+ __u32 decoder_len;
};
/*
@@ -160,18 +161,18 @@ enum shirudo_upatch_prog_role {
};
struct shirudo_upatch_prog_entry {
- uint32_t role; /* enum shirudo_upatch_prog_role */
- uint32_t kind; /* enum shirudo_upatch_attach_kind */
- uint64_t offset; /* kprobe offset; zero for kretprobe */
+ __u32 role; /* enum shirudo_upatch_prog_role */
+ __u32 kind; /* enum shirudo_upatch_attach_kind */
+ __u64 offset; /* kprobe offset; zero for kretprobe */
char target[SHIRUDO_UPATCH_ATTACH_TARGET_MAX]; /* kprobe symbol */
char map[SHIRUDO_UPATCH_PROG_MAP_MAX];
};
struct shirudo_upatch_prog_metadata {
- uint32_t magic; /* SHIRUDO_UPATCH_PROG_MAGIC */
- uint32_t version; /* SHIRUDO_UPATCH_PROG_VERSION */
- uint32_t nr_progs;
- uint32_t reserved;
+ __u32 magic; /* SHIRUDO_UPATCH_PROG_MAGIC */
+ __u32 version; /* SHIRUDO_UPATCH_PROG_VERSION */
+ __u32 nr_progs;
+ __u32 reserved;
/*
* Then: nr_progs struct shirudo_upatch_prog_entry entries in
* libbpf/gen_loader program iteration order.
@@ -184,41 +185,41 @@ struct shirudo_upatch_prog_metadata {
* entries. It is required when any program has UPROBE_MULTI attach kind
* and absent otherwise. Each variable-length record is followed by
* path_len path bytes (without NUL), build_id_len build-ID bytes, and
- * nr_offsets little-endian uint64_t file offsets. Records have no padding.
+ * nr_offsets little-endian __u64 file offsets. Records have no padding.
*/
struct shirudo_upatch_uprobe_targets {
- uint32_t magic; /* SHIRUDO_UPATCH_UPROBE_TARGET_MAGIC */
- uint32_t version; /* SHIRUDO_UPATCH_UPROBE_TARGET_VERSION */
- uint32_t nr_targets;
- uint32_t reserved;
+ __u32 magic; /* SHIRUDO_UPATCH_UPROBE_TARGET_MAGIC */
+ __u32 version; /* SHIRUDO_UPATCH_UPROBE_TARGET_VERSION */
+ __u32 nr_targets;
+ __u32 reserved;
};
struct shirudo_upatch_uprobe_target {
- uint32_t prog_idx;
- uint32_t path_len;
- uint32_t build_id_len;
- uint32_t nr_offsets;
+ __u32 prog_idx;
+ __u32 path_len;
+ __u32 build_id_len;
+ __u32 nr_offsets;
};
-static inline uint16_t rd_le16(const void *p)
+static inline __u16 rd_le16(const void *p)
{
- uint16_t v;
+ __u16 v;
memcpy(&v, p, sizeof(v));
return le16toh(v);
}
-static inline uint32_t rd_le32(const void *p)
+static inline __u32 rd_le32(const void *p)
{
- uint32_t v;
+ __u32 v;
memcpy(&v, p, sizeof(v));
return le32toh(v);
}
-static inline uint64_t rd_le64(const void *p)
+static inline __u64 rd_le64(const void *p)
{
- uint64_t v;
+ __u64 v;
memcpy(&v, p, sizeof(v));
return le64toh(v);
diff --git a/testing/helper/test-bpf-load.c b/testing/helper/test-bpf-load.c
index 92e4927b..8ddfc2cc 100644
--- a/testing/helper/test-bpf-load.c
+++ b/testing/helper/test-bpf-load.c
@@ -10,13 +10,14 @@
*/
#include <errno.h>
-#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/bpf.h>
+#include <shirudo/misc.h>
+
int main(void)
{
/* r0 = 0; exit; -- a minimal valid program. */
@@ -29,8 +30,8 @@ int main(void)
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr.insn_cnt = 2;
- attr.insns = (uint64_t)(unsigned long)insns;
- attr.license = (uint64_t)(unsigned long)"GPL";
+ attr.insns = ptr_to_u64(insns);
+ attr.license = ptr_to_u64("GPL");
fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
if (fd >= 0) {
diff --git a/testing/helper/test-chain.c b/testing/helper/test-chain.c
index 525a909a..254cdb40 100644
--- a/testing/helper/test-chain.c
+++ b/testing/helper/test-chain.c
@@ -7,14 +7,15 @@
* so a shell test can assert on it.
*/
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "chain.h"
-static void puthex(const uint8_t *b, size_t n)
+static void puthex(const __u8 *b, size_t n)
{
char h[2 * 64 + 1];
@@ -28,7 +29,7 @@ int main(int argc, char **argv)
return 2;
if (strcmp(argv[1], "sha256") == 0 && argc == 3) {
- uint8_t d[32];
+ __u8 d[32];
shirudo_sha256(argv[2], strlen(argv[2]), d);
puthex(d, 32);
@@ -36,7 +37,7 @@ int main(int argc, char **argv)
}
if (strcmp(argv[1], "decode") == 0 && argc == 4) {
size_t n = (size_t)strtoul(argv[3], NULL, 10);
- uint8_t buf[256];
+ __u8 buf[256];
int rc;
if (n > sizeof(buf))
@@ -48,7 +49,7 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "cteq") == 0 && argc == 4) {
- uint8_t a[256], b[256];
+ __u8 a[256], b[256];
size_t na = strlen(argv[2]) / 2, nb = strlen(argv[3]) / 2;
if (na != nb || na > sizeof(a))
diff --git a/testing/helper/test-eventlog.c b/testing/helper/test-eventlog.c
index 82e40b10..97926ed3 100644
--- a/testing/helper/test-eventlog.c
+++ b/testing/helper/test-eventlog.c
@@ -14,6 +14,8 @@
#include <string.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "chain.h"
#include "eventlog.h"
#include "hex.h"
@@ -22,12 +24,12 @@
/* An init record of this boot with fixed values, bound where told. */
static int append_init(const char *path, const char *bound_in,
- unsigned int reset_count)
+ __u32 reset_count)
{
char buf[4096], ts[32], boot_id[64], nv_index[16], hex[65];
struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
char name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
- uint8_t zero[SHIRUDO_TPM_NAME_LEN] = {};
+ __u8 zero[SHIRUDO_TPM_NAME_LEN] = {};
shirudo_eventlog_timestamp(ts, sizeof(ts));
shirudo_eventlog_boot_id(boot_id, sizeof(boot_id));
@@ -60,7 +62,7 @@ static int append_init(const char *path, const char *bound_in,
static int count_lines(const char *line, size_t len, void *ctx)
{
- unsigned int *n = ctx;
+ __u32 *n = ctx;
(void)line;
(void)len;
@@ -79,11 +81,11 @@ int main(int argc, char **argv)
const char *path = strcmp(argv[2], "pending") == 0 ?
shirudo_eventlog_pending_path() :
shirudo_eventlog_path();
- unsigned int reset = 1;
+ __u32 reset = 1;
int rc;
if (argc == 5)
- reset = (unsigned int)strtoul(argv[4], NULL, 10);
+ reset = (__u32)strtoul(argv[4], NULL, 10);
rc = append_init(path, argv[3], reset);
printf("rc=%d\n", rc);
@@ -102,7 +104,7 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "lines") == 0) {
- unsigned int n = 0;
+ __u32 n = 0;
int rc = shirudo_eventlog_boot_lines(count_lines, &n);
printf("rc=%d\nlines=%u\n", rc, n);
diff --git a/testing/helper/test-exec-churn.c b/testing/helper/test-exec-churn.c
index 8d5feedb..d8db73a6 100644
--- a/testing/helper/test-exec-churn.c
+++ b/testing/helper/test-exec-churn.c
@@ -2,7 +2,6 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
-#include <inttypes.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
@@ -12,24 +11,26 @@
#include <time.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "io.h"
extern char **environ;
-static uint64_t monotonic_ns(void)
+static __u64 monotonic_ns(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
return 0;
- return (uint64_t)ts.tv_sec * UINT64_C(1000000000) +
- (uint64_t)ts.tv_nsec;
+ return (__u64)ts.tv_sec * 1000000000ULL +
+ (__u64)ts.tv_nsec;
}
-static int parse_positive(const char *text, unsigned long *value)
+static int parse_positive(const char *text, __u64 *value)
{
char *end;
- unsigned long parsed;
+ __u64 parsed;
errno = 0;
parsed = strtoul(text, &end, 10);
@@ -39,10 +40,10 @@ static int parse_positive(const char *text, unsigned long *value)
return 0;
}
-static int worker(uint64_t deadline, int fd)
+static int worker(__u64 deadline, int fd)
{
char *const argv[] = { (char *)"/bin/true", NULL };
- uint64_t completed = 0;
+ __u64 completed = 0;
for (;;) {
pid_t child;
@@ -75,8 +76,8 @@ static int worker(uint64_t deadline, int fd)
int main(int argc, char **argv)
{
- uint64_t start, deadline, elapsed, completed = 0;
- unsigned long duration_sec = 0, workers = 0;
+ __u64 start, deadline, elapsed, completed = 0;
+ __u64 duration_sec = 0, workers = 0;
int exit_status = 0;
int result_fd[2];
pid_t *children;
@@ -97,17 +98,17 @@ int main(int argc, char **argv)
if (!duration_sec || !workers || workers > 1024)
return 2;
start = monotonic_ns();
- if (!start || duration_sec > UINT64_MAX / UINT64_C(1000000000) ||
- start > UINT64_MAX - duration_sec * UINT64_C(1000000000))
+ if (!start || duration_sec > UINT64_MAX / 1000000000ULL ||
+ start > UINT64_MAX - duration_sec * 1000000000ULL)
return 1;
- deadline = start + duration_sec * UINT64_C(1000000000);
+ deadline = start + duration_sec * 1000000000ULL;
if (pipe(result_fd) != 0)
return 1;
children = calloc(workers, sizeof(*children));
if (!children)
return 1;
- for (unsigned long i = 0; i < workers; i++) {
+ for (__u64 i = 0; i < workers; i++) {
children[i] = fork();
if (children[i] == 0) {
close(result_fd[0]);
@@ -119,8 +120,8 @@ int main(int argc, char **argv)
}
}
close(result_fd[1]);
- for (unsigned long i = 0; i < workers; i++) {
- uint64_t count;
+ for (__u64 i = 0; i < workers; i++) {
+ __u64 count;
if (!children[i])
continue;
@@ -136,8 +137,8 @@ int main(int argc, char **argv)
if (exit_status)
return 1;
elapsed = monotonic_ns() - start;
- printf("execs=%" PRIu64 " elapsed_ns=%" PRIu64
- " execs_per_sec=%.3f workers=%lu\n", completed, elapsed,
+ printf("execs=%llu elapsed_ns=%llu"
+ " execs_per_sec=%.3f workers=%llu\n", completed, elapsed,
elapsed ? (double)completed * 1e9 / (double)elapsed : 0.0,
workers);
return 0;
diff --git a/testing/helper/test-json-parser.c b/testing/helper/test-json-parser.c
index 031a1d97..0d92d4e7 100644
--- a/testing/helper/test-json-parser.c
+++ b/testing/helper/test-json-parser.c
@@ -9,9 +9,10 @@
* arbitrary bytes -- it must never crash, only return a clean -errno.
*/
-#include <stdint.h>
#include <stdio.h>
+#include <shirudo/misc.h>
+
#include "json.h"
#include "macros.h"
@@ -45,7 +46,7 @@ int main(void)
{
char digest[65] = {}, type[64] = {};
static char buf[2 * 1024 * 1024];
- int64_t schema = -1;
+ __s64 schema = -1;
struct shirudo_json_array name_sink = { name_on_elem, NULL };
struct shirudo_json_field name_sub[] = {
{ "", SHIRUDO_JSON_T_STRING, name_scratch,
@@ -73,7 +74,7 @@ int main(void)
printf("rc=%d\n", rc);
if (rc == 0) {
printf("schema=%lld type=%s digest=%s\n",
- (long long)schema, type, digest);
+ (__s64)schema, type, digest);
printf("names=%d first_name=%s events=%d last_type=%s\n",
names_seen, first_name, events_seen, last_event_type);
}
diff --git a/testing/helper/test-kernel-build-id.c b/testing/helper/test-kernel-build-id.c
index 7b50c9a0..c56f5908 100644
--- a/testing/helper/test-kernel-build-id.c
+++ b/testing/helper/test-kernel-build-id.c
@@ -6,9 +6,11 @@
#include <stdio.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "util/kernel_build_id.h"
-static void put_u32(unsigned char *p, uint32_t value)
+static void put_u32(__u8 *p, __u32 value)
{
memcpy(p, &value, sizeof(value));
}
@@ -16,11 +18,11 @@ static void put_u32(unsigned char *p, uint32_t value)
int main(void)
{
/* One GNU build-id note with a four-byte descriptor: 01 23 45 67. */
- static const unsigned char notes[] = {
+ static const __u8 notes[] = {
4, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0,
'G', 'N', 'U', 0, 1, 0x23, 0x45, 0x67,
};
- unsigned char malformed[sizeof(notes)];
+ __u8 malformed[sizeof(notes)];
char build_id[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX];
if (shirudo_kernel_build_id_from_notes(notes, sizeof(notes), build_id) ||
diff --git a/testing/helper/test-keys.c b/testing/helper/test-keys.c
index b2774adb..ffebc176 100644
--- a/testing/helper/test-keys.c
+++ b/testing/helper/test-keys.c
@@ -12,12 +12,14 @@
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "crypto.h"
#include "hex.h"
#include "keys.h"
#include "upatch.h"
-static void puthex(const uint8_t *b, size_t n)
+static void puthex(const __u8 *b, size_t n)
{
char h[65];
@@ -28,7 +30,7 @@ static void puthex(const uint8_t *b, size_t n)
int main(int argc, char **argv)
{
struct shirudo_trusted_set set;
- uint8_t digest[32];
+ __u8 digest[32];
size_t i;
int rc;
diff --git a/testing/helper/test-otel-map.c b/testing/helper/test-otel-map.c
index a44691da..8e41d46c 100644
--- a/testing/helper/test-otel-map.c
+++ b/testing/helper/test-otel-map.c
@@ -4,6 +4,7 @@
#include <errno.h>
#include <endian.h>
#include <fcntl.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -15,6 +16,7 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include "json.h"
@@ -30,8 +32,8 @@ struct fixture {
struct shirudo_map_view view;
struct shirudo_map_entry entry;
struct btf *btf;
- uint32_t key;
- unsigned char values[16];
+ __u32 key;
+ __u8 values[16];
};
/* Build the smallest valid artifact carrying a decoder for offline tests. */
@@ -79,7 +81,7 @@ static int make_decoder(bool metric, bool counter, bool snapshot,
struct shirudo_upatch_decoder_entry entry = {};
struct shirudo_upatch_decoder_blob blob = {};
struct shirudo_upatch_header header = {};
- unsigned char *file;
+ __u8 *file;
char template[] = "/tmp/test-otel-map-decoder-XXXXXX";
int fd, rc;
@@ -146,11 +148,11 @@ static int make_decoder(bool metric, bool counter, bool snapshot,
return 0;
}
-static int fixture_init(struct fixture *f, uint32_t map_type, bool is_signed,
+static int fixture_init(struct fixture *f, __u32 map_type, bool is_signed,
bool with_btf)
{
int key_int, value_int, key_type, value_type;
- uint32_t cpu0 = 5, cpu1 = 7, poison = 0xdeadbeef;
+ __u32 cpu0 = 5, cpu1 = 7, poison = 0xdeadbeef;
memset(f, 0, sizeof(*f));
snprintf(f->view.upatch, sizeof(f->view.upatch), "test-upatch");
@@ -188,8 +190,8 @@ static int fixture_init(struct fixture *f, uint32_t map_type, bool is_signed,
if (key_int < 0 || value_int < 0 || key_type < 0 || value_type < 0)
return -EINVAL;
f->view.btf = f->btf;
- f->view.key_type_id = (uint32_t)key_type;
- f->view.value_type_id = (uint32_t)value_type;
+ f->view.key_type_id = (__u32)key_type;
+ f->view.value_type_id = (__u32)value_type;
snprintf(f->view.btf_key_type, sizeof(f->view.btf_key_type), "__u32");
snprintf(f->view.btf_value_type, sizeof(f->view.btf_value_type), "%s",
is_signed ? "__s32" : "__u32");
@@ -201,7 +203,7 @@ static void fixture_destroy(struct fixture *f)
btf__free(f->btf);
}
-static int print_classification(uint32_t type, bool is_signed, bool with_btf,
+static int print_classification(__u32 type, bool is_signed, bool with_btf,
bool serialized)
{
struct fixture f;
@@ -279,7 +281,7 @@ static int print_local_json(void)
return e.err != 0;
}
-static int send_log(const char *url, unsigned int retries, bool insecure)
+static int send_log(const char *url, __u32 retries, bool insecure)
{
struct shirudo_otel_opts opts = {
.endpoint = url,
@@ -351,17 +353,17 @@ static int send_metric(const char *url)
static bool large_serialized_fixture(struct fixture *f)
{
- const uint32_t pages = 1;
- const uint32_t bytes = 1024;
+ const __u32 pages = 1;
+ const __u32 bytes = 1024;
f->view.serialized = true;
f->view.n_entries = 0;
f->view.n_serial_pages = pages;
- f->view.serial_bytes_total = (uint64_t)pages * bytes;
+ f->view.serial_bytes_total = (__u64)pages * bytes;
f->view.serial_pages = xcalloc(pages, sizeof(*f->view.serial_pages));
if (!f->view.serial_pages)
return false;
- for (uint32_t i = 0; i < pages; i++) {
+ for (__u32 i = 0; i < pages; i++) {
struct shirudo_serial_page *page = &f->view.serial_pages[i];
page->page_index = i;
@@ -383,7 +385,7 @@ static void large_serialized_destroy(struct fixture *f)
{
if (!f->view.serial_pages)
return;
- for (uint32_t i = 0; i < f->view.n_serial_pages; i++)
+ for (__u32 i = 0; i < f->view.n_serial_pages; i++)
xfree(f->view.serial_pages[i].data,
f->view.serial_pages[i].bytes);
xfree(f->view.serial_pages,
@@ -391,7 +393,7 @@ static void large_serialized_destroy(struct fixture *f)
f->view.serial_pages = NULL;
}
-static int send_s3(const char *endpoint, unsigned int retries, bool large)
+static int send_s3(const char *endpoint, __u32 retries, bool large)
{
struct shirudo_otel_opts opts = {
.s3_uri = "s3://test-bucket/prefix",
@@ -562,10 +564,10 @@ int main(int argc, char **argv)
if (strcmp(argv[1], "local-json") == 0)
return print_local_json();
if (strcmp(argv[1], "send-log") == 0 && argc == 4)
- return send_log(argv[2], (unsigned int)strtoul(argv[3], NULL, 10),
+ return send_log(argv[2], (__u32)strtoul(argv[3], NULL, 10),
false);
if (strcmp(argv[1], "send-log-insecure") == 0 && argc == 4)
- return send_log(argv[2], (unsigned int)strtoul(argv[3], NULL, 10),
+ return send_log(argv[2], (__u32)strtoul(argv[3], NULL, 10),
true);
if (strcmp(argv[1], "send-auto-log") == 0 && argc == 3)
return send_auto(argv[2], true);
@@ -575,10 +577,10 @@ int main(int argc, char **argv)
return send_metric(argv[2]);
if (strcmp(argv[1], "send-s3") == 0 && argc == 4)
return send_s3(argv[2],
- (unsigned int)strtoul(argv[3], NULL, 10), false);
+ (__u32)strtoul(argv[3], NULL, 10), false);
if (strcmp(argv[1], "send-s3-large") == 0 && argc == 4)
return send_s3(argv[2],
- (unsigned int)strtoul(argv[3], NULL, 10), true);
+ (__u32)strtoul(argv[3], NULL, 10), true);
if (strcmp(argv[1], "s3-validation") == 0)
return s3_validation();
if (strcmp(argv[1], "tenant-validation") == 0)
diff --git a/testing/helper/test-register.c b/testing/helper/test-register.c
index a77a54ac..4d4152ef 100644
--- a/testing/helper/test-register.c
+++ b/testing/helper/test-register.c
@@ -12,6 +12,8 @@
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "attest.h"
#include "chain.h"
#include "config.h"
@@ -22,7 +24,7 @@
#include "paths.h"
#include "tpm.h"
-static void puthex(const uint8_t *b, size_t n)
+static void puthex(const __u8 *b, size_t n)
{
char h[2 * 1024 + 1];
@@ -33,7 +35,7 @@ static void puthex(const uint8_t *b, size_t n)
}
/* Record an init the way shirudo init does, minus the set details. */
-static int append_init(const uint8_t event[32],
+static int append_init(const __u8 event[32],
const struct shirudo_chain_init_info *info)
{
char buf[4096], ts[32], boot_id[64], nv_index[16], hex[256];
@@ -73,7 +75,7 @@ static int append_init(const uint8_t event[32],
}
static int append_event(const char *type, const char *name,
- const uint8_t event[32], const uint8_t chain[32])
+ const __u8 event[32], const __u8 chain[32])
{
char buf[4096], ts[32], boot_id[64], nv_index[16], hex[65];
struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
@@ -103,7 +105,7 @@ static int append_event(const char *type, const char *name,
int main(int argc, char **argv)
{
- uint8_t event[32], chain[32];
+ __u8 event[32], chain[32];
int rc;
if (argc < 2)
@@ -135,7 +137,7 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "load") == 0 && argc == 4) {
- uint8_t payload[32];
+ __u8 payload[32];
if (shirudo_hex_decode(argv[3], payload, 32))
return 2;
@@ -187,22 +189,22 @@ int main(int argc, char **argv)
/* A measurement into a PCR, as systemd's boot phases make them. */
if (strcmp(argv[1], "pcr-extend") == 0 && argc == 4) {
struct shirudo_tpm *tpm = NULL;
- uint8_t d[32];
+ __u8 d[32];
if (shirudo_hex_decode(argv[3], d, 32) || shirudo_tpm_open(&tpm))
return 2;
- rc = shirudo_tpm_pcr_extend(tpm, (unsigned int)atoi(argv[2]), d);
+ rc = shirudo_tpm_pcr_extend(tpm, (__u32)atoi(argv[2]), d);
shirudo_tpm_close(tpm);
printf("rc=%d\n", rc);
return rc ? 1 : 0;
}
if (strcmp(argv[1], "pcr") == 0 && argc == 3) {
struct shirudo_tpm *tpm = NULL;
- uint8_t v[32];
+ __u8 v[32];
if (shirudo_tpm_open(&tpm))
return 1;
- rc = shirudo_tpm_pcr_read(tpm, (unsigned int)atoi(argv[2]), v);
+ rc = shirudo_tpm_pcr_read(tpm, (__u32)atoi(argv[2]), v);
shirudo_tpm_close(tpm);
if (rc)
return 1;
@@ -258,10 +260,10 @@ int main(int argc, char **argv)
}
/* The attack: try the init branch again after the anchor. */
if (strcmp(argv[1], "first-write") == 0 && argc == 3) {
- uint8_t branch_written[32], branch_init[32], policy[32];
+ __u8 branch_written[32], branch_init[32], policy[32];
struct shirudo_eventlog_init rec;
struct shirudo_tpm *tpm = NULL;
- uint8_t values[64];
+ __u8 values[64];
if (shirudo_hex_decode(argv[2], event, 32) ||
shirudo_eventlog_boot_init(&rec))
@@ -283,7 +285,7 @@ int main(int argc, char **argv)
}
if (strcmp(argv[1], "evidence") == 0 && (argc == 3 || argc == 4)) {
struct shirudo_attest_challenge challenge = {};
- uint8_t nonce[64];
+ __u8 nonce[64];
size_t nonce_len = strlen(argv[2]) / 2, len, cap;
char *doc;
diff --git a/testing/helper/test-signed-load.c b/testing/helper/test-signed-load.c
index 0691e427..453464b5 100644
--- a/testing/helper/test-signed-load.c
+++ b/testing/helper/test-signed-load.c
@@ -29,6 +29,8 @@
#include <linux/bpf.h>
#include <linux/keyctl.h>
+#include <shirudo/misc.h>
+
#include "shirudo/upatch.h"
static long sys_bpf(int cmd, union bpf_attr *a)
@@ -39,7 +41,7 @@ static long sys_bpf(int cmd, union bpf_attr *a)
int main(int argc, char **argv)
{
const struct shirudo_upatch_header *h;
- unsigned char *m, cert[16384]; /* an ML-DSA-87 cert is ~7.5 KiB */
+ __u8 *m, cert[16384]; /* an ML-DSA-87 cert is ~7.5 KiB */
const void *insns, *sig;
int ufd, cfd, cn, prog_fd;
long serial;
@@ -93,12 +95,12 @@ int main(int argc, char **argv)
memset(&a, 0, sizeof(a));
a.prog_type = BPF_PROG_TYPE_SYSCALL;
a.insn_cnt = rd_le32(&h->loader_insns_len) / sizeof(struct bpf_insn);
- a.insns = (uint64_t)(uintptr_t)insns;
- a.license = (uint64_t)(uintptr_t)"Dual BSD/GPL";
+ a.insns = (__u64)(uintptr_t)insns;
+ a.license = (__u64)(uintptr_t)"Dual BSD/GPL";
a.prog_flags = BPF_F_SLEEPABLE;
- a.signature = (uint64_t)(uintptr_t)sig;
+ a.signature = (__u64)(uintptr_t)sig;
a.signature_size = rd_le32(&h->signature_len);
- a.keyring_id = (int32_t)serial;
+ a.keyring_id = (__s32)serial;
memcpy(a.prog_name, "sigtest", 7);
prog_fd = sys_bpf(BPF_PROG_LOAD, &a);
if (prog_fd >= 0) {
diff --git a/testing/helper/test-tpm-wire.c b/testing/helper/test-tpm-wire.c
index 19f8a7c4..b0496a1a 100644
--- a/testing/helper/test-tpm-wire.c
+++ b/testing/helper/test-tpm-wire.c
@@ -6,15 +6,16 @@
* structure parsing. Subcommand on argv[1]; hex inputs as argv.
*/
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "hex.h"
#include "tpm_wire.h"
-static void puthex(const uint8_t *b, size_t n)
+static void puthex(const __u8 *b, size_t n)
{
char h[2 * 2048 + 1];
@@ -24,7 +25,7 @@ static void puthex(const uint8_t *b, size_t n)
printf("%s\n", h);
}
-static int gethex(const char *hex, uint8_t *out, size_t cap, size_t *len)
+static int gethex(const char *hex, __u8 *out, size_t cap, size_t *len)
{
size_t n = strlen(hex);
@@ -36,7 +37,7 @@ static int gethex(const char *hex, uint8_t *out, size_t cap, size_t *len)
int main(int argc, char **argv)
{
- uint8_t a[32], b[32], out[32], buf[2048];
+ __u8 a[32], b[32], out[32], buf[2048];
size_t len;
if (argc < 2)
@@ -52,7 +53,7 @@ int main(int argc, char **argv)
if (shirudo_hex_decode(argv[2], a, 32) ||
shirudo_hex_decode(argv[4], b, 32))
return 2;
- shirudo_tpm_policy_pcr(a, (uint32_t)strtoul(argv[3], NULL, 0),
+ shirudo_tpm_policy_pcr(a, (__u32)strtoul(argv[3], NULL, 0),
b, out);
puthex(out, 32);
return 0;
@@ -66,12 +67,12 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "register-policy") == 0 && argc == 5) {
- uint8_t w[32], i[32], p[32], values[64];
+ __u8 w[32], i[32], p[32], values[64];
if (shirudo_hex_decode(argv[3], values, 32) ||
shirudo_hex_decode(argv[4], values + 32, 32))
return 2;
- shirudo_tpm_register_policy((uint32_t)strtoul(argv[2], NULL, 0),
+ shirudo_tpm_register_policy((__u32)strtoul(argv[2], NULL, 0),
values, 2, w, i, p);
puthex(w, 32);
puthex(i, 32);
@@ -79,18 +80,18 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "nv-name") == 0 && argc == 5) {
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
if (shirudo_hex_decode(argv[4], a, 32))
return 2;
- shirudo_tpm_nv_name((uint32_t)strtoul(argv[2], NULL, 0),
- (uint32_t)strtoul(argv[3], NULL, 0), a, 32,
+ shirudo_tpm_nv_name((__u32)strtoul(argv[2], NULL, 0),
+ (__u32)strtoul(argv[3], NULL, 0), a, 32,
name);
puthex(name, sizeof(name));
return 0;
}
if (strcmp(argv[1], "object-name") == 0 && argc == 3) {
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
if (gethex(argv[2], buf, sizeof(buf), &len) ||
shirudo_tpm_object_name(buf, len, name))
@@ -116,7 +117,7 @@ int main(int argc, char **argv)
}
if (strcmp(argv[1], "public") == 0 && argc == 3) {
struct shirudo_tpm_public pub;
- uint8_t again[512];
+ __u8 again[512];
size_t again_len = 0;
int rc;
@@ -139,7 +140,7 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "kdfa") == 0 && argc == 6) {
- uint8_t key[64], ctx[64], res[64];
+ __u8 key[64], ctx[64], res[64];
size_t key_len, ctx_len, res_len = (size_t)atoi(argv[5]);
if (gethex(argv[2], key, sizeof(key), &key_len) ||
@@ -153,7 +154,7 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "kdfe") == 0 && argc == 6) {
- uint8_t z[32], u[64], v[64], res[32];
+ __u8 z[32], u[64], v[64], res[32];
size_t z_len, u_len, v_len;
if (gethex(argv[2], z, sizeof(z), &z_len) || z_len != 32 ||
@@ -169,8 +170,8 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "credential") == 0 && argc == 5) {
- uint8_t seed[32], name[SHIRUDO_TPM_NAME_LEN], cred[32];
- uint8_t blob[SHIRUDO_TPM_ID_OBJECT_LEN];
+ __u8 seed[32], name[SHIRUDO_TPM_NAME_LEN], cred[32];
+ __u8 blob[SHIRUDO_TPM_ID_OBJECT_LEN];
size_t n;
if (gethex(argv[2], seed, sizeof(seed), &n) || n != 32 ||
@@ -184,7 +185,7 @@ int main(int argc, char **argv)
return 0;
}
if (strcmp(argv[1], "signature") == 0 && argc == 3) {
- uint8_t r[32], s[32];
+ __u8 r[32], s[32];
int rc;
if (gethex(argv[2], buf, sizeof(buf), &len))
diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.c
index 3af0c679..f084f841 100644
--- a/testing/helper/test-upatch-prog-metadata.c
+++ b/testing/helper/test-upatch-prog-metadata.c
@@ -6,10 +6,13 @@
*/
#include <endian.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "upatch.h"
static int fail(const char *msg)
@@ -18,12 +21,12 @@ static int fail(const char *msg)
return 1;
}
-static int expect_attach(const struct shirudo_upatch *u, uint32_t kind,
- const char *target, uint64_t offset)
+static int expect_attach(const struct shirudo_upatch *u, __u32 kind,
+ const char *target, __u64 offset)
{
struct shirudo_upatch_prog_metadata md;
struct shirudo_upatch_prog_entry prog;
- const unsigned char *raw;
+ const __u8 *raw;
if (u->hdr.nr_progs != 1)
return fail("unexpected metadata prog count");
@@ -62,7 +65,7 @@ static int expect_serializer(const struct shirudo_upatch *u)
{
struct shirudo_upatch_prog_metadata md;
struct shirudo_upatch_prog_entry prog;
- const unsigned char *raw;
+ const __u8 *raw;
if (u->hdr.nr_maps != 3 || u->hdr.nr_progs != 1)
return fail("unexpected map/prog counts");
@@ -88,15 +91,15 @@ static int expect_serializer(const struct shirudo_upatch *u)
static int expect_uprobe_multi(const struct shirudo_upatch *u,
const char *path, const char *pattern,
- uint32_t expected_offsets)
+ __u32 expected_offsets)
{
struct shirudo_upatch_uprobe_targets targets;
struct shirudo_upatch_uprobe_target record;
struct shirudo_upatch_prog_metadata md;
struct shirudo_upatch_prog_entry prog;
- const unsigned char *raw, *cursor, *end;
- uint32_t path_len, build_id_len, nr_offsets, i;
- uint64_t previous = 0;
+ const __u8 *raw, *cursor, *end;
+ __u32 path_len, build_id_len, nr_offsets, i;
+ __u64 previous = 0;
if (u->hdr.nr_progs != 1)
return fail("unexpected metadata prog count");
@@ -134,12 +137,12 @@ static int expect_uprobe_multi(const struct shirudo_upatch *u,
nr_offsets != expected_offsets)
return fail("uprobe target record mismatch");
if ((size_t)(end - cursor) != (size_t)path_len + build_id_len +
- (size_t)nr_offsets * sizeof(uint64_t) ||
+ (size_t)nr_offsets * sizeof(__u64) ||
memcmp(cursor, path, path_len) != 0)
return fail("uprobe path or metadata length mismatch");
cursor += path_len + build_id_len;
for (i = 0; i < nr_offsets; i++) {
- uint64_t offset;
+ __u64 offset;
memcpy(&offset, cursor, sizeof(offset));
cursor += sizeof(offset);
@@ -188,13 +191,13 @@ int main(int argc, char **argv)
NULL, 0);
} else if (strcmp(mode, "uprobe-multi") == 0 && argc == 6) {
char *end = NULL;
- unsigned long count = strtoul(argv[5], &end, 10);
+ __u64 count = strtoul(argv[5], &end, 10);
if (!end || *end || count > UINT32_MAX)
rc = fail("invalid expected uprobe offset count");
else
rc = expect_uprobe_multi(&u, argv[3], argv[4],
- (uint32_t)count);
+ (__u32)count);
} else {
rc = fail("unknown metadata check mode");
}
diff --git a/testing/helper/test-upatch-serializer-pages.c b/testing/helper/test-upatch-serializer-pages.c
index 90f9779e..cd1e69f5 100644
--- a/testing/helper/test-upatch-serializer-pages.c
+++ b/testing/helper/test-upatch-serializer-pages.c
@@ -11,6 +11,8 @@
#include <string.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "json.h"
#include "serializer.h"
#include "upatch_maps.h"
@@ -29,7 +31,7 @@ static int has(const char *haystack, const char *needle)
static int check_abi_and_nonce(void)
{
- uint64_t generated = 0;
+ __u64 generated = 0;
struct shirudo_serial_chunk chunk = {
.magic = SHIRUDO_SERIAL_MAGIC,
.version = SHIRUDO_SERIAL_VERSION,
@@ -107,8 +109,8 @@ out:
int main(void)
{
- unsigned char page0_data[] = { 0xaa, 0xbb };
- unsigned char page1_data[] = { 0xcc };
+ __u8 page0_data[] = { 0xaa, 0xbb };
+ __u8 page1_data[] = { 0xcc };
struct shirudo_serial_page pages[2] = {
{
.page_index = 0,
diff --git a/testing/helper/tpmsim-cmd.c b/testing/helper/tpmsim-cmd.c
index e8c27996..13c518b0 100644
--- a/testing/helper/tpmsim-cmd.c
+++ b/testing/helper/tpmsim-cmd.c
@@ -8,7 +8,6 @@
#include <errno.h>
#include <fcntl.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,17 +17,19 @@
#include <libtpms/tpm_error.h>
#include <libtpms/tpm_library.h>
+#include <shirudo/misc.h>
+
#define SIM_CMD_MAX (64u * 1024u)
/* TPM2_Startup(TPM_SU_CLEAR): sent when no volatile state was restored. */
-static const unsigned char startup_clear[] = {
+static const __u8 startup_clear[] = {
0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x44, 0x00, 0x00,
};
static int read_full(int fd, void *buf, size_t n)
{
- unsigned char *p = buf;
size_t off = 0;
+ __u8 *p = buf;
while (off < n) {
ssize_t got = read(fd, p + off, n - off);
@@ -44,7 +45,7 @@ static int read_full(int fd, void *buf, size_t n)
static int write_full(int fd, const void *buf, size_t n)
{
- const unsigned char *p = buf;
+ const __u8 *p = buf;
size_t off = 0;
while (off < n) {
@@ -59,10 +60,10 @@ static int write_full(int fd, const void *buf, size_t n)
return 0;
}
-static int load_blob(const char *path, unsigned char **out, uint32_t *len)
+static int load_blob(const char *path, __u8 **out, __u32 *len)
{
- unsigned char *buf;
struct stat st;
+ __u8 *buf;
int fd;
*out = NULL;
@@ -83,12 +84,12 @@ static int load_blob(const char *path, unsigned char **out, uint32_t *len)
}
close(fd);
*out = buf;
- *len = (uint32_t)st.st_size;
+ *len = (__u32)st.st_size;
return 0;
}
-static int store_blob(const char *path, const unsigned char *buf,
- uint32_t len)
+static int store_blob(const char *path, const __u8 *buf,
+ __u32 len)
{
char tmp[4096 + 8];
int fd, rc;
@@ -105,11 +106,11 @@ static int store_blob(const char *path, const unsigned char *buf,
return rc;
}
-static int process(unsigned char **resp, uint32_t *resp_size,
- uint32_t *resp_cap, const unsigned char *cmd, uint32_t len)
+static int process(__u8 **resp, __u32 *resp_size,
+ __u32 *resp_cap, const __u8 *cmd, __u32 len)
{
TPM_RESULT r = TPMLIB_Process(resp, resp_size, resp_cap,
- (unsigned char *)cmd, len);
+ (__u8 *)cmd, len);
return r == TPM_SUCCESS ? 0 : -1;
}
@@ -128,8 +129,8 @@ static int save_state(const char *dir)
int rc = 0;
for (i = 0; i < sizeof(blobs) / sizeof(blobs[0]); i++) {
- unsigned char *blob = NULL;
- uint32_t blob_len = 0;
+ __u32 blob_len = 0;
+ __u8 *blob = NULL;
if (TPMLIB_GetState(blobs[i].type, &blob, &blob_len) !=
TPM_SUCCESS || !blob) {
@@ -146,8 +147,8 @@ static int save_state(const char *dir)
int main(int argc, char **argv)
{
- unsigned char *resp = NULL, *permanent = NULL, *vol = NULL;
- uint32_t resp_size = 0, resp_cap = 0, permanent_len, vol_len;
+ __u8 *resp = NULL, *permanent = NULL, *vol = NULL;
+ __u32 resp_size = 0, resp_cap = 0, permanent_len, vol_len;
char path[4096];
int rc = 0;
@@ -191,13 +192,13 @@ int main(int argc, char **argv)
}
for (;;) {
- unsigned char cmd[SIM_CMD_MAX];
- uint32_t len;
+ __u8 cmd[SIM_CMD_MAX];
+ __u32 len;
if (read_full(STDIN_FILENO, cmd, 10) != 0)
break;
- len = ((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) |
- ((uint32_t)cmd[4] << 8) | cmd[5];
+ len = ((__u32)cmd[2] << 24) | ((__u32)cmd[3] << 16) |
+ ((__u32)cmd[4] << 8) | cmd[5];
if (len < 10 || len > sizeof(cmd) ||
(len > 10 && read_full(STDIN_FILENO, cmd + 10,
len - 10) != 0)) {
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index d070f36a..2d599233 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -22,6 +22,8 @@
#include <unistd.h>
#include <zlib.h>
+#include <shirudo/misc.h>
+
#include "bpm.h"
#include "compile.h"
#include "crypto.h"
@@ -57,10 +59,10 @@ static char *build_trim(char *s)
{
char *end;
- while (isspace((unsigned char)*s))
+ while (isspace((__u8)*s))
s++;
end = s + strlen(s);
- while (end > s && isspace((unsigned char)end[-1]))
+ while (end > s && isspace((__u8)end[-1]))
*--end = '\0';
return s;
}
@@ -93,11 +95,11 @@ static void build_strip_comment(char *s)
}
}
-static int build_read_file(const char *path, unsigned char **out, size_t *len,
+static int build_read_file(const char *path, __u8 **out, size_t *len,
size_t max)
{
struct stat st;
- unsigned char *body;
+ __u8 *body;
ssize_t n;
int fd;
@@ -141,16 +143,16 @@ static int build_parse_string(const char *value, char *out, size_t cap)
char quote;
size_t n = 0;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (*p != '"' && *p != '\'')
return -EINVAL;
quote = *p++;
while (*p && *p != quote) {
- unsigned char c = (unsigned char)*p++;
+ __u8 c = (__u8)*p++;
if (c == '\\' && quote == '"') {
- c = (unsigned char)*p++;
+ c = (__u8)*p++;
switch (c) {
case 'n':
c = '\n';
@@ -175,7 +177,7 @@ static int build_parse_string(const char *value, char *out, size_t cap)
if (*p != quote)
return -EINVAL;
p++;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (*p)
return -EINVAL;
@@ -188,14 +190,14 @@ static int build_parse_value(const char *value, char *out, size_t cap,
{
const char *p = value;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (array_ok && *p == '[') {
const char *end = strrchr(p, ']');
if (!end)
return -EINVAL;
- while (isspace((unsigned char)end[1]))
+ while (isspace((__u8)end[1]))
end++;
return end[1] ? -EINVAL : 0;
}
@@ -215,7 +217,7 @@ static int build_parse_string_array(const char *value,
const char *p = value;
int count = 0;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (*p++ != '[')
return -EINVAL;
@@ -223,7 +225,7 @@ static int build_parse_string_array(const char *value,
const char *start, *end;
char quote;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (*p == ']')
break;
@@ -248,7 +250,7 @@ static int build_parse_string_array(const char *value,
if (build_parse_string(item, out[count], SHIRUDO_TREE_STRING_MAX))
return -EINVAL;
count++;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (*p == ',') {
p++;
@@ -258,7 +260,7 @@ static int build_parse_string_array(const char *value,
return -EINVAL;
}
p++;
- while (isspace((unsigned char)*p))
+ while (isspace((__u8)*p))
p++;
if (*p || !count)
return -EINVAL;
@@ -300,9 +302,9 @@ static int build_parse_selector_kv(const char *key, const char *value,
static bool build_name_ok(const char *s)
{
- const unsigned char *p = (const unsigned char *)s;
+ const __u8 *p = (const __u8 *)s;
- if (!s || !*s || !isalnum((unsigned char)s[0]) || strlen(s) >= 128)
+ if (!s || !*s || !isalnum((__u8)s[0]) || strlen(s) >= 128)
return false;
for (; *p; p++) {
if (!(isalnum(*p) || *p == '.' || *p == '_' || *p == '-'))
@@ -313,9 +315,9 @@ static bool build_name_ok(const char *s)
static bool build_version_ok(const char *s)
{
- const unsigned char *p = (const unsigned char *)s;
+ const __u8 *p = (const __u8 *)s;
- if (!s || !*s || !isalnum((unsigned char)s[0]) || strlen(s) >= 128)
+ if (!s || !*s || !isalnum((__u8)s[0]) || strlen(s) >= 128)
return false;
for (; *p; p++) {
if (!(isalnum(*p) || *p == '.' || *p == '_' || *p == '-' || *p == '+'))
@@ -554,7 +556,7 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
if (!u->type[0])
build_copy(u->type, sizeof(u->type), "bpf");
for (p = u->type; *p; p++)
- *p = (char)tolower((unsigned char)*p);
+ *p = (char)tolower((__u8)*p);
if (strcmp(u->type, "bpf") || build_default_targets(u))
return -EINVAL;
if (!u->tree.present)
@@ -574,10 +576,10 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
return 0;
}
-int bpm_build_parse_beefile(const unsigned char *body, size_t len,
+int bpm_build_parse_beefile(const __u8 *body, size_t len,
struct bpm_build_spec *spec)
{
- unsigned char *scratch;
+ __u8 *scratch;
int rc;
if (!body || !spec || memchr(body, '\0', len))
@@ -793,9 +795,9 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
for (size_t i = 0; i < spec->n_upatches; i++) {
char source[PATH_MAX], load[PATH_MAX], unload[PATH_MAX];
char object[PATH_MAX], name[SHIRUDO_UPATCH_NAME_MAX];
- unsigned char payload[SHIRUDO_CRYPTO_SHA256_LEN];
struct bpm_build_upatch *u = &spec->upatches[i];
const char *bpf_o = source, *src_path = NULL;
+ __u8 payload[SHIRUDO_CRYPTO_SHA256_LEN];
struct shirudo_upatch parsed = {};
int rc;
@@ -864,7 +866,7 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
static int build_environment(const struct bpm_build_spec *spec,
const char *version, const char *fingerprint,
- unsigned char **out, size_t *out_len)
+ __u8 **out, size_t *out_len)
{
char *body = xmalloc(BPM_BUILD_MAX_JSON);
struct shirudo_json_emit e = { .buf = body, .cap = BPM_BUILD_MAX_JSON };
@@ -922,7 +924,7 @@ static int build_environment(const struct bpm_build_spec *spec,
xfree(body, BPM_BUILD_MAX_JSON);
return e.err;
}
- *out = (unsigned char *)body;
+ *out = (__u8 *)body;
*out_len = e.len;
return 0;
}
@@ -935,26 +937,26 @@ static int tar_write(struct bpm_tar *tar, const void *data, size_t len)
{
if (!len)
return 0;
- return gzwrite(tar->gz, data, (unsigned int)len) == (int)len ? 0 : -EIO;
+ return gzwrite(tar->gz, data, (__u32)len) == (int)len ? 0 : -EIO;
}
static int tar_entry(struct bpm_tar *tar, const char *name, const void *data,
size_t len, mode_t mode, char type)
{
- unsigned char h[512] = {};
- unsigned int sum = 0;
- unsigned char zeros[512] = {};
+ __u8 h[512] = {};
+ __u32 sum = 0;
+ __u8 zeros[512] = {};
if (strlen(name) >= 100 || (type == '0' && len > 077777777777ULL))
return -ENAMETOOLONG;
memcpy(h, name, strlen(name));
- snprintf((char *)h + 100, 8, "%07o", (unsigned int)(mode & 07777));
+ snprintf((char *)h + 100, 8, "%07o", (__u32)(mode & 07777));
snprintf((char *)h + 108, 8, "%07o", 0u);
snprintf((char *)h + 116, 8, "%07o", 0u);
- snprintf((char *)h + 124, 12, "%011o", (unsigned int)len);
+ snprintf((char *)h + 124, 12, "%011o", (__u32)len);
snprintf((char *)h + 136, 12, "%011o", 0u);
memset(h + 148, ' ', 8);
- h[156] = (unsigned char)type;
+ h[156] = (__u8)type;
memcpy(h + 257, "ustar\0", 6);
memcpy(h + 263, "00", 2);
for (size_t i = 0; i < sizeof(h); i++)
@@ -969,7 +971,7 @@ static int tar_entry(struct bpm_tar *tar, const char *name, const void *data,
static int tar_path(struct bpm_tar *tar, const char *name, const char *path)
{
- unsigned char *body;
+ __u8 *body;
size_t len;
struct stat st;
int rc;
@@ -991,8 +993,8 @@ static int build_archive(const struct bpm_build_spec *spec, const char *version,
const char *out_dir)
{
char output[PATH_MAX], top[PATH_MAX], name[PATH_MAX];
- unsigned char *environment = NULL, *cert = NULL;
size_t environment_len = 0, cert_len = 0;
+ __u8 *environment = NULL, *cert = NULL;
struct bpm_tar tar;
int rc;
@@ -1075,7 +1077,7 @@ static int build_archive(const struct bpm_build_spec *spec, const char *version,
rc = tar_entry(&tar, name, environment, environment_len, 0644, '0');
}
if (!rc) {
- unsigned char end[1024] = {};
+ __u8 end[1024] = {};
rc = tar_write(&tar, end, sizeof(end));
}
@@ -1123,13 +1125,13 @@ static int shirudo_build_bpm_build(int argc, char **argv)
char build_file[PATH_MAX], resolved_file[PATH_MAX], keys_dir[PATH_MAX];
char keys_abs[PATH_MAX], pkey[PATH_MAX], cert_path[PATH_MAX];
char fingerprint[SHIRUDO_CRYPTO_SHA256_LEN * 2 + 1];
- unsigned char digest[SHIRUDO_CRYPTO_SHA256_LEN];
+ __u8 digest[SHIRUDO_CRYPTO_SHA256_LEN];
struct shirudo_cert cert = {};
struct bpm_build_spec *spec;
- unsigned char *body = NULL;
int positional = 0, rc;
char out_dir[PATH_MAX];
size_t body_len = 0;
+ __u8 *body = NULL;
for (int i = 1; i < argc; i++) {
if (shirudo_build_is_help(argv[i])) {
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 015ca61d..fbaaf561 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -9,7 +9,6 @@
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,6 +17,8 @@
#include <unistd.h>
#include <curl/curl.h>
+#include <shirudo/misc.h>
+
#include "bpm.h"
#include "bpm_oci.h"
#include "bpm_package.h"
@@ -65,7 +66,7 @@ static int publish_fail(const char *action, int rc)
*/
static int sign_detached(const void *payload, size_t len,
const char *key, const char *cert,
- unsigned char **sig, size_t *sig_len)
+ __u8 **sig, size_t *sig_len)
{
struct shirudo_sign_io io = {
.insns = payload, .insns_sz = len, .signature_sz = BPM_SIGN_MAX,
@@ -85,7 +86,7 @@ static int sign_detached(const void *payload, size_t len,
return 0;
}
-static void sig_free(unsigned char *sig)
+static void sig_free(__u8 *sig)
{
if (sig)
xfree(sig, BPM_SIGN_MAX);
@@ -147,7 +148,7 @@ static int fetch_index(struct bpm_oci_client *c, const char *cert,
rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
if (rc)
goto out;
- if ((layers[i].size >= 0 && (int64_t)dst->len != layers[i].size) ||
+ if ((layers[i].size >= 0 && (__s64)dst->len != layers[i].size) ||
bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||
!bpm_digest_matches(layers[i].digest, hex)) {
rc = -EKEYREJECTED;
@@ -192,9 +193,9 @@ static int push_index(struct bpm_oci_client *c, const char *repository,
struct shirudo_json_emit e = {};
char timestamp[32], digest[72];
char empty_config[] = "{}";
- unsigned char *sig = NULL;
size_t cap, sig_len = 0;
time_t now = time(NULL);
+ __u8 *sig = NULL;
int n = 0, rc;
entries = xcalloc(BPM_MAX_ENTRIES, sizeof(*entries));
@@ -303,7 +304,7 @@ static int push_index(struct bpm_oci_client *c, const char *repository,
goto out;
}
rc = bpm_oci_put_manifest(c, BPM_INDEX_REPOSITORY, BPM_INDEX_TAG,
- (const unsigned char *)manifest, e.len, digest,
+ (const __u8 *)manifest, e.len, digest,
sizeof(digest));
out:
if (manifest)
@@ -323,7 +324,7 @@ static int push_package(const char *package_path, const char *raw_repository,
struct bpm_package_info info = {};
struct bpm_descriptor config, layer, key_config, key_layer, subject;
struct shirudo_json_emit e = {};
- unsigned char *archive = NULL, *manifest = NULL;
+ __u8 *archive = NULL, *manifest = NULL;
const char *base;
char repository[BPM_MAX_ENDPOINT], package_digest[72], key_digest[72];
char hex[65];
@@ -398,7 +399,7 @@ static int push_package(const char *package_path, const char *raw_repository,
goto out;
}
manifest_len = e.len;
- manifest = (unsigned char *)manifest_body;
+ manifest = (__u8 *)manifest_body;
if (bpm_sha256_hex(manifest, manifest_len, hex, sizeof(hex)) ||
snprintf(package_digest, sizeof(package_digest), "sha256:%s", hex) >=
(int)sizeof(package_digest)) {
@@ -431,7 +432,7 @@ static int push_package(const char *package_path, const char *raw_repository,
.media = "application/vnd.oci.image.manifest.v1+json",
};
bpm_copy_string(subject.digest, sizeof(subject.digest), package_digest);
- subject.size = (int64_t)manifest_len;
+ subject.size = (__s64)manifest_len;
key_manifest_body = xmalloc(BPM_MANIFEST_CAP);
if (!key_manifest_body) {
rc = -ENOMEM;
@@ -465,7 +466,7 @@ static int push_package(const char *package_path, const char *raw_repository,
goto out;
}
rc = bpm_oci_put_manifest(&c, repository, key_digest,
- (const unsigned char *)key_manifest_body,
+ (const __u8 *)key_manifest_body,
key_manifest_len, key_digest,
sizeof(key_digest));
if (rc)
@@ -529,7 +530,7 @@ static void tree_selector_canonicalize(struct shirudo_tree_selector *s)
*/
static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,
const char *raw_repository,
- const unsigned char *body, size_t len,
+ const __u8 *body, size_t len,
const char *id, bool unload,
struct shirudo_tree_artifact *out)
{
@@ -599,7 +600,7 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,
rc = e.err;
if (!rc)
rc = bpm_oci_put_manifest(c, repository, tag,
- (const unsigned char *)manifest, e.len,
+ (const __u8 *)manifest, e.len,
out->manifest_digest,
sizeof(out->manifest_digest));
xfree(manifest, BPM_MANIFEST_CAP);
@@ -620,12 +621,12 @@ static int cmd_bpm_tree_append(int argc, char **argv)
const char *reference = "stable", *operation = "add";
char key_path[PATH_MAX], cert_path[PATH_MAX];
char parent[BPM_MAX_DIGEST] = {}, digest[72];
- unsigned char *beefile = NULL, *sig = NULL;
struct shirudo_tree_publication pub = {};
struct shirudo_tree_operation *op = NULL;
size_t len, payload_len = 0, sig_len = 0;
char *payload = NULL, *manifest = NULL;
struct bpm_build_spec *spec = NULL;
+ __u8 *beefile = NULL, *sig = NULL;
struct bpm_package_info info = {};
struct bpm_oci_client c = {};
struct shirudo_json_emit e;
@@ -762,8 +763,8 @@ static int cmd_bpm_tree_append(int argc, char **argv)
}
for (size_t i = 0; i < spec->n_upatches; i++) {
const struct bpm_build_upatch *u = &spec->upatches[i];
- unsigned char *body = NULL;
const char *member;
+ __u8 *body = NULL;
size_t body_len;
if (!u->tree.present)
@@ -837,7 +838,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)
rc = e.err;
if (!rc)
rc = bpm_oci_put_manifest(&c, repository, reference,
- (const unsigned char *)manifest, e.len,
+ (const __u8 *)manifest, e.len,
digest, sizeof(digest));
if (!rc)
printf("oci://%s/%s:%s %s\n", c.registry, repository, reference,
diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.h
index 74bedfc5..e21fb7d8 100644
--- a/tools/shirudo-build/bpm.h
+++ b/tools/shirudo-build/bpm.h
@@ -12,6 +12,8 @@
#include <stdbool.h>
#include <stddef.h>
+#include <shirudo/misc.h>
+
#include "bpm_tree.h"
#include "crypto.h"
#include "upatch.h"
@@ -53,7 +55,7 @@ struct bpm_build_spec {
char name[128];
char scope[PATH_MAX];
char stage[PATH_MAX];
- unsigned char *build_file;
+ __u8 *build_file;
size_t build_file_len;
struct bpm_build_file files[BPM_BUILD_MAX_ENTRIES];
size_t n_files;
@@ -66,7 +68,7 @@ struct bpm_build_spec {
* in build_file (the archive embeds them verbatim). Release with
* bpm_build_spec_free(); the spec itself is large and belongs on the heap.
*/
-int bpm_build_parse_beefile(const unsigned char *body, size_t len,
+int bpm_build_parse_beefile(const __u8 *body, size_t len,
struct bpm_build_spec *spec);
void bpm_build_spec_free(struct bpm_build_spec *spec);
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 4fce0bb9..df967f32 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -21,6 +21,7 @@
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include "bpf/lib/shirudo_serializer.h"
@@ -47,14 +48,14 @@ static struct bpf_map *find_rodata(struct bpf_object *obj);
struct shirudo_unload_params {
char name[SHIRUDO_UPATCH_NAME_MAX];
- uint8_t expected_sha256[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
+ __u8 expected_sha256[SHIRUDO_UPATCH_PAYLOAD_HASH_LEN];
};
struct decoder_manifest_view {
- uint32_t language;
- uint32_t api_version;
- uint32_t input_format;
- uint32_t export_signal;
+ __u32 language;
+ __u32 api_version;
+ __u32 input_format;
+ __u32 export_signal;
char upatch[64];
char map[16];
char source[64];
@@ -65,23 +66,23 @@ struct decoder_file {
struct decoder_manifest_view manifest;
char *path;
char *source;
- uint32_t source_len;
+ __u32 source_len;
};
struct decoder_set {
struct decoder_file *items;
- uint32_t nr_items;
+ __u32 nr_items;
};
static void pack_header(struct shirudo_upatch_header *h,
- const char *name, uint32_t type, uint32_t flags,
- const uint8_t *payload_sha256,
- uint32_t loader_off, uint32_t loader_len,
- uint32_t sig_off, uint32_t sig_len,
- uint32_t meta_off, uint32_t meta_len,
- uint32_t data_off, uint32_t data_len,
- uint32_t log_buf_sz,
- uint32_t nr_maps, uint32_t nr_progs)
+ const char *name, __u32 type, __u32 flags,
+ const __u8 *payload_sha256,
+ __u32 loader_off, __u32 loader_len,
+ __u32 sig_off, __u32 sig_len,
+ __u32 meta_off, __u32 meta_len,
+ __u32 data_off, __u32 data_len,
+ __u32 log_buf_sz,
+ __u32 nr_maps, __u32 nr_progs)
{
memset(h, 0, sizeof(*h));
h->magic = htole64(SHIRUDO_UPATCH_MAGIC);
@@ -204,8 +205,8 @@ static bool default_attach_section(const char *sec)
return false;
}
-static uint32_t attach_kind_from_section(const char *sec,
- const char **target)
+static __u32 attach_kind_from_section(const char *sec,
+ const char **target)
{
*target = NULL;
if (!sec)
@@ -238,7 +239,7 @@ static uint32_t attach_kind_from_section(const char *sec,
}
static int split_uprobe_target(const char *src, char **path,
- uint32_t *path_len, char *pattern,
+ __u32 *path_len, char *pattern,
size_t pattern_sz)
{
const char *colon;
@@ -261,13 +262,13 @@ static int split_uprobe_target(const char *src, char **path,
*path = xstrndup(src, plen);
if (!*path)
return -ENOMEM;
- *path_len = (uint32_t)plen;
+ *path_len = (__u32)plen;
memcpy(pattern, colon + 1, pat_len + 1);
return 0;
}
static int split_probe_target(const char *src, char *target,
- size_t target_sz, uint64_t *offset)
+ size_t target_sz, __u64 *offset)
{
const char *plus = strrchr(src, '+');
size_t len;
@@ -277,7 +278,7 @@ static int split_probe_target(const char *src, char *target,
return -EINVAL;
if (plus) {
char *end = NULL;
- unsigned long long parsed;
+ __u64 parsed;
if (plus == src || plus[1] == '\0')
return -EINVAL;
@@ -285,7 +286,7 @@ static int split_probe_target(const char *src, char *target,
parsed = strtoull(plus + 1, &end, 0);
if (errno || !end || *end != '\0')
return -EINVAL;
- *offset = (uint64_t)parsed;
+ *offset = (__u64)parsed;
len = (size_t)(plus - src);
} else {
len = strlen(src);
@@ -391,7 +392,7 @@ static int make_decoder_path(const char *src_path, const char *rel,
return 0;
}
-static bool parse_decoder_manifest(const unsigned char *p, size_t n,
+static bool parse_decoder_manifest(const __u8 *p, size_t n,
struct decoder_manifest_view *out)
{
const struct shirudo_decoder_manifest *m = (const void *)p;
@@ -435,7 +436,7 @@ static bool parse_decoder_manifest(const unsigned char *p, size_t n,
static void free_decoder_set(struct decoder_set *set)
{
- uint32_t i;
+ __u32 i;
for (i = 0; i < set->nr_items; i++) {
xstrfree(set->items[i].path);
@@ -483,7 +484,7 @@ static int decoder_set_add(struct decoder_set *set,
item->manifest = *manifest;
item->path = path;
item->source = source;
- item->source_len = (uint32_t)source_len;
+ item->source_len = (__u32)source_len;
return 0;
}
@@ -493,8 +494,8 @@ static int collect_decoder_manifests(struct bpf_object *obj,
struct decoder_set *out)
{
struct bpf_map *rodata = find_rodata(obj);
- const unsigned char *data;
size_t data_sz, off;
+ const __u8 *data;
int err;
memset(out, 0, sizeof(*out));
@@ -533,7 +534,7 @@ static int collect_decoder_manifests(struct bpf_object *obj,
err = -EINVAL;
goto err_free;
}
- for (uint32_t i = 0; i < out->nr_items; i++) {
+ for (__u32 i = 0; i < out->nr_items; i++) {
if (strcmp(out->items[i].manifest.upatch,
manifest.upatch) == 0 &&
strcmp(out->items[i].manifest.map,
@@ -556,11 +557,11 @@ err_free:
}
static int build_decoder_blob(const struct decoder_set *set,
- void **out, uint32_t *out_len)
+ void **out, __u32 *out_len)
{
struct shirudo_upatch_decoder_blob *blob;
- uint64_t entries_end, total;
- uint32_t off;
+ __u64 entries_end, total;
+ __u32 off;
size_t i;
*out = NULL;
@@ -568,7 +569,7 @@ static int build_decoder_blob(const struct decoder_set *set,
if (set->nr_items == 0)
return 0;
entries_end = sizeof(*blob) +
- (uint64_t)set->nr_items *
+ (__u64)set->nr_items *
sizeof(struct shirudo_upatch_decoder_entry);
total = entries_end;
for (i = 0; i < set->nr_items; i++)
@@ -584,7 +585,7 @@ static int build_decoder_blob(const struct decoder_set *set,
blob->nr_decoders = htole32(set->nr_items);
blob->reserved = 0;
- off = (uint32_t)entries_end;
+ off = (__u32)entries_end;
for (i = 0; i < set->nr_items; i++) {
const struct decoder_file *file = &set->items[i];
struct shirudo_upatch_decoder_entry *d = &blob->decoders[i];
@@ -602,32 +603,32 @@ static int build_decoder_blob(const struct decoder_set *set,
file->manifest.entrypoint);
d->source_off = htole32(off);
d->source_len = htole32(file->source_len);
- memcpy((unsigned char *)blob + off, file->source,
+ memcpy((__u8 *)blob + off, file->source,
file->source_len);
off += file->source_len;
}
*out = blob;
- *out_len = (uint32_t)total;
+ *out_len = (__u32)total;
return 0;
}
-static int compose_signed_data(const void *data, uint32_t data_len,
+static int compose_signed_data(const void *data, __u32 data_len,
const void *decoder_blob,
- uint32_t decoder_blob_len,
- void **out, uint32_t *out_len)
+ __u32 decoder_blob_len,
+ void **out, __u32 *out_len)
{
struct shirudo_upatch_decoder_trailer trailer = {};
- uint64_t payload_len, total;
- uint32_t pad_len;
+ __u64 payload_len, total;
+ __u32 pad_len;
char *buf;
*out = NULL;
*out_len = data_len;
if (!decoder_blob_len)
return 0;
- payload_len = (uint64_t)data_len + decoder_blob_len;
- pad_len = (uint32_t)((8 - (payload_len & 7)) & 7);
+ payload_len = (__u64)data_len + decoder_blob_len;
+ pad_len = (__u32)((8 - (payload_len & 7)) & 7);
total = payload_len + pad_len;
total += sizeof(trailer);
if (total > UINT32_MAX)
@@ -648,7 +649,7 @@ static int compose_signed_data(const void *data, uint32_t data_len,
sizeof(trailer));
*out = buf;
- *out_len = (uint32_t)total;
+ *out_len = (__u32)total;
return 0;
}
@@ -666,7 +667,7 @@ static int validate_serializer_output_map(struct bpf_object *obj)
SHIRUDO_SERIAL_MAP_NAME);
return -EINVAL;
}
- if (bpf_map__key_size(m) != sizeof(uint32_t)) {
+ if (bpf_map__key_size(m) != sizeof(__u32)) {
pr_err("%s must use a u32 key",
SHIRUDO_SERIAL_MAP_NAME);
return -EINVAL;
@@ -691,13 +692,13 @@ static int validate_serializer_output_map(struct bpf_object *obj)
return -EINVAL;
}
-static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
- void **out, uint32_t *out_len)
+static int build_prog_metadata(struct bpf_object *obj, __u32 nr_progs,
+ void **out, __u32 *out_len)
{
struct build_uprobe_target {
- uint32_t prog_idx;
+ __u32 prog_idx;
char *path;
- uint32_t path_len;
+ __u32 path_len;
struct shirudo_elf_target elf;
};
struct shirudo_upatch_prog_metadata *md;
@@ -705,7 +706,7 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
char (*seen_maps)[SHIRUDO_UPATCH_PROG_MAP_MAX] = NULL;
struct bpf_program *p;
size_t len, base_len, md_alloc_len;
- uint32_t i = 0, n_seen = 0, nr_uprobe_targets = 0;
+ __u32 i = 0, n_seen = 0, nr_uprobe_targets = 0;
bool has_serializer = false, has_test_run = false;
int err;
@@ -750,8 +751,8 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
const char *sec = bpf_program__section_name(p);
const char *ser_map = serializer_map_name(p);
const char *target = NULL;
- uint64_t offset = 0;
- uint32_t kind, j;
+ __u64 offset = 0;
+ __u32 kind, j;
if (ser_map) {
size_t ser_len = strlen(ser_map);
@@ -922,8 +923,8 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
if (nr_uprobe_targets) {
struct shirudo_upatch_uprobe_targets targets = {};
- unsigned char *cursor;
- uint32_t j;
+ __u8 *cursor;
+ __u32 j;
if (len > UINT32_MAX - sizeof(targets)) {
err = -EOVERFLOW;
@@ -934,7 +935,7 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
const struct build_uprobe_target *ut = &uprobe_targets[j];
size_t record_len = sizeof(struct shirudo_upatch_uprobe_target) +
ut->path_len + ut->elf.build_id_len +
- (size_t)ut->elf.nr_offsets * sizeof(uint64_t);
+ (size_t)ut->elf.nr_offsets * sizeof(__u64);
if (record_len > UINT32_MAX || len > UINT32_MAX - record_len) {
err = -EOVERFLOW;
@@ -955,13 +956,13 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
targets.magic = htole32(SHIRUDO_UPATCH_UPROBE_TARGET_MAGIC);
targets.version = htole32(SHIRUDO_UPATCH_UPROBE_TARGET_VERSION);
targets.nr_targets = htole32(nr_uprobe_targets);
- cursor = (unsigned char *)md + base_len;
+ cursor = (__u8 *)md + base_len;
memcpy(cursor, &targets, sizeof(targets));
cursor += sizeof(targets);
for (j = 0; j < nr_uprobe_targets; j++) {
const struct build_uprobe_target *ut = &uprobe_targets[j];
struct shirudo_upatch_uprobe_target record = {};
- uint32_t k;
+ __u32 k;
record.prog_idx = htole32(ut->prog_idx);
record.path_len = htole32(ut->path_len);
@@ -974,7 +975,7 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
memcpy(cursor, ut->elf.build_id, ut->elf.build_id_len);
cursor += ut->elf.build_id_len;
for (k = 0; k < ut->elf.nr_offsets; k++) {
- uint64_t offset = htole64(ut->elf.offsets[k]);
+ __u64 offset = htole64(ut->elf.offsets[k]);
memcpy(cursor, &offset, sizeof(offset));
cursor += sizeof(offset);
@@ -983,7 +984,7 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,
}
*out = md;
- *out_len = (uint32_t)len;
+ *out_len = (__u32)len;
xfree(seen_maps, (size_t)nr_progs * sizeof(*seen_maps));
for (i = 0; i < nr_uprobe_targets; i++) {
xstrfree(uprobe_targets[i].path);
@@ -1069,16 +1070,16 @@ static void unregister_target_attach_types(int handler_ids[2])
* NULL for a .bpf.o input or an unload directive.
*/
static int finalize_object(struct bpf_object *obj, const char *out_path,
- const char *upatch_name, uint32_t flags,
- const void *meta, uint32_t meta_len,
+ const char *upatch_name, __u32 flags,
+ const void *meta, __u32 meta_len,
const char *src_path,
const char *pkey_path, const char *cert_path)
{
LIBBPF_OPTS(gen_loader_opts, opts);
- uint32_t loader_off, sig_off, data_off, data_len, meta_off;
- uint32_t write_meta_len = meta_len, attach_meta_len = 0;
- uint32_t decoder_blob_len = 0, signed_data_len = 0;
- uint32_t nr_maps = 0, nr_progs = 0;
+ __u32 loader_off, sig_off, data_off, data_len, meta_off;
+ __u32 write_meta_len = meta_len, attach_meta_len = 0;
+ __u32 decoder_blob_len = 0, signed_data_len = 0;
+ __u32 nr_maps = 0, nr_progs = 0;
struct shirudo_upatch_header hdr;
struct decoder_set decoders = {};
struct shirudo_sign_io sio = {};
@@ -1086,7 +1087,7 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
const void *write_meta = meta;
void *signed_data_buf = NULL;
void *decoder_blob = NULL;
- uint32_t nr_decoders = 0;
+ __u32 nr_decoders = 0;
void *attach_meta = NULL;
struct bpf_program *p;
void *sig_buf = NULL;
@@ -1148,7 +1149,7 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
err = -EOVERFLOW;
goto out;
}
- err = compose_signed_data(opts.data, (uint32_t)opts.data_sz,
+ err = compose_signed_data(opts.data, (__u32)opts.data_sz,
decoder_blob, decoder_blob_len,
&signed_data_buf, &signed_data_len);
if (err)
@@ -1172,16 +1173,16 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
goto out;
}
- loader_off = (uint32_t)sizeof(hdr);
+ loader_off = (__u32)sizeof(hdr);
sig_off = loader_off + opts.insns_sz;
- data_off = sig_off + (uint32_t)sio.signature_sz;
+ data_off = sig_off + (__u32)sio.signature_sz;
data_len = signed_data_len;
meta_off = data_off + data_len;
pack_header(&hdr, upatch_name, SHIRUDO_UPATCH_TYPE_BPF, flags,
sio.payload_sha256,
loader_off, opts.insns_sz,
- sig_off, (uint32_t)sio.signature_sz,
+ sig_off, (__u32)sio.signature_sz,
write_meta_len ? meta_off : 0, write_meta_len,
data_len ? data_off : 0, data_len,
1u * 1024u * 1024u,
@@ -1273,7 +1274,7 @@ static struct bpf_map *find_rodata(struct bpf_object *obj)
}
int shirudo_build_package_unload(const char *upatch_name,
- const unsigned char payload_sha256[32],
+ const __u8 payload_sha256[32],
const char *out_upatch,
const char *pkey_path,
const char *cert_path)
diff --git a/tools/shirudo-build/package.h b/tools/shirudo-build/package.h
index 22175e9b..2f3df153 100644
--- a/tools/shirudo-build/package.h
+++ b/tools/shirudo-build/package.h
@@ -8,12 +8,14 @@
#ifndef SHIRUDO_BUILD_PACKAGE_H
#define SHIRUDO_BUILD_PACKAGE_H
+#include <shirudo/misc.h>
+
int shirudo_build_package_load(const char *bpf_o, const char *src_path,
const char *out_upatch, const char *upatch_name,
const char *pkey_path, const char *cert_path);
int shirudo_build_package_unload(const char *name,
- const unsigned char payload_sha256[32],
+ const __u8 payload_sha256[32],
const char *out_upatch,
const char *pkey_path,
const char *cert_path);
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.c
index f63b8c97..62e13010 100644
--- a/tools/shirudo-build/shirudo-build.c
+++ b/tools/shirudo-build/shirudo-build.c
@@ -9,13 +9,14 @@
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "bpm.h"
#include "compile.h"
#include "log.h"
@@ -213,7 +214,7 @@ int main(int argc, char **argv)
const char *keys_dir;
const char *vmlinux_override = NULL;
const char *extra_cflags = NULL;
- uint8_t payload_sha256[32] = {};
+ __u8 payload_sha256[32] = {};
const char *unload_name = NULL;
const char *output = NULL;
const char *input = NULL;
diff --git a/tools/shirudo-build/vouch.c b/tools/shirudo-build/vouch.c
index 1bd6ecd0..34ed8b6c 100644
--- a/tools/shirudo-build/vouch.c
+++ b/tools/shirudo-build/vouch.c
@@ -13,6 +13,8 @@
#include <string.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "bpm.h"
#include "util/crypto.h"
#include "util/hex.h"
@@ -37,7 +39,7 @@ static void usage(FILE *fp)
"key. -o defaults to <cert.der>.vouch.\n");
}
-static int cert_algorithm_ok(const unsigned char *der, size_t der_sz,
+static int cert_algorithm_ok(const __u8 *der, size_t der_sz,
const char *what)
{
struct shirudo_cert cert = {};
@@ -78,10 +80,10 @@ int shirudo_build_keys_vouch(int argc, char **argv)
{
const char *pkey = SHIRUDO_SIGNING_KEY_PATH, *cert = SHIRUDO_CERT_PATH;
const char *keys_dir_arg = NULL, *input = NULL, *output = NULL;
- unsigned char *der = NULL, *own = NULL, *payload = NULL;
+ __u8 *der = NULL, *own = NULL, *payload = NULL;
size_t der_sz = 0, own_sz = 0, payload_sz = 0;
struct shirudo_sign_io io = {};
- uint8_t sha[32], fp[32], own_fp[32];
+ __u8 sha[32], fp[32], own_fp[32];
int pkey_explicit = 0, cert_explicit = 0;
char fp_hex[65], own_hex[65];
char pkey_path[PATH_MAX];
diff --git a/util/attest.c b/util/attest.c
index 2c77400f..5f7e929c 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -9,6 +9,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -16,6 +17,8 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "attest.h"
#include "chain.h"
#include "crypto.h"
@@ -47,7 +50,7 @@
*/
static int read_file_base64(const char *path, char **out, size_t *out_cap)
{
- unsigned char *raw;
+ __u8 *raw;
size_t len = 0, cap;
ssize_t got;
int fd, rc = 0;
@@ -99,7 +102,7 @@ static int emit_line(const char *line, size_t len, void *ctx)
}
static void emit_hex(struct shirudo_json_emit *e, const char *key,
- const uint8_t *data, size_t len)
+ const __u8 *data, size_t len)
{
char hex[2 * SHIRUDO_TPM_ATTEST_MAX + 1];
@@ -113,12 +116,12 @@ static void emit_hex(struct shirudo_json_emit *e, const char *key,
/* A quote is accepted only when it covers the PCR values read beside it. */
static bool quote_covers(const struct shirudo_tpm_attestation *quote,
- uint32_t mask, uint8_t values[24][32])
+ __u32 mask, __u8 values[24][32])
{
struct shirudo_tpm_attest a;
struct shirudo_sha256_ctx ctx;
- uint8_t digest[32];
- unsigned int i;
+ __u8 digest[32];
+ __u32 i;
if (shirudo_tpm_attest_parse(quote->attest, quote->attest_len, &a) ||
a.type != SHIRUDO_TPM_ST_ATTEST_QUOTE || a.pcr_mask != mask)
@@ -137,8 +140,8 @@ static bool quote_covers(const struct shirudo_tpm_attestation *quote,
return shirudo_ct_eq(digest, a.pcr_digest, 32);
}
-int shirudo_attest_evidence(const uint8_t *nonce, size_t nonce_len,
- uint32_t pcr_mask,
+int shirudo_attest_evidence(const __u8 *nonce, size_t nonce_len,
+ __u32 pcr_mask,
const struct shirudo_attest_challenge *challenge,
char **out, size_t *len, size_t *cap)
{
@@ -148,14 +151,14 @@ int shirudo_attest_evidence(const uint8_t *nonce, size_t nonce_len,
struct shirudo_tpm_nv_info nv;
struct shirudo_tpm *tpm = NULL;
struct shirudo_json_emit e;
- uint8_t values[24][32] = {};
- uint8_t reg[32], cred[SHIRUDO_TPM_CREDENTIAL_LEN], *ek_cert = NULL;
+ __u8 values[24][32] = {};
+ __u8 reg[32], cred[SHIRUDO_TPM_CREDENTIAL_LEN], *ek_cert = NULL;
size_t ek_cert_len = 0, ek_b64_cap = 0, fw_cap = 0, sd_cap = 0, need;
size_t cred_len = 0;
char *ek_b64 = NULL, *fw = NULL, *sd = NULL;
char ts[32], boot_id[64], nv_index[16], nonce_hex[2 * 64 + 1];
struct stat st;
- unsigned int i, attempt;
+ __u32 i, attempt;
bool ek_cert_rsa = false, ek_rsa;
int rc;
@@ -381,28 +384,28 @@ out:
struct record {
char type[16];
char label[SHIRUDO_UPATCH_NAME_MAX + 64];
- uint8_t digest[32];
+ __u8 digest[32];
};
struct init_record {
bool present;
bool have_reset_count;
bool initrd;
- uint32_t reset_count;
- unsigned int pcr;
- unsigned int phase_pcr;
- uint8_t digest[32];
- uint8_t pcr_before[32];
- uint8_t phase_before[32];
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
- uint8_t anchor_digest[32];
- uint8_t chain[32];
+ __u32 reset_count;
+ __u32 pcr;
+ __u32 phase_pcr;
+ __u8 digest[32];
+ __u8 pcr_before[32];
+ __u8 phase_before[32];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
+ __u8 anchor_digest[32];
+ __u8 chain[32];
char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];
};
struct parse {
char nonce_hex[2 * SHIRUDO_ATTEST_NONCE_MAX + 1];
- int64_t pcr_mask;
+ __s64 pcr_mask;
char ak_public_hex[2 * SHIRUDO_TPM_PUBLIC_MAX + 1];
char ak_name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
char quote_attest_hex[2 * SHIRUDO_TPM_ATTEST_MAX + 1];
@@ -412,15 +415,15 @@ struct parse {
char ek_name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
char cred_hex[2 * SHIRUDO_TPM_CREDENTIAL_LEN + 1];
char reg_index[16];
- int64_t reg_pcr;
+ __s64 reg_pcr;
char reg_name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
char reg_value_hex[65];
char reg_attest_hex[2 * SHIRUDO_TPM_ATTEST_MAX + 1];
char reg_sig_hex[2 * SHIRUDO_TPM_SIGNATURE_MAX + 1];
- int64_t pcr_index;
+ __s64 pcr_index;
char pcr_value_hex[65];
- uint32_t pcr_have;
- uint8_t pcrs[24][32];
+ __u32 pcr_have;
+ __u8 pcrs[24][32];
char type[16];
char digest_hex[65];
char pcr_before_hex[65];
@@ -430,18 +433,18 @@ struct parse {
char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];
char anchor_digest_hex[65];
char chain_hex[65];
- int64_t rec_pcr;
- int64_t rec_phase_pcr;
- int64_t rec_reset;
+ __s64 rec_pcr;
+ __s64 rec_phase_pcr;
+ __s64 rec_reset;
char upatch_name[SHIRUDO_UPATCH_NAME_MAX + 1];
char tree_id[SHIRUDO_UPATCH_NAME_MAX + 1];
char tree_op[16];
- int64_t tree_seq;
+ __s64 tree_seq;
char key_fingerprint[65];
struct record *recs;
size_t n_recs;
- unsigned int inits; /* init records this boot */
- unsigned int inits_epoch; /* for the last reset count */
+ __u32 inits; /* init records this boot */
+ __u32 inits_epoch; /* for the last reset count */
struct init_record init;
int err;
};
@@ -471,7 +474,7 @@ static int on_record(void *ctx)
return -EINVAL;
if (!strcmp(p->type, "init")) {
struct init_record *in = &p->init;
- uint32_t prev_reset = in->reset_count;
+ __u32 prev_reset = in->reset_count;
bool prev_have = in->have_reset_count;
memset(in, 0, sizeof(*in));
@@ -489,11 +492,11 @@ static int on_record(void *ctx)
shirudo_hex_decode(p->chain_hex, in->chain, 32))
return -EINVAL;
memcpy(in->digest, r->digest, 32);
- in->pcr = (unsigned int)p->rec_pcr;
- in->phase_pcr = (unsigned int)p->rec_phase_pcr;
+ in->pcr = (__u32)p->rec_pcr;
+ in->phase_pcr = (__u32)p->rec_phase_pcr;
in->initrd = !strcmp(p->bound_in, "initrd");
in->have_reset_count = p->rec_reset >= 0;
- in->reset_count = (uint32_t)p->rec_reset;
+ in->reset_count = (__u32)p->rec_reset;
snprintf(in->anchor, sizeof(in->anchor), "%s", p->anchor);
in->present = true;
/*
@@ -516,7 +519,7 @@ static int on_record(void *ctx)
snprintf(r->label, sizeof(r->label), "%s", p->upatch_name);
} else if (!strcmp(p->type, "tree_op")) {
snprintf(r->label, sizeof(r->label), "%s %s seq %lld",
- p->tree_op, p->tree_id, (long long)p->tree_seq);
+ p->tree_op, p->tree_id, (__s64)p->tree_seq);
} else if (!strncmp(p->type, "key_", 4)) {
snprintf(r->label, sizeof(r->label), "%s", p->key_fingerprint);
}
@@ -596,7 +599,7 @@ static void note(struct reporter *r, const char *fmt, ...)
}
}
-static int hex_bytes(const char *hex, uint8_t *out, size_t cap, size_t *len)
+static int hex_bytes(const char *hex, __u8 *out, size_t cap, size_t *len)
{
size_t n = strlen(hex);
@@ -609,13 +612,13 @@ static int hex_bytes(const char *hex, uint8_t *out, size_t cap, size_t *len)
static int check_signed(struct reporter *rep, const char *what,
const char *attest_hex, const char *sig_hex,
const struct shirudo_tpm_ecc_public *ak,
- const uint8_t ak_name[SHIRUDO_TPM_NAME_LEN],
- const uint8_t *nonce, size_t nonce_len,
- uint8_t *attest, size_t attest_cap, size_t *attest_len,
+ const __u8 ak_name[SHIRUDO_TPM_NAME_LEN],
+ const __u8 *nonce, size_t nonce_len,
+ __u8 *attest, size_t attest_cap, size_t *attest_len,
struct shirudo_tpm_attest *parsed)
{
- uint8_t sig[SHIRUDO_TPM_SIGNATURE_MAX], r[32], s[32], digest[32];
- uint8_t qn[SHIRUDO_TPM_NAME_LEN];
+ __u8 sig[SHIRUDO_TPM_SIGNATURE_MAX], r[32], s[32], digest[32];
+ __u8 qn[SHIRUDO_TPM_NAME_LEN];
size_t sig_len;
int rc;
@@ -650,19 +653,19 @@ int shirudo_attest_verify(const char *evidence, size_t len,
struct reporter rep = { .buf = report, .cap = report_cap };
struct shirudo_tpm_attest quote = {}, certify = {};
struct shirudo_tpm_ecc_public ak_pub;
- uint8_t ak_public[SHIRUDO_TPM_PUBLIC_MAX];
- uint8_t ak_name[SHIRUDO_TPM_NAME_LEN];
- uint8_t reg_name[SHIRUDO_TPM_NAME_LEN], reg_value[32];
- uint8_t attest[SHIRUDO_TPM_ATTEST_MAX];
- uint8_t nonce[SHIRUDO_ATTEST_NONCE_MAX], chain[32] = {};
- uint8_t branch_written[32], branch_init[32], policy[32], bind[64];
- uint8_t expected_name[SHIRUDO_TPM_NAME_LEN], pcr_expected[32];
- uint8_t anchor_digest[32], cred[SHIRUDO_TPM_CREDENTIAL_LEN];
+ __u8 ak_public[SHIRUDO_TPM_PUBLIC_MAX];
+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 reg_name[SHIRUDO_TPM_NAME_LEN], reg_value[32];
+ __u8 attest[SHIRUDO_TPM_ATTEST_MAX];
+ __u8 nonce[SHIRUDO_ATTEST_NONCE_MAX], chain[32] = {};
+ __u8 branch_written[32], branch_init[32], policy[32], bind[64];
+ __u8 expected_name[SHIRUDO_TPM_NAME_LEN], pcr_expected[32];
+ __u8 anchor_digest[32], cred[SHIRUDO_TPM_CREDENTIAL_LEN];
struct parse *p;
size_t ak_public_len, ak_name_len, nonce_len, attest_len;
size_t reg_name_len, reg_value_len, cred_len = 0, i, runtime = 0;
- uint32_t reg_index;
- unsigned int bit;
+ __u32 reg_index;
+ __u32 bit;
bool have_ek = false, have_cred = false;
int rc = 0, qrc = -1, crc = -1;
@@ -837,7 +840,7 @@ int shirudo_attest_verify(const char *evidence, size_t len,
say(&rep, (ak_pub.attrs & AK_ATTRS_REQUIRED) == AK_ATTRS_REQUIRED,
"attestation key: restricted, fixed to this TPM");
{
- uint8_t computed[SHIRUDO_TPM_NAME_LEN];
+ __u8 computed[SHIRUDO_TPM_NAME_LEN];
shirudo_tpm_object_name(ak_public, ak_public_len, computed);
say(&rep, shirudo_ct_eq(computed, ak_name, sizeof(computed)),
@@ -852,8 +855,8 @@ int shirudo_attest_verify(const char *evidence, size_t len,
p->ak_name_hex);
if (have_ek) {
- uint8_t ek_public[SHIRUDO_TPM_PUBLIC_MAX];
- uint8_t ek_name[SHIRUDO_TPM_NAME_LEN], computed[SHIRUDO_TPM_NAME_LEN];
+ __u8 ek_public[SHIRUDO_TPM_PUBLIC_MAX];
+ __u8 ek_name[SHIRUDO_TPM_NAME_LEN], computed[SHIRUDO_TPM_NAME_LEN];
struct shirudo_tpm_public ek_pub;
size_t ek_public_len, ek_name_len;
bool parsed;
@@ -880,7 +883,7 @@ int shirudo_attest_verify(const char *evidence, size_t len,
note(&rep, "no endorsement key in the evidence");
}
if (expect->have_ek_name) {
- uint8_t ek_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 ek_name[SHIRUDO_TPM_NAME_LEN];
size_t n;
say(&rep, have_ek &&
@@ -911,10 +914,10 @@ int shirudo_attest_verify(const char *evidence, size_t len,
qrc = rc;
if (rc == 0) {
struct shirudo_sha256_ctx ctx;
- uint8_t digest[32] = {};
+ __u8 digest[32] = {};
bool complete =
quote.type == SHIRUDO_TPM_ST_ATTEST_QUOTE &&
- quote.pcr_mask == (uint32_t)p->pcr_mask &&
+ quote.pcr_mask == (__u32)p->pcr_mask &&
p->pcr_have == quote.pcr_mask;
bool vouched;
@@ -989,13 +992,13 @@ int shirudo_attest_verify(const char *evidence, size_t len,
shirudo_tpm_nv_name(reg_index,
SHIRUDO_TPM_REGISTER_ATTRS | SHIRUDO_TPM_NV_WRITTEN,
policy, 32, expected_name);
- say(&rep, (unsigned int)p->reg_pcr == p->init.pcr &&
+ say(&rep, (__u32)p->reg_pcr == p->init.pcr &&
shirudo_ct_eq(expected_name, reg_name, SHIRUDO_TPM_NAME_LEN) &&
shirudo_ct_eq(expected_name, p->init.name, SHIRUDO_TPM_NAME_LEN),
"register: public area is bound to PCRs %u and %u as recorded",
p->init.pcr, p->init.phase_pcr);
for (bit = 0; bit < 24; bit++) {
- const uint8_t *held;
+ const __u8 *held;
if (!(expect->bind_expect_mask & (1u << bit)))
continue;
@@ -1070,7 +1073,7 @@ int shirudo_attest_verify(const char *evidence, size_t len,
say(&rep, shirudo_ct_eq(reg_value, expect->chain, 32),
"state: register equals the expected value");
} else if (expect->have_init) {
- uint8_t predicted[32] = {};
+ __u8 predicted[32] = {};
shirudo_chain_fold(predicted, expect->init_event);
if (runtime == 0)
@@ -1171,7 +1174,7 @@ static bool cert_describes(const struct shirudo_cert_pubkey *key,
int shirudo_attest_challenge_make(const char *evidence, size_t len,
const struct shirudo_attest_challenge_policy *pol,
- uint8_t credential[SHIRUDO_TPM_CREDENTIAL_LEN],
+ __u8 credential[SHIRUDO_TPM_CREDENTIAL_LEN],
char **out, size_t *out_len, size_t *cap,
char *report, size_t report_cap)
{
@@ -1181,10 +1184,10 @@ int shirudo_attest_challenge_make(const char *evidence, size_t len,
char ek_public_hex[2 * SHIRUDO_TPM_PUBLIC_MAX + 1], hex[2 * 256 + 1];
char ak_hex[2 * SHIRUDO_TPM_NAME_LEN + 1], ek_type[8];
char ek_name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
- uint8_t ak_name[SHIRUDO_TPM_NAME_LEN], ek_name[SHIRUDO_TPM_NAME_LEN];
- uint8_t ek_public[SHIRUDO_TPM_PUBLIC_MAX], computed[SHIRUDO_TPM_NAME_LEN];
- uint8_t seed[32], z[32], eph_x[32], eph_y[32], secret[256];
- uint8_t blob[SHIRUDO_TPM_ID_OBJECT_LEN], *der = NULL;
+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN], ek_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 ek_public[SHIRUDO_TPM_PUBLIC_MAX], computed[SHIRUDO_TPM_NAME_LEN];
+ __u8 seed[32], z[32], eph_x[32], eph_y[32], secret[256];
+ __u8 blob[SHIRUDO_TPM_ID_OBJECT_LEN], *der = NULL;
struct shirudo_cert_pubkey cert_key;
struct shirudo_tpm_public pub;
struct shirudo_cert cert = {};
diff --git a/util/attest.h b/util/attest.h
index 847379cf..36f94c1c 100644
--- a/util/attest.h
+++ b/util/attest.h
@@ -11,7 +11,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "chain.h"
#include "tpm_wire.h"
@@ -30,10 +31,10 @@
*/
struct shirudo_attest_challenge {
bool ek_rsa;
- uint8_t ak_name[SHIRUDO_TPM_NAME_LEN];
- uint8_t blob[SHIRUDO_TPM_ID_OBJECT_LEN];
+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 blob[SHIRUDO_TPM_ID_OBJECT_LEN];
size_t blob_len;
- uint8_t secret[256];
+ __u8 secret[256];
size_t secret_len;
};
@@ -46,8 +47,8 @@ int shirudo_attest_challenge_read(const char *path,
* Target side. The buffer is xmalloc'd to *cap and NUL-terminated;
* -EKEYREJECTED when the TPM refuses the challenge.
*/
-int shirudo_attest_evidence(const uint8_t *nonce, size_t nonce_len,
- uint32_t pcr_mask,
+int shirudo_attest_evidence(const __u8 *nonce, size_t nonce_len,
+ __u32 pcr_mask,
const struct shirudo_attest_challenge *challenge,
char **out, size_t *len, size_t *cap);
@@ -58,37 +59,37 @@ int shirudo_attest_evidence(const uint8_t *nonce, size_t nonce_len,
*/
struct shirudo_attest_challenge_policy {
const char *ek_ca; /* issuer bundle, or NULL */
- const uint8_t *ak_name; /* pinned attestation key, or NULL */
- const uint8_t *ek_name; /* pinned endorsement key, or NULL */
+ const __u8 *ak_name; /* pinned attestation key, or NULL */
+ const __u8 *ek_name; /* pinned endorsement key, or NULL */
bool trust_presented; /* accept an unvouched endorsement key */
};
int shirudo_attest_challenge_make(const char *evidence, size_t len,
const struct shirudo_attest_challenge_policy *pol,
- uint8_t credential[SHIRUDO_TPM_CREDENTIAL_LEN],
+ __u8 credential[SHIRUDO_TPM_CREDENTIAL_LEN],
char **out, size_t *out_len, size_t *cap,
char *report, size_t report_cap);
struct shirudo_attest_expect {
- const uint8_t *nonce;
+ const __u8 *nonce;
size_t nonce_len;
bool have_ak_name;
- uint8_t ak_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN];
bool have_chain;
- uint8_t chain[32];
+ __u8 chain[32];
bool have_init; /* cert, set, tree root known */
- uint8_t init_event[32];
+ __u8 init_event[32];
bool allow_runtime_events;
bool strict_anchor;
- uint32_t pcr_expect_mask;
- uint8_t pcrs[24][32];
+ __u32 pcr_expect_mask;
+ __u8 pcrs[24][32];
bool have_credential;
- uint8_t credential[SHIRUDO_TPM_CREDENTIAL_LEN];
+ __u8 credential[SHIRUDO_TPM_CREDENTIAL_LEN];
bool have_ek_name;
- uint8_t ek_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 ek_name[SHIRUDO_TPM_NAME_LEN];
bool require_initrd; /* the binding must be the initrd's */
- uint32_t bind_expect_mask; /* PCR values at binding time */
- uint8_t bind_values[24][32];
+ __u32 bind_expect_mask; /* PCR values at binding time */
+ __u8 bind_values[24][32];
};
/*
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 887d454c..6f89b3b9 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -11,7 +11,6 @@
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -21,6 +20,8 @@
#include <unistd.h>
#include <curl/curl.h>
+#include <shirudo/misc.h>
+
#include "bpm_oci.h"
#include "crypto.h"
#include "io.h"
@@ -47,7 +48,7 @@ void bpm_copy_string(char *dst, size_t dst_sz, const char *src)
static bool bpm_component_ok(const char *s, bool plus)
{
- const unsigned char *p = (const unsigned char *)s;
+ const __u8 *p = (const __u8 *)s;
if (!s || !*s || strlen(s) >= BPM_MAX_NAME || s[0] == '.')
return false;
@@ -72,7 +73,7 @@ bool bpm_version_ok(const char *s)
static int bpm_buffer_append(struct bpm_buffer *b, const void *data, size_t len)
{
size_t want;
- unsigned char *p;
+ __u8 *p;
if (len >= BPM_MAX_HTTP_BODY - b->len)
return -E2BIG;
@@ -119,7 +120,7 @@ static size_t bpm_write_memory(void *ptr, size_t size, size_t nmemb, void *ud)
/* A download bounded by the size its manifest declares. */
struct bpm_file_sink {
FILE *fp;
- uint64_t room;
+ __u64 room;
bool overflow;
};
@@ -149,18 +150,18 @@ static size_t bpm_header(void *ptr, size_t size, size_t nmemb, void *ud)
line[n] = '\0';
if (!strncasecmp(line, "WWW-Authenticate:", 17)) {
v = line + 17;
- while (isspace((unsigned char)*v))
+ while (isspace((__u8)*v))
v++;
bpm_copy_string(r->www_auth, sizeof(r->www_auth), v);
} else if (!strncasecmp(line, "Content-Type:", 13)) {
v = line + 13;
- while (isspace((unsigned char)*v))
+ while (isspace((__u8)*v))
v++;
v[strcspn(v, " ;\r\n")] = '\0';
bpm_copy_string(r->content_type, sizeof(r->content_type), v);
} else if (!strncasecmp(line, "Location:", 9)) {
v = line + 9;
- while (isspace((unsigned char)*v))
+ while (isspace((__u8)*v))
v++;
v[strcspn(v, "\r\n")] = '\0';
bpm_copy_string(r->location, sizeof(r->location), v);
@@ -186,23 +187,23 @@ static int base64_value(int c)
static int decode_basic(const char *in, char *user, size_t usz,
char *pass, size_t psz)
{
- unsigned char out[BPM_OCI_BASIC_RAW_SIZE];
+ __u8 out[BPM_OCI_BASIC_RAW_SIZE];
size_t n = 0, i = 0;
- unsigned int val = 0;
+ __u32 val = 0;
int bits = -8, x;
char *colon;
for (; *in && *in != '"' && n < sizeof(out); in++) {
- if (isspace((unsigned char)*in) || *in == '=')
+ if (isspace((__u8)*in) || *in == '=')
continue;
- x = base64_value((unsigned char)*in);
+ x = base64_value((__u8)*in);
if (x < 0)
return -EINVAL;
/* No more than 12 bits are ever pending. */
- val = ((val << 6) | (unsigned int)x) & 0xfff;
+ val = ((val << 6) | (__u32)x) & 0xfff;
bits += 6;
if (bits >= 0) {
- out[n++] = (unsigned char)((val >> bits) & 0xff);
+ out[n++] = (__u8)((val >> bits) & 0xff);
bits -= 8;
}
}
@@ -236,12 +237,12 @@ static int encode_basic(const char *user, const char *pass,
if (need > out_sz)
return -ENOSPC;
for (i = 0; i < len; i += 3) {
- unsigned int v = (unsigned char)raw[i] << 16;
+ __u32 v = (__u8)raw[i] << 16;
if (i + 1 < len)
- v |= (unsigned char)raw[i + 1] << 8;
+ v |= (__u8)raw[i + 1] << 8;
if (i + 2 < len)
- v |= (unsigned char)raw[i + 2];
+ v |= (__u8)raw[i + 2];
out[o++] = alphabet[(v >> 18) & 63];
out[o++] = alphabet[(v >> 12) & 63];
out[o++] = i + 1 < len ? alphabet[(v >> 6) & 63] : '=';
@@ -257,7 +258,7 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
const char *docker = getenv("DOCKER_CONFIG");
char path[PATH_MAX];
char needle[BPM_MAX_ENDPOINT + 8];
- unsigned char *body = NULL;
+ __u8 *body = NULL;
size_t body_len = 0;
char *p, *end, *auth;
int rc;
@@ -312,7 +313,7 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
return -EINVAL;
}
auth++;
- while (*auth && isspace((unsigned char)*auth))
+ while (*auth && isspace((__u8)*auth))
auth++;
if (*auth != '"') {
xfree(body, body_len + 1);
@@ -625,7 +626,7 @@ static int get_bearer_token(struct bpm_oci_client *c, const char *challenge)
if (!token)
goto out;
token++;
- while (*token && isspace((unsigned char)*token))
+ while (*token && isspace((__u8)*token))
token++;
if (*token++ != '"')
goto out;
@@ -703,7 +704,7 @@ int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,
}
int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
- const char *accept, const char *path, int64_t size)
+ const char *accept, const char *path, __s64 size)
{
struct bpm_http_response r = {};
struct bpm_file_sink sink = {};
@@ -716,7 +717,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
fp = fopen(path, "wb");
if (!fp)
return -errno;
- sink = (struct bpm_file_sink){ .fp = fp, .room = (uint64_t)size };
+ sink = (struct bpm_file_sink){ .fp = fp, .room = (__u64)size };
rc = curl_once(c, "GET", url, accept, NULL, &r, &sink, true);
if (!rc && r.status == 401 && r.www_auth[0] && !strncasecmp(r.www_auth, "Bearer", 6)) {
fclose(fp);
@@ -727,7 +728,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
bpm_buffer_free(&r.body);
return rc;
}
- sink = (struct bpm_file_sink){ .fp = fp, .room = (uint64_t)size };
+ sink = (struct bpm_file_sink){ .fp = fp, .room = (__u64)size };
rc = get_bearer_token(c, r.www_auth);
if (!rc) {
snprintf(auth, sizeof(auth), "Bearer %s", c->token);
@@ -746,7 +747,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
bpm_buffer_free(&r.body);
return rc;
}
- sink = (struct bpm_file_sink){ .fp = fp, .room = (uint64_t)size };
+ sink = (struct bpm_file_sink){ .fp = fp, .room = (__u64)size };
rc = encode_basic(c->username, c->password, encoded, sizeof(encoded));
if (!rc) {
snprintf(auth, sizeof(auth), "Basic %s", encoded);
@@ -842,7 +843,7 @@ static int on_descriptor(void *ud)
return 0;
}
-int bpm_oci_parse_manifest(const unsigned char *body, size_t len,
+int bpm_oci_parse_manifest(const __u8 *body, size_t len,
struct bpm_descriptor *layers, int max_layers,
int *n_layers, char *package_name,
size_t package_name_sz)
@@ -851,7 +852,7 @@ int bpm_oci_parse_manifest(const unsigned char *body, size_t len,
struct shirudo_json_array arr = { on_descriptor, &sink };
struct shirudo_json_field layer_annotations[1], manifest_annotations[1];
struct shirudo_json_field sub[4], root[3];
- int64_t schema = 0;
+ __s64 schema = 0;
memset(sub, 0, sizeof(sub));
sub[0] = (struct shirudo_json_field){ .key = "mediaType", .type = SHIRUDO_JSON_T_STRING,
@@ -886,9 +887,9 @@ int bpm_oci_parse_manifest(const unsigned char *body, size_t len,
bool bpm_package_name_ok(const char *s)
{
- const unsigned char *p = (const unsigned char *)s;
+ const __u8 *p = (const __u8 *)s;
- if (!s || !isalnum((unsigned char)*p))
+ if (!s || !isalnum((__u8)*p))
return false;
for (; *p; p++) {
if (!(isalnum(*p) || *p == '.' || *p == '_' || *p == '-'))
@@ -944,7 +945,7 @@ int bpm_oci_parse_pull_ref(const char *raw, struct bpm_oci_client *c,
}
if (!bpm_package_name_ok(strrchr(repository, '/') ?
strrchr(repository, '/') + 1 : repository) ||
- !bpm_version_ok(tag) || !isalnum((unsigned char)tag[0]))
+ !bpm_version_ok(tag) || !isalnum((__u8)tag[0]))
return -EINVAL;
return bpm_oci_parse_endpoint(endpoint, c);
}
@@ -995,10 +996,10 @@ void bpm_trim(const char *in, char *out, size_t out_sz)
{
const char *start = in, *end;
- while (*start && isspace((unsigned char)*start))
+ while (*start && isspace((__u8)*start))
start++;
end = start + strlen(start);
- while (end > start && isspace((unsigned char)end[-1]))
+ while (end > start && isspace((__u8)end[-1]))
end--;
if ((size_t)(end - start) >= out_sz)
end = start + out_sz - 1;
@@ -1021,11 +1022,11 @@ int bpm_oci_url(const struct bpm_oci_client *c, const char *repo,
int bpm_sha256_hex(const void *data, size_t len,
char *hex, size_t hex_sz)
{
- unsigned char digest[SHIRUDO_CRYPTO_SHA256_LEN];
+ __u8 digest[SHIRUDO_CRYPTO_SHA256_LEN];
if (hex_sz < 65 || shirudo_sha256_buffer(data, len, digest) != 0)
return -EINVAL;
- for (unsigned int i = 0; i < sizeof(digest); i++)
+ for (__u32 i = 0; i < sizeof(digest); i++)
snprintf(hex + i * 2, 3, "%02x", digest[i]);
hex[64] = 0;
return 0;
@@ -1081,7 +1082,7 @@ int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
if (!rc) {
bpm_copy_string(out->media, sizeof(out->media), media);
bpm_copy_string(out->digest, sizeof(out->digest), digest);
- out->size = (int64_t)body_len;
+ out->size = (__s64)body_len;
return 0;
}
if (rc != -ENOENT)
@@ -1116,7 +1117,7 @@ int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
if (!rc) {
bpm_copy_string(out->media, sizeof(out->media), media);
bpm_copy_string(out->digest, sizeof(out->digest), digest);
- out->size = (int64_t)body_len;
+ out->size = (__s64)body_len;
}
out:
bpm_buffer_free(&r.body);
@@ -1142,7 +1143,7 @@ void bpm_oci_emit_descriptor(struct shirudo_json_emit *e, const char *key,
}
int bpm_oci_put_manifest(struct bpm_oci_client *c, const char *repository,
- const char *reference, const unsigned char *body,
+ const char *reference, const __u8 *body,
size_t body_len, char *digest, size_t digest_sz)
{
struct bpm_http_response r = {};
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 5d3a9585..6f392072 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -11,9 +11,10 @@
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
#include <sys/types.h>
+#include <shirudo/misc.h>
+
#include "crypto.h"
struct shirudo_json_emit;
@@ -53,7 +54,7 @@ struct shirudo_json_emit;
#define BPM_UPATCH_MEDIA "application/vnd.shirudo.upatch.v1"
struct bpm_buffer {
- unsigned char *data;
+ __u8 *data;
size_t len;
size_t cap;
};
@@ -80,7 +81,7 @@ struct bpm_descriptor {
char media[BPM_MAX_MEDIA];
char digest[BPM_MAX_DIGEST];
char title[PATH_MAX];
- int64_t size;
+ __s64 size;
};
void bpm_copy_string(char *dst, size_t dst_sz, const char *src);
@@ -116,14 +117,14 @@ int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,
const char *accept, struct bpm_buffer *out);
/* At most size bytes, the size the manifest declares; -EFBIG beyond. */
int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
- const char *accept, const char *path, int64_t size);
+ const char *accept, const char *path, __s64 size);
int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
const void *body, size_t body_len, const char *media,
struct bpm_descriptor *out);
int bpm_oci_put_manifest(struct bpm_oci_client *c, const char *repository,
- const char *reference, const unsigned char *body,
+ const char *reference, const __u8 *body,
size_t body_len, char *digest, size_t digest_sz);
-int bpm_oci_parse_manifest(const unsigned char *body, size_t len,
+int bpm_oci_parse_manifest(const __u8 *body, size_t len,
struct bpm_descriptor *layers, int max_layers,
int *n_layers, char *package_name,
size_t package_name_sz);
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 808560f7..fe86b1f3 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -18,6 +18,8 @@
#include <openssl/pem.h>
#include <openssl/x509.h>
+#include <shirudo/misc.h>
+
#include "bpm_oci.h"
#include "bpm_package.h"
#include "bpm_tree.h"
@@ -47,13 +49,13 @@ static int on_index_entry(void *ud)
return 0;
}
-int bpm_index_parse(const unsigned char *body, size_t len,
+int bpm_index_parse(const __u8 *body, size_t len,
struct bpm_index_entry *entries, int *n_entries)
{
struct index_sink sink = { .items = entries };
struct shirudo_json_array arr = { on_index_entry, &sink };
struct shirudo_json_field sub[7], root[2];
- int64_t schema = 0;
+ __s64 schema = 0;
memset(sub, 0, sizeof(sub));
sub[0] = (struct shirudo_json_field){
@@ -103,8 +105,8 @@ int bpm_index_parse(const unsigned char *body, size_t len,
return 0;
}
-int bpm_verify_cms(const unsigned char *payload, size_t payload_len,
- const unsigned char *sig, size_t sig_len, const char *cert_path)
+int bpm_verify_cms(const __u8 *payload, size_t payload_len,
+ const __u8 *sig, size_t sig_len, const char *cert_path)
{
struct shirudo_cert cert = {};
int rc = -EINVAL;
@@ -115,17 +117,17 @@ int bpm_verify_cms(const unsigned char *payload, size_t payload_len,
return rc;
}
-int bpm_tar_octal(const char *p, size_t n, uint64_t *out)
+int bpm_tar_octal(const char *p, size_t n, __u64 *out)
{
- uint64_t v = 0;
size_t i = 0;
+ __u64 v = 0;
while (i < n && (p[i] == ' ' || p[i] == '\0'))
i++;
for (; i < n && p[i] >= '0' && p[i] <= '7'; i++) {
if (v > (UINT64_MAX >> 3))
return -EOVERFLOW;
- v = (v << 3) | (unsigned int)(p[i] - '0');
+ v = (v << 3) | (__u32)(p[i] - '0');
}
*out = v;
return 0;
@@ -161,12 +163,12 @@ static bool package_member_name_ok(const char *name, const char *member)
int bpm_pkg_read_member_in(const char *archive, const char *member,
const char *package_dir, char *found_dir,
- size_t found_dir_sz, unsigned char **out,
+ size_t found_dir_sz, __u8 **out,
size_t *out_len, size_t max)
{
gzFile gz = gzopen(archive, "rb");
- unsigned char h[512];
int rc = -ENOENT;
+ __u8 h[512];
if (!gz)
return -errno;
@@ -176,7 +178,7 @@ int bpm_pkg_read_member_in(const char *archive, const char *member,
}
for (;;) {
int n = gzread(gz, h, sizeof(h));
- uint64_t size, remain, pad;
+ __u64 size, remain, pad;
char name[101];
bool zero = true;
@@ -206,7 +208,7 @@ int bpm_pkg_read_member_in(const char *archive, const char *member,
((size_t)(strchr(name, '/') - name) == strlen(package_dir) &&
!strncmp(name, package_dir, strlen(package_dir))))) {
size_t dir_len = (size_t)(strchr(name, '/') - name);
- unsigned char *body;
+ __u8 *body;
if ((h[156] && h[156] != '0') || h[345] ||
(found_dir && dir_len >= found_dir_sz)) {
@@ -225,7 +227,7 @@ int bpm_pkg_read_member_in(const char *archive, const char *member,
while (remain) {
size_t want = remain > 65536 ? 65536 : (size_t)remain;
- n = gzread(gz, body + size - remain, (unsigned int)want);
+ n = gzread(gz, body + size - remain, (__u32)want);
if (n <= 0) {
xfree(body, (size_t)size + 1);
rc = -EINVAL;
@@ -244,10 +246,10 @@ int bpm_pkg_read_member_in(const char *archive, const char *member,
break;
}
while (remain) {
- unsigned char discard[65536];
+ __u8 discard[65536];
size_t want = remain > sizeof(discard) ? sizeof(discard) : (size_t)remain;
- n = gzread(gz, discard, (unsigned int)want);
+ n = gzread(gz, discard, (__u32)want);
if (n <= 0) {
rc = -EINVAL;
goto done;
@@ -256,10 +258,10 @@ int bpm_pkg_read_member_in(const char *archive, const char *member,
}
pad = (512 - (size % 512)) % 512;
while (pad) {
- unsigned char discard[512];
+ __u8 discard[512];
size_t want = pad > sizeof(discard) ? sizeof(discard) : (size_t)pad;
- n = gzread(gz, discard, (unsigned int)want);
+ n = gzread(gz, discard, (__u32)want);
if (n <= 0) {
rc = -EINVAL;
goto done;
@@ -278,13 +280,13 @@ done:
}
int bpm_pkg_read_member(const char *archive, const char *member,
- unsigned char **out, size_t *out_len, size_t max)
+ __u8 **out, size_t *out_len, size_t max)
{
return bpm_pkg_read_member_in(archive, member, NULL, NULL, 0,
out, out_len, max);
}
-static X509 *read_x509_memory(const unsigned char *body, size_t len)
+static X509 *read_x509_memory(const __u8 *body, size_t len)
{
BIO *b;
X509 *x;
@@ -307,11 +309,11 @@ static X509 *read_x509_memory(const unsigned char *body, size_t len)
* X509_pubkey_digest(), whose meaning is easy to confuse with a digest of
* the key material itself when diagnosing package signer mismatches.
*/
-static int x509_spki_sha256(const X509 *cert, unsigned char digest[32])
+static int x509_spki_sha256(const X509 *cert, __u8 digest[32])
{
X509_PUBKEY *spki;
- unsigned char *der = NULL, *p;
- unsigned int digest_len = 0;
+ __u8 *der = NULL, *p;
+ __u32 digest_len = 0;
int der_len, encoded_len, rc = -EINVAL;
spki = X509_get_X509_PUBKEY((X509 *)cert);
@@ -338,14 +340,14 @@ out:
int bpm_pkg_load_info(const char *archive, struct bpm_package_info *info)
{
- unsigned char *environment = NULL;
+ __u8 *environment = NULL;
struct {
char name[BPM_MAX_NAME];
char version[BPM_MAX_NAME];
char signer[BPM_MAX_DIGEST];
} parsed = {};
struct shirudo_json_field package_fields[2], shirudo_fields[1], root[2];
- unsigned char digest[32];
+ __u8 digest[32];
X509 *cert = NULL;
char hex[EVP_MAX_MD_SIZE * 2 + 1];
size_t environment_len;
@@ -394,7 +396,7 @@ int bpm_pkg_load_info(const char *archive, struct bpm_package_info *info)
rc = -EINVAL;
goto out;
}
- for (unsigned int i = 0; i < sizeof(digest); i++)
+ for (__u32 i = 0; i < sizeof(digest); i++)
snprintf(hex + i * 2, 3, "%02x", digest[i]);
hex[sizeof(digest) * 2] = '\0';
if (parsed.signer[0] && strcasecmp(parsed.signer, hex)) {
@@ -483,7 +485,7 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,
rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
if (rc)
goto out;
- if ((layers[i].size >= 0 && (int64_t)dst->len != layers[i].size) ||
+ if ((layers[i].size >= 0 && (__s64)dst->len != layers[i].size) ||
bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||
!bpm_digest_matches(layers[i].digest, hex)) {
rc = -EKEYREJECTED;
diff --git a/util/bpm_package.h b/util/bpm_package.h
index c059f41b..34217771 100644
--- a/util/bpm_package.h
+++ b/util/bpm_package.h
@@ -10,7 +10,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "bpm_oci.h"
#include "bpm_tree.h"
@@ -22,24 +23,24 @@ struct bpm_index_entry {
char manifest_digest[BPM_MAX_DIGEST];
char package_digest[BPM_MAX_DIGEST];
char signer[BPM_MAX_DIGEST];
- int64_t package_size;
+ __s64 package_size;
};
struct bpm_package_info {
char name[BPM_MAX_NAME];
char version[BPM_MAX_NAME];
char signer[BPM_MAX_DIGEST];
- unsigned char *cert;
+ __u8 *cert;
size_t cert_len;
};
-int bpm_index_parse(const unsigned char *body, size_t len,
+int bpm_index_parse(const __u8 *body, size_t len,
struct bpm_index_entry *entries, int *n_entries);
-int bpm_verify_cms(const unsigned char *payload, size_t payload_len,
- const unsigned char *sig, size_t sig_len,
+int bpm_verify_cms(const __u8 *payload, size_t payload_len,
+ const __u8 *sig, size_t sig_len,
const char *cert_path);
-int bpm_tar_octal(const char *p, size_t n, uint64_t *out);
+int bpm_tar_octal(const char *p, size_t n, __u64 *out);
bool bpm_archive_rel_ok(const char *name);
/*
@@ -50,10 +51,10 @@ bool bpm_archive_rel_ok(const char *name);
*/
int bpm_pkg_read_member_in(const char *archive, const char *member,
const char *package_dir, char *found_dir,
- size_t found_dir_sz, unsigned char **out,
+ size_t found_dir_sz, __u8 **out,
size_t *out_len, size_t max);
int bpm_pkg_read_member(const char *archive, const char *member,
- unsigned char **out, size_t *out_len, size_t max);
+ __u8 **out, size_t *out_len, size_t max);
int bpm_pkg_load_info(const char *archive, struct bpm_package_info *info);
/*
diff --git a/util/bpm_source_config.c b/util/bpm_source_config.c
index 292587a7..5eeeff19 100644
--- a/util/bpm_source_config.c
+++ b/util/bpm_source_config.c
@@ -8,6 +8,8 @@
#include <stdio.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "bpm_source_config.h"
int shirudo_bpm_parse_source_line(char *line,
@@ -20,18 +22,18 @@ int shirudo_bpm_parse_source_line(char *line,
return -EINVAL;
key = line;
- while (isspace((unsigned char)*key))
+ while (isspace((__u8)*key))
key++;
eq = strchr(key, '=');
if (!eq)
return 0;
*eq++ = '\0';
key_end = eq - 1;
- while (key_end > key && isspace((unsigned char)key_end[-1]))
+ while (key_end > key && isspace((__u8)key_end[-1]))
*--key_end = '\0';
val = eq;
- while (isspace((unsigned char)*val))
+ while (isspace((__u8)*val))
val++;
val[strcspn(val, "\r\n")] = '\0';
diff --git a/util/bpm_tree.c b/util/bpm_tree.c
index 762e8759..a7c1a3cd 100644
--- a/util/bpm_tree.c
+++ b/util/bpm_tree.c
@@ -8,6 +8,8 @@
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "bpm_tree.h"
#include "crypto.h"
#include "hex.h"
@@ -23,12 +25,12 @@
struct tree_parser {
const char *p;
const char *end;
- unsigned int depth;
+ __u32 depth;
};
static void tp_ws(struct tree_parser *p)
{
- while (p->p < p->end && isspace((unsigned char)*p->p))
+ while (p->p < p->end && isspace((__u8)*p->p))
p->p++;
}
@@ -49,7 +51,7 @@ static int tp_string(struct tree_parser *p, char *out, size_t cap)
if (p->p >= p->end || *p->p++ != '"')
return -EINVAL;
while (p->p < p->end) {
- unsigned char c = (unsigned char)*p->p++;
+ __u8 c = (__u8)*p->p++;
if (c == '"') {
if (cap)
@@ -59,7 +61,7 @@ static int tp_string(struct tree_parser *p, char *out, size_t cap)
if (c == '\\') {
if (p->p >= p->end)
return -EINVAL;
- c = (unsigned char)*p->p++;
+ c = (__u8)*p->p++;
switch (c) {
case '"':
case '\\':
@@ -133,14 +135,14 @@ static int tp_skip(struct tree_parser *p)
return -EINVAL;
}
}
- if (*p->p == '-' || isdigit((unsigned char)*p->p)) {
+ if (*p->p == '-' || isdigit((__u8)*p->p)) {
if (*p->p == '-')
p->p++;
- while (p->p < p->end && isdigit((unsigned char)*p->p))
+ while (p->p < p->end && isdigit((__u8)*p->p))
p->p++;
if (p->p < p->end && *p->p == '.') {
p->p++;
- while (p->p < p->end && isdigit((unsigned char)*p->p))
+ while (p->p < p->end && isdigit((__u8)*p->p))
p->p++;
}
return 0;
@@ -160,10 +162,10 @@ static int tp_skip(struct tree_parser *p)
return -EINVAL;
}
-static int tp_int(struct tree_parser *p, int64_t *out)
+static int tp_int(struct tree_parser *p, __s64 *out)
{
char *end;
- long long v;
+ __s64 v;
tp_ws(p);
errno = 0;
@@ -171,7 +173,7 @@ static int tp_int(struct tree_parser *p, int64_t *out)
if (end == p->p || end > p->end || errno == ERANGE)
return -EINVAL;
p->p = end;
- *out = (int64_t)v;
+ *out = (__s64)v;
return 0;
}
@@ -424,7 +426,7 @@ int shirudo_tree_publication_append(struct shirudo_tree_publication *pub,
/* The "base" and "head" objects: {"seq": <int>} plus fields to skip. */
static int tp_seq_object(struct tree_parser *p, char *key, size_t cap,
- int64_t *seq, bool *seen_seq)
+ __s64 *seq, bool *seen_seq)
{
bool first = true;
@@ -457,7 +459,7 @@ int shirudo_tree_parse(const char *body, size_t len,
bool seen_schema = false, seen_generation = false;
bool seen_base = false, seen_base_seq = false;
bool seen_operations = false, seen_head = false, seen_head_seq = false;
- int64_t schema = 0;
+ __s64 schema = 0;
int rc;
if (!body || !out || len > SHIRUDO_JSON_MAX_BYTES)
@@ -701,7 +703,7 @@ int shirudo_tree_operation_digest(const struct shirudo_tree_operation *op,
size_t body_cap = 1024u * 1024u;
struct shirudo_json_emit e;
char *body, hex[65];
- uint8_t digest[32];
+ __u8 digest[32];
int rc;
body = xmalloc(body_cap);
diff --git a/util/bpm_tree.h b/util/bpm_tree.h
index 866c8c51..5f507dd5 100644
--- a/util/bpm_tree.h
+++ b/util/bpm_tree.h
@@ -5,7 +5,8 @@
#define SHIRUDO_BPM_TREE_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "chain.h"
@@ -49,7 +50,7 @@ struct shirudo_tree_artifact {
};
struct shirudo_tree_operation {
- int64_t seq;
+ __s64 seq;
char op[16];
char id[SHIRUDO_TREE_STRING_MAX];
struct shirudo_tree_selector selector;
@@ -61,20 +62,20 @@ struct shirudo_tree_operation {
struct shirudo_tree_publication {
int schema;
char tree_id[SHIRUDO_TREE_STRING_MAX];
- int64_t generation;
+ __s64 generation;
char parent_manifest_digest[128];
- int64_t base_seq;
+ __s64 base_seq;
struct shirudo_tree_operation *operations;
int operations_capacity;
int n_operations;
- int64_t head_seq;
+ __s64 head_seq;
};
struct shirudo_tree_state {
int schema;
char tree_id[SHIRUDO_TREE_STRING_MAX];
- int64_t generation;
- int64_t last_seq;
+ __s64 generation;
+ __s64 last_seq;
/* Software projection is only a crash-recovery cursor when TPM is absent. */
char projection_root[65];
char projection_root_tpm[65];
@@ -107,9 +108,9 @@ int shirudo_tree_selector_matches(const struct shirudo_tree_selector *selector);
int shirudo_tree_state_read(struct shirudo_tree_state *out);
int shirudo_tree_state_write(const struct shirudo_tree_state *state);
-int shirudo_tree_event_append(int64_t seq, const char *operation_digest,
- const uint8_t projection_root[32]);
-int shirudo_tree_event_replay(uint8_t out[32]);
+int shirudo_tree_event_append(__s64 seq, const char *operation_digest,
+ const __u8 projection_root[32]);
+int shirudo_tree_event_replay(__u8 out[32]);
void shirudo_tree_emit_status(struct shirudo_json_emit *e);
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 36036826..f9e9f5be 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -21,6 +21,8 @@
#include <unistd.h>
#include <zlib.h>
+#include <shirudo/misc.h>
+
#include "bpm_tree.h"
#include "chain.h"
#include "io.h"
@@ -121,21 +123,21 @@ static int read_config_file(const char *path, struct shirudo_tree_kv *out,
while (fgets(line, sizeof(line), f)) {
char *start = line, *end, *eq;
- while (*start && isspace((unsigned char)*start))
+ while (*start && isspace((__u8)*start))
start++;
if (*start == '#' || *start == '\0')
continue;
end = start + strlen(start);
- while (end > start && isspace((unsigned char)end[-1]))
+ while (end > start && isspace((__u8)end[-1]))
*--end = '\0';
eq = strchr(start, '=');
if (!eq)
continue;
*eq++ = '\0';
end = eq + strlen(eq);
- while (end > eq && isspace((unsigned char)end[-1]))
+ while (end > eq && isspace((__u8)end[-1]))
*--end = '\0';
- while (*eq && isspace((unsigned char)*eq))
+ while (*eq && isspace((__u8)*eq))
eq++;
rc = kv_add(out, n, max, start, eq);
if (rc)
@@ -154,12 +156,12 @@ static int read_config_file(const char *path, struct shirudo_tree_kv *out,
*/
#define SHIRUDO_TREE_SELECTOR_HASH_SIZE 128
-static unsigned int selector_hash(const char *key)
+static __u32 selector_hash(const char *key)
{
- uint32_t hash = 2166136261u;
+ __u32 hash = 2166136261u;
for (; *key; key++)
- hash = (hash ^ (unsigned char)*key) * 16777619u;
+ hash = (hash ^ (__u8)*key) * 16777619u;
return hash;
}
@@ -168,7 +170,7 @@ static void selector_hash_build(const struct shirudo_tree_selector *s,
{
memset(slots, 0xff, sizeof(int) * SHIRUDO_TREE_SELECTOR_HASH_SIZE);
for (int i = 0; i < s->n_kconfig; i++) {
- unsigned int slot = selector_hash(s->kconfig[i].key) &
+ __u32 slot = selector_hash(s->kconfig[i].key) &
(SHIRUDO_TREE_SELECTOR_HASH_SIZE - 1);
while (slots[slot] >= 0 &&
@@ -183,7 +185,7 @@ static int selector_hash_find(const struct shirudo_tree_selector *s,
const int slots[SHIRUDO_TREE_SELECTOR_HASH_SIZE],
const char *key)
{
- unsigned int slot = selector_hash(key) &
+ __u32 slot = selector_hash(key) &
(SHIRUDO_TREE_SELECTOR_HASH_SIZE - 1);
for (int probes = 0; probes < SHIRUDO_TREE_SELECTOR_HASH_SIZE;
@@ -210,7 +212,7 @@ static int selector_config_line(const char *line,
return 0;
strcpy(copy, line);
start = copy;
- while (*start && isspace((unsigned char)*start))
+ while (*start && isspace((__u8)*start))
start++;
if (!*start)
return 0;
@@ -230,12 +232,12 @@ static int selector_config_line(const char *line,
return 0;
*eq++ = '\0';
end = start + strlen(start);
- while (end > start && isspace((unsigned char)end[-1]))
+ while (end > start && isspace((__u8)end[-1]))
*--end = '\0';
- while (*eq && isspace((unsigned char)*eq))
+ while (*eq && isspace((__u8)*eq))
eq++;
end = eq + strlen(eq);
- while (end > eq && isspace((unsigned char)end[-1]))
+ while (end > eq && isspace((__u8)end[-1]))
*--end = '\0';
value = eq;
if (value[0] == '"') {
@@ -406,10 +408,10 @@ int shirudo_tree_selector_matches(const struct shirudo_tree_selector *s)
return 1;
}
-static int tree_read_file(const char *path, unsigned char **body, size_t *len)
+static int tree_read_file(const char *path, __u8 **body, size_t *len)
{
struct stat st;
- unsigned char *p;
+ __u8 *p;
ssize_t got;
int fd = open(path, O_RDONLY | O_CLOEXEC);
@@ -436,11 +438,19 @@ static int tree_read_file(const char *path, unsigned char **body, size_t *len)
return 0;
}
+/* hex is exactly 64 hex digits: a SHA-256 digest. */
+static bool hex32_valid(const char *hex)
+{
+ __u8 raw[32];
+
+ return !shirudo_hex_decode(hex, raw, sizeof(raw));
+}
+
int shirudo_tree_state_read(struct shirudo_tree_state *out)
{
- unsigned char *body;
+ __u8 *body;
size_t len;
- int64_t schema = 0;
+ __s64 schema = 0;
struct shirudo_json_field f[] = {
{ "schema", SHIRUDO_JSON_T_INT, &schema, 0, 0, NULL, 0 },
{ "tree_id", SHIRUDO_JSON_T_STRING, out->tree_id,
@@ -469,15 +479,15 @@ int shirudo_tree_state_read(struct shirudo_tree_state *out)
sizeof(out->projection_root_tpm));
}
if (!rc && (strlen(out->projection_root) != 64 ||
- shirudo_hex_decode(out->projection_root, (uint8_t[32]){}, 32) ||
+ !hex32_valid(out->projection_root) ||
(out->projection_root_tpm[0] &&
(strlen(out->projection_root_tpm) != 64 ||
- shirudo_hex_decode(out->projection_root_tpm, (uint8_t[32]){}, 32)))))
+ !hex32_valid(out->projection_root_tpm)))))
rc = -EINVAL;
if (!rc && out->manifest_digest[0] &&
(strlen(out->manifest_digest) != 71 ||
strncmp(out->manifest_digest, "sha256:", 7) ||
- shirudo_hex_decode(out->manifest_digest + 7, (uint8_t[32]){}, 32)))
+ !hex32_valid(out->manifest_digest + 7)))
rc = -EINVAL;
out->schema = (int)schema;
xfree(body, len + 1);
@@ -521,8 +531,8 @@ int shirudo_tree_state_write(const struct shirudo_tree_state *s)
return tree_atomic_write(SHIRUDO_BPM_TREE_STATE_PATH, buf, e.len);
}
-int shirudo_tree_event_append(int64_t seq, const char *operation_digest,
- const uint8_t projection_root[32])
+int shirudo_tree_event_append(__s64 seq, const char *operation_digest,
+ const __u8 projection_root[32])
{
char buf[1024], root[65];
struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
@@ -530,7 +540,7 @@ int shirudo_tree_event_append(int64_t seq, const char *operation_digest,
int fd, rc;
if (!operation_digest || strncmp(operation_digest, "sha256:", 7) ||
- shirudo_hex_decode(operation_digest + 7, (uint8_t[32]){}, 32))
+ !hex32_valid(operation_digest + 7))
return -EINVAL;
shirudo_hex_encode(projection_root, 32, root);
shirudo_json_obj_open(&e);
@@ -573,13 +583,13 @@ out:
return rc;
}
-int shirudo_tree_event_replay(uint8_t out[32])
+int shirudo_tree_event_replay(__u8 out[32])
{
- unsigned char *body;
+ __u8 *body;
size_t len;
char *save, *line;
- uint8_t chain[32] = {};
- int64_t previous_seq = 0;
+ __u8 chain[32] = {};
+ __s64 previous_seq = 0;
int rc = tree_read_file(SHIRUDO_BPM_TREE_EVENTLOG_PATH, &body, &len);
if (rc)
@@ -588,7 +598,7 @@ int shirudo_tree_event_replay(uint8_t out[32])
line = strtok_r(NULL, "\n", &save)) {
char digest[128] = {};
char projection[65] = {};
- int64_t seq = 0;
+ __s64 seq = 0;
struct shirudo_json_field f[] = {
{ "seq", SHIRUDO_JSON_T_INT, &seq, 0, 0, NULL, 0 },
{ "digest", SHIRUDO_JSON_T_STRING, digest, sizeof(digest),
@@ -596,7 +606,7 @@ int shirudo_tree_event_replay(uint8_t out[32])
{ "projection_root", SHIRUDO_JSON_T_STRING, projection,
sizeof(projection), 0, NULL, 0 },
};
- uint8_t d[32], next[64], recorded[32];
+ __u8 d[32], next[64], recorded[32];
rc = shirudo_json_parse(line, strlen(line), f, ARRAY_SIZE(f));
if (rc || !f[0].seen || !f[1].seen || !f[2].seen ||
@@ -628,7 +638,7 @@ int shirudo_tree_event_replay(uint8_t out[32])
void shirudo_tree_emit_status(struct shirudo_json_emit *e)
{
struct shirudo_tree_state s = {};
- uint8_t live[32], replay[32];
+ __u8 live[32], replay[32];
char hex[65];
bool configured = access(SHIRUDO_BPM_TREE_ETC_PATH, R_OK) == 0;
diff --git a/util/chain.c b/util/chain.c
index 903d70ed..e8322806 100644
--- a/util/chain.c
+++ b/util/chain.c
@@ -15,6 +15,8 @@
#include <sys/types.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "chain.h"
#include "config.h"
#include "crypto.h"
@@ -24,14 +26,14 @@
#include "paths.h"
#include "tpm.h"
-void shirudo_sha256(const void *data, size_t len, uint8_t out[32])
+void shirudo_sha256(const void *data, size_t len, __u8 out[32])
{
if (shirudo_sha256_buffer(data, len, out) != 0)
memset(out, 0, 32);
}
void shirudo_sha256_2(const void *a, size_t a_len,
- const void *b, size_t b_len, uint8_t out[32])
+ const void *b, size_t b_len, __u8 out[32])
{
struct shirudo_sha256_ctx ctx;
@@ -49,8 +51,8 @@ void shirudo_sha256_2(const void *a, size_t a_len,
int shirudo_ct_eq(const void *a, const void *b, size_t n)
{
- const uint8_t *pa = a, *pb = b;
- uint8_t diff = 0;
+ const __u8 *pa = a, *pb = b;
+ __u8 diff = 0;
size_t i;
for (i = 0; i < n; i++)
@@ -64,12 +66,12 @@ static int member_cmp(const void *a, const void *b)
((const struct shirudo_measured_upatch *)b)->name);
}
-void shirudo_chain_init_event(const uint8_t cert_sha[32],
+void shirudo_chain_init_event(const __u8 cert_sha[32],
struct shirudo_measured_upatch *set, size_t n,
- const uint8_t tree_root[32],
- uint8_t set_digest[32], uint8_t event[32])
+ const __u8 tree_root[32],
+ __u8 set_digest[32], __u8 event[32])
{
- uint8_t tag[sizeof("shirudo-init") + 96];
+ __u8 tag[sizeof("shirudo-init") + 96];
size_t taglen = sizeof("shirudo-init"), listing_len = 0, off = 0, i;
char *listing;
@@ -100,9 +102,9 @@ void shirudo_chain_init_event(const uint8_t cert_sha[32],
}
void shirudo_chain_upatch_event(const char *tag, const char *name,
- const uint8_t payload[32], uint8_t event[32])
+ const __u8 payload[32], __u8 event[32])
{
- uint8_t buf[sizeof("shirudo-unload") + 32 + SHIRUDO_UPATCH_NAME_MAX];
+ __u8 buf[sizeof("shirudo-unload") + 32 + SHIRUDO_UPATCH_NAME_MAX];
size_t taglen = strlen(tag) + 1, off = 0;
size_t namelen = strnlen(name, SHIRUDO_UPATCH_NAME_MAX);
@@ -115,36 +117,36 @@ void shirudo_chain_upatch_event(const char *tag, const char *name,
shirudo_sha256(buf, off, event);
}
-void shirudo_chain_key_event(const char *tag, const uint8_t cert_sha[32],
- uint8_t event[32])
+void shirudo_chain_key_event(const char *tag, const __u8 cert_sha[32],
+ __u8 event[32])
{
shirudo_sha256_2(tag, strlen(tag) + 1, cert_sha, 32, event);
}
-void shirudo_chain_tree_event(int64_t seq, const uint8_t operation_digest[32],
- uint8_t event[32])
+void shirudo_chain_tree_event(__s64 seq, const __u8 operation_digest[32],
+ __u8 event[32])
{
- uint8_t buf[sizeof("shirudo-tree") + 8 + 32];
+ __u8 buf[sizeof("shirudo-tree") + 8 + 32];
size_t taglen = sizeof("shirudo-tree"), i;
- uint64_t s = (uint64_t)seq;
+ __u64 s = (__u64)seq;
memcpy(buf, "shirudo-tree", taglen);
for (i = 0; i < 8; i++)
- buf[taglen + i] = (uint8_t)(s >> (56 - 8 * i));
+ buf[taglen + i] = (__u8)(s >> (56 - 8 * i));
memcpy(buf + taglen + 8, operation_digest, 32);
shirudo_sha256(buf, taglen + 40, event);
}
-void shirudo_chain_fold(uint8_t chain[32], const uint8_t event[32])
+void shirudo_chain_fold(__u8 chain[32], const __u8 event[32])
{
- uint8_t cat[64];
+ __u8 cat[64];
memcpy(cat, chain, 32);
memcpy(cat + 32, event, 32);
shirudo_sha256(cat, sizeof(cat), chain);
}
-void shirudo_chain_anchor(const uint8_t name[SHIRUDO_TPM_NAME_LEN],
+void shirudo_chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
char *out, size_t cap)
{
char hex[2 * SHIRUDO_TPM_NAME_LEN + 1];
@@ -155,8 +157,8 @@ void shirudo_chain_anchor(const uint8_t name[SHIRUDO_TPM_NAME_LEN],
}
/* The binding PCR values in PCR order, as PolicyPCR hashes them. */
-static void bind_values(const uint8_t pcr_before[32],
- const uint8_t phase_before[32], uint8_t out[64])
+static void bind_values(const __u8 pcr_before[32],
+ const __u8 phase_before[32], __u8 out[64])
{
memcpy(out, pcr_before, 32);
memcpy(out + 32, phase_before, 32);
@@ -169,15 +171,15 @@ static void bind_values(const uint8_t pcr_before[32],
* the written name is the one the record, the anchor and every later
* comparison use.
*/
-static void register_expectation(const uint8_t pcr_before[32],
- const uint8_t phase_before[32],
- uint8_t branch_written[32],
- uint8_t branch_init[32],
- uint8_t policy[32],
- uint8_t name_unwritten[SHIRUDO_TPM_NAME_LEN],
- uint8_t name_written[SHIRUDO_TPM_NAME_LEN])
+static void register_expectation(const __u8 pcr_before[32],
+ const __u8 phase_before[32],
+ __u8 branch_written[32],
+ __u8 branch_init[32],
+ __u8 policy[32],
+ __u8 name_unwritten[SHIRUDO_TPM_NAME_LEN],
+ __u8 name_written[SHIRUDO_TPM_NAME_LEN])
{
- uint8_t values[64];
+ __u8 values[64];
bind_values(pcr_before, phase_before, values);
shirudo_tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2,
@@ -190,14 +192,14 @@ static void register_expectation(const uint8_t pcr_before[32],
}
static bool name_equals(const struct shirudo_tpm_nv_info *info,
- const uint8_t name[SHIRUDO_TPM_NAME_LEN])
+ const __u8 name[SHIRUDO_TPM_NAME_LEN])
{
return info->name_len == SHIRUDO_TPM_NAME_LEN &&
shirudo_ct_eq(info->name, name, SHIRUDO_TPM_NAME_LEN);
}
static int register_define(struct shirudo_tpm *tpm, bool defined,
- const uint8_t policy[32])
+ const __u8 policy[32])
{
struct shirudo_config cfg = {};
int rc;
@@ -217,11 +219,11 @@ out:
return rc;
}
-int shirudo_chain_init(const uint8_t event[32],
+int shirudo_chain_init(const __u8 event[32],
struct shirudo_chain_init_info *info)
{
- uint8_t unwritten[SHIRUDO_TPM_NAME_LEN], written[SHIRUDO_TPM_NAME_LEN];
- uint8_t branch_written[32], branch_init[32], policy[32];
+ __u8 unwritten[SHIRUDO_TPM_NAME_LEN], written[SHIRUDO_TPM_NAME_LEN];
+ __u8 branch_written[32], branch_init[32], policy[32];
struct shirudo_eventlog_init rec;
struct shirudo_tpm_nv_info nv;
struct shirudo_tpm *tpm = NULL;
@@ -336,9 +338,9 @@ out:
return rc;
}
-int shirudo_chain_extend(const uint8_t event[32], uint8_t out_chain[32])
+int shirudo_chain_extend(const __u8 event[32], __u8 out_chain[32])
{
- uint8_t branch_written[32], branch_init[32], policy[32], values[64];
+ __u8 branch_written[32], branch_init[32], policy[32], values[64];
struct shirudo_eventlog_init rec;
struct shirudo_tpm_nv_info nv;
struct shirudo_tpm *tpm = NULL;
@@ -381,7 +383,7 @@ out:
return rc;
}
-int shirudo_chain_read(uint8_t out[32])
+int shirudo_chain_read(__u8 out[32])
{
struct shirudo_tpm *tpm = NULL;
int rc;
@@ -398,7 +400,7 @@ int shirudo_chain_status(struct shirudo_chain_status *st)
{
struct shirudo_tpm_nv_info nv;
struct shirudo_tpm *tpm = NULL;
- uint32_t reset_count = 0;
+ __u32 reset_count = 0;
int rc;
memset(st, 0, sizeof(*st));
diff --git a/util/chain.h b/util/chain.h
index 98f787f9..e1c8e844 100644
--- a/util/chain.h
+++ b/util/chain.h
@@ -11,7 +11,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "eventlog.h"
#include "hex.h"
@@ -34,11 +35,11 @@
#define SHIRUDO_CHAIN_ANCHOR_MAX 128
void shirudo_sha256(const void *data, size_t len,
- uint8_t out[SHIRUDO_CHAIN_DIGEST_LEN]);
+ __u8 out[SHIRUDO_CHAIN_DIGEST_LEN]);
void shirudo_sha256_2(const void *a, size_t a_len,
const void *b, size_t b_len,
- uint8_t out[SHIRUDO_CHAIN_DIGEST_LEN]);
+ __u8 out[SHIRUDO_CHAIN_DIGEST_LEN]);
/* Constant time; 1 when equal. */
int shirudo_ct_eq(const void *a, const void *b, size_t n);
@@ -52,34 +53,34 @@ struct shirudo_measured_upatch {
* The event digests every measuring path and the predictor share.
* The set is sorted by name before hashing.
*/
-void shirudo_chain_init_event(const uint8_t cert_sha[32],
+void shirudo_chain_init_event(const __u8 cert_sha[32],
struct shirudo_measured_upatch *set, size_t n,
- const uint8_t tree_root[32],
- uint8_t set_digest[32], uint8_t event[32]);
+ const __u8 tree_root[32],
+ __u8 set_digest[32], __u8 event[32]);
/* SHA256(tag || NUL || cert_sha256), tag "shirudo-key-revoke". */
-void shirudo_chain_key_event(const char *tag, const uint8_t cert_sha[32],
- uint8_t event[32]);
+void shirudo_chain_key_event(const char *tag, const __u8 cert_sha[32],
+ __u8 event[32]);
void shirudo_chain_upatch_event(const char *tag, const char *name,
- const uint8_t payload[32], uint8_t event[32]);
-void shirudo_chain_tree_event(int64_t seq, const uint8_t operation_digest[32],
- uint8_t event[32]);
-void shirudo_chain_fold(uint8_t chain[32], const uint8_t event[32]);
+ const __u8 payload[32], __u8 event[32]);
+void shirudo_chain_tree_event(__s64 seq, const __u8 operation_digest[32],
+ __u8 event[32]);
+void shirudo_chain_fold(__u8 chain[32], const __u8 event[32]);
/* The PCR record anchoring a register name: nvpcr-init:shirudo:0x..:<name>. */
-void shirudo_chain_anchor(const uint8_t name[SHIRUDO_TPM_NAME_LEN],
+void shirudo_chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
char *out, size_t cap);
struct shirudo_chain_init_info {
bool reused; /* written and on record */
bool initrd; /* bound while the initrd ran */
- uint32_t reset_count; /* the TPM's, for the record */
- uint8_t value[SHIRUDO_CHAIN_DIGEST_LEN];
- uint8_t pcr_before[SHIRUDO_CHAIN_DIGEST_LEN];
- uint8_t phase_before[SHIRUDO_CHAIN_DIGEST_LEN];
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u32 reset_count; /* the TPM's, for the record */
+ __u8 value[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 pcr_before[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 phase_before[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
size_t name_len;
char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];
- uint8_t anchor_digest[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 anchor_digest[SHIRUDO_CHAIN_DIGEST_LEN];
};
/*
@@ -90,7 +91,7 @@ struct shirudo_chain_init_info {
* when the owner hierarchy refuses, -EPROTO when the TPM's name is not
* the computed one.
*/
-int shirudo_chain_init(const uint8_t event[SHIRUDO_CHAIN_DIGEST_LEN],
+int shirudo_chain_init(const __u8 event[SHIRUDO_CHAIN_DIGEST_LEN],
struct shirudo_chain_init_info *info);
/*
@@ -99,11 +100,11 @@ int shirudo_chain_init(const uint8_t event[SHIRUDO_CHAIN_DIGEST_LEN],
* init record, index unwritten), -EACCES when the index on the TPM is
* not the one init recorded (recreated), -EIO on a TPM failure.
*/
-int shirudo_chain_extend(const uint8_t event[SHIRUDO_CHAIN_DIGEST_LEN],
- uint8_t out_chain[SHIRUDO_CHAIN_DIGEST_LEN]);
+int shirudo_chain_extend(const __u8 event[SHIRUDO_CHAIN_DIGEST_LEN],
+ __u8 out_chain[SHIRUDO_CHAIN_DIGEST_LEN]);
/* The live value; -ENODEV when no register is written this boot. */
-int shirudo_chain_read(uint8_t out[SHIRUDO_CHAIN_DIGEST_LEN]);
+int shirudo_chain_read(__u8 out[SHIRUDO_CHAIN_DIGEST_LEN]);
struct shirudo_chain_status {
bool tpm; /* a TPM answered */
@@ -112,8 +113,8 @@ struct shirudo_chain_status {
bool logged; /* init record this boot */
bool anchored; /* recorded name, same reset */
bool rebound; /* two records for one reset */
- uint8_t value[SHIRUDO_CHAIN_DIGEST_LEN];
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u8 value[SHIRUDO_CHAIN_DIGEST_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
size_t name_len;
struct shirudo_eventlog_init init;
};
diff --git a/util/conditions.c b/util/conditions.c
index a810dc00..b8463e97 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -8,6 +8,8 @@
#include <stdio.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "conditions.h"
size_t shirudo_conditions(const struct shirudo_conditions_input *in,
@@ -34,20 +36,20 @@ size_t shirudo_conditions(const struct shirudo_conditions_input *in,
c[n].status = "ALERT";
snprintf(c[n].detail, sizeof(c[n].detail),
"pin serial=%lld != live serial=%lld (swapped?)",
- (long long)in->keyring_serial_pinned,
- (long long)in->keyring_serial);
+ (__s64)in->keyring_serial_pinned,
+ (__s64)in->keyring_serial);
} else if (in->staging) {
c[n].status = "WARN";
snprintf(c[n].detail, sizeof(c[n].detail),
"staging mode: serial=%lld in the user "
"keyring; boot with bpf.keyring_unsealed=1 "
"and run init from the initrd",
- (long long)in->keyring_serial);
+ (__s64)in->keyring_serial);
} else {
c[n].status = "OK";
snprintf(c[n].detail, sizeof(c[n].detail),
"serial=%lld in the bpf keyring, %s",
- (long long)in->keyring_serial,
+ (__s64)in->keyring_serial,
in->ksp_seen ? "pin matches"
: "no pin recorded");
}
diff --git a/util/conditions.h b/util/conditions.h
index f40f557e..25f90f15 100644
--- a/util/conditions.h
+++ b/util/conditions.h
@@ -11,7 +11,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
/*
* The *_seen flags say whether the JSON carried a non-null value,
@@ -25,8 +26,8 @@ struct shirudo_conditions_input {
int lsm_attached; /* 0/1 */
int keyring_present;
int staging; /* enforcing with the cert in the user keyring */
- int64_t keyring_serial;
- int64_t keyring_serial_pinned;
+ __s64 keyring_serial;
+ __s64 keyring_serial_pinned;
int keyring_serial_matches; /* 0/1 if ksm_seen */
int ks_seen, ksp_seen, ksm_seen;
int chain_seen;
diff --git a/util/config.c b/util/config.c
index 79650b34..d3dc2efb 100644
--- a/util/config.c
+++ b/util/config.c
@@ -12,6 +12,8 @@
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "config.h"
#include "hex.h"
#include "json.h"
@@ -151,8 +153,8 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)
key = trim(p);
val = trim(eq + 1);
if (strcmp(key, "eventlog.max_bytes") == 0) {
- unsigned long v;
char *end;
+ __u64 v;
/* strtoul takes "-1" and "" as numbers. */
errno = 0;
@@ -201,8 +203,8 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)
map_export_bad(cfg, key, val,
"expected true or false");
} else if (strcmp(key, "map_export.otel_retries") == 0) {
- unsigned long v;
char *end;
+ __u64 v;
errno = 0;
v = strtoul(val, &end, 10);
@@ -211,24 +213,24 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)
"expected 0..10");
else
cfg->map_export_otel_retries =
- (unsigned int)v;
+ (__u32)v;
} else if (strcmp(key, "map_export.s3_uri") == 0) {
if (!set_string(cfg->map_export_s3_uri,
sizeof(cfg->map_export_s3_uri), val))
map_export_bad(cfg, key, val, "value too long");
} else if (strcmp(key, "map_export.log_record_max_bytes") == 0 ||
strcmp(key, "map_export.otlp_max_bytes") == 0) {
- unsigned long v;
char *end;
+ __u64 v;
errno = 0;
v = strtoul(val, &end, 10);
if (errno || *end || v == 0 || v > SHIRUDO_JSON_MAX_BYTES)
map_export_bad(cfg, key, val, "expected 1..8388608 bytes");
else if (strcmp(key, "map_export.log_record_max_bytes") == 0)
- cfg->map_export_log_record_max_bytes = (unsigned int)v;
+ cfg->map_export_log_record_max_bytes = (__u32)v;
else
- cfg->map_export_otlp_max_bytes = (unsigned int)v;
+ cfg->map_export_otlp_max_bytes = (__u32)v;
} else {
if (strncmp(key, "map_export.", 11) == 0)
map_export_bad(cfg, key, val, "unknown key");
diff --git a/util/config.h b/util/config.h
index f46fb7ae..4ed47d87 100644
--- a/util/config.h
+++ b/util/config.h
@@ -11,6 +11,8 @@
#include <stdbool.h>
#include <stddef.h>
+#include <shirudo/misc.h>
+
#include "tenant_id.h"
#define SHIRUDO_OWNER_AUTH_MAX 256
@@ -22,7 +24,7 @@
#define SHIRUDO_MAP_EXPORT_OTLP_MAX_DEFAULT (4u * 1024u * 1024u)
struct shirudo_config {
- unsigned long eventlog_max_bytes; /* 0 = unlimited */
+ __u64 eventlog_max_bytes; /* 0 = unlimited */
/*
* Owner-hierarchy auth used to define the NV index on TPMs whose
* owner auth is set. Empty (len 0) = empty-password session.
@@ -40,10 +42,10 @@ struct shirudo_config {
char map_export_otel_endpoint[SHIRUDO_MAP_EXPORT_ENDPOINT_MAX];
char map_export_tenant_id[SHIRUDO_TENANT_ID_MAX + 1];
bool map_export_otel_insecure;
- unsigned int map_export_otel_retries;
+ __u32 map_export_otel_retries;
char map_export_s3_uri[SHIRUDO_MAP_EXPORT_URI_MAX];
- unsigned int map_export_log_record_max_bytes;
- unsigned int map_export_otlp_max_bytes;
+ __u32 map_export_log_record_max_bytes;
+ __u32 map_export_otlp_max_bytes;
};
/* A missing file is not an error. */
diff --git a/util/crypto.h b/util/crypto.h
index 3392e132..a16b6895 100644
--- a/util/crypto.h
+++ b/util/crypto.h
@@ -12,10 +12,11 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
#include <curl/curl.h>
+#include <shirudo/misc.h>
+
#define SHIRUDO_CRYPTO_SHA256_LEN 32
struct shirudo_sha256_ctx {
@@ -26,17 +27,17 @@ int shirudo_sha256_init(struct shirudo_sha256_ctx *ctx);
int shirudo_sha256_update(struct shirudo_sha256_ctx *ctx,
const void *data, size_t len);
int shirudo_sha256_final(struct shirudo_sha256_ctx *ctx,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN]);
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN]);
void shirudo_sha256_free(struct shirudo_sha256_ctx *ctx);
int shirudo_sha256_buffer(const void *data, size_t len,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN]);
-int shirudo_base64_encode(const uint8_t *data, size_t len,
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN]);
+int shirudo_base64_encode(const __u8 *data, size_t len,
char *out, size_t out_sz);
/* 0 when valid, -EKEYREJECTED when not. */
-int shirudo_ecdsa_p256_verify(const uint8_t x[32], const uint8_t y[32],
- const uint8_t digest[SHIRUDO_CRYPTO_SHA256_LEN],
- const uint8_t r[32], const uint8_t s[32]);
+int shirudo_ecdsa_p256_verify(const __u8 x[32], const __u8 y[32],
+ const __u8 digest[SHIRUDO_CRYPTO_SHA256_LEN],
+ const __u8 r[32], const __u8 s[32]);
struct shirudo_cert {
void *impl;
@@ -45,19 +46,19 @@ struct shirudo_cert {
int shirudo_cert_read_file(const char *path, struct shirudo_cert *cert);
void shirudo_cert_free(struct shirudo_cert *cert);
int shirudo_cert_pubkey_digest(const struct shirudo_cert *cert,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN]);
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN]);
/* The provider's name for the cert's public key, e.g. "ML-DSA-87". */
int shirudo_cert_key_algorithm(const struct shirudo_cert *cert,
char *out, size_t out_sz);
-int shirudo_verify_detached(const uint8_t *payload, size_t payload_len,
- const uint8_t *signature, size_t signature_len,
+int shirudo_verify_detached(const __u8 *payload, size_t payload_len,
+ const __u8 *signature, size_t signature_len,
const struct shirudo_cert *cert);
/*
* Whether a signer of the CMS blob names this certificate, by key id
* or by issuer and serial: 1 when one does, 0 when none, -EINVAL when
* the blob does not parse. Says nothing about the signature's validity.
*/
-int shirudo_cms_signed_by(const uint8_t *signature, size_t signature_len,
+int shirudo_cms_signed_by(const __u8 *signature, size_t signature_len,
const struct shirudo_cert *cert);
struct shirudo_tls_ca {
@@ -77,44 +78,44 @@ void shirudo_tls_ca_cleanup(struct shirudo_tls_ca *ca);
*/
int shirudo_tls_curl_policy(CURL *curl, struct shirudo_tls_ca *ca);
-int shirudo_hmac_sha256(const uint8_t *key, size_t key_len,
+int shirudo_hmac_sha256(const __u8 *key, size_t key_len,
const void *data, size_t len,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN]);
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN]);
/* AES-128 in CFB mode with a zero IV and no padding. */
-int shirudo_aes128_cfb_encrypt(const uint8_t key[16], const uint8_t *in,
- size_t len, uint8_t *out);
+int shirudo_aes128_cfb_encrypt(const __u8 key[16], const __u8 *in,
+ size_t len, __u8 *out);
/*
* RSA-OAEP with SHA-256, MGF1-SHA-256 and the given label to the
* modulus n with the public exponent e (0 for 65537).
*/
-int shirudo_rsa_oaep_encrypt(const uint8_t *n, size_t n_len, uint32_t e,
+int shirudo_rsa_oaep_encrypt(const __u8 *n, size_t n_len, __u32 e,
const void *label, size_t label_len,
- const uint8_t *in, size_t in_len,
- uint8_t *out, size_t cap, size_t *out_len);
+ const __u8 *in, size_t in_len,
+ __u8 *out, size_t cap, size_t *out_len);
/*
* One ephemeral P-256 ECDH against the point (x, y): the ephemeral
* public point and the shared secret's x coordinate.
*/
-int shirudo_ecdh_p256_ephemeral(const uint8_t x[32], const uint8_t y[32],
- uint8_t eph_x[32], uint8_t eph_y[32],
- uint8_t z[32]);
+int shirudo_ecdh_p256_ephemeral(const __u8 x[32], const __u8 y[32],
+ __u8 eph_x[32], __u8 eph_y[32],
+ __u8 z[32]);
-int shirudo_base64_decode(const char *in, size_t in_len, uint8_t *out,
+int shirudo_base64_decode(const char *in, size_t in_len, __u8 *out,
size_t cap, size_t *out_len);
-int shirudo_cert_read_der(const uint8_t *der, size_t len,
+int shirudo_cert_read_der(const __u8 *der, size_t len,
struct shirudo_cert *cert);
struct shirudo_cert_pubkey {
bool rsa;
- uint8_t n[512];
+ __u8 n[512];
size_t n_len;
- uint32_t e;
- uint8_t x[32];
- uint8_t y[32];
+ __u32 e;
+ __u8 x[32];
+ __u8 y[32];
};
int shirudo_cert_pubkey(const struct shirudo_cert *cert,
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index 0bc27cf5..9a939a4a 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -24,6 +24,8 @@
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
+#include <shirudo/misc.h>
+
#include "crypto.h"
#include "log.h"
#include "malloc.h"
@@ -73,9 +75,9 @@ int shirudo_sha256_update(struct shirudo_sha256_ctx *ctx,
}
int shirudo_sha256_final(struct shirudo_sha256_ctx *ctx,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN])
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN])
{
- unsigned int len = 0;
+ __u32 len = 0;
int rc;
if (!ctx || !ctx->impl || !out)
@@ -95,7 +97,7 @@ void shirudo_sha256_free(struct shirudo_sha256_ctx *ctx)
}
int shirudo_sha256_buffer(const void *data, size_t len,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN])
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN])
{
struct shirudo_sha256_ctx ctx;
int rc;
@@ -110,24 +112,24 @@ int shirudo_sha256_buffer(const void *data, size_t len,
return rc;
}
-int shirudo_base64_encode(const uint8_t *data, size_t len,
+int shirudo_base64_encode(const __u8 *data, size_t len,
char *out, size_t out_sz)
{
size_t need = 4 * ((len + 2) / 3) + 1;
if (!data || !out || out_sz < need || len > INT_MAX)
return -EINVAL;
- if (EVP_EncodeBlock((unsigned char *)out, data, (int)len) < 0)
+ if (EVP_EncodeBlock((__u8 *)out, data, (int)len) < 0)
return -EIO;
out[need - 1] = '\0';
return 0;
}
-int shirudo_ecdsa_p256_verify(const uint8_t x[32], const uint8_t y[32],
- const uint8_t digest[32],
- const uint8_t r[32], const uint8_t s[32])
+int shirudo_ecdsa_p256_verify(const __u8 x[32], const __u8 y[32],
+ const __u8 digest[32],
+ const __u8 r[32], const __u8 s[32])
{
- unsigned char point[65], *der = NULL;
+ __u8 point[65], *der = NULL;
EVP_PKEY_CTX *kctx = NULL, *vctx = NULL;
OSSL_PARAM params[3];
BIGNUM *br = NULL, *bs = NULL;
@@ -200,11 +202,11 @@ void shirudo_cert_free(struct shirudo_cert *cert)
* marshal as a bare EVP_PKEY on older releases.
*/
int shirudo_cert_pubkey_digest(const struct shirudo_cert *cert,
- uint8_t out[SHIRUDO_CRYPTO_SHA256_LEN])
+ __u8 out[SHIRUDO_CRYPTO_SHA256_LEN])
{
- unsigned char *der = NULL, *p;
+ __u8 *der = NULL, *p;
int der_len, encoded_len, rc = -EINVAL;
- unsigned int len = 0;
+ __u32 len = 0;
X509_PUBKEY *spki;
if (!cert || !cert->impl || !out)
@@ -252,8 +254,8 @@ int shirudo_cert_key_algorithm(const struct shirudo_cert *cert,
* with signedAttrs and SHA-512 or bare on OpenSSL 4, and an RSA one
* verify through one call; ML-DSA needs libcrypto >= 3.5.
*/
-int shirudo_verify_detached(const uint8_t *payload, size_t payload_len,
- const uint8_t *signature, size_t signature_len,
+int shirudo_verify_detached(const __u8 *payload, size_t payload_len,
+ const __u8 *signature, size_t signature_len,
const struct shirudo_cert *cert)
{
BIO *sig = NULL, *data = NULL, *out = NULL;
@@ -292,7 +294,7 @@ done:
return rc;
}
-int shirudo_cms_signed_by(const uint8_t *signature, size_t signature_len,
+int shirudo_cms_signed_by(const __u8 *signature, size_t signature_len,
const struct shirudo_cert *cert)
{
STACK_OF(CMS_SignerInfo) *sis;
@@ -325,8 +327,8 @@ done:
return rc;
}
-int shirudo_hmac_sha256(const uint8_t *key, size_t key_len,
- const void *data, size_t len, uint8_t out[32])
+int shirudo_hmac_sha256(const __u8 *key, size_t key_len,
+ const void *data, size_t len, __u8 out[32])
{
EVP_MAC_CTX *ctx = NULL;
OSSL_PARAM params[2];
@@ -350,12 +352,12 @@ int shirudo_hmac_sha256(const uint8_t *key, size_t key_len,
return rc;
}
-int shirudo_aes128_cfb_encrypt(const uint8_t key[16], const uint8_t *in,
- size_t len, uint8_t *out)
+int shirudo_aes128_cfb_encrypt(const __u8 key[16], const __u8 *in,
+ size_t len, __u8 *out)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int n = 0, fin = 0, rc = -EIO;
- uint8_t iv[16] = {};
+ __u8 iv[16] = {};
if (!ctx || len > INT_MAX)
goto out;
@@ -370,16 +372,16 @@ out:
return rc;
}
-int shirudo_rsa_oaep_encrypt(const uint8_t *n, size_t n_len, uint32_t e,
+int shirudo_rsa_oaep_encrypt(const __u8 *n, size_t n_len, __u32 e,
const void *label, size_t label_len,
- const uint8_t *in, size_t in_len,
- uint8_t *out, size_t cap, size_t *out_len)
+ const __u8 *in, size_t in_len,
+ __u8 *out, size_t cap, size_t *out_len)
{
EVP_PKEY_CTX *kctx = NULL, *ectx = NULL;
BIGNUM *bn = NULL, *be = NULL;
OSSL_PARAM_BLD *bld = NULL;
OSSL_PARAM *params = NULL;
- unsigned char *lbl = NULL;
+ __u8 *lbl = NULL;
EVP_PKEY *pkey = NULL;
int rc = -EIO;
@@ -427,14 +429,14 @@ out:
return rc;
}
-int shirudo_ecdh_p256_ephemeral(const uint8_t x[32], const uint8_t y[32],
- uint8_t eph_x[32], uint8_t eph_y[32],
- uint8_t z[32])
+int shirudo_ecdh_p256_ephemeral(const __u8 x[32], const __u8 y[32],
+ __u8 eph_x[32], __u8 eph_y[32],
+ __u8 z[32])
{
EVP_PKEY_CTX *kctx = NULL, *dctx = NULL;
EVP_PKEY *eph = NULL, *peer = NULL;
BIGNUM *bx = NULL, *by = NULL;
- unsigned char point[65];
+ __u8 point[65];
OSSL_PARAM params[3];
size_t zlen = 32;
int rc = -EIO;
@@ -475,7 +477,7 @@ out:
return rc;
}
-int shirudo_base64_decode(const char *in, size_t in_len, uint8_t *out,
+int shirudo_base64_decode(const char *in, size_t in_len, __u8 *out,
size_t cap, size_t *out_len)
{
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
@@ -484,7 +486,7 @@ int shirudo_base64_decode(const char *in, size_t in_len, uint8_t *out,
if (!ctx || in_len > INT_MAX || cap < 3 * (in_len / 4) + 3)
goto out;
EVP_DecodeInit(ctx);
- if (EVP_DecodeUpdate(ctx, out, &n, (const unsigned char *)in,
+ if (EVP_DecodeUpdate(ctx, out, &n, (const __u8 *)in,
(int)in_len) < 0 ||
EVP_DecodeFinal(ctx, out + n, &fin) < 0)
goto out;
@@ -495,10 +497,10 @@ out:
return rc;
}
-int shirudo_cert_read_der(const uint8_t *der, size_t len,
+int shirudo_cert_read_der(const __u8 *der, size_t len,
struct shirudo_cert *cert)
{
- const unsigned char *p = der;
+ const __u8 *p = der;
if (!der || !cert || len > LONG_MAX)
return -EINVAL;
@@ -526,7 +528,7 @@ int shirudo_cert_pubkey(const struct shirudo_cert *cert,
goto out;
out->rsa = true;
out->n_len = (size_t)BN_bn2bin(a, out->n);
- out->e = (uint32_t)BN_get_word(b);
+ out->e = (__u32)BN_get_word(b);
rc = 0;
} else if (id == EVP_PKEY_EC) {
if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME,
diff --git a/util/elf_target.c b/util/elf_target.c
index 893f3548..36b27182 100644
--- a/util/elf_target.c
+++ b/util/elf_target.c
@@ -17,6 +17,8 @@
#include <string.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "elf_target.h"
#include "malloc.h"
@@ -33,22 +35,22 @@ extern int elf_resolve_pattern_offsets(const char *binary_path,
static int u64_cmp(const void *a, const void *b)
{
- uint64_t av = *(const uint64_t *)a;
- uint64_t bv = *(const uint64_t *)b;
+ __u64 av = *(const __u64 *)a;
+ __u64 bv = *(const __u64 *)b;
return av > bv ? 1 : av < bv ? -1 : 0;
}
static int note_build_id(Elf_Data *data,
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
- uint32_t *build_id_len)
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
+ __u32 *build_id_len)
{
size_t off = 0;
while (off < data->d_size) {
GElf_Nhdr nhdr;
size_t name_off, desc_off, next;
- const unsigned char *p = data->d_buf;
+ const __u8 *p = data->d_buf;
next = gelf_getnote(data, off, &nhdr, &name_off, &desc_off);
if (!next)
@@ -72,8 +74,8 @@ static int note_build_id(Elf_Data *data,
}
static int elf_build_id(Elf *elf,
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
- uint32_t *build_id_len)
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
+ __u32 *build_id_len)
{
Elf_Scn *scn = NULL;
GElf_Ehdr ehdr;
@@ -119,8 +121,8 @@ static int elf_build_id(Elf *elf,
}
int shirudo_elf_build_id_fd(int fd,
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
- uint32_t *build_id_len)
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
+ __u32 *build_id_len)
{
Elf *elf;
int err;
@@ -146,7 +148,7 @@ int shirudo_elf_target_resolve(const char *path, const char *pattern,
{
Elf *elf = NULL;
unsigned long *resolved_offsets = NULL;
- uint32_t i, unique;
+ __u32 i, unique;
size_t nr_resolved = 0;
int fd = -1, err;
@@ -192,7 +194,7 @@ int shirudo_elf_target_resolve(const char *path, const char *pattern,
}
for (i = 0; i < nr_resolved; i++)
out->offsets[i] = resolved_offsets[i];
- out->nr_offsets = (uint32_t)nr_resolved;
+ out->nr_offsets = (__u32)nr_resolved;
shirudo_libc_free(resolved_offsets);
resolved_offsets = NULL;
diff --git a/util/elf_target.h b/util/elf_target.h
index 68028b4c..e31f75ad 100644
--- a/util/elf_target.h
+++ b/util/elf_target.h
@@ -6,23 +6,22 @@
#ifndef SHIRUDO_ELF_TARGET_H
#define SHIRUDO_ELF_TARGET_H
-#include <stdint.h>
-
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
struct shirudo_elf_target {
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX];
- uint32_t build_id_len;
- uint64_t *offsets;
- uint32_t nr_offsets;
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX];
+ __u32 build_id_len;
+ __u64 *offsets;
+ __u32 nr_offsets;
};
int shirudo_elf_target_resolve(const char *path, const char *pattern,
struct shirudo_elf_target *out);
int shirudo_elf_build_id_fd(int fd,
- uint8_t build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
- uint32_t *build_id_len);
+ __u8 build_id[SHIRUDO_UPATCH_BUILD_ID_MAX],
+ __u32 *build_id_len);
void shirudo_elf_target_free(struct shirudo_elf_target *target);
diff --git a/util/eventlog.c b/util/eventlog.c
index bf7258f3..9184346e 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -8,6 +8,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
@@ -15,6 +16,8 @@
#include <time.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "chain.h"
#include "eventlog.h"
#include "timefmt.h"
@@ -101,15 +104,15 @@ out:
return 0;
}
-int shirudo_eventlog_would_exceed(unsigned long max_bytes)
+int shirudo_eventlog_would_exceed(__u64 max_bytes)
{
- unsigned long cur = 0;
struct stat st;
+ __u64 cur = 0;
if (max_bytes == 0)
return 0; /* unlimited */
if (stat(shirudo_eventlog_path(), &st) == 0)
- cur = (unsigned long)st.st_size;
+ cur = (__u64)st.st_size;
return cur + SHIRUDO_EVENTLOG_LINE_MAX > max_bytes;
}
@@ -305,8 +308,8 @@ static int replay_line(const char *line, size_t len, bool is_init, void *ctx)
{ "digest", SHIRUDO_JSON_T_STRING, digest_hex,
sizeof(digest_hex), 0, NULL, 0 },
};
- uint8_t digest[32], cat[64];
- uint8_t *chain = ctx;
+ __u8 digest[32], cat[64];
+ __u8 *chain = ctx;
if (shirudo_json_parse(line, len, f, 1) || !f[0].seen ||
shirudo_hex_decode(digest_hex, digest, sizeof(digest)))
@@ -319,9 +322,9 @@ static int replay_line(const char *line, size_t len, bool is_init, void *ctx)
return 0;
}
-int shirudo_eventlog_replay(uint8_t out[32])
+int shirudo_eventlog_replay(__u8 out[32])
{
- uint8_t chain[32] = {};
+ __u8 chain[32] = {};
int rc;
rc = walk_boot(replay_line, chain);
@@ -335,7 +338,7 @@ static int init_line(const char *line, size_t len, bool is_init, void *ctx)
char pcr_before[2 * 32 + 1], name[2 * SHIRUDO_TPM_NAME_LEN + 1];
char phase_before[2 * 32 + 1], anchor[2 * 32 + 1], bound_in[8];
struct shirudo_eventlog_init *out = ctx;
- int64_t pcr = -1, reset_count = -1;
+ __s64 pcr = -1, reset_count = -1;
struct shirudo_json_field f[] = {
{ "pcr_before", SHIRUDO_JSON_T_STRING, pcr_before,
sizeof(pcr_before), 0, NULL, 0 },
@@ -375,14 +378,14 @@ static int init_line(const char *line, size_t len, bool is_init, void *ctx)
out->records++;
if (out->records == 1 ||
(have_reset_count && out->have_reset_count &&
- (uint32_t)reset_count != out->reset_count))
+ (__u32)reset_count != out->reset_count))
out->records_epoch = 1;
else
out->records_epoch++;
out->have_reset_count = have_reset_count;
- out->reset_count = (uint32_t)reset_count;
+ out->reset_count = (__u32)reset_count;
out->name_len = SHIRUDO_TPM_NAME_LEN;
- out->pcr = (unsigned int)pcr;
+ out->pcr = (__u32)pcr;
return 0;
}
diff --git a/util/eventlog.h b/util/eventlog.h
index 6c2da159..dced5338 100644
--- a/util/eventlog.h
+++ b/util/eventlog.h
@@ -10,7 +10,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "tpm_wire.h"
@@ -42,7 +43,7 @@ int shirudo_eventlog_adopt_pending(void);
* the register accumulates them. -ENOENT when this boot has no init
* record, -EINVAL on a malformed line.
*/
-int shirudo_eventlog_replay(uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
+int shirudo_eventlog_replay(__u8 out[SHIRUDO_TPM_DIGEST_LEN]);
/*
* What the last init record of this boot says about the register, and
@@ -50,17 +51,17 @@ int shirudo_eventlog_replay(uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
* the last one names means the register was bound again.
*/
struct shirudo_eventlog_init {
- unsigned int pcr;
+ __u32 pcr;
bool initrd; /* bound while the initrd ran */
- uint8_t pcr_before[SHIRUDO_TPM_DIGEST_LEN];
- uint8_t phase_before[SHIRUDO_TPM_DIGEST_LEN];
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u8 pcr_before[SHIRUDO_TPM_DIGEST_LEN];
+ __u8 phase_before[SHIRUDO_TPM_DIGEST_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
size_t name_len;
- uint8_t anchor_digest[SHIRUDO_TPM_DIGEST_LEN];
+ __u8 anchor_digest[SHIRUDO_TPM_DIGEST_LEN];
bool have_reset_count;
- uint32_t reset_count;
- unsigned int records; /* init records this boot */
- unsigned int records_epoch; /* for this reset count */
+ __u32 reset_count;
+ __u32 records; /* init records this boot */
+ __u32 records_epoch; /* for this reset count */
};
int shirudo_eventlog_boot_init(struct shirudo_eventlog_init *out);
@@ -74,7 +75,7 @@ int shirudo_eventlog_boot_lines(int (*cb)(const char *line, size_t len,
* must stay in lockstep, so a caller refuses the whole operation (no
* PROG_LOAD, no extend, no append) when this returns 1.
*/
-int shirudo_eventlog_would_exceed(unsigned long max_bytes);
+int shirudo_eventlog_would_exceed(__u64 max_bytes);
void shirudo_eventlog_timestamp(char *out, size_t cap); /* ISO-8601 UTC */
void shirudo_eventlog_boot_id(char *out, size_t cap); /* /proc boot_id */
diff --git a/util/format.c b/util/format.c
index 2ae1bee7..58efda88 100644
--- a/util/format.c
+++ b/util/format.c
@@ -7,9 +7,11 @@
#include <stdio.h>
+#include <shirudo/misc.h>
+
#include "format.h"
-void shirudo_human_size(uint64_t bytes, char *out, size_t out_cap)
+void shirudo_human_size(__u64 bytes, char *out, size_t out_cap)
{
double v = (double)bytes;
const char *suf = "B";
@@ -25,7 +27,7 @@ void shirudo_human_size(uint64_t bytes, char *out, size_t out_cap)
suf = "K";
} else {
snprintf(out, out_cap, "%llu%s",
- (unsigned long long)bytes, suf);
+ (__u64)bytes, suf);
return;
}
if (v >= 100.0)
diff --git a/util/format.h b/util/format.h
index 633e2af8..06edc0d8 100644
--- a/util/format.h
+++ b/util/format.h
@@ -9,13 +9,14 @@
#define SHIRUDO_UTIL_FORMAT_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
/*
* Format a byte count in IEC-binary human units: B/K/M/G. Precision is
* picked so the rendered value stays within four significant digits
* regardless of magnitude (16K / 1.8M / 5.57G / 1.23G / 124G).
*/
-void shirudo_human_size(uint64_t bytes, char *out, size_t out_cap);
+void shirudo_human_size(__u64 bytes, char *out, size_t out_cap);
#endif /* SHIRUDO_UTIL_FORMAT_H */
diff --git a/util/hex.c b/util/hex.c
index 22891508..7271bd72 100644
--- a/util/hex.c
+++ b/util/hex.c
@@ -4,10 +4,12 @@
#include <errno.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "hex.h"
#include "malloc.h"
-void shirudo_hex_encode(const uint8_t *in, size_t n, char *out)
+void shirudo_hex_encode(const __u8 *in, size_t n, char *out)
{
static const char d[] = "0123456789abcdef";
size_t i;
@@ -39,7 +41,7 @@ int shirudo_hex_nibble(int c)
return -1;
}
-int shirudo_hex_decode(const char *hex, uint8_t *out, size_t n)
+int shirudo_hex_decode(const char *hex, __u8 *out, size_t n)
{
size_t i;
@@ -51,7 +53,7 @@ int shirudo_hex_decode(const char *hex, uint8_t *out, size_t n)
if (hi < 0 || lo < 0)
return -EINVAL;
- out[i] = (uint8_t)((hi << 4) | lo);
+ out[i] = (__u8)((hi << 4) | lo);
}
return 0;
}
diff --git a/util/hex.h b/util/hex.h
index 1220a924..e188548b 100644
--- a/util/hex.h
+++ b/util/hex.h
@@ -9,10 +9,11 @@
#define SHIRUDO_HEX_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
/* out needs 2 * n + 1 bytes. */
-void shirudo_hex_encode(const uint8_t *in, size_t n, char *out);
+void shirudo_hex_encode(const __u8 *in, size_t n, char *out);
/* The same, xmalloc'd to 2 * n + 1 bytes. */
char *shirudo_hex_alloc(const void *in, size_t n);
@@ -20,6 +21,6 @@ char *shirudo_hex_alloc(const void *in, size_t n);
int shirudo_hex_nibble(int c);
/* Exactly 2 * n hex digits, either case. 0 or -EINVAL. */
-int shirudo_hex_decode(const char *hex, uint8_t *out, size_t n);
+int shirudo_hex_decode(const char *hex, __u8 *out, size_t n);
#endif /* SHIRUDO_HEX_H */
diff --git a/util/identity.c b/util/identity.c
index 0f4bce89..b0e2db8d 100644
--- a/util/identity.c
+++ b/util/identity.c
@@ -6,7 +6,6 @@
*/
#include <errno.h>
-#include <stdint.h>
#include <string.h>
#include <unistd.h>
@@ -14,14 +13,16 @@
#include <bpf/bpf.h>
+#include <shirudo/misc.h>
+
#include "identity.h"
#include "paths.h"
int shirudo_identity_read(struct shirudo_identity *out)
{
struct bpf_map_info info = {};
- uint32_t len = sizeof(info);
- uint32_t zero = 0;
+ __u32 len = sizeof(info);
+ __u32 zero = 0;
int fd, rc;
memset(out, 0, sizeof(*out));
diff --git a/util/identity.h b/util/identity.h
index fcb4dcaa..2e6e3dff 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -8,7 +8,7 @@
#ifndef SHIRUDO_UTIL_IDENTITY_H
#define SHIRUDO_UTIL_IDENTITY_H
-#include <stdint.h>
+#include <shirudo/misc.h>
/* The keyring kinds the kernel names in a verdict (enum bpf_sig_keyring). */
#define SHIRUDO_SIG_KEYRING_USER 4
@@ -25,17 +25,17 @@
* lsm/key_permission guards with the keyring itself.
*/
struct shirudo_identity {
- uint32_t sb_dev;
- uint32_t exe_dev;
- uint64_t exe_ino;
- int32_t keyring_serial;
- uint32_t keyring_type;
- uint32_t alt_exe_dev;
- uint32_t pad;
- uint64_t alt_exe_ino;
- uint32_t n_keys;
- uint32_t pad2;
- int32_t key_serials[SHIRUDO_IDENTITY_KEYS_MAX];
+ __u32 sb_dev;
+ __u32 exe_dev;
+ __u64 exe_ino;
+ __s32 keyring_serial;
+ __u32 keyring_type;
+ __u32 alt_exe_dev;
+ __u32 pad;
+ __u64 alt_exe_ino;
+ __u32 n_keys;
+ __u32 pad2;
+ __s32 key_serials[SHIRUDO_IDENTITY_KEYS_MAX];
};
/*
diff --git a/util/io.c b/util/io.c
index a3a04850..f054d812 100644
--- a/util/io.c
+++ b/util/io.c
@@ -7,12 +7,14 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "io.h"
#include "malloc.h"
int shirudo_write_all(int fd, const void *buf, size_t len)
{
- const unsigned char *p = buf;
+ const __u8 *p = buf;
while (len) {
ssize_t n = write(fd, p, len);
diff --git a/util/json.c b/util/json.c
index 6a15287b..11febe76 100644
--- a/util/json.c
+++ b/util/json.c
@@ -7,11 +7,12 @@
*/
#include <errno.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "hex.h"
#include "json.h"
@@ -39,11 +40,11 @@ static void put_raw(struct shirudo_json_emit *e, const char *s)
*/
static void put_string(struct shirudo_json_emit *e, const char *s)
{
- const unsigned char *p = (const unsigned char *)s;
+ const __u8 *p = (const __u8 *)s;
put(e, '"');
while (*p && !e->err) {
- unsigned char c = *p;
+ __u8 c = *p;
if (c == '"' || c == '\\') {
put(e, '\\');
@@ -169,14 +170,14 @@ void shirudo_json_kv_string(struct shirudo_json_emit *e,
}
void shirudo_json_kv_int(struct shirudo_json_emit *e,
- const char *key, int64_t val)
+ const char *key, __s64 val)
{
char num[24];
sep(e);
put_string(e, key);
put(e, ':');
- snprintf(num, sizeof(num), "%lld", (long long)val);
+ snprintf(num, sizeof(num), "%lld", (__s64)val);
put_raw(e, num);
}
@@ -227,7 +228,7 @@ void shirudo_json_raw(struct shirudo_json_emit *e, const char *value,
struct pctx {
const char *p;
const char *end;
- unsigned int depth;
+ __u32 depth;
struct shirudo_json_field *fields;
size_t n_fields;
};
@@ -258,7 +259,7 @@ static int hex4(const char *s)
return v;
}
-static int emit_byte(char *dst, size_t cap, size_t *len, unsigned char b)
+static int emit_byte(char *dst, size_t cap, size_t *len, __u8 b)
{
size_t n = *len;
@@ -273,7 +274,7 @@ static int emit_byte(char *dst, size_t cap, size_t *len, unsigned char b)
return 0;
}
-static int utf8_emit(char *dst, size_t cap, size_t *len, unsigned int cp)
+static int utf8_emit(char *dst, size_t cap, size_t *len, __u32 cp)
{
int rc;
@@ -302,11 +303,11 @@ static int parse_string(struct pctx *c, char *dst, size_t cap)
return -EINVAL;
c->p++;
for (;;) {
- unsigned char ch;
+ __u8 ch;
if (c->p >= c->end)
return -EINVAL; /* unterminated */
- ch = (unsigned char)*c->p++;
+ ch = (__u8)*c->p++;
if (ch == '"')
break;
if (ch == '\\') {
@@ -586,7 +587,7 @@ static int parse_number(struct pctx *c, struct shirudo_json_field *f)
return -EINVAL;
memcpy(numbuf, start, nlen);
numbuf[nlen] = '\0';
- *(int64_t *)f->dst = (int64_t)strtoll(numbuf, NULL, 10);
+ *(__s64 *)f->dst = (__s64)strtoll(numbuf, NULL, 10);
f->seen = 1;
}
return 0;
diff --git a/util/json.h b/util/json.h
index 9739066b..748b3420 100644
--- a/util/json.h
+++ b/util/json.h
@@ -9,7 +9,8 @@
#define SHIRUDO_JSON_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#ifndef SHIRUDO_JSON_MAX_BYTES
#define SHIRUDO_JSON_MAX_BYTES (8u * 1024u * 1024u)
@@ -26,8 +27,8 @@ struct shirudo_json_emit {
size_t cap;
size_t len;
int err; /* sticky; -ENOSPC, -EILSEQ, -E2BIG */
- unsigned int depth; /* open-container nesting */
- uint32_t nonempty; /* bit d set: container at depth d has a member */
+ __u32 depth; /* open-container nesting */
+ __u32 nonempty; /* bit d set: container at depth d has a member */
};
/*
@@ -43,7 +44,7 @@ void shirudo_json_arr_close(struct shirudo_json_emit *e);
void shirudo_json_kv_string(struct shirudo_json_emit *e,
const char *key, const char *val);
void shirudo_json_kv_int(struct shirudo_json_emit *e,
- const char *key, int64_t val);
+ const char *key, __s64 val);
void shirudo_json_kv_bool(struct shirudo_json_emit *e,
const char *key, int val);
void shirudo_json_kv_null(struct shirudo_json_emit *e, const char *key);
@@ -61,7 +62,7 @@ void shirudo_json_raw(struct shirudo_json_emit *e, const char *value,
*/
enum shirudo_json_vtype {
SHIRUDO_JSON_T_STRING, /* dst: char[dst_cap] (NUL-terminated) */
- SHIRUDO_JSON_T_INT, /* dst: int64_t * */
+ SHIRUDO_JSON_T_INT, /* dst: __s64 * */
SHIRUDO_JSON_T_BOOL, /* dst: int * (0/1) */
SHIRUDO_JSON_T_OBJECT, /* nested object -> sub[] sub-schema */
SHIRUDO_JSON_T_ARRAY, /* sub[] schema; dst = shirudo_json_array * */
diff --git a/util/kernel_abi.c b/util/kernel_abi.c
index 6519130c..50a08913 100644
--- a/util/kernel_abi.c
+++ b/util/kernel_abi.c
@@ -11,7 +11,6 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -21,6 +20,8 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
+
#include "kernel_abi.h"
#define SHIRUDO_MAP_SHA_SIZE 32U
@@ -38,7 +39,7 @@ static int abi_fail(char *detail, size_t detail_sz, const char *fmt, ...)
return -EOPNOTSUPP;
}
-static const struct btf_type *unwrap_type(const struct btf *btf, uint32_t id)
+static const struct btf_type *unwrap_type(const struct btf *btf, __u32 id)
{
const struct btf_type *t;
int i;
@@ -57,10 +58,10 @@ 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)
+ __u32 *index)
{
const struct btf_member *members = btf_members(t);
- uint32_t i;
+ __u32 i;
for (i = 0; i < btf_vlen(t); i++) {
const char *member_name =
@@ -74,14 +75,14 @@ static const struct btf_member *find_member(const struct btf *btf,
return NULL;
}
-static bool is_unsigned_int(const struct btf_type *t, uint32_t bytes)
+static bool is_unsigned_int(const struct btf_type *t, __u32 bytes)
{
return t && btf_is_int(t) && t->size == bytes &&
btf_int_offset(t) == 0 && btf_int_bits(t) == bytes * 8 &&
!(btf_int_encoding(t) & BTF_INT_SIGNED);
}
-static bool is_signed_int(const struct btf_type *t, uint32_t bytes)
+static bool is_signed_int(const struct btf_type *t, __u32 bytes)
{
return t && btf_is_int(t) && t->size == bytes &&
btf_int_offset(t) == 0 && btf_int_bits(t) == bytes * 8 &&
@@ -102,7 +103,7 @@ int shirudo_kernel_abi_validate_btf(const struct btf *btf,
const struct btf_type *map, *aux, *sha_type, *elem_type;
const struct btf_type *excl_type, *sig_type, *member_type;
const struct btf_array *array;
- uint32_t member_idx;
+ __u32 member_idx;
int map_id, aux_id;
if (detail && detail_sz)
@@ -115,7 +116,7 @@ int shirudo_kernel_abi_validate_btf(const struct btf *btf,
if (map_id < 0)
return abi_fail(detail, detail_sz,
"kernel BTF has no struct bpf_map; boot a Shirudo-patched kernel");
- map = btf__type_by_id(btf, (uint32_t)map_id);
+ map = btf__type_by_id(btf, (__u32)map_id);
if (!map)
return abi_fail(detail, detail_sz,
"kernel BTF cannot resolve struct bpf_map");
@@ -158,7 +159,7 @@ int shirudo_kernel_abi_validate_btf(const struct btf *btf,
if (aux_id < 0)
return abi_fail(detail, detail_sz,
"kernel BTF has no struct bpf_prog_aux; boot a Shirudo-patched kernel");
- aux = btf__type_by_id(btf, (uint32_t)aux_id);
+ aux = btf__type_by_id(btf, (__u32)aux_id);
if (!aux)
return abi_fail(detail, detail_sz,
"kernel BTF cannot resolve struct bpf_prog_aux");
diff --git a/util/kernel_build_id.c b/util/kernel_build_id.c
index e9388701..384119c9 100644
--- a/util/kernel_build_id.c
+++ b/util/kernel_build_id.c
@@ -4,10 +4,11 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
-#include <stdint.h>
#include <string.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "kernel_build_id.h"
#define KERNEL_NOTES_PATH "/sys/kernel/notes"
@@ -18,7 +19,7 @@ static size_t align4(size_t value)
return (value + 3u) & ~(size_t)3u;
}
-static int read_u32(const unsigned char *p, size_t remaining, uint32_t *out)
+static int read_u32(const __u8 *p, size_t remaining, __u32 *out)
{
if (remaining < sizeof(*out))
return -EINVAL;
@@ -29,13 +30,13 @@ static int read_u32(const unsigned char *p, size_t remaining, uint32_t *out)
int shirudo_kernel_build_id_from_notes(const void *notes, size_t notes_len,
char out[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX])
{
- const unsigned char *p = notes;
+ const __u8 *p = notes;
if (!notes || !out)
return -EINVAL;
out[0] = '\0';
while (notes_len) {
- uint32_t namesz, descsz, type;
+ __u32 namesz, descsz, type;
size_t name_off, desc_off, next;
if (notes_len < 12 || read_u32(p, notes_len, &namesz) ||
@@ -54,7 +55,7 @@ int shirudo_kernel_build_id_from_notes(const void *notes, size_t notes_len,
if (type == 3 && namesz >= 3 && !memcmp(p + name_off, "GNU", 3)) {
if (!descsz || descsz > SHIRUDO_KERNEL_BUILD_ID_MAX)
return -E2BIG;
- for (uint32_t i = 0; i < descsz; i++)
+ for (__u32 i = 0; i < descsz; i++)
snprintf(out + i * 2, 3, "%02x", p[desc_off + i]);
return 0;
}
@@ -66,7 +67,7 @@ int shirudo_kernel_build_id_from_notes(const void *notes, size_t notes_len,
int shirudo_kernel_build_id_read(char out[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX])
{
- unsigned char notes[KERNEL_NOTES_MAX];
+ __u8 notes[KERNEL_NOTES_MAX];
size_t len = 0;
int fd, rc;
diff --git a/util/keyring.c b/util/keyring.c
index 7f9a9db7..3df4b65b 100644
--- a/util/keyring.c
+++ b/util/keyring.c
@@ -9,7 +9,6 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,6 +17,8 @@
#include <linux/keyctl.h>
+#include <shirudo/misc.h>
+
#include "keyring.h"
#include "io.h"
#include "malloc.h"
@@ -246,7 +247,7 @@ static void holds_cert_cb(key_serial_t serial, const char *type,
}
int shirudo_keyring_holds_cert(key_serial_t keyring,
- const unsigned char *der, size_t der_len)
+ const __u8 *der, size_t der_len)
{
struct holds_cert_ctx ctx = {};
const char *type, *description;
@@ -274,7 +275,7 @@ int shirudo_keyring_holds_cert(key_serial_t keyring,
}
key_serial_t shirudo_keyring_find_cert(key_serial_t keyring,
- const unsigned char *der,
+ const __u8 *der,
size_t der_len)
{
struct holds_cert_ctx ctx = {};
@@ -316,7 +317,7 @@ key_serial_t shirudo_keyring_bpf_serial(void)
return ret < 0 ? -errno : (key_serial_t)ret;
}
-key_serial_t shirudo_keyring_bpf_add_cert(const unsigned char *cert,
+key_serial_t shirudo_keyring_bpf_add_cert(const __u8 *cert,
size_t cert_len)
{
long ret = sys_add_key("asymmetric", NULL, cert, cert_len,
diff --git a/util/keyring.h b/util/keyring.h
index 308fd379..19ef1483 100644
--- a/util/keyring.h
+++ b/util/keyring.h
@@ -9,9 +9,10 @@
#define SHIRUDO_KEYRING_H
#include <stddef.h>
-#include <stdint.h>
-typedef int32_t key_serial_t;
+#include <shirudo/misc.h>
+
+typedef __s32 key_serial_t;
typedef void (*shirudo_keyring_cb)(key_serial_t serial,
const char *type,
@@ -52,11 +53,11 @@ int shirudo_keyring_seal(key_serial_t keyring);
* <keyring>; no key material leaves the kernel.
*/
int shirudo_keyring_holds_cert(key_serial_t keyring,
- const unsigned char *der, size_t der_len);
+ const __u8 *der, size_t der_len);
/* The serial of <keyring>'s key for the certificate, -ENOKEY, -errno. */
key_serial_t shirudo_keyring_find_cert(key_serial_t keyring,
- const unsigned char *der,
+ const __u8 *der,
size_t der_len);
/* Unlink <key> from <keyring>; sealing does not stop this, the LSM may. */
@@ -79,7 +80,7 @@ int shirudo_keyring_list(key_serial_t keyring,
/* Its serial, or -errno on a kernel without one. */
key_serial_t shirudo_keyring_bpf_serial(void);
/* Enrol a DER certificate; -EPERM once the keyring is sealed. */
-key_serial_t shirudo_keyring_bpf_add_cert(const unsigned char *cert,
+key_serial_t shirudo_keyring_bpf_add_cert(const __u8 *cert,
size_t cert_len);
/* Seal it for the rest of the boot: no key can be linked afterwards. */
int shirudo_keyring_bpf_seal(void);
diff --git a/util/keys.c b/util/keys.c
index b577ff6f..464d831f 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -14,6 +14,8 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "crypto.h"
#include "hex.h"
#include "io.h"
@@ -27,18 +29,18 @@
struct keys_candidate {
char path[PATH_MAX];
- unsigned char *der;
+ __u8 *der;
size_t der_sz;
- uint8_t sha256[32];
- uint8_t fingerprint[32];
+ __u8 sha256[32];
+ __u8 fingerprint[32];
bool done;
};
-int shirudo_keys_read_der(const char *path, unsigned char **out,
+int shirudo_keys_read_der(const char *path, __u8 **out,
size_t *out_sz)
{
- unsigned char *buf;
struct stat st;
+ __u8 *buf;
ssize_t n;
int fd;
@@ -72,8 +74,8 @@ int shirudo_keys_read_der(const char *path, unsigned char **out,
return 0;
}
-int shirudo_keys_digest_der(const unsigned char *der, size_t der_sz,
- uint8_t sha256[32], uint8_t fingerprint[32])
+int shirudo_keys_digest_der(const __u8 *der, size_t der_sz,
+ __u8 sha256[32], __u8 fingerprint[32])
{
struct shirudo_cert cert = {};
int rc;
@@ -89,11 +91,11 @@ int shirudo_keys_digest_der(const unsigned char *der, size_t der_sz,
return rc;
}
-static int shirudo_keys_digest_file(const char *path, uint8_t sha256[32],
- uint8_t fingerprint[32])
+static int shirudo_keys_digest_file(const char *path, __u8 sha256[32],
+ __u8 fingerprint[32])
{
- unsigned char *der = NULL;
size_t der_sz = 0;
+ __u8 *der = NULL;
int rc;
rc = shirudo_keys_read_der(path, &der, &der_sz);
@@ -104,7 +106,7 @@ static int shirudo_keys_digest_file(const char *path, uint8_t sha256[32],
return rc;
}
-int shirudo_keys_path_for(const char *dir, const uint8_t fingerprint[32],
+int shirudo_keys_path_for(const char *dir, const __u8 fingerprint[32],
const char *suffix, char *out, size_t cap)
{
char hex[65];
@@ -116,7 +118,7 @@ int shirudo_keys_path_for(const char *dir, const uint8_t fingerprint[32],
}
bool shirudo_keys_is_revoked(const char *revoked_dir,
- const uint8_t fingerprint[32])
+ const __u8 fingerprint[32])
{
char path[PATH_MAX];
@@ -126,11 +128,11 @@ bool shirudo_keys_is_revoked(const char *revoked_dir,
return access(path, F_OK) == 0;
}
-int shirudo_keys_vouch_payload(const unsigned char *der, size_t der_sz,
- unsigned char **out, size_t *out_sz)
+int shirudo_keys_vouch_payload(const __u8 *der, size_t der_sz,
+ __u8 **out, size_t *out_sz)
{
size_t n = sizeof(SHIRUDO_KEYS_VOUCH_PREFIX) + der_sz;
- unsigned char *buf = xmalloc(n);
+ __u8 *buf = xmalloc(n);
if (!buf)
return -ENOMEM;
@@ -143,12 +145,12 @@ int shirudo_keys_vouch_payload(const unsigned char *der, size_t der_sz,
}
int shirudo_keys_verify_vouch(const struct shirudo_trusted_set *set,
- const unsigned char *der, size_t der_sz,
- const unsigned char *sig, size_t sig_sz,
- uint8_t voucher[32])
+ const __u8 *der, size_t der_sz,
+ const __u8 *sig, size_t sig_sz,
+ __u8 voucher[32])
{
- unsigned char *payload = NULL;
size_t payload_sz = 0, i;
+ __u8 *payload = NULL;
int rc;
rc = shirudo_keys_vouch_payload(der, der_sz, &payload, &payload_sz);
@@ -178,7 +180,7 @@ static int cert_cmp(const void *a, const void *b)
}
static bool set_holds(const struct shirudo_trusted_set *set,
- const uint8_t fingerprint[32])
+ const __u8 fingerprint[32])
{
size_t i;
@@ -190,8 +192,8 @@ static bool set_holds(const struct shirudo_trusted_set *set,
}
static int set_add(struct shirudo_trusted_set *set, const char *path,
- const uint8_t sha256[32], const uint8_t fingerprint[32],
- const uint8_t voucher[32])
+ const __u8 sha256[32], const __u8 fingerprint[32],
+ const __u8 voucher[32])
{
struct shirudo_trusted_cert *c;
@@ -256,10 +258,10 @@ static int admit_vouched(struct shirudo_trusted_set *set,
progress = false;
for (i = 0; i < n_cand; i++) {
struct keys_candidate *c = &cand[i];
- unsigned char *sig = NULL;
char vouch[PATH_MAX];
- uint8_t voucher[32];
size_t sig_sz = 0;
+ __u8 *sig = NULL;
+ __u8 voucher[32];
int rc;
if (c->done)
@@ -359,7 +361,7 @@ int shirudo_keys_load(const char *keys_dir, const char *revoked_dir,
bool with_trusted, struct shirudo_trusted_set *set)
{
struct keys_candidate *cand = NULL;
- uint8_t sha256[32], fingerprint[32];
+ __u8 sha256[32], fingerprint[32];
char revoked[PATH_MAX];
char path[PATH_MAX];
size_t n_cand = 0;
@@ -403,10 +405,10 @@ static int digest_cmp(const void *a, const void *b)
return memcmp(a, b, 32);
}
-void shirudo_keys_anchor_digest(const uint8_t (*sha256)[32], size_t n,
- uint8_t out[32])
+void shirudo_keys_anchor_digest(const __u8 sha256[][32], size_t n,
+ __u8 out[32])
{
- uint8_t sorted[SHIRUDO_KEYS_MAX][32];
+ __u8 sorted[SHIRUDO_KEYS_MAX][32];
size_t i;
if (n == 1) {
@@ -424,9 +426,9 @@ void shirudo_keys_anchor_digest(const uint8_t (*sha256)[32], size_t n,
}
void shirudo_keys_set_digest(const struct shirudo_trusted_set *set,
- uint8_t out[32])
+ __u8 out[32])
{
- uint8_t sha[SHIRUDO_KEYS_MAX][32];
+ __u8 sha[SHIRUDO_KEYS_MAX][32];
size_t i;
for (i = 0; i < set->n; i++)
@@ -439,11 +441,11 @@ int shirudo_keys_write_file(const char *path, const void *data, size_t len)
return shirudo_seal_write_file(path, data, len, 0644);
}
-int shirudo_keys_install(const char *keys_dir, const unsigned char *der,
- size_t der_sz, const unsigned char *sig,
+int shirudo_keys_install(const char *keys_dir, const __u8 *der,
+ size_t der_sz, const __u8 *sig,
size_t sig_sz, char *out, size_t cap)
{
- uint8_t sha256[32], fingerprint[32];
+ __u8 sha256[32], fingerprint[32];
char vouch[PATH_MAX];
char dir[PATH_MAX];
int rc;
diff --git a/util/keys.h b/util/keys.h
index 26eaa934..2f88782e 100644
--- a/util/keys.h
+++ b/util/keys.h
@@ -11,7 +11,8 @@
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#define SHIRUDO_KEYS_MAX 16
#define SHIRUDO_KEYS_DER_MAX (1024 * 1024)
@@ -25,9 +26,9 @@
/* voucher is the admitting member's fingerprint, zero for the primary. */
struct shirudo_trusted_cert {
char path[PATH_MAX];
- uint8_t sha256[32]; /* of the DER file */
- uint8_t fingerprint[32]; /* of the public key */
- uint8_t voucher[32];
+ __u8 sha256[32]; /* of the DER file */
+ __u8 fingerprint[32]; /* of the public key */
+ __u8 voucher[32];
bool primary; /* cert.der */
};
@@ -55,32 +56,32 @@ int shirudo_keys_load(const char *keys_dir, const char *revoked_dir,
bool with_trusted, struct shirudo_trusted_set *set);
/* A whole file of at most SHIRUDO_KEYS_DER_MAX bytes, xmalloc'd. */
-int shirudo_keys_read_der(const char *path, unsigned char **out,
+int shirudo_keys_read_der(const char *path, __u8 **out,
size_t *out_sz);
-int shirudo_keys_digest_der(const unsigned char *der, size_t der_sz,
- uint8_t sha256[32], uint8_t fingerprint[32]);
+int shirudo_keys_digest_der(const __u8 *der, size_t der_sz,
+ __u8 sha256[32], __u8 fingerprint[32]);
/*
* The digest the init event covers: the one certificate's digest, or
* the SHA-256 over the sorted digests of several. The same function
* serves the operator predicting a register for a set.
*/
-void shirudo_keys_anchor_digest(const uint8_t (*sha256)[32], size_t n,
- uint8_t out[32]);
+void shirudo_keys_anchor_digest(const __u8 sha256[][32], size_t n,
+ __u8 out[32]);
void shirudo_keys_set_digest(const struct shirudo_trusted_set *set,
- uint8_t out[32]);
+ __u8 out[32]);
bool shirudo_keys_is_revoked(const char *revoked_dir,
- const uint8_t fingerprint[32]);
+ const __u8 fingerprint[32]);
/* <dir>/<fingerprint hex><suffix>, the name a certificate has in a set. */
-int shirudo_keys_path_for(const char *dir, const uint8_t fingerprint[32],
+int shirudo_keys_path_for(const char *dir, const __u8 fingerprint[32],
const char *suffix, char *out, size_t cap);
/* The bytes a vouch signs: the prefix and the certificate, xmalloc'd. */
-int shirudo_keys_vouch_payload(const unsigned char *der, size_t der_sz,
- unsigned char **out, size_t *out_sz);
+int shirudo_keys_vouch_payload(const __u8 *der, size_t der_sz,
+ __u8 **out, size_t *out_sz);
/*
* Whether a member of the set signed the vouch for this certificate:
@@ -88,9 +89,9 @@ int shirudo_keys_vouch_payload(const unsigned char *der, size_t der_sz,
* did.
*/
int shirudo_keys_verify_vouch(const struct shirudo_trusted_set *set,
- const unsigned char *der, size_t der_sz,
- const unsigned char *sig, size_t sig_sz,
- uint8_t voucher[32]);
+ const __u8 *der, size_t der_sz,
+ const __u8 *sig, size_t sig_sz,
+ __u8 voucher[32]);
int shirudo_keys_write_file(const char *path, const void *data, size_t len);
@@ -99,8 +100,8 @@ int shirudo_keys_write_file(const char *path, const void *data, size_t len);
* by fingerprint, the vouch first so a visible certificate always has
* one. out gets the certificate's path.
*/
-int shirudo_keys_install(const char *keys_dir, const unsigned char *der,
- size_t der_sz, const unsigned char *sig,
+int shirudo_keys_install(const char *keys_dir, const __u8 *der,
+ size_t der_sz, const __u8 *sig,
size_t sig_sz, char *out, size_t cap);
#endif /* SHIRUDO_UTIL_KEYS_H */
diff --git a/util/kmodules.c b/util/kmodules.c
index 10c23325..c84d83b4 100644
--- a/util/kmodules.c
+++ b/util/kmodules.c
@@ -8,7 +8,6 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -17,6 +16,8 @@
#include <time.h>
+#include <shirudo/misc.h>
+
#include "format.h"
#include "kmodules.h"
#include "malloc.h"
@@ -95,7 +96,7 @@ static int kmod_find(const struct shirudo_kmod_set *set, const char *name)
int shirudo_kmodules_read(struct shirudo_kmod_set *set)
{
size_t mod_cap = 64, deps_cap = 256, edge_cap = 256, n_edges = 0;
- struct edge { uint16_t user, dep; } *edges = NULL;
+ struct edge { __u16 user, dep; } *edges = NULL;
char *buf = NULL, *line, *end;
size_t buf_cap = 0, i, j;
int rc;
@@ -153,7 +154,7 @@ int shirudo_kmodules_read(struct shirudo_kmod_set *set)
memcpy(m->name, p, name_len);
m->name[name_len] = '\0';
m->size = strtoull(e, &p, 10);
- m->refcount = (uint32_t)strtoul(p, NULL, 10);
+ m->refcount = (__u32)strtoul(p, NULL, 10);
}
qsort(set->mods, set->n, sizeof(*set->mods), kmod_cmp);
@@ -220,14 +221,14 @@ int shirudo_kmodules_read(struct shirudo_kmod_set *set)
edges = np;
edge_cap *= 2;
}
- edges[n_edges].user = (uint16_t)user;
- edges[n_edges].dep = (uint16_t)dep;
+ edges[n_edges].user = (__u16)user;
+ edges[n_edges].dep = (__u16)dep;
n_edges++;
}
}
for (i = 0; i < set->n; i++) {
- set->mods[i].deps_off = (uint16_t)set->n_deps;
+ set->mods[i].deps_off = (__u16)set->n_deps;
set->mods[i].n_deps = 0;
for (j = 0; j < n_edges; j++) {
if (edges[j].user != i)
@@ -263,8 +264,8 @@ int shirudo_kmodules_read(struct shirudo_kmod_set *set)
snprintf(path, sizeof(path), "/sys/module/%s", m->name);
if (stat(path, &st) == 0) {
- m->loaded_at_sec = (int64_t)st.st_mtim.tv_sec;
- m->loaded_at_nsec = (int32_t)st.st_mtim.tv_nsec;
+ m->loaded_at_sec = (__s64)st.st_mtim.tv_sec;
+ m->loaded_at_nsec = (__s32)st.st_mtim.tv_nsec;
}
}
rc = 0;
@@ -287,7 +288,7 @@ void shirudo_kmodules_free(struct shirudo_kmod_set *set)
}
static void print_subtree(const struct shirudo_kmod_set *set, size_t i,
- const char *prefix, int is_last, uint8_t *seen)
+ const char *prefix, int is_last, __u8 *seen)
{
const struct shirudo_kmod *m = &set->mods[i];
const char *branch = is_last ? "└── " : "├── ";
@@ -301,15 +302,15 @@ static void print_subtree(const struct shirudo_kmod_set *set, size_t i,
printf("%s%s%s (shared)\n", prefix, branch, m->name);
return;
}
- seen[i >> 3] |= (uint8_t)(1U << (i & 7));
+ seen[i >> 3] |= (__u8)(1U << (i & 7));
printf("%s%s%s (%s, refs=%u)\n", prefix, branch, m->name,
- size_s, (unsigned int)m->refcount);
+ size_s, (__u32)m->refcount);
snprintf(child_prefix, sizeof(child_prefix), "%s%s", prefix,
is_last ? " " : "│ ");
- for (uint16_t k = 0; k < m->n_deps; k++) {
+ for (__u16 k = 0; k < m->n_deps; k++) {
size_t dep = set->deps[m->deps_off + k];
int last_child = (k == m->n_deps - 1);
@@ -319,8 +320,8 @@ static void print_subtree(const struct shirudo_kmod_set *set, size_t i,
void shirudo_kmodules_print_tree(const struct shirudo_kmod_set *set)
{
- uint8_t *used_by;
- uint8_t *seen;
+ __u8 *used_by;
+ __u8 *seen;
size_t bitmap_bytes;
size_t n_roots = 0;
size_t *roots;
@@ -349,10 +350,10 @@ void shirudo_kmodules_print_tree(const struct shirudo_kmod_set *set)
for (size_t i = 0; i < set->n; i++) {
const struct shirudo_kmod *m = &set->mods[i];
- for (uint16_t k = 0; k < m->n_deps; k++) {
+ for (__u16 k = 0; k < m->n_deps; k++) {
size_t d = set->deps[m->deps_off + k];
- used_by[d >> 3] |= (uint8_t)(1U << (d & 7));
+ used_by[d >> 3] |= (__u8)(1U << (d & 7));
}
}
for (size_t i = 0; i < set->n; i++) {
@@ -399,7 +400,7 @@ static int recent_cmp(const void *a, const void *b)
}
void shirudo_kmodules_print_recent(const struct shirudo_kmod_set *set,
- size_t last_n, int64_t boot_sec)
+ size_t last_n, __s64 boot_sec)
{
struct recent_idx *all;
size_t take, i;
@@ -435,7 +436,7 @@ void shirudo_kmodules_print_recent(const struct shirudo_kmod_set *set,
snprintf(age, sizeof(age), "?");
printf(" [%s] %s (%s, refs=%u, %s since boot)\n",
- ts, m->name, size_s, (unsigned int)m->refcount, age);
+ ts, m->name, size_s, (__u32)m->refcount, age);
}
xfree(all, set->n * sizeof(*all));
}
diff --git a/util/kmodules.h b/util/kmodules.h
index ccb71849..cbbb5e3c 100644
--- a/util/kmodules.h
+++ b/util/kmodules.h
@@ -9,30 +9,31 @@
#define SHIRUDO_UTIL_KMODULES_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#define SHIRUDO_KMOD_NAME_LEN 64
struct shirudo_kmod {
char name[SHIRUDO_KMOD_NAME_LEN];
- uint64_t size; /* bytes */
- uint32_t refcount; /* /proc/modules column 3 */
- uint16_t n_deps;
- uint16_t deps_off; /* index into the global deps[] */
+ __u64 size; /* bytes */
+ __u32 refcount; /* /proc/modules column 3 */
+ __u16 n_deps;
+ __u16 deps_off; /* index into the global deps[] */
/*
* sysfs `/sys/module/<name>` mtime -- the moment the kernel's
* kobject for this module was created, i.e. when it was loaded.
* Zero if the stat() failed (race: rmmod between /proc/modules
* read and the per-module stat).
*/
- int64_t loaded_at_sec;
- int32_t loaded_at_nsec;
+ __s64 loaded_at_sec;
+ __s32 loaded_at_nsec;
};
struct shirudo_kmod_set {
struct shirudo_kmod *mods; /* sorted by name, ascending */
size_t n;
- uint16_t *deps; /* flat: mods[i].deps_off..+n_deps */
+ __u16 *deps; /* flat: mods[i].deps_off..+n_deps */
size_t n_deps;
};
@@ -46,6 +47,6 @@ void shirudo_kmodules_print_tree(const struct shirudo_kmod_set *set);
* elapsed from boot, not time ago.
*/
void shirudo_kmodules_print_recent(const struct shirudo_kmod_set *set,
- size_t last_n, int64_t boot_sec);
+ size_t last_n, __s64 boot_sec);
#endif /* SHIRUDO_UTIL_KMODULES_H */
diff --git a/util/kmsg.c b/util/kmsg.c
index 9f528557..88e908fd 100644
--- a/util/kmsg.c
+++ b/util/kmsg.c
@@ -9,13 +9,14 @@
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "kmsg.h"
#include "timefmt.h"
#include "malloc.h"
@@ -39,7 +40,7 @@ static ssize_t read_record(int fd, char *buf, size_t cap)
}
static int parse_record(const char *rec, size_t len,
- uint64_t *monotonic_us_out,
+ __u64 *monotonic_us_out,
const char **msg_out, size_t *msg_len_out)
{
const char *semi, *end;
@@ -55,11 +56,11 @@ static int parse_record(const char *rec, size_t len,
if (*p == ',') {
commas++;
if (commas == 2) {
- uint64_t us = 0;
+ __u64 us = 0;
const char *q = p + 1;
while (q < semi && *q >= '0' && *q <= '9') {
- us = us * 10 + (uint64_t)(*q - '0');
+ us = us * 10 + (__u64)(*q - '0');
q++;
}
*monotonic_us_out = us;
@@ -90,7 +91,7 @@ static void copy_msg(char *dst, size_t dst_cap,
if (dst_cap == 0)
return;
for (i = 0; i < src_len && o + 1 < dst_cap; i++) {
- unsigned char c = (unsigned char)src[i];
+ __u8 c = (__u8)src[i];
if (c == '\t' || c == ' ' || (c >= 0x20 && c < 0x7f))
dst[o++] = (char)c;
@@ -100,20 +101,20 @@ static void copy_msg(char *dst, size_t dst_cap,
dst[o] = '\0';
}
-static void monotonic_to_iso(uint64_t mono_us,
+static void monotonic_to_iso(__u64 mono_us,
const struct timespec *rt_at_open,
const struct timespec *mt_at_open,
char *out, size_t out_cap)
{
struct timespec rec;
- int64_t delta_ns;
+ __s64 delta_ns;
/*
* record_realtime = realtime_at_open + (record_monotonic
* - monotonic_at_open)
*/
- delta_ns = (int64_t)mono_us * 1000
- - ((int64_t)mt_at_open->tv_sec * 1000000000LL
+ delta_ns = (__s64)mono_us * 1000
+ - ((__s64)mt_at_open->tv_sec * 1000000000LL
+ mt_at_open->tv_nsec);
rec.tv_sec = rt_at_open->tv_sec + delta_ns / 1000000000LL;
rec.tv_nsec = rt_at_open->tv_nsec + delta_ns % 1000000000LL;
@@ -153,7 +154,7 @@ int shirudo_kmsg_tail(struct shirudo_kmsg_record *out, size_t cap)
}
for (;;) {
- uint64_t mono_us = 0;
+ __u64 mono_us = 0;
const char *msg = NULL;
size_t msg_len = 0;
size_t slot;
diff --git a/util/preflight.c b/util/preflight.c
index fa271955..3444ef01 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -16,6 +16,8 @@
#include <linux/keyctl.h>
+#include <shirudo/misc.h>
+
#include "config.h"
#include "initrd.h"
#include "crypto.h"
@@ -77,7 +79,7 @@ static void check_root(struct shirudo_preflight_result *r)
pass(c);
else
fail(r, c, "must run as root (EUID=%u)",
- (unsigned int)geteuid());
+ (__u32)geteuid());
}
static bool lsm_lists_bpf(void)
@@ -184,10 +186,10 @@ static void check_cert(struct shirudo_preflight_result *r)
shirudo_cert_path());
}
-static int read_cert(const char *path, unsigned char **out, size_t *out_sz)
+static int read_cert(const char *path, __u8 **out, size_t *out_sz)
{
- unsigned char *buf;
struct stat st;
+ __u8 *buf;
ssize_t n;
int fd;
@@ -299,8 +301,8 @@ static void check_cert_kernel_algo(struct shirudo_preflight_result *r)
return;
}
for (i = 0; i < set.n; i++) {
- unsigned char *der = NULL;
size_t der_sz = 0;
+ __u8 *der = NULL;
long key;
int rc;
diff --git a/util/serializer.c b/util/serializer.c
index 78ceef7b..54ffae95 100644
--- a/util/serializer.c
+++ b/util/serializer.c
@@ -9,11 +9,13 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "paths.h"
#include "seal.h"
#include "serializer.h"
-int shirudo_serializer_lock_acquire_at(const char *dir, uint32_t map_id)
+int shirudo_serializer_lock_acquire_at(const char *dir, __u32 map_id)
{
char name[64];
struct stat st;
@@ -62,15 +64,15 @@ int shirudo_serializer_lock_acquire_at(const char *dir, uint32_t map_id)
return fd;
}
-int shirudo_serializer_lock_acquire(uint32_t map_id)
+int shirudo_serializer_lock_acquire(__u32 map_id)
{
return shirudo_serializer_lock_acquire_at(SHIRUDO_LOCKS_DIR, map_id);
}
-int shirudo_serializer_new_invocation_id(uint64_t *out)
+int shirudo_serializer_new_invocation_id(__u64 *out)
{
- uint64_t id;
- unsigned char *p = (unsigned char *)&id;
+ __u64 id;
+ __u8 *p = (__u8 *)&id;
size_t off;
if (!out)
@@ -95,12 +97,12 @@ int shirudo_serializer_new_invocation_id(uint64_t *out)
}
int shirudo_serial_chunk_validate(const struct shirudo_serial_chunk *c,
- uint32_t want_index,
- uint32_t total_chunks,
- uint32_t page_index,
- uint64_t cursor,
- uint64_t next_cursor,
- uint64_t invocation_id)
+ __u32 want_index,
+ __u32 total_chunks,
+ __u32 page_index,
+ __u64 cursor,
+ __u64 next_cursor,
+ __u64 invocation_id)
{
if (!c || invocation_id == 0)
return -EINVAL;
diff --git a/util/serializer.h b/util/serializer.h
index ec831628..6a002832 100644
--- a/util/serializer.h
+++ b/util/serializer.h
@@ -4,20 +4,20 @@
#ifndef SHIRUDO_UTIL_SERIALIZER_H
#define SHIRUDO_UTIL_SERIALIZER_H
-#include <stdint.h>
+#include <shirudo/misc.h>
#include "bpf/lib/shirudo_serializer.h"
-int shirudo_serializer_lock_acquire(uint32_t map_id);
-int shirudo_serializer_lock_acquire_at(const char *dir, uint32_t map_id);
-int shirudo_serializer_new_invocation_id(uint64_t *out);
+int shirudo_serializer_lock_acquire(__u32 map_id);
+int shirudo_serializer_lock_acquire_at(const char *dir, __u32 map_id);
+int shirudo_serializer_new_invocation_id(__u64 *out);
int shirudo_serial_chunk_validate(const struct shirudo_serial_chunk *chunk,
- uint32_t want_index,
- uint32_t total_chunks,
- uint32_t page_index,
- uint64_t cursor,
- uint64_t next_cursor,
- uint64_t invocation_id);
+ __u32 want_index,
+ __u32 total_chunks,
+ __u32 page_index,
+ __u64 cursor,
+ __u64 next_cursor,
+ __u64 invocation_id);
#endif /* SHIRUDO_UTIL_SERIALIZER_H */
diff --git a/util/sign.h b/util/sign.h
index beb25bcd..2bbd1e05 100644
--- a/util/sign.h
+++ b/util/sign.h
@@ -9,7 +9,8 @@
#define SHIRUDO_SIGN_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#define SHIRUDO_SHA256_LEN 32
@@ -28,7 +29,7 @@ struct shirudo_sign_io {
void *signature;
size_t signature_sz;
- uint8_t payload_sha256[SHIRUDO_SHA256_LEN];
+ __u8 payload_sha256[SHIRUDO_SHA256_LEN];
};
/*
diff --git a/util/sysinfo.c b/util/sysinfo.c
index f579373a..e9efbf10 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -15,6 +15,8 @@
#include <sys/utsname.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "json.h"
#include "kernel_build_id.h"
#include "malloc.h"
@@ -112,7 +114,7 @@ static void read_mem(long *total_kb, long *avail_kb)
static void read_root_disk(long *total_kb, long *used_kb)
{
struct statvfs s;
- uint64_t tot_b, free_b;
+ __u64 tot_b, free_b;
*total_kb = -1;
*used_kb = -1;
@@ -123,8 +125,8 @@ static void read_root_disk(long *total_kb, long *used_kb)
* against; tot/used in raw bytes, then KiB so the JSON wire type
* matches mem_*_kb.
*/
- tot_b = (uint64_t)s.f_blocks * s.f_frsize;
- free_b = (uint64_t)s.f_bfree * s.f_frsize;
+ tot_b = (__u64)s.f_blocks * s.f_frsize;
+ free_b = (__u64)s.f_bfree * s.f_frsize;
*total_kb = (long)(tot_b / 1024);
*used_kb = (long)((tot_b - free_b) / 1024);
}
@@ -133,10 +135,10 @@ static char *trim_config_text(char *text)
{
char *end;
- while (*text && isspace((unsigned char)*text))
+ while (*text && isspace((__u8)*text))
text++;
end = text + strlen(text);
- while (end > text && isspace((unsigned char)end[-1]))
+ while (end > text && isspace((__u8)end[-1]))
*--end = '\0';
return text;
}
diff --git a/util/tamper.c b/util/tamper.c
index d5501971..2ae65ab4 100644
--- a/util/tamper.c
+++ b/util/tamper.c
@@ -5,13 +5,14 @@
* Read back the tamper counters init pinned in the private bpffs.
*/
#include <errno.h>
-#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <linux/bpf.h>
#include <bpf/bpf.h>
+#include <shirudo/misc.h>
+
#include "paths.h"
#include "tamper.h"
@@ -94,9 +95,9 @@ const char *shirudo_tamper_meaning(enum shirudo_tamper_point point)
int shirudo_tamper_read(struct shirudo_tamper_counts *out)
{
struct bpf_map_info info = {};
- uint32_t len = sizeof(info);
- uint32_t key;
+ __u32 len = sizeof(info);
int fd, rc;
+ __u32 key;
memset(out, 0, sizeof(*out));
fd = bpf_obj_get(SHIRUDO_TAMPER_PIN);
diff --git a/util/tamper.h b/util/tamper.h
index 3e5e9036..7dd30c36 100644
--- a/util/tamper.h
+++ b/util/tamper.h
@@ -7,13 +7,13 @@
#ifndef SHIRUDO_UTIL_TAMPER_H
#define SHIRUDO_UTIL_TAMPER_H
-#include <stdint.h>
+#include <shirudo/misc.h>
#include "bpf/lib/shirudo_tamper.h"
struct shirudo_tamper_counts {
- uint64_t count[SHIRUDO_TAMPER_MAX];
- uint64_t total;
+ __u64 count[SHIRUDO_TAMPER_MAX];
+ __u64 total;
};
const char *shirudo_tamper_name(enum shirudo_tamper_point point);
diff --git a/util/tenant_id.h b/util/tenant_id.h
index 05f75978..a7cdcd93 100644
--- a/util/tenant_id.h
+++ b/util/tenant_id.h
@@ -7,6 +7,8 @@
#include <stdbool.h>
#include <stddef.h>
+#include <shirudo/misc.h>
+
#define SHIRUDO_TENANT_ID_MAX 128
static inline bool shirudo_tenant_id_valid(const char *id)
@@ -16,7 +18,7 @@ static inline bool shirudo_tenant_id_valid(const char *id)
if (!id || !id[0])
return false;
for (i = 0; id[i]; i++) {
- unsigned char c = (unsigned char)id[i];
+ __u8 c = (__u8)id[i];
if (i >= SHIRUDO_TENANT_ID_MAX ||
!((c >= 'a' && c <= 'z') ||
diff --git a/util/tpm.c b/util/tpm.c
index 06e16858..b13c6220 100644
--- a/util/tpm.c
+++ b/util/tpm.c
@@ -13,6 +13,8 @@
#include <tss2/tss2_esys.h>
#include <tss2/tss2_tctildr.h>
+#include <shirudo/misc.h>
+
#include "config.h"
#include "log.h"
#include "malloc.h"
@@ -62,7 +64,7 @@ int shirudo_tpm_open(struct shirudo_tpm **out)
rc = Tss2_TctiLdr_Initialize(tcti, &t->tcti);
if (rc != TSS2_RC_SUCCESS)
pr_err("tpm: TctiLdr_Initialize(%s): rc=0x%08x", tcti,
- (unsigned int)rc);
+ (__u32)rc);
explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */
if (rc != TSS2_RC_SUCCESS) {
xfree(t, sizeof(*t));
@@ -70,7 +72,7 @@ int shirudo_tpm_open(struct shirudo_tpm **out)
}
rc = Esys_Initialize(&t->esys, t->tcti, NULL);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: Esys_Initialize: rc=0x%08x", (unsigned int)rc);
+ pr_err("tpm: Esys_Initialize: rc=0x%08x", (__u32)rc);
Tss2_TctiLdr_Finalize(&t->tcti);
xfree(t, sizeof(*t));
return -EIO;
@@ -106,19 +108,19 @@ static bool rc_is_auth_failure(TSS2_RC rc)
rc_is(rc, TPM2_RC_POLICY_FAIL);
}
-static void pcr_selection(uint32_t mask, TPML_PCR_SELECTION *sel)
+static void pcr_selection(__u32 mask, TPML_PCR_SELECTION *sel)
{
memset(sel, 0, sizeof(*sel));
sel->count = 1;
sel->pcrSelections[0].hash = TPM2_ALG_SHA256;
sel->pcrSelections[0].sizeofSelect = SHIRUDO_TPM_PCR_SELECT_LEN;
- sel->pcrSelections[0].pcrSelect[0] = (uint8_t)mask;
- sel->pcrSelections[0].pcrSelect[1] = (uint8_t)(mask >> 8);
- sel->pcrSelections[0].pcrSelect[2] = (uint8_t)(mask >> 16);
+ sel->pcrSelections[0].pcrSelect[0] = (__u8)mask;
+ sel->pcrSelections[0].pcrSelect[1] = (__u8)(mask >> 8);
+ sel->pcrSelections[0].pcrSelect[2] = (__u8)(mask >> 16);
}
-int shirudo_tpm_pcr_read(struct shirudo_tpm *t, unsigned int pcr,
- uint8_t out[32])
+int shirudo_tpm_pcr_read(struct shirudo_tpm *t, __u32 pcr,
+ __u8 out[32])
{
TPML_PCR_SELECTION sel;
TPML_DIGEST *values = NULL;
@@ -131,7 +133,7 @@ int shirudo_tpm_pcr_read(struct shirudo_tpm *t, unsigned int pcr,
rc = Esys_PCR_Read(t->esys, ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
&sel, &counter, NULL, &values);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: PCR_Read(%u): rc=0x%08x", pcr, (unsigned int)rc);
+ pr_err("tpm: PCR_Read(%u): rc=0x%08x", pcr, (__u32)rc);
return -EIO;
}
if (values->count != 1 || values->digests[0].size != 32) {
@@ -144,8 +146,8 @@ int shirudo_tpm_pcr_read(struct shirudo_tpm *t, unsigned int pcr,
return 0;
}
-int shirudo_tpm_pcr_extend(struct shirudo_tpm *t, unsigned int pcr,
- const uint8_t digest[32])
+int shirudo_tpm_pcr_extend(struct shirudo_tpm *t, __u32 pcr,
+ const __u8 digest[32])
{
TPML_DIGEST_VALUES dv = { .count = 1 };
TSS2_RC rc;
@@ -157,13 +159,13 @@ int shirudo_tpm_pcr_extend(struct shirudo_tpm *t, unsigned int pcr,
rc = Esys_PCR_Extend(t->esys, ESYS_TR_PCR0 + pcr, ESYS_TR_PASSWORD,
ESYS_TR_NONE, ESYS_TR_NONE, &dv);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: PCR_Extend(%u): rc=0x%08x", pcr, (unsigned int)rc);
+ pr_err("tpm: PCR_Extend(%u): rc=0x%08x", pcr, (__u32)rc);
return -EIO;
}
return 0;
}
-int shirudo_tpm_reset_count(struct shirudo_tpm *t, uint32_t *out)
+int shirudo_tpm_reset_count(struct shirudo_tpm *t, __u32 *out)
{
TPMS_TIME_INFO *info = NULL;
TSS2_RC rc;
@@ -171,7 +173,7 @@ int shirudo_tpm_reset_count(struct shirudo_tpm *t, uint32_t *out)
rc = Esys_ReadClock(t->esys, ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
&info);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: ReadClock: rc=0x%08x", (unsigned int)rc);
+ pr_err("tpm: ReadClock: rc=0x%08x", (__u32)rc);
return -EIO;
}
*out = info->clockInfo.resetCount;
@@ -179,7 +181,7 @@ int shirudo_tpm_reset_count(struct shirudo_tpm *t, uint32_t *out)
return 0;
}
-static int nv_handle(struct shirudo_tpm *t, uint32_t index, ESYS_TR *out)
+static int nv_handle(struct shirudo_tpm *t, __u32 index, ESYS_TR *out)
{
TSS2_RC rc;
@@ -189,13 +191,13 @@ static int nv_handle(struct shirudo_tpm *t, uint32_t index, ESYS_TR *out)
return -ENOENT; /* quiet: undefined is normal */
if (rc != TSS2_RC_SUCCESS) {
pr_err("tpm: TR_FromTPMPublic(0x%08x): rc=0x%08x", index,
- (unsigned int)rc);
+ (__u32)rc);
return -EIO;
}
return 0;
}
-int shirudo_tpm_nv_info(struct shirudo_tpm *t, uint32_t index,
+int shirudo_tpm_nv_info(struct shirudo_tpm *t, __u32 index,
struct shirudo_tpm_nv_info *info)
{
TPM2B_NV_PUBLIC *pub = NULL;
@@ -215,7 +217,7 @@ int shirudo_tpm_nv_info(struct shirudo_tpm *t, uint32_t index,
Esys_TR_Close(t->esys, &nv);
if (rc != TSS2_RC_SUCCESS) {
pr_err("tpm: NV_ReadPublic(0x%08x): rc=0x%08x", index,
- (unsigned int)rc);
+ (__u32)rc);
return -EIO;
}
info->defined = true;
@@ -242,12 +244,12 @@ static int owner_auth_set(struct shirudo_tpm *t, const void *auth, size_t len)
return -EINVAL;
if (len) {
memcpy(owner.buffer, auth, len);
- owner.size = (uint16_t)len;
+ owner.size = (__u16)len;
}
rc = Esys_TR_SetAuth(t->esys, ESYS_TR_RH_OWNER, &owner);
explicit_bzero(owner.buffer, sizeof(owner.buffer));
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: TR_SetAuth(owner): rc=0x%08x", (unsigned int)rc);
+ pr_err("tpm: TR_SetAuth(owner): rc=0x%08x", (__u32)rc);
return -EIO;
}
return 0;
@@ -260,22 +262,22 @@ static void owner_auth_clear(struct shirudo_tpm *t)
Esys_TR_SetAuth(t->esys, ESYS_TR_RH_OWNER, &empty);
}
-static int owner_auth_error(const char *op, uint32_t index, TSS2_RC rc)
+static int owner_auth_error(const char *op, __u32 index, TSS2_RC rc)
{
if (rc_is_auth_failure(rc)) {
pr_err("tpm: %s(0x%08x): owner-hierarchy authorization "
"failed (rc=0x%08x). This TPM has owner auth set; "
"supply it via tpm.owner_auth in %s (or "
"SHIRUDO_TPM_OWNER_AUTH), or clear the TPM owner "
- "auth.", op, index, (unsigned int)rc, SHIRUDO_CONF_PATH);
+ "auth.", op, index, (__u32)rc, SHIRUDO_CONF_PATH);
return -EACCES;
}
- pr_err("tpm: %s(0x%08x): rc=0x%08x", op, index, (unsigned int)rc);
+ pr_err("tpm: %s(0x%08x): rc=0x%08x", op, index, (__u32)rc);
return -EIO;
}
-int shirudo_tpm_nv_define(struct shirudo_tpm *t, uint32_t index,
- const uint8_t policy[32],
+int shirudo_tpm_nv_define(struct shirudo_tpm *t, __u32 index,
+ const __u8 policy[32],
const void *owner_auth, size_t owner_auth_len)
{
TPM2B_NV_PUBLIC pub = {};
@@ -303,7 +305,7 @@ int shirudo_tpm_nv_define(struct shirudo_tpm *t, uint32_t index,
return 0;
}
-int shirudo_tpm_nv_undefine(struct shirudo_tpm *t, uint32_t index,
+int shirudo_tpm_nv_undefine(struct shirudo_tpm *t, __u32 index,
const void *owner_auth, size_t owner_auth_len)
{
ESYS_TR nv;
@@ -325,7 +327,7 @@ int shirudo_tpm_nv_undefine(struct shirudo_tpm *t, uint32_t index,
return 0;
}
-int shirudo_tpm_nv_read(struct shirudo_tpm *t, uint32_t index, uint8_t out[32])
+int shirudo_tpm_nv_read(struct shirudo_tpm *t, __u32 index, __u8 out[32])
{
TPM2B_MAX_NV_BUFFER *data = NULL;
ESYS_TR nv;
@@ -341,7 +343,7 @@ int shirudo_tpm_nv_read(struct shirudo_tpm *t, uint32_t index, uint8_t out[32])
if (rc_is(rc, TPM2_RC_NV_UNINITIALIZED))
return -ENODATA; /* quiet: nothing written this boot */
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: NV_Read(0x%08x): rc=0x%08x", index, (unsigned int)rc);
+ pr_err("tpm: NV_Read(0x%08x): rc=0x%08x", index, (__u32)rc);
return -EIO;
}
if (data->size != SHIRUDO_TPM_NV_DIGEST_LEN) {
@@ -365,7 +367,7 @@ static int policy_session_start(struct shirudo_tpm *t, ESYS_TR *session)
NULL, TPM2_SE_POLICY, &sym, TPM2_ALG_SHA256,
session);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: StartAuthSession: rc=0x%08x", (unsigned int)rc);
+ pr_err("tpm: StartAuthSession: rc=0x%08x", (__u32)rc);
return -EIO;
}
return 0;
@@ -384,14 +386,14 @@ static void session_flush(struct shirudo_tpm *t, ESYS_TR session)
* instead of as an unexplained TPM2_RC_POLICY_FAIL.
*/
static int policy_session_register(struct shirudo_tpm *t, bool init,
- uint32_t pcr_mask,
- const uint8_t branch_written[32],
- const uint8_t branch_init[32],
+ __u32 pcr_mask,
+ const __u8 branch_written[32],
+ const __u8 branch_init[32],
ESYS_TR *session)
{
TPML_DIGEST branches = { .count = 2 };
TPM2B_DIGEST *digest = NULL;
- uint8_t expected[32];
+ __u8 expected[32];
TSS2_RC rc;
int err;
@@ -422,7 +424,7 @@ static int policy_session_register(struct shirudo_tpm *t, bool init,
"can only be initialised in the boot window");
else
pr_err("tpm: register policy assertion: rc=0x%08x",
- (unsigned int)rc);
+ (__u32)rc);
session_flush(t, *session);
*session = ESYS_TR_NONE;
return -EACCES;
@@ -433,7 +435,7 @@ static int policy_session_register(struct shirudo_tpm *t, bool init,
if (rc != TSS2_RC_SUCCESS || digest->size != 32 ||
memcmp(digest->buffer, expected, 32) != 0) {
pr_err("tpm: register policy digest does not match the "
- "computed policy (rc=0x%08x)", (unsigned int)rc);
+ "computed policy (rc=0x%08x)", (__u32)rc);
Esys_Free(digest);
session_flush(t, *session);
*session = ESYS_TR_NONE;
@@ -443,8 +445,8 @@ static int policy_session_register(struct shirudo_tpm *t, bool init,
return 0;
}
-static int nv_extend_session(struct shirudo_tpm *t, uint32_t index,
- ESYS_TR session, const uint8_t event[32])
+static int nv_extend_session(struct shirudo_tpm *t, __u32 index,
+ ESYS_TR session, const __u8 event[32])
{
TPM2B_MAX_NV_BUFFER data = { .size = SHIRUDO_TPM_NV_DIGEST_LEN };
ESYS_TR nv;
@@ -460,17 +462,17 @@ static int nv_extend_session(struct shirudo_tpm *t, uint32_t index,
Esys_TR_Close(t->esys, &nv);
if (rc != TSS2_RC_SUCCESS) {
pr_err("tpm: NV_Extend(0x%08x): rc=0x%08x", index,
- (unsigned int)rc);
+ (__u32)rc);
return rc_is_auth_failure(rc) ? -EACCES : -EIO;
}
return 0;
}
-int shirudo_tpm_nv_extend_first(struct shirudo_tpm *t, uint32_t index,
- uint32_t pcr_mask,
- const uint8_t branch_written[32],
- const uint8_t branch_init[32],
- const uint8_t event[32])
+int shirudo_tpm_nv_extend_first(struct shirudo_tpm *t, __u32 index,
+ __u32 pcr_mask,
+ const __u8 branch_written[32],
+ const __u8 branch_init[32],
+ const __u8 event[32])
{
ESYS_TR session = ESYS_TR_NONE;
int err;
@@ -484,10 +486,10 @@ int shirudo_tpm_nv_extend_first(struct shirudo_tpm *t, uint32_t index,
return err;
}
-int shirudo_tpm_nv_extend(struct shirudo_tpm *t, uint32_t index,
- const uint8_t branch_written[32],
- const uint8_t branch_init[32],
- const uint8_t event[32])
+int shirudo_tpm_nv_extend(struct shirudo_tpm *t, __u32 index,
+ const __u8 branch_written[32],
+ const __u8 branch_init[32],
+ const __u8 event[32])
{
ESYS_TR session = ESYS_TR_NONE;
int err;
@@ -501,7 +503,7 @@ int shirudo_tpm_nv_extend(struct shirudo_tpm *t, uint32_t index,
return err;
}
-static int public_from_tss(const TPMT_PUBLIC *p, uint8_t *out, size_t cap,
+static int public_from_tss(const TPMT_PUBLIC *p, __u8 *out, size_t cap,
size_t *len)
{
struct shirudo_tpm_public pub = {};
@@ -552,7 +554,7 @@ static int public_from_tss(const TPMT_PUBLIC *p, uint8_t *out, size_t cap,
return shirudo_tpm_public_marshal(&pub, out, cap, len);
}
-static int marshal_signature(const TPMT_SIGNATURE *sig, uint8_t *out,
+static int marshal_signature(const TPMT_SIGNATURE *sig, __u8 *out,
size_t cap, size_t *len)
{
const TPMS_SIGNATURE_ECDSA *e = &sig->signature.ecdsa;
@@ -585,7 +587,7 @@ static int endorsement_primary(struct shirudo_tpm *t,
TPM2B_PUBLIC *out_pub = NULL;
TPM2B_NAME *name = NULL;
TPM2B_DATA outside = {};
- uint8_t computed[SHIRUDO_TPM_NAME_LEN];
+ __u8 computed[SHIRUDO_TPM_NAME_LEN];
ESYS_TR handle;
TSS2_RC rc;
int err;
@@ -599,10 +601,10 @@ static int endorsement_primary(struct shirudo_tpm *t,
if (rc_is_auth_failure(rc))
pr_err("tpm: CreatePrimary(%s): the endorsement "
"hierarchy has an auth value (rc=0x%08x)", what,
- (unsigned int)rc);
+ (__u32)rc);
else
pr_err("tpm: CreatePrimary(%s): rc=0x%08x", what,
- (unsigned int)rc);
+ (__u32)rc);
return -EIO;
}
key->handle = handle;
@@ -707,7 +709,7 @@ static int attestation_copy(const TPM2B_ATTEST *attest,
}
int shirudo_tpm_quote(struct shirudo_tpm *t, const struct shirudo_tpm_key *ak,
- const uint8_t *nonce, size_t nonce_len, uint32_t pcr_mask,
+ const __u8 *nonce, size_t nonce_len, __u32 pcr_mask,
struct shirudo_tpm_attestation *out)
{
TPMT_SIG_SCHEME scheme = { .scheme = TPM2_ALG_NULL };
@@ -721,12 +723,12 @@ int shirudo_tpm_quote(struct shirudo_tpm *t, const struct shirudo_tpm_key *ak,
if (nonce_len > sizeof(qual.buffer))
return -EINVAL;
memcpy(qual.buffer, nonce, nonce_len);
- qual.size = (uint16_t)nonce_len;
+ qual.size = (__u16)nonce_len;
pcr_selection(pcr_mask, &sel);
rc = Esys_Quote(t->esys, ak->handle, ESYS_TR_PASSWORD, ESYS_TR_NONE,
ESYS_TR_NONE, &qual, &scheme, &sel, &quoted, &sig);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: Quote: rc=0x%08x", (unsigned int)rc);
+ pr_err("tpm: Quote: rc=0x%08x", (__u32)rc);
return -EIO;
}
err = attestation_copy(quoted, sig, out);
@@ -736,8 +738,8 @@ int shirudo_tpm_quote(struct shirudo_tpm *t, const struct shirudo_tpm_key *ak,
}
int shirudo_tpm_nv_certify(struct shirudo_tpm *t,
- const struct shirudo_tpm_key *ak, uint32_t index,
- const uint8_t *nonce, size_t nonce_len,
+ const struct shirudo_tpm_key *ak, __u32 index,
+ const __u8 *nonce, size_t nonce_len,
struct shirudo_tpm_attestation *out)
{
TPMT_SIG_SCHEME scheme = { .scheme = TPM2_ALG_NULL };
@@ -751,7 +753,7 @@ int shirudo_tpm_nv_certify(struct shirudo_tpm *t,
if (nonce_len > sizeof(qual.buffer))
return -EINVAL;
memcpy(qual.buffer, nonce, nonce_len);
- qual.size = (uint16_t)nonce_len;
+ qual.size = (__u16)nonce_len;
err = nv_handle(t, index, &nv);
if (err)
return err;
@@ -761,7 +763,7 @@ int shirudo_tpm_nv_certify(struct shirudo_tpm *t,
Esys_TR_Close(t->esys, &nv);
if (rc != TSS2_RC_SUCCESS) {
pr_err("tpm: NV_Certify(0x%08x): rc=0x%08x", index,
- (unsigned int)rc);
+ (__u32)rc);
return rc_is(rc, TPM2_RC_NV_UNINITIALIZED) ? -ENODATA : -EIO;
}
err = attestation_copy(info, sig, out);
@@ -773,9 +775,9 @@ int shirudo_tpm_nv_certify(struct shirudo_tpm *t,
int shirudo_tpm_activate_credential(struct shirudo_tpm *t,
const struct shirudo_tpm_key *ak,
const struct shirudo_tpm_key *ek,
- const uint8_t *blob, size_t blob_len,
- const uint8_t *secret, size_t secret_len,
- uint8_t *out, size_t cap, size_t *out_len)
+ const __u8 *blob, size_t blob_len,
+ const __u8 *secret, size_t secret_len,
+ __u8 *out, size_t cap, size_t *out_len)
{
TPM2B_ENCRYPTED_SECRET sec = {};
TPM2B_DIGEST *cert_info = NULL;
@@ -786,9 +788,9 @@ int shirudo_tpm_activate_credential(struct shirudo_tpm *t,
if (blob_len > sizeof(id.credential) || secret_len > sizeof(sec.secret))
return -EINVAL;
- id.size = (uint16_t)blob_len;
+ id.size = (__u16)blob_len;
memcpy(id.credential, blob, blob_len);
- sec.size = (uint16_t)secret_len;
+ sec.size = (__u16)secret_len;
memcpy(sec.secret, secret, secret_len);
err = policy_session_start(t, &session);
if (err)
@@ -799,7 +801,7 @@ int shirudo_tpm_activate_credential(struct shirudo_tpm *t,
NULL, NULL, NULL, 0, NULL, NULL);
if (rc != TSS2_RC_SUCCESS) {
pr_err("tpm: PolicySecret(endorsement): rc=0x%08x",
- (unsigned int)rc);
+ (__u32)rc);
session_flush(t, session);
return -EACCES;
}
@@ -808,7 +810,7 @@ int shirudo_tpm_activate_credential(struct shirudo_tpm *t,
&id, &sec, &cert_info);
session_flush(t, session);
if (rc != TSS2_RC_SUCCESS) {
- pr_err("tpm: ActivateCredential: rc=0x%08x", (unsigned int)rc);
+ pr_err("tpm: ActivateCredential: rc=0x%08x", (__u32)rc);
return (rc & 0xFFFF0000u) == 0 ? -EKEYREJECTED : -EIO;
}
if (cert_info->size > cap) {
@@ -821,7 +823,7 @@ int shirudo_tpm_activate_credential(struct shirudo_tpm *t,
return 0;
}
-static int nv_read_all(struct shirudo_tpm *t, uint32_t index, uint8_t *out,
+static int nv_read_all(struct shirudo_tpm *t, __u32 index, __u8 *out,
size_t cap, size_t *len)
{
TPM2B_NV_PUBLIC *pub = NULL;
@@ -872,7 +874,7 @@ static int nv_read_all(struct shirudo_tpm *t, uint32_t index, uint8_t *out,
return 0;
}
-int shirudo_tpm_ek_certificate(struct shirudo_tpm *t, uint8_t *out,
+int shirudo_tpm_ek_certificate(struct shirudo_tpm *t, __u8 *out,
size_t cap, size_t *len, bool *rsa)
{
int err;
diff --git a/util/tpm.h b/util/tpm.h
index 75d9659f..c66a2fe4 100644
--- a/util/tpm.h
+++ b/util/tpm.h
@@ -10,7 +10,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "tpm_wire.h"
@@ -24,24 +25,24 @@ struct shirudo_tpm;
int shirudo_tpm_open(struct shirudo_tpm **out);
void shirudo_tpm_close(struct shirudo_tpm *t);
-int shirudo_tpm_pcr_read(struct shirudo_tpm *t, unsigned int pcr,
- uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
-int shirudo_tpm_pcr_extend(struct shirudo_tpm *t, unsigned int pcr,
- const uint8_t digest[SHIRUDO_TPM_DIGEST_LEN]);
+int shirudo_tpm_pcr_read(struct shirudo_tpm *t, __u32 pcr,
+ __u8 out[SHIRUDO_TPM_DIGEST_LEN]);
+int shirudo_tpm_pcr_extend(struct shirudo_tpm *t, __u32 pcr,
+ const __u8 digest[SHIRUDO_TPM_DIGEST_LEN]);
/*
* The TPM's reset count, a new value at every TPM reset: the identity
* of the period a clear-on-reset index lives through.
*/
-int shirudo_tpm_reset_count(struct shirudo_tpm *t, uint32_t *out);
+int shirudo_tpm_reset_count(struct shirudo_tpm *t, __u32 *out);
struct shirudo_tpm_nv_info {
bool defined;
bool written;
- uint32_t attrs;
- uint16_t data_size;
- uint8_t policy[SHIRUDO_TPM_DIGEST_LEN];
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u32 attrs;
+ __u16 data_size;
+ __u8 policy[SHIRUDO_TPM_DIGEST_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
size_t name_len;
};
@@ -55,25 +56,25 @@ struct shirudo_tpm_nv_info {
* only satisfies while the anchoring PCR still holds the value the
* policy was computed from, every later one the written branch.
*/
-int shirudo_tpm_nv_info(struct shirudo_tpm *t, uint32_t index,
+int shirudo_tpm_nv_info(struct shirudo_tpm *t, __u32 index,
struct shirudo_tpm_nv_info *info);
-int shirudo_tpm_nv_define(struct shirudo_tpm *t, uint32_t index,
- const uint8_t policy[SHIRUDO_TPM_DIGEST_LEN],
+int shirudo_tpm_nv_define(struct shirudo_tpm *t, __u32 index,
+ const __u8 policy[SHIRUDO_TPM_DIGEST_LEN],
const void *owner_auth, size_t owner_auth_len);
-int shirudo_tpm_nv_undefine(struct shirudo_tpm *t, uint32_t index,
+int shirudo_tpm_nv_undefine(struct shirudo_tpm *t, __u32 index,
const void *owner_auth, size_t owner_auth_len);
/* -ENOENT: not defined; -ENODATA: defined but not written this boot. */
-int shirudo_tpm_nv_read(struct shirudo_tpm *t, uint32_t index,
- uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
-int shirudo_tpm_nv_extend_first(struct shirudo_tpm *t, uint32_t index,
- uint32_t pcr_mask,
- const uint8_t branch_written[32],
- const uint8_t branch_init[32],
- const uint8_t event[32]);
-int shirudo_tpm_nv_extend(struct shirudo_tpm *t, uint32_t index,
- const uint8_t branch_written[32],
- const uint8_t branch_init[32],
- const uint8_t event[32]);
+int shirudo_tpm_nv_read(struct shirudo_tpm *t, __u32 index,
+ __u8 out[SHIRUDO_TPM_DIGEST_LEN]);
+int shirudo_tpm_nv_extend_first(struct shirudo_tpm *t, __u32 index,
+ __u32 pcr_mask,
+ const __u8 branch_written[32],
+ const __u8 branch_init[32],
+ const __u8 event[32]);
+int shirudo_tpm_nv_extend(struct shirudo_tpm *t, __u32 index,
+ const __u8 branch_written[32],
+ const __u8 branch_init[32],
+ const __u8 event[32]);
/*
* The attestation key: a restricted P-256 ECDSA primary under the
@@ -81,17 +82,17 @@ int shirudo_tpm_nv_extend(struct shirudo_tpm *t, uint32_t index,
* yields the same key and an operator can pin it by name.
*/
struct shirudo_tpm_key {
- uint32_t handle;
- uint8_t public[SHIRUDO_TPM_PUBLIC_MAX];
+ __u32 handle;
+ __u8 public[SHIRUDO_TPM_PUBLIC_MAX];
size_t public_len;
- uint8_t name[SHIRUDO_TPM_NAME_LEN];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
size_t name_len;
};
struct shirudo_tpm_attestation {
- uint8_t attest[SHIRUDO_TPM_ATTEST_MAX];
+ __u8 attest[SHIRUDO_TPM_ATTEST_MAX];
size_t attest_len;
- uint8_t signature[SHIRUDO_TPM_SIGNATURE_MAX];
+ __u8 signature[SHIRUDO_TPM_SIGNATURE_MAX];
size_t signature_len;
};
@@ -113,23 +114,23 @@ int shirudo_tpm_ek_create(struct shirudo_tpm *t, bool rsa,
int shirudo_tpm_activate_credential(struct shirudo_tpm *t,
const struct shirudo_tpm_key *ak,
const struct shirudo_tpm_key *ek,
- const uint8_t *blob, size_t blob_len,
- const uint8_t *secret, size_t secret_len,
- uint8_t *out, size_t cap, size_t *out_len);
+ const __u8 *blob, size_t blob_len,
+ const __u8 *secret, size_t secret_len,
+ __u8 *out, size_t cap, size_t *out_len);
void shirudo_tpm_key_flush(struct shirudo_tpm *t, struct shirudo_tpm_key *key);
int shirudo_tpm_quote(struct shirudo_tpm *t, const struct shirudo_tpm_key *ak,
- const uint8_t *nonce, size_t nonce_len, uint32_t pcr_mask,
+ const __u8 *nonce, size_t nonce_len, __u32 pcr_mask,
struct shirudo_tpm_attestation *out);
int shirudo_tpm_nv_certify(struct shirudo_tpm *t,
- const struct shirudo_tpm_key *ak, uint32_t index,
- const uint8_t *nonce, size_t nonce_len,
+ const struct shirudo_tpm_key *ak, __u32 index,
+ const __u8 *nonce, size_t nonce_len,
struct shirudo_tpm_attestation *out);
/*
* The endorsement key certificate from its TCG NV index, the ECC one
* first; *rsa tells which key it describes. -ENOENT if none.
*/
-int shirudo_tpm_ek_certificate(struct shirudo_tpm *t, uint8_t *out,
+int shirudo_tpm_ek_certificate(struct shirudo_tpm *t, __u8 *out,
size_t cap, size_t *len, bool *rsa);
#endif /* SHIRUDO_TPM_H */
diff --git a/util/tpm_wire.c b/util/tpm_wire.c
index 3d0f3e2f..5ae9de02 100644
--- a/util/tpm_wire.c
+++ b/util/tpm_wire.c
@@ -8,6 +8,8 @@
#include <errno.h>
#include <string.h>
+#include <shirudo/misc.h>
+
#include "crypto.h"
#include "tpm_wire.h"
@@ -16,11 +18,11 @@
#define TPM_CC_POLICY_NV_WRITTEN 0x0000018Fu
struct reader {
- const uint8_t *p;
+ const __u8 *p;
size_t left;
};
-static int get8(struct reader *r, uint8_t *v)
+static int get8(struct reader *r, __u8 *v)
{
if (r->left < 1)
return -EINVAL;
@@ -30,41 +32,41 @@ static int get8(struct reader *r, uint8_t *v)
return 0;
}
-static int get16(struct reader *r, uint16_t *v)
+static int get16(struct reader *r, __u16 *v)
{
if (r->left < 2)
return -EINVAL;
- *v = (uint16_t)(((uint16_t)r->p[0] << 8) | r->p[1]);
+ *v = (__u16)(((__u16)r->p[0] << 8) | r->p[1]);
r->p += 2;
r->left -= 2;
return 0;
}
-static int get32(struct reader *r, uint32_t *v)
+static int get32(struct reader *r, __u32 *v)
{
if (r->left < 4)
return -EINVAL;
- *v = ((uint32_t)r->p[0] << 24) | ((uint32_t)r->p[1] << 16) |
- ((uint32_t)r->p[2] << 8) | r->p[3];
+ *v = ((__u32)r->p[0] << 24) | ((__u32)r->p[1] << 16) |
+ ((__u32)r->p[2] << 8) | r->p[3];
r->p += 4;
r->left -= 4;
return 0;
}
-static int get64(struct reader *r, uint64_t *v)
+static int get64(struct reader *r, __u64 *v)
{
- uint32_t hi, lo;
+ __u32 hi, lo;
if (get32(r, &hi) || get32(r, &lo))
return -EINVAL;
- *v = ((uint64_t)hi << 32) | lo;
+ *v = ((__u64)hi << 32) | lo;
return 0;
}
-static int get2b(struct reader *r, const uint8_t **data, size_t *len,
+static int get2b(struct reader *r, const __u8 **data, size_t *len,
size_t max)
{
- uint16_t n;
+ __u16 n;
if (get16(r, &n) || n > r->left || n > max)
return -EINVAL;
@@ -75,9 +77,9 @@ static int get2b(struct reader *r, const uint8_t **data, size_t *len,
return 0;
}
-static int get2b_fixed(struct reader *r, uint8_t *out, size_t want)
+static int get2b_fixed(struct reader *r, __u8 *out, size_t want)
{
- const uint8_t *d;
+ const __u8 *d;
size_t n;
if (get2b(r, &d, &n, want) || n != want)
@@ -86,12 +88,12 @@ static int get2b_fixed(struct reader *r, uint8_t *out, size_t want)
return 0;
}
-static void policy_hash(const uint8_t prev[32], uint32_t cc,
- const uint8_t *a, size_t a_len,
- const uint8_t *b, size_t b_len, uint8_t out[32])
+static void policy_hash(const __u8 prev[32], __u32 cc,
+ const __u8 *a, size_t a_len,
+ const __u8 *b, size_t b_len, __u8 out[32])
{
struct shirudo_sha256_ctx ctx;
- uint8_t cc_be[4];
+ __u8 cc_be[4];
tpm_put32(cc_be, cc);
if (shirudo_sha256_init(&ctx) != 0) {
@@ -108,51 +110,51 @@ static void policy_hash(const uint8_t prev[32], uint32_t cc,
}
}
-void shirudo_tpm_policy_nv_written(const uint8_t prev[32], bool written,
- uint8_t out[32])
+void shirudo_tpm_policy_nv_written(const __u8 prev[32], bool written,
+ __u8 out[32])
{
- uint8_t set = written ? 1 : 0;
+ __u8 set = written ? 1 : 0;
policy_hash(prev, TPM_CC_POLICY_NV_WRITTEN, &set, 1, NULL, 0, out);
}
static void
-shirudo_tpm_pcr_selection(uint32_t pcr_mask,
- uint8_t out[SHIRUDO_TPM_PCR_SELECTION_LEN])
+shirudo_tpm_pcr_selection(__u32 pcr_mask,
+ __u8 out[SHIRUDO_TPM_PCR_SELECTION_LEN])
{
tpm_put32(out, 1);
tpm_put16(out + 4, SHIRUDO_TPM_ALG_SHA256);
out[6] = SHIRUDO_TPM_PCR_SELECT_LEN;
- out[7] = (uint8_t)pcr_mask;
- out[8] = (uint8_t)(pcr_mask >> 8);
- out[9] = (uint8_t)(pcr_mask >> 16);
+ out[7] = (__u8)pcr_mask;
+ out[8] = (__u8)(pcr_mask >> 8);
+ out[9] = (__u8)(pcr_mask >> 16);
}
-void shirudo_tpm_policy_pcr(const uint8_t prev[32], uint32_t pcr_mask,
- const uint8_t pcr_digest[32], uint8_t out[32])
+void shirudo_tpm_policy_pcr(const __u8 prev[32], __u32 pcr_mask,
+ const __u8 pcr_digest[32], __u8 out[32])
{
- uint8_t sel[SHIRUDO_TPM_PCR_SELECTION_LEN];
+ __u8 sel[SHIRUDO_TPM_PCR_SELECTION_LEN];
shirudo_tpm_pcr_selection(pcr_mask, sel);
policy_hash(prev, TPM_CC_POLICY_PCR, sel, sizeof(sel), pcr_digest, 32,
out);
}
-void shirudo_tpm_policy_or(const uint8_t a[32], const uint8_t b[32],
- uint8_t out[32])
+void shirudo_tpm_policy_or(const __u8 a[32], const __u8 b[32],
+ __u8 out[32])
{
- static const uint8_t zero[32];
+ static const __u8 zero[32];
policy_hash(zero, TPM_CC_POLICY_OR, a, 32, b, 32, out);
}
-void shirudo_tpm_register_policy(uint32_t pcr_mask,
- const uint8_t *pcr_values, size_t n_values,
- uint8_t branch_written[32],
- uint8_t branch_init[32], uint8_t policy[32])
+void shirudo_tpm_register_policy(__u32 pcr_mask,
+ const __u8 *pcr_values, size_t n_values,
+ __u8 branch_written[32],
+ __u8 branch_init[32], __u8 policy[32])
{
- static const uint8_t zero[32];
- uint8_t pcr_digest[32], unwritten[32];
+ static const __u8 zero[32];
+ __u8 pcr_digest[32], unwritten[32];
shirudo_tpm_policy_nv_written(zero, true, branch_written);
shirudo_tpm_policy_nv_written(zero, false, unwritten);
@@ -161,11 +163,11 @@ void shirudo_tpm_register_policy(uint32_t pcr_mask,
shirudo_tpm_policy_or(branch_written, branch_init, policy);
}
-void shirudo_tpm_nv_name(uint32_t index, uint32_t attrs,
- const uint8_t policy[32], uint16_t data_size,
- uint8_t name[SHIRUDO_TPM_NAME_LEN])
+void shirudo_tpm_nv_name(__u32 index, __u32 attrs,
+ const __u8 policy[32], __u16 data_size,
+ __u8 name[SHIRUDO_TPM_NAME_LEN])
{
- uint8_t pub[4 + 2 + 4 + 2 + 32 + 2];
+ __u8 pub[4 + 2 + 4 + 2 + 32 + 2];
tpm_put32(pub, index);
tpm_put16(pub + 4, SHIRUDO_TPM_ALG_SHA256);
@@ -177,11 +179,11 @@ void shirudo_tpm_nv_name(uint32_t index, uint32_t attrs,
shirudo_sha256_buffer(pub, sizeof(pub), name + 2);
}
-int shirudo_tpm_object_name(const uint8_t *public, size_t len,
- uint8_t name[SHIRUDO_TPM_NAME_LEN])
+int shirudo_tpm_object_name(const __u8 *public, size_t len,
+ __u8 name[SHIRUDO_TPM_NAME_LEN])
{
struct reader r = { public, len };
- uint16_t type, alg;
+ __u16 type, alg;
if (get16(&r, &type) || get16(&r, &alg) ||
alg != SHIRUDO_TPM_ALG_SHA256)
@@ -191,11 +193,11 @@ int shirudo_tpm_object_name(const uint8_t *public, size_t len,
return 0;
}
-void shirudo_tpm_qualified_name(uint32_t hierarchy,
- const uint8_t name[SHIRUDO_TPM_NAME_LEN],
- uint8_t out[SHIRUDO_TPM_NAME_LEN])
+void shirudo_tpm_qualified_name(__u32 hierarchy,
+ const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ __u8 out[SHIRUDO_TPM_NAME_LEN])
{
- uint8_t buf[4 + SHIRUDO_TPM_NAME_LEN];
+ __u8 buf[4 + SHIRUDO_TPM_NAME_LEN];
tpm_put32(buf, hierarchy);
memcpy(buf + 4, name, SHIRUDO_TPM_NAME_LEN);
@@ -203,18 +205,18 @@ void shirudo_tpm_qualified_name(uint32_t hierarchy,
shirudo_sha256_buffer(buf, sizeof(buf), out + 2);
}
-const uint8_t shirudo_tpm_ek_policy[32] = {
+const __u8 shirudo_tpm_ek_policy[32] = {
0x83, 0x71, 0x97, 0x67, 0x44, 0x84, 0xb3, 0xf8, 0x1a, 0x90, 0xcc,
0x8d, 0x46, 0xa5, 0xd7, 0x24, 0xfd, 0x52, 0xd7, 0x6e, 0x06, 0x52,
0x0b, 0x64, 0xf2, 0xa1, 0xda, 0x1b, 0x33, 0x14, 0x69, 0xaa,
};
-int shirudo_tpm_public_parse(const uint8_t *buf, size_t len,
+int shirudo_tpm_public_parse(const __u8 *buf, size_t len,
struct shirudo_tpm_public *out)
{
struct reader r = { buf, len };
- const uint8_t *policy, *n;
- uint16_t name_alg, kdf;
+ const __u8 *policy, *n;
+ __u16 name_alg, kdf;
memset(out, 0, sizeof(*out));
if (get16(&r, &out->type) || get16(&r, &name_alg) ||
@@ -262,7 +264,7 @@ int shirudo_tpm_public_parse(const uint8_t *buf, size_t len,
}
int shirudo_tpm_public_marshal(const struct shirudo_tpm_public *pub,
- uint8_t *out, size_t cap, size_t *len)
+ __u8 *out, size_t cap, size_t *len)
{
size_t need = 2 + 2 + 4 + 2 + pub->policy_len + 2 + 2, off = 0;
@@ -316,7 +318,7 @@ int shirudo_tpm_public_marshal(const struct shirudo_tpm_public *pub,
return 0;
}
-int shirudo_tpm_ecc_public_parse(const uint8_t *buf, size_t len,
+int shirudo_tpm_ecc_public_parse(const __u8 *buf, size_t len,
struct shirudo_tpm_ecc_public *out)
{
struct shirudo_tpm_public pub;
@@ -339,14 +341,14 @@ int shirudo_tpm_ecc_public_parse(const uint8_t *buf, size_t len,
return 0;
}
-int shirudo_tpm_kdfa(const uint8_t *key, size_t key_len, const char *label,
- const uint8_t *ctx_u, size_t u_len,
- const uint8_t *ctx_v, size_t v_len,
- uint8_t *out, size_t out_len)
+int shirudo_tpm_kdfa(const __u8 *key, size_t key_len, const char *label,
+ const __u8 *ctx_u, size_t u_len,
+ const __u8 *ctx_v, size_t v_len,
+ __u8 *out, size_t out_len)
{
- uint8_t buf[4 + 64 + 1 + 2 * SHIRUDO_TPM_NAME_LEN + 4], block[32];
+ __u8 buf[4 + 64 + 1 + 2 * SHIRUDO_TPM_NAME_LEN + 4], block[32];
size_t label_len = strlen(label), done = 0, off, n;
- uint32_t i;
+ __u32 i;
if (label_len > 64 || u_len > SHIRUDO_TPM_NAME_LEN ||
v_len > SHIRUDO_TPM_NAME_LEN || out_len > 4096)
@@ -363,7 +365,7 @@ int shirudo_tpm_kdfa(const uint8_t *key, size_t key_len, const char *label,
if (v_len)
memcpy(buf + off, ctx_v, v_len);
off += v_len;
- tpm_put32(buf + off, (uint32_t)(out_len * 8));
+ tpm_put32(buf + off, (__u32)(out_len * 8));
off += 4;
if (shirudo_hmac_sha256(key, key_len, buf, off, block))
return -EIO;
@@ -375,11 +377,11 @@ int shirudo_tpm_kdfa(const uint8_t *key, size_t key_len, const char *label,
return 0;
}
-void shirudo_tpm_kdfe(const uint8_t z[32], const char *label,
- const uint8_t *party_u, size_t u_len,
- const uint8_t *party_v, size_t v_len, uint8_t out[32])
+void shirudo_tpm_kdfe(const __u8 z[32], const char *label,
+ const __u8 *party_u, size_t u_len,
+ const __u8 *party_v, size_t v_len, __u8 out[32])
{
- uint8_t buf[4 + 32 + 64 + 1 + 2 * 64];
+ __u8 buf[4 + 32 + 64 + 1 + 2 * 64];
size_t label_len = strlen(label), off = 4;
if (label_len > 64 || u_len > 64 || v_len > 64) {
@@ -402,14 +404,14 @@ void shirudo_tpm_kdfe(const uint8_t z[32], const char *label,
explicit_bzero(buf, sizeof(buf));
}
-int shirudo_tpm_credential_protect(const uint8_t seed[32],
- const uint8_t name[SHIRUDO_TPM_NAME_LEN],
- const uint8_t credential[SHIRUDO_TPM_CREDENTIAL_LEN],
- uint8_t out[SHIRUDO_TPM_ID_OBJECT_LEN])
+int shirudo_tpm_credential_protect(const __u8 seed[32],
+ const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ const __u8 credential[SHIRUDO_TPM_CREDENTIAL_LEN],
+ __u8 out[SHIRUDO_TPM_ID_OBJECT_LEN])
{
- uint8_t plain[2 + SHIRUDO_TPM_CREDENTIAL_LEN], enc[sizeof(plain)];
- uint8_t mac_in[sizeof(enc) + SHIRUDO_TPM_NAME_LEN], mac[32];
- uint8_t sym_key[16], hmac_key[32];
+ __u8 plain[2 + SHIRUDO_TPM_CREDENTIAL_LEN], enc[sizeof(plain)];
+ __u8 mac_in[sizeof(enc) + SHIRUDO_TPM_NAME_LEN], mac[32];
+ __u8 sym_key[16], hmac_key[32];
int rc = -EIO;
if (shirudo_tpm_kdfa(seed, 32, "STORAGE", name, SHIRUDO_TPM_NAME_LEN,
@@ -437,11 +439,11 @@ out:
return rc;
}
-int shirudo_tpm_ecdsa_signature_parse(const uint8_t *buf, size_t len,
- uint8_t r_out[32], uint8_t s_out[32])
+int shirudo_tpm_ecdsa_signature_parse(const __u8 *buf, size_t len,
+ __u8 r_out[32], __u8 s_out[32])
{
struct reader r = { buf, len };
- uint16_t alg, hash;
+ __u16 alg, hash;
if (get16(&r, &alg) || alg != SHIRUDO_TPM_ALG_ECDSA ||
get16(&r, &hash) || hash != SHIRUDO_TPM_ALG_SHA256 ||
@@ -451,9 +453,9 @@ int shirudo_tpm_ecdsa_signature_parse(const uint8_t *buf, size_t len,
return 0;
}
-static int get_name(struct reader *r, uint8_t *name, size_t *name_len)
+static int get_name(struct reader *r, __u8 *name, size_t *name_len)
{
- const uint8_t *d;
+ const __u8 *d;
size_t n;
if (get2b(r, &d, &n, SHIRUDO_TPM_NAME_LEN))
@@ -463,12 +465,12 @@ static int get_name(struct reader *r, uint8_t *name, size_t *name_len)
return 0;
}
-int shirudo_tpm_attest_parse(const uint8_t *buf, size_t len,
+int shirudo_tpm_attest_parse(const __u8 *buf, size_t len,
struct shirudo_tpm_attest *out)
{
struct reader r = { buf, len };
- uint32_t magic;
- uint8_t safe;
+ __u32 magic;
+ __u8 safe;
memset(out, 0, sizeof(*out));
if (get32(&r, &magic) || magic != SHIRUDO_TPM_GENERATED ||
@@ -482,16 +484,16 @@ int shirudo_tpm_attest_parse(const uint8_t *buf, size_t len,
out->safe = safe != 0;
if (out->type == SHIRUDO_TPM_ST_ATTEST_QUOTE) {
- uint32_t count;
- uint16_t alg;
- uint8_t n;
+ __u32 count;
+ __u16 alg;
+ __u8 n;
if (get32(&r, &count) || count != 1 || get16(&r, &alg) ||
alg != SHIRUDO_TPM_ALG_SHA256 || get8(&r, &n) ||
n != SHIRUDO_TPM_PCR_SELECT_LEN || r.left < n)
return -EINVAL;
- out->pcr_mask = (uint32_t)r.p[0] | ((uint32_t)r.p[1] << 8) |
- ((uint32_t)r.p[2] << 16);
+ out->pcr_mask = (__u32)r.p[0] | ((__u32)r.p[1] << 8) |
+ ((__u32)r.p[2] << 16);
r.p += n;
r.left -= n;
if (get2b_fixed(&r, out->pcr_digest, 32) || r.left != 0)
diff --git a/util/tpm_wire.h b/util/tpm_wire.h
index 4aab2ce1..0a581111 100644
--- a/util/tpm_wire.h
+++ b/util/tpm_wire.h
@@ -11,27 +11,28 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
#include <string.h>
-static inline void tpm_put16(uint8_t *p, uint16_t v)
+#include <shirudo/misc.h>
+
+static inline void tpm_put16(__u8 *p, __u16 v)
{
- p[0] = (uint8_t)(v >> 8);
- p[1] = (uint8_t)v;
+ p[0] = (__u8)(v >> 8);
+ p[1] = (__u8)v;
}
-static inline void tpm_put32(uint8_t *p, uint32_t v)
+static inline void tpm_put32(__u8 *p, __u32 v)
{
- p[0] = (uint8_t)(v >> 24);
- p[1] = (uint8_t)(v >> 16);
- p[2] = (uint8_t)(v >> 8);
- p[3] = (uint8_t)v;
+ p[0] = (__u8)(v >> 24);
+ p[1] = (__u8)(v >> 16);
+ p[2] = (__u8)(v >> 8);
+ p[3] = (__u8)v;
}
/* A TPM2B: the size, then the bytes. */
-static inline size_t tpm_put2b(uint8_t *p, const uint8_t *data, size_t len)
+static inline size_t tpm_put2b(__u8 *p, const __u8 *data, size_t len)
{
- tpm_put16(p, (uint16_t)len);
+ tpm_put16(p, (__u16)len);
memcpy(p + 2, data, len);
return 2 + len;
}
@@ -62,16 +63,16 @@ static inline size_t tpm_put2b(uint8_t *p, const uint8_t *data, size_t len)
#define SHIRUDO_TPM_REGISTER_ATTRS 0x0E060048u
#define SHIRUDO_TPM_NV_WRITTEN 0x20000000u
-void shirudo_tpm_policy_nv_written(const uint8_t prev[SHIRUDO_TPM_DIGEST_LEN],
+void shirudo_tpm_policy_nv_written(const __u8 prev[SHIRUDO_TPM_DIGEST_LEN],
bool written,
- uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
-void shirudo_tpm_policy_pcr(const uint8_t prev[SHIRUDO_TPM_DIGEST_LEN],
- uint32_t pcr_mask,
- const uint8_t pcr_digest[SHIRUDO_TPM_DIGEST_LEN],
- uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
-void shirudo_tpm_policy_or(const uint8_t a[SHIRUDO_TPM_DIGEST_LEN],
- const uint8_t b[SHIRUDO_TPM_DIGEST_LEN],
- uint8_t out[SHIRUDO_TPM_DIGEST_LEN]);
+ __u8 out[SHIRUDO_TPM_DIGEST_LEN]);
+void shirudo_tpm_policy_pcr(const __u8 prev[SHIRUDO_TPM_DIGEST_LEN],
+ __u32 pcr_mask,
+ const __u8 pcr_digest[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 out[SHIRUDO_TPM_DIGEST_LEN]);
+void shirudo_tpm_policy_or(const __u8 a[SHIRUDO_TPM_DIGEST_LEN],
+ const __u8 b[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 out[SHIRUDO_TPM_DIGEST_LEN]);
/*
* The register's write policy from the values the binding PCRs held
@@ -79,29 +80,29 @@ void shirudo_tpm_policy_or(const uint8_t a[SHIRUDO_TPM_DIGEST_LEN],
* PolicyNvWritten(NO) + PolicyPCR(mask, SHA-256(values))). The two
* branch digests are what a session has to present to PolicyOR.
*/
-void shirudo_tpm_register_policy(uint32_t pcr_mask,
- const uint8_t *pcr_values, size_t n_values,
- uint8_t branch_written[32],
- uint8_t branch_init[32],
- uint8_t policy[32]);
+void shirudo_tpm_register_policy(__u32 pcr_mask,
+ const __u8 *pcr_values, size_t n_values,
+ __u8 branch_written[32],
+ __u8 branch_init[32],
+ __u8 policy[32]);
/* Name of an NV index: nameAlg || SHA-256(TPMS_NV_PUBLIC). */
-void shirudo_tpm_nv_name(uint32_t index, uint32_t attrs,
- const uint8_t policy[SHIRUDO_TPM_DIGEST_LEN],
- uint16_t data_size,
- uint8_t name[SHIRUDO_TPM_NAME_LEN]);
+void shirudo_tpm_nv_name(__u32 index, __u32 attrs,
+ const __u8 policy[SHIRUDO_TPM_DIGEST_LEN],
+ __u16 data_size,
+ __u8 name[SHIRUDO_TPM_NAME_LEN]);
-int shirudo_tpm_object_name(const uint8_t *public, size_t len,
- uint8_t name[SHIRUDO_TPM_NAME_LEN]);
+int shirudo_tpm_object_name(const __u8 *public, size_t len,
+ __u8 name[SHIRUDO_TPM_NAME_LEN]);
/*
* Qualified name of a primary object: nameAlg || SHA-256(hierarchy
* handle || name). Attestations carry the signer's qualified name.
*/
#define SHIRUDO_TPM_RH_ENDORSEMENT 0x4000000Bu
-void shirudo_tpm_qualified_name(uint32_t hierarchy,
- const uint8_t name[SHIRUDO_TPM_NAME_LEN],
- uint8_t out[SHIRUDO_TPM_NAME_LEN]);
+void shirudo_tpm_qualified_name(__u32 hierarchy,
+ const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ __u8 out[SHIRUDO_TPM_NAME_LEN]);
/*
* The TCG default endorsement key templates: a restricted decryption
@@ -110,7 +111,7 @@ void shirudo_tpm_qualified_name(uint32_t hierarchy,
* adminWithPolicy | restricted | decrypt).
*/
#define SHIRUDO_TPM_EK_ATTRS 0x000300B2u
-extern const uint8_t shirudo_tpm_ek_policy[SHIRUDO_TPM_DIGEST_LEN];
+extern const __u8 shirudo_tpm_ek_policy[SHIRUDO_TPM_DIGEST_LEN];
/*
* A TPMT_PUBLIC of the shapes shirudo makes or checks: a SHA-256-named
@@ -118,39 +119,39 @@ extern const uint8_t shirudo_tpm_ek_policy[SHIRUDO_TPM_DIGEST_LEN];
* AES-128-CFB, its scheme absent or ECDSA with SHA-256.
*/
struct shirudo_tpm_public {
- uint16_t type;
- uint32_t attrs;
+ __u16 type;
+ __u32 attrs;
size_t policy_len; /* 0 or 32 */
- uint8_t policy[SHIRUDO_TPM_DIGEST_LEN];
- uint16_t sym_alg;
- uint16_t sym_bits;
- uint16_t sym_mode;
- uint16_t scheme;
- uint16_t scheme_hash;
- uint16_t curve; /* ECC */
- uint8_t x[32];
- uint8_t y[32];
- uint16_t rsa_bits; /* RSA */
- uint32_t rsa_exponent;
+ __u8 policy[SHIRUDO_TPM_DIGEST_LEN];
+ __u16 sym_alg;
+ __u16 sym_bits;
+ __u16 sym_mode;
+ __u16 scheme;
+ __u16 scheme_hash;
+ __u16 curve; /* ECC */
+ __u8 x[32];
+ __u8 y[32];
+ __u16 rsa_bits; /* RSA */
+ __u32 rsa_exponent;
size_t n_len;
- uint8_t n[256];
+ __u8 n[256];
};
-int shirudo_tpm_public_parse(const uint8_t *buf, size_t len,
+int shirudo_tpm_public_parse(const __u8 *buf, size_t len,
struct shirudo_tpm_public *out);
int shirudo_tpm_public_marshal(const struct shirudo_tpm_public *pub,
- uint8_t *out, size_t cap, size_t *len);
+ __u8 *out, size_t cap, size_t *len);
struct shirudo_tpm_ecc_public {
- uint32_t attrs;
- uint16_t scheme;
- uint16_t hash;
- uint16_t curve;
- uint8_t x[32];
- uint8_t y[32];
+ __u32 attrs;
+ __u16 scheme;
+ __u16 hash;
+ __u16 curve;
+ __u8 x[32];
+ __u8 y[32];
};
-int shirudo_tpm_ecc_public_parse(const uint8_t *buf, size_t len,
+int shirudo_tpm_ecc_public_parse(const __u8 *buf, size_t len,
struct shirudo_tpm_ecc_public *out);
/*
@@ -158,13 +159,13 @@ int shirudo_tpm_ecc_public_parse(const uint8_t *buf, size_t len,
* in counter mode) and KDFe (SP800-56A one-step). Labels are given
* without their terminating NUL, which the derivation adds.
*/
-int shirudo_tpm_kdfa(const uint8_t *key, size_t key_len, const char *label,
- const uint8_t *ctx_u, size_t u_len,
- const uint8_t *ctx_v, size_t v_len,
- uint8_t *out, size_t out_len);
-void shirudo_tpm_kdfe(const uint8_t z[32], const char *label,
- const uint8_t *party_u, size_t u_len,
- const uint8_t *party_v, size_t v_len, uint8_t out[32]);
+int shirudo_tpm_kdfa(const __u8 *key, size_t key_len, const char *label,
+ const __u8 *ctx_u, size_t u_len,
+ const __u8 *ctx_v, size_t v_len,
+ __u8 *out, size_t out_len);
+void shirudo_tpm_kdfe(const __u8 z[32], const char *label,
+ const __u8 *party_u, size_t u_len,
+ const __u8 *party_v, size_t v_len, __u8 out[32]);
/*
* Credential protection (Part 1, 24.4): from a seed shared with the
@@ -175,33 +176,33 @@ void shirudo_tpm_kdfe(const uint8_t z[32], const char *label,
*/
#define SHIRUDO_TPM_CREDENTIAL_LEN 32
#define SHIRUDO_TPM_ID_OBJECT_LEN (2 + 32 + 2 + SHIRUDO_TPM_CREDENTIAL_LEN)
-int shirudo_tpm_credential_protect(const uint8_t seed[32],
- const uint8_t name[SHIRUDO_TPM_NAME_LEN],
- const uint8_t credential[SHIRUDO_TPM_CREDENTIAL_LEN],
- uint8_t out[SHIRUDO_TPM_ID_OBJECT_LEN]);
+int shirudo_tpm_credential_protect(const __u8 seed[32],
+ const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ const __u8 credential[SHIRUDO_TPM_CREDENTIAL_LEN],
+ __u8 out[SHIRUDO_TPM_ID_OBJECT_LEN]);
-int shirudo_tpm_ecdsa_signature_parse(const uint8_t *buf, size_t len,
- uint8_t r[32], uint8_t s[32]);
+int shirudo_tpm_ecdsa_signature_parse(const __u8 *buf, size_t len,
+ __u8 r[32], __u8 s[32]);
struct shirudo_tpm_attest {
- uint16_t type;
- uint8_t signer[SHIRUDO_TPM_NAME_LEN];
+ __u16 type;
+ __u8 signer[SHIRUDO_TPM_NAME_LEN];
size_t signer_len;
- const uint8_t *extra;
+ const __u8 *extra;
size_t extra_len;
- uint64_t clock;
- uint32_t reset_count;
- uint32_t restart_count;
+ __u64 clock;
+ __u32 reset_count;
+ __u32 restart_count;
bool safe;
- uint64_t firmware_version;
+ __u64 firmware_version;
/* TPM_ST_ATTEST_QUOTE */
- uint32_t pcr_mask;
- uint8_t pcr_digest[SHIRUDO_TPM_DIGEST_LEN];
+ __u32 pcr_mask;
+ __u8 pcr_digest[SHIRUDO_TPM_DIGEST_LEN];
/* TPM_ST_ATTEST_NV */
- uint8_t index_name[SHIRUDO_TPM_NAME_LEN];
+ __u8 index_name[SHIRUDO_TPM_NAME_LEN];
size_t index_name_len;
- uint16_t offset;
- const uint8_t *nv_data;
+ __u16 offset;
+ const __u8 *nv_data;
size_t nv_len;
};
@@ -210,7 +211,7 @@ struct shirudo_tpm_attest {
* single SHA-256 PCR bank is accepted in a quote. Pointers into the
* input stay valid as long as buf does.
*/
-int shirudo_tpm_attest_parse(const uint8_t *buf, size_t len,
+int shirudo_tpm_attest_parse(const __u8 *buf, size_t len,
struct shirudo_tpm_attest *out);
#endif /* SHIRUDO_TPM_WIRE_H */
diff --git a/util/upatch.c b/util/upatch.c
index 9569ef80..9704e000 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -15,12 +15,14 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <shirudo/misc.h>
+
#include "upatch.h"
-static int parse_header(const unsigned char *m, size_t n,
+static int parse_header(const __u8 *m, size_t n,
struct shirudo_upatch_header *out)
{
- const unsigned char *p = m;
+ const __u8 *p = m;
if (n < SHIRUDO_UPATCH_HEADER_SIZE)
return -EINVAL;
@@ -77,11 +79,11 @@ static int parse_header(const unsigned char *m, size_t n,
return 0;
}
-static int section_inside(uint32_t off, uint32_t len, size_t n)
+static int section_inside(__u32 off, __u32 len, size_t n)
{
if (off > n || len > n)
return 0;
- if ((uint64_t)off + (uint64_t)len > (uint64_t)n)
+ if ((__u64)off + (__u64)len > (__u64)n)
return 0;
if (len != 0 && off < SHIRUDO_UPATCH_HEADER_SIZE)
return 0;
@@ -117,9 +119,9 @@ static bool has_nul(const char *s, size_t n)
*/
static void parse_decoders(struct shirudo_upatch *u)
{
- const unsigned char *data, *trp, *bp;
- uint32_t magic, version, decoder_off, decoder_len, nr_decoders;
- uint64_t entries_end;
+ const __u8 *data, *trp, *bp;
+ __u32 magic, version, decoder_off, decoder_len, nr_decoders;
+ __u64 entries_end;
size_t i;
if (u->hdr.data_len < sizeof(struct shirudo_upatch_decoder_trailer))
@@ -139,8 +141,8 @@ static void parse_decoders(struct shirudo_upatch *u)
decoder_len = rd_le32(trp + 12);
if (decoder_len < sizeof(struct shirudo_upatch_decoder_blob))
return;
- if ((uint64_t)decoder_off + decoder_len >
- (uint64_t)u->hdr.data_len -
+ if ((__u64)decoder_off + decoder_len >
+ (__u64)u->hdr.data_len -
sizeof(struct shirudo_upatch_decoder_trailer))
return;
@@ -153,7 +155,7 @@ static void parse_decoders(struct shirudo_upatch *u)
if (nr_decoders == 0)
return;
entries_end = sizeof(struct shirudo_upatch_decoder_blob) +
- (uint64_t)nr_decoders *
+ (__u64)nr_decoders *
sizeof(struct shirudo_upatch_decoder_entry);
if (entries_end > decoder_len)
return;
@@ -161,7 +163,7 @@ static void parse_decoders(struct shirudo_upatch *u)
/* The blob sits behind the signature, so entries are unaligned. */
for (i = 0; i < nr_decoders; i++) {
struct shirudo_upatch_decoder_entry d;
- uint32_t source_off, source_len;
+ __u32 source_off, source_len;
memcpy(&d, bp + sizeof(struct shirudo_upatch_decoder_blob) +
i * sizeof(d), sizeof(d));
@@ -293,11 +295,11 @@ bool shirudo_upatch_has_decoders(const struct shirudo_upatch *u)
return u->map && u->decoder_blob && u->nr_decoders > 0;
}
-int shirudo_upatch_decoder_view(const struct shirudo_upatch *u, uint32_t idx,
+int shirudo_upatch_decoder_view(const struct shirudo_upatch *u, __u32 idx,
struct shirudo_upatch_decoder_view *out)
{
struct shirudo_upatch_decoder_entry d;
- const unsigned char *bp;
+ const __u8 *bp;
if (!out)
return -EINVAL;
@@ -305,7 +307,7 @@ int shirudo_upatch_decoder_view(const struct shirudo_upatch *u, uint32_t idx,
if (!shirudo_upatch_has_decoders(u) || idx >= u->nr_decoders)
return -ENOENT;
- bp = (const unsigned char *)u->decoder_blob;
+ bp = (const __u8 *)u->decoder_blob;
memcpy(&d, bp + sizeof(struct shirudo_upatch_decoder_blob) +
(size_t)idx * sizeof(d), sizeof(d));
out->language = le32toh(d.language);
diff --git a/util/upatch.h b/util/upatch.h
index 5fa30e78..b6337d7f 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -8,6 +8,7 @@
#ifndef SHIRUDO_UTIL_UPATCH_H
#define SHIRUDO_UTIL_UPATCH_H
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include <dirent.h>
#include <stdbool.h>
@@ -18,24 +19,24 @@
struct shirudo_upatch {
struct shirudo_upatch_header hdr; /* host byte order */
- const unsigned char *map;
+ const __u8 *map;
size_t map_size;
const struct shirudo_upatch_decoder_blob *decoder_blob;
- uint32_t decoder_blob_len;
- uint32_t nr_decoders;
+ __u32 decoder_blob_len;
+ __u32 nr_decoders;
};
struct shirudo_upatch_decoder_view {
- uint32_t language;
- uint32_t api_version;
- uint32_t input_format;
- uint32_t export_signal;
+ __u32 language;
+ __u32 api_version;
+ __u32 input_format;
+ __u32 export_signal;
char upatch[64];
char map[16];
char source_name[64];
char entrypoint[32];
const char *source;
- uint32_t source_len;
+ __u32 source_len;
};
/*
@@ -58,7 +59,7 @@ bool shirudo_upatch_is_unload(const struct shirudo_upatch *u);
const char *shirudo_upatch_type_str(const struct shirudo_upatch *u);
bool shirudo_upatch_has_decoders(const struct shirudo_upatch *u);
-int shirudo_upatch_decoder_view(const struct shirudo_upatch *u, uint32_t idx,
+int shirudo_upatch_decoder_view(const struct shirudo_upatch *u, __u32 idx,
struct shirudo_upatch_decoder_view *out);
/*
diff --git a/util/upatch_decode.c b/util/upatch_decode.c
index 6da29abc..3e007f90 100644
--- a/util/upatch_decode.c
+++ b/util/upatch_decode.c
@@ -8,6 +8,8 @@
#include <errno.h>
+#include <shirudo/misc.h>
+
#include "upatch_decode.h"
#ifndef SHIRUDO_HAVE_LUAJIT
@@ -66,7 +68,6 @@ int shirudo_upatch_decode_map_file_otel(const char *path,
#include <ctype.h>
#include <errno.h>
-#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
@@ -106,8 +107,8 @@ static int lua_absindex(lua_State *L, int idx)
#endif
struct lua_slot {
- uint32_t len;
- unsigned char data[];
+ __u32 len;
+ __u8 data[];
};
struct json_out {
@@ -223,7 +224,7 @@ static void json_put_string(struct json_out *out, const char *s, size_t n)
json_putc(out, '"');
for (i = 0; i < n && !out->err; i++) {
- unsigned char c = s[i];
+ __u8 c = s[i];
switch (c) {
case '"':
@@ -269,7 +270,7 @@ static void json_put_string(struct json_out *out, const char *s, size_t n)
break;
}
for (j = 1; j < width; j++) {
- if (((unsigned char)s[i + j] & 0xc0) != 0x80) {
+ if (((__u8)s[i + j] & 0xc0) != 0x80) {
out->err = -EILSEQ;
break;
}
@@ -553,15 +554,15 @@ static int lua_json_emit_value(lua_State *L, int idx, struct json_out *out,
}
static int hex_decode_alloc(const char *hex, size_t len,
- unsigned char **out, size_t *out_len)
+ __u8 **out, size_t *out_len)
{
- unsigned char *buf;
+ __u8 *buf;
size_t digits = 0, off = 0, i;
*out = NULL;
*out_len = 0;
for (i = 0; i < len; i++) {
- if (!isspace((unsigned char)hex[i]))
+ if (!isspace((__u8)hex[i]))
digits++;
}
if (digits & 1)
@@ -572,23 +573,23 @@ static int hex_decode_alloc(const char *hex, size_t len,
for (i = 0; i < len;) {
int hi, lo;
- while (i < len && isspace((unsigned char)hex[i]))
+ while (i < len && isspace((__u8)hex[i]))
i++;
if (i >= len)
break;
- hi = shirudo_hex_nibble((unsigned char)hex[i++]);
- while (i < len && isspace((unsigned char)hex[i]))
+ hi = shirudo_hex_nibble((__u8)hex[i++]);
+ while (i < len && isspace((__u8)hex[i]))
i++;
if (i >= len) {
xfree(buf, digits / 2);
return -EINVAL;
}
- lo = shirudo_hex_nibble((unsigned char)hex[i++]);
+ lo = shirudo_hex_nibble((__u8)hex[i++]);
if (hi < 0 || lo < 0) {
xfree(buf, digits / 2);
return -EINVAL;
}
- buf[off++] = (unsigned char)((hi << 4) | lo);
+ buf[off++] = (__u8)((hi << 4) | lo);
}
*out = buf;
*out_len = off;
@@ -619,20 +620,20 @@ static int lua_slot_u8(lua_State *L)
return 1;
}
-static uint16_t rd_u16(const unsigned char *p)
+static __u16 rd_u16(const __u8 *p)
{
- return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
+ return (__u16)p[0] | ((__u16)p[1] << 8);
}
-static uint32_t rd_u32(const unsigned char *p)
+static __u32 rd_u32(const __u8 *p)
{
- return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
- ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
+ return (__u32)p[0] | ((__u32)p[1] << 8) |
+ ((__u32)p[2] << 16) | ((__u32)p[3] << 24);
}
-static uint64_t rd_u64(const unsigned char *p)
+static __u64 rd_u64(const __u8 *p)
{
- return (uint64_t)rd_u32(p) | ((uint64_t)rd_u32(p + 4) << 32);
+ return (__u64)rd_u32(p) | ((__u64)rd_u32(p + 4) << 32);
}
static int lua_slot_u16(lua_State *L)
@@ -660,11 +661,11 @@ static int lua_slot_u64(lua_State *L)
struct lua_slot *slot = lua_check_slot(L);
char buf[32];
int off = lua_check_c_int(L, 2);
- uint64_t v = 0;
+ __u64 v = 0;
if (off >= 0 && off <= (int)slot->len - 8)
v = rd_u64(slot->data + off);
- snprintf(buf, sizeof(buf), "%" PRIu64, v);
+ snprintf(buf, sizeof(buf), "%llu", v);
lua_pushstring(L, buf);
return 1;
}
@@ -691,18 +692,18 @@ static int lua_slot_cstring(lua_State *L)
return 1;
}
-static void lua_push_slot(lua_State *L, const unsigned char *data, size_t len)
+static void lua_push_slot(lua_State *L, const __u8 *data, size_t len)
{
struct lua_slot *slot;
slot = lua_newuserdata(L, sizeof(*slot) + len);
- slot->len = (uint32_t)len;
+ slot->len = (__u32)len;
memcpy(slot->data, data, len);
luaL_getmetatable(L, SHIRUDO_LUA_SLOT_MT);
lua_setmetatable(L, -2);
}
-static uint64_t lua_page_u64(lua_State *L, int idx, const char *field)
+static __u64 lua_page_u64(lua_State *L, int idx, const char *field)
{
lua_Number n;
@@ -712,11 +713,11 @@ static uint64_t lua_page_u64(lua_State *L, int idx, const char *field)
lua_pop(L, 1);
if (!isfinite((double)n) || n < 0 || n > 9007199254740991.0)
return 0;
- return (uint64_t)n;
+ return (__u64)n;
}
-static int lua_store_slot(lua_State *L, int slots_idx, uint64_t abs,
- const unsigned char *data, size_t slot_size)
+static int lua_store_slot(lua_State *L, int slots_idx, __u64 abs,
+ const __u8 *data, size_t slot_size)
{
if (abs >= INT_MAX)
return luaL_error(L, "slot index too large");
@@ -726,8 +727,8 @@ static int lua_store_slot(lua_State *L, int slots_idx, uint64_t abs,
}
static void lua_split_page_slots(lua_State *L, int slots_idx,
- const unsigned char *data, size_t len,
- uint64_t cursor, uint64_t chunks,
+ const __u8 *data, size_t len,
+ __u64 cursor, __u64 chunks,
size_t slot_size)
{
size_t slots_per_chunk = SHIRUDO_SERIAL_CHUNK_PAYLOAD / slot_size;
@@ -735,7 +736,7 @@ static void lua_split_page_slots(lua_State *L, int slots_idx,
if (chunks > 1 && slots_per_chunk > 0 &&
SHIRUDO_SERIAL_CHUNK_PAYLOAD % slot_size != 0) {
size_t off = 0;
- uint64_t chunk;
+ __u64 chunk;
for (chunk = 0; chunk < chunks && off < len; chunk++) {
size_t chunk_bytes = len - off;
@@ -747,7 +748,7 @@ static void lua_split_page_slots(lua_State *L, int slots_idx,
if (local_slots > slots_per_chunk)
local_slots = slots_per_chunk;
for (i = 0; i < local_slots; i++) {
- uint64_t page_slot = chunk * slots_per_chunk + i;
+ __u64 page_slot = chunk * slots_per_chunk + i;
if (page_slot == 0)
continue;
@@ -765,14 +766,14 @@ static void lua_split_page_slots(lua_State *L, int slots_idx,
data + i, slot_size);
}
-static void lua_push_u64_string(lua_State *L, uint64_t value);
+static void lua_push_u64_string(lua_State *L, __u64 value);
static int lua_shirudo_page_slots(lua_State *L)
{
const char *hex;
- unsigned char *data = NULL;
+ __u8 *data = NULL;
size_t hex_len, data_len = 0, off = 0, per_chunk;
- uint64_t chunks;
+ __u64 chunks;
int slot_size, n = 0, rc;
luaL_checktype(L, 1, LUA_TTABLE);
@@ -814,21 +815,21 @@ static int lua_shirudo_page_slots(lua_State *L)
return 1;
}
-static int lua_check_u64_string(lua_State *L, int arg, uint64_t *out)
+static int lua_check_u64_string(lua_State *L, int arg, __u64 *out)
{
const char *s = luaL_checkstring(L, arg);
- uint64_t v = 0;
+ __u64 v = 0;
size_t i;
if (!s[0] || strlen(s) > 20)
return luaL_argerror(L, arg, "expected a decimal u64 string");
for (i = 0; s[i]; i++) {
- unsigned int digit;
+ __u32 digit;
if (s[i] < '0' || s[i] > '9')
return luaL_argerror(L, arg,
"expected a decimal u64 string");
- digit = (unsigned int)(s[i] - '0');
+ digit = (__u32)(s[i] - '0');
if (v > (UINT64_MAX - digit) / 10)
return luaL_argerror(L, arg, "u64 string out of range");
v = v * 10 + digit;
@@ -840,7 +841,7 @@ static int lua_check_u64_string(lua_State *L, int arg, uint64_t *out)
/* Exact 64-bit arithmetic on decimal strings; Lua numbers lose nanoseconds. */
static int lua_shirudo_u64_add(lua_State *L)
{
- uint64_t a, b;
+ __u64 a, b;
lua_check_u64_string(L, 1, &a);
lua_check_u64_string(L, 2, &b);
@@ -850,7 +851,7 @@ static int lua_shirudo_u64_add(lua_State *L)
static int lua_shirudo_u64_sub(lua_State *L)
{
- uint64_t a, b;
+ __u64 a, b;
lua_check_u64_string(L, 1, &a);
lua_check_u64_string(L, 2, &b);
@@ -863,7 +864,7 @@ static int lua_shirudo_hex_u64(lua_State *L)
const char *hex;
size_t hex_len;
int off = lua_check_c_int(L, 2);
- uint64_t v = 0;
+ __u64 v = 0;
int i;
hex = luaL_checklstring(L, 1, &hex_len);
@@ -880,7 +881,7 @@ static int lua_shirudo_hex_u64(lua_State *L)
lua_push_u64_string(L, 0);
return 1;
}
- v = (v << 8) | (uint64_t)((hi << 4) | lo);
+ v = (v << 8) | (__u64)((hi << 4) | lo);
}
lua_push_u64_string(L, v);
return 1;
@@ -903,8 +904,8 @@ static int lua_shirudo_fixed_records(lua_State *L)
n_pages = lua_objlen(L, pages_idx);
for (i = 1; i <= n_pages; i++) {
const char *hex;
- unsigned char *data = NULL;
- uint64_t cursor, chunks;
+ __u8 *data = NULL;
+ __u64 cursor, chunks;
size_t hex_len, data_len = 0;
int rc;
@@ -1002,29 +1003,29 @@ static void lua_install_budget(lua_State *L, struct lua_budget *budget)
lua_sethook(L, lua_budget_hook, LUA_MASKCOUNT, SHIRUDO_LUA_HOOK_COUNT);
}
-static uint64_t decoder_realtime_ns(void)
+static __u64 decoder_realtime_ns(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
return 0;
- return (uint64_t)ts.tv_sec * 1000000000ULL +
- (uint64_t)ts.tv_nsec;
+ return (__u64)ts.tv_sec * 1000000000ULL +
+ (__u64)ts.tv_nsec;
}
-static void lua_push_u64_string(lua_State *L, uint64_t value)
+static void lua_push_u64_string(lua_State *L, __u64 value)
{
char buf[32];
- snprintf(buf, sizeof(buf), "%" PRIu64, value);
+ snprintf(buf, sizeof(buf), "%llu", value);
lua_pushstring(L, buf);
}
static void lua_push_map_input(lua_State *L, const struct shirudo_map_view *v)
{
- uint64_t now_ns = decoder_realtime_ns();
- uint64_t start_ns = v->lifetime_start_ns ?: now_ns;
- uint32_t p, i;
+ __u64 now_ns = decoder_realtime_ns();
+ __u64 start_ns = v->lifetime_start_ns ?: now_ns;
+ __u32 p, i;
lua_newtable(L);
lua_pushinteger(L, 1);
@@ -1263,7 +1264,7 @@ static int decode_map_file(const char *path, const struct shirudo_map_view *v,
char **out, size_t *out_len)
{
struct shirudo_upatch u;
- uint32_t i;
+ __u32 i;
int rc;
if (!path || !v || !out || !out_len)
@@ -1291,11 +1292,11 @@ 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)
+ __u32 *signal_out)
{
struct shirudo_upatch u;
char path[PATH_MAX];
- uint32_t i;
+ __u32 i;
int rc;
if (!v || !signal_out || !shirudo_upatch_name_valid(v->upatch))
diff --git a/util/upatch_decode.h b/util/upatch_decode.h
index 5eada405..cccfb4f9 100644
--- a/util/upatch_decode.h
+++ b/util/upatch_decode.h
@@ -9,7 +9,8 @@
#define SHIRUDO_UTIL_UPATCH_DECODE_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
struct shirudo_map_view;
@@ -42,6 +43,6 @@ int shirudo_upatch_decode_map_file_otel(const char *path,
char **out, size_t *out_len);
int shirudo_upatch_decode_map_signal(const struct shirudo_map_view *v,
- uint32_t *signal_out);
+ __u32 *signal_out);
#endif /* SHIRUDO_UTIL_UPATCH_DECODE_H */
diff --git a/util/upatch_map_types.h b/util/upatch_map_types.h
index 210c7311..4ecc6ad8 100644
--- a/util/upatch_map_types.h
+++ b/util/upatch_map_types.h
@@ -9,9 +9,10 @@
#define SHIRUDO_UTIL_UPATCH_MAP_TYPES_H
#include <linux/bpf.h>
-#include <stdint.h>
-static inline const char *shirudo_map_type_name(uint32_t type)
+#include <shirudo/misc.h>
+
+static inline const char *shirudo_map_type_name(__u32 type)
{
switch (type) {
case BPF_MAP_TYPE_ARRAY:
@@ -41,7 +42,7 @@ static inline const char *shirudo_map_type_name(uint32_t type)
}
}
-static inline const char *shirudo_map_skip_reason(uint32_t type)
+static inline const char *shirudo_map_skip_reason(__u32 type)
{
switch (type) {
case BPF_MAP_TYPE_RINGBUF:
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index 5e698982..b615202f 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -9,7 +9,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
-#include <inttypes.h>
+#include <stdint.h>
#include <linux/bpf.h>
#include <linux/btf.h>
#include <stdio.h>
@@ -22,6 +22,7 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include "json.h"
@@ -60,34 +61,34 @@ static void resolve_map_location(struct shirudo_map_view *v)
#define SHIRUDO_SERIAL_MAX_BYTES \
(SHIRUDO_SERIAL_MAX_CHUNKS * SHIRUDO_SERIAL_CHUNK_PAYLOAD)
#define SHIRUDO_SERIAL_MAX_TOTAL_BYTES \
- ((uint64_t)SHIRUDO_SERIAL_MAX_PAGES * SHIRUDO_SERIAL_MAX_BYTES)
+ ((__u64)SHIRUDO_SERIAL_MAX_PAGES * SHIRUDO_SERIAL_MAX_BYTES)
-bool shirudo_upatch_map_is_percpu(uint32_t type)
+bool shirudo_upatch_map_is_percpu(__u32 type)
{
return type == BPF_MAP_TYPE_PERCPU_ARRAY ||
type == BPF_MAP_TYPE_PERCPU_HASH ||
type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
}
-static uint32_t map_value_stride(uint32_t type, uint32_t value_size)
+static __u32 map_value_stride(__u32 type, __u32 value_size)
{
if (!shirudo_upatch_map_is_percpu(type))
return value_size;
return (value_size + 7U) & ~7U;
}
-uint32_t shirudo_upatch_map_value_stride(const struct shirudo_map_view *v)
+__u32 shirudo_upatch_map_value_stride(const struct shirudo_map_view *v)
{
return v->value_stride ? v->value_stride : v->value_size;
}
-static bool is_array_type(uint32_t type)
+static bool is_array_type(__u32 type)
{
return type == BPF_MAP_TYPE_ARRAY ||
type == BPF_MAP_TYPE_PERCPU_ARRAY;
}
-static bool is_hash_type(uint32_t type)
+static bool is_hash_type(__u32 type)
{
return type == BPF_MAP_TYPE_HASH ||
type == BPF_MAP_TYPE_PERCPU_HASH ||
@@ -95,7 +96,7 @@ static bool is_hash_type(uint32_t type)
type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
}
-static void btf_type_name(struct btf *btf, uint32_t type_id,
+static void btf_type_name(struct btf *btf, __u32 type_id,
char *dst, size_t dst_cap)
{
const struct btf_type *t;
@@ -117,7 +118,7 @@ static void btf_type_name(struct btf *btf, uint32_t type_id,
* label (e.g. unnamed PTR -> named STRUCT). Bounded loop.
*/
{
- uint32_t id = type_id;
+ __u32 id = type_id;
int hops;
for (hops = 0; hops < 4; hops++) {
@@ -138,7 +139,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 uint32_t btf_unwrap(const struct btf *btf, uint32_t id)
+static __u32 btf_unwrap(const struct btf *btf, __u32 id)
{
const struct btf_type *t;
int hops;
@@ -167,7 +168,7 @@ static uint32_t btf_unwrap(const struct btf *btf, uint32_t id)
* prefix; for typedefs and primitives the BTF name carries enough
* context on its own (no prefix needed).
*/
-static const char *btf_kind_prefix(uint16_t kind)
+static const char *btf_kind_prefix(__u16 kind)
{
switch (kind) {
case BTF_KIND_STRUCT:
@@ -183,7 +184,7 @@ static const char *btf_kind_prefix(uint16_t kind)
}
}
-static void btf_type_signature_into(const struct btf *btf, uint32_t id,
+static void btf_type_signature_into(const struct btf *btf, __u32 id,
char *buf, size_t cap, size_t *off,
int depth);
@@ -213,14 +214,14 @@ static void append_indent(char *buf, size_t cap, size_t *off, int depth)
buf[*off] = '\0';
}
-static void btf_type_signature_into(const struct btf *btf, uint32_t id,
+static void btf_type_signature_into(const struct btf *btf, __u32 id,
char *buf, size_t cap, size_t *off,
int depth)
{
const struct btf_type *t;
const char *name;
- uint16_t kind;
int i, vlen;
+ __u16 kind;
if (depth >= SHIRUDO_BTF_MAX_DEPTH || !btf || !id || *off >= cap)
return;
@@ -256,13 +257,13 @@ static void btf_type_signature_into(const struct btf *btf, uint32_t id,
* recursing, to keep the signature single-screen.
*/
{
- uint32_t mid = btf_unwrap(btf, m[i].type);
+ __u32 mid = btf_unwrap(btf, m[i].type);
const struct btf_type *mt = btf__type_by_id(btf, mid);
const char *mtype = mt ?
btf__name_by_offset(btf,
mt->name_off) :
NULL;
- uint16_t mkind = mt ? BTF_INFO_KIND(mt->info)
+ __u16 mkind = mt ? BTF_INFO_KIND(mt->info)
: 0;
append_str(buf, cap, off, btf_kind_prefix(mkind));
@@ -299,8 +300,8 @@ static void btf_type_signature_into(const struct btf *btf, uint32_t id,
append_str(buf, cap, off, name && name[0] ? name : "?");
}
-static char *btf_format_value(const struct btf *btf, uint32_t type_id,
- const void *bytes, uint32_t bytes_size,
+static char *btf_format_value(const struct btf *btf, __u32 type_id,
+ const void *bytes, __u32 bytes_size,
int depth);
static int strbuf_append_take(char **out, size_t *cap, size_t *off,
@@ -335,33 +336,33 @@ static int strbuf_append_take(char **out, size_t *cap, size_t *off,
}
static char *btf_format_int(const struct btf_type *t, const void *bytes,
- uint32_t bytes_size)
+ __u32 bytes_size)
{
- uint32_t enc_word = *(const uint32_t *)(t + 1);
- uint32_t bits = BTF_INT_BITS(enc_word);
- uint32_t encoding = BTF_INT_ENCODING(enc_word);
+ __u32 enc_word = *(const __u32 *)(t + 1);
+ __u32 bits = BTF_INT_BITS(enc_word);
+ __u32 encoding = BTF_INT_ENCODING(enc_word);
bool is_signed = (encoding & BTF_INT_SIGNED) != 0;
- uint8_t bytes_used = (uint8_t)((bits + 7) / 8);
+ __u8 bytes_used = (__u8)((bits + 7) / 8);
if (bytes_used == 0 || bytes_used > bytes_size || bytes_used > 8)
return fmt_alloc("?");
if (is_signed) {
- uint64_t v = 0;
- uint8_t i;
+ __u64 v = 0;
+ __u8 i;
- /* Shifting into the sign bit of an int64_t is undefined. */
+ /* Shifting into the sign bit of an __s64 is undefined. */
for (i = 0; i < bytes_used; i++)
- v |= (uint64_t)((const uint8_t *)bytes)[i] << (8 * i);
- if (bits < 64 && (v & ((uint64_t)1 << (bits - 1))))
- v |= ~(((uint64_t)1 << bits) - 1);
- return fmt_alloc("%" PRId64, (int64_t)v);
+ v |= (__u64)((const __u8 *)bytes)[i] << (8 * i);
+ if (bits < 64 && (v & ((__u64)1 << (bits - 1))))
+ v |= ~(((__u64)1 << bits) - 1);
+ return fmt_alloc("%lld", (__s64)v);
} else {
- uint64_t v = 0;
- uint8_t i;
+ __u64 v = 0;
+ __u8 i;
for (i = 0; i < bytes_used; i++)
- v |= (uint64_t)((const uint8_t *)bytes)[i] << (8 * i);
- return fmt_alloc("%" PRIu64, v);
+ v |= (__u64)((const __u8 *)bytes)[i] << (8 * i);
+ return fmt_alloc("%llu", v);
}
}
@@ -369,42 +370,42 @@ static char *btf_format_int(const struct btf_type *t, const void *bytes,
* A struct member declared with a bit width: BTF gives its offset and
* width in bits, and the value is no whole number of bytes.
*/
-static char *btf_format_bitfield(const struct btf *btf, uint32_t type_id,
- const void *bytes, uint32_t bytes_size,
- uint32_t bit_off, uint32_t bit_size)
+static char *btf_format_bitfield(const struct btf *btf, __u32 type_id,
+ const void *bytes, __u32 bytes_size,
+ __u32 bit_off, __u32 bit_size)
{
- uint32_t shift = bit_off % 8, n = (shift + bit_size + 7) / 8, i;
- const uint8_t *b = (const uint8_t *)bytes + bit_off / 8;
+ __u32 shift = bit_off % 8, n = (shift + bit_size + 7) / 8, i;
+ const __u8 *b = (const __u8 *)bytes + bit_off / 8;
const struct btf_type *t;
bool is_signed = false;
- uint64_t v = 0;
+ __u64 v = 0;
if (!bit_size || shift + bit_size > 64 || bit_off / 8 + n > bytes_size)
return fmt_alloc("?");
for (i = 0; i < n; i++)
- v |= (uint64_t)b[i] << (8 * i);
+ v |= (__u64)b[i] << (8 * i);
v >>= shift;
t = btf__type_by_id(btf, btf_unwrap(btf, type_id));
if (bit_size < 64)
- v &= ((uint64_t)1 << bit_size) - 1;
+ v &= ((__u64)1 << bit_size) - 1;
if (t && BTF_INFO_KIND(t->info) == BTF_KIND_INT)
- is_signed = BTF_INT_ENCODING(*(const uint32_t *)(t + 1)) &
+ is_signed = BTF_INT_ENCODING(*(const __u32 *)(t + 1)) &
BTF_INT_SIGNED;
else if (t && (BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64))
is_signed = BTF_INFO_KFLAG(t->info);
- if (is_signed && bit_size < 64 && (v & ((uint64_t)1 << (bit_size - 1))))
- v |= ~(((uint64_t)1 << bit_size) - 1);
- return is_signed ? fmt_alloc("%" PRId64, (int64_t)v) :
- fmt_alloc("%" PRIu64, v);
+ if (is_signed && bit_size < 64 && (v & ((__u64)1 << (bit_size - 1))))
+ v |= ~(((__u64)1 << bit_size) - 1);
+ return is_signed ? fmt_alloc("%lld", (__s64)v) :
+ fmt_alloc("%llu", v);
}
-static char *btf_format_value(const struct btf *btf, uint32_t type_id,
- const void *bytes, uint32_t bytes_size,
+static char *btf_format_value(const struct btf *btf, __u32 type_id,
+ const void *bytes, __u32 bytes_size,
int depth)
{
const struct btf_type *t;
- uint16_t kind;
+ __u16 kind;
if (depth >= SHIRUDO_BTF_MAX_DEPTH || !btf || !type_id || !bytes ||
!bytes_size)
@@ -419,51 +420,51 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
return btf_format_int(t, bytes, bytes_size);
if (kind == BTF_KIND_PTR) {
- uint64_t v = 0;
+ __u64 v = 0;
if (bytes_size >= 8)
memcpy(&v, bytes, 8);
else if (bytes_size >= 4)
memcpy(&v, bytes, 4);
- return fmt_alloc("0x%" PRIx64, v);
+ return fmt_alloc("0x%llx", v);
}
if (kind == BTF_KIND_ENUM) {
const struct btf_enum *e = (const struct btf_enum *)(t + 1);
int vlen = BTF_INFO_VLEN(t->info);
- uint32_t v = 0, size = t->size, i;
+ __u32 v = 0, size = t->size, i;
/* An enum is as wide as BTF says, one byte for a packed one. */
if (!size || size > 4 || size > bytes_size)
return fmt_alloc("?");
for (i = 0; i < size; i++)
- v |= (uint32_t)((const uint8_t *)bytes)[i] << (8 * i);
+ v |= (__u32)((const __u8 *)bytes)[i] << (8 * i);
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++) {
- if ((uint32_t)e[i].val == v) {
+ for (i = 0; i < (__u32)vlen; i++) {
+ if ((__u32)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);
+ fmt_alloc("%d", (__s32)v) :
+ fmt_alloc("%u", v);
}
if (kind == BTF_KIND_ENUM64) {
const struct btf_enum64 *e =
(const struct btf_enum64 *)(t + 1);
int vlen = BTF_INFO_VLEN(t->info);
- uint64_t v = 0;
+ __u64 v = 0;
int i;
if (bytes_size >= 8)
memcpy(&v, bytes, 8);
for (i = 0; i < vlen; i++) {
- uint64_t ev = ((uint64_t)e[i].val_hi32 << 32) |
+ __u64 ev = ((__u64)e[i].val_hi32 << 32) |
e[i].val_lo32;
if (ev == v) {
@@ -472,7 +473,7 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
return fmt_alloc("%s", nm && nm[0] ? nm : "?");
}
}
- return fmt_alloc("0x%" PRIx64, v);
+ return fmt_alloc("0x%llx", v);
}
if (kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION) {
@@ -492,12 +493,12 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
return NULL;
}
for (i = 0; i < vlen; i++) {
- uint32_t bit_off = BTF_INFO_KFLAG(t->info) ?
+ __u32 bit_off = BTF_INFO_KFLAG(t->info) ?
BTF_MEMBER_BIT_OFFSET(m[i].offset) :
m[i].offset;
- uint32_t byte_off = bit_off / 8, bit_size = 0;
+ __u32 byte_off = bit_off / 8, bit_size = 0;
const struct btf_type *ft;
- uint32_t fsize = 0;
+ __u32 fsize = 0;
const char *fname;
char *fval;
@@ -508,10 +509,10 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
ft = btf__type_by_id(btf, btf_unwrap(btf, m[i].type));
fname = btf__name_by_offset(btf, m[i].name_off);
if (ft) {
- int64_t sz = btf__resolve_size(btf, m[i].type);
+ __s64 sz = btf__resolve_size(btf, m[i].type);
if (sz > 0)
- fsize = (uint32_t)sz;
+ fsize = (__u32)sz;
}
if (!fsize || byte_off + fsize > bytes_size)
fsize = bytes_size - byte_off;
@@ -540,12 +541,12 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
if (kind == BTF_KIND_ARRAY) {
const struct btf_array *a = (const struct btf_array *)(t + 1);
- int64_t esz = btf__resolve_size(btf, a->type);
- uint32_t i, n = a->nelems;
+ __s64 esz = btf__resolve_size(btf, a->type);
size_t cap = 64, off = 0;
+ __u32 i, n = a->nelems;
char *out;
- if (esz <= 0 || (uint64_t)esz * n > bytes_size)
+ if (esz <= 0 || (__u64)esz * n > bytes_size)
return NULL;
out = xmalloc(cap);
if (!out)
@@ -555,7 +556,7 @@ static char *btf_format_value(const struct btf *btf, uint32_t type_id,
for (i = 0; i < n && i < 16; i++) {
char *ev = btf_format_value(btf, a->type,
(const char *)bytes + i * (size_t)esz,
- (uint32_t)esz, depth + 1);
+ (__u32)esz, depth + 1);
strbuf_append_take(&out, &cap, &off,
fmt_alloc("%s%s",
@@ -604,7 +605,7 @@ static int nproc_possible(void)
return n > 0 ? (int)n : 1;
}
-static char *format_value(const void *value, uint32_t value_size,
+static char *format_value(const void *value, __u32 value_size,
const char *btf_type)
{
if (btf_type && btf_type[0]) {
@@ -612,59 +613,59 @@ static char *format_value(const void *value, uint32_t value_size,
(strcmp(btf_type, "__u64") == 0 ||
strcmp(btf_type, "u64") == 0 ||
strcmp(btf_type, "long long unsigned int") == 0)) {
- uint64_t v;
+ __u64 v;
memcpy(&v, value, 8);
- return fmt_alloc("%" PRIu64, v);
+ return fmt_alloc("%llu", v);
}
if (value_size == 8 &&
(strcmp(btf_type, "__s64") == 0 ||
strcmp(btf_type, "s64") == 0 ||
strcmp(btf_type, "long long int") == 0)) {
- int64_t v;
+ __s64 v;
memcpy(&v, value, 8);
- return fmt_alloc("%" PRId64, v);
+ return fmt_alloc("%lld", v);
}
if (value_size == 4 &&
(strcmp(btf_type, "__u32") == 0 ||
strcmp(btf_type, "u32") == 0 ||
strcmp(btf_type, "unsigned int") == 0)) {
- uint32_t v;
+ __u32 v;
memcpy(&v, value, 4);
- return fmt_alloc("%" PRIu32, v);
+ return fmt_alloc("%u", v);
}
if (value_size == 4 &&
(strcmp(btf_type, "__s32") == 0 ||
strcmp(btf_type, "s32") == 0 ||
strcmp(btf_type, "int") == 0)) {
- int32_t v;
+ __s32 v;
memcpy(&v, value, 4);
- return fmt_alloc("%" PRId32, v);
+ return fmt_alloc("%d", v);
}
}
if (value_size == 8) {
- uint64_t v;
+ __u64 v;
memcpy(&v, value, 8);
- return fmt_alloc("%" PRIu64, v);
+ return fmt_alloc("%llu", v);
}
if (value_size == 4) {
- uint32_t v;
+ __u32 v;
memcpy(&v, value, 4);
- return fmt_alloc("%" PRIu32, v);
+ return fmt_alloc("%u", v);
}
/* Hex fallback (cap at 32 bytes shown for readability). */
{
- const unsigned char *p = value;
- uint32_t cap = value_size < 32 ? value_size : 32;
+ const __u8 *p = value;
+ __u32 cap = value_size < 32 ? value_size : 32;
size_t buf_sz = (size_t)cap * 2 + 8;
char *out = xmalloc(buf_sz);
size_t off = 0;
- uint32_t i;
+ __u32 i;
if (!out)
return NULL;
@@ -689,35 +690,35 @@ char *shirudo_upatch_map_format_percpu_sum(const struct shirudo_map_view *v,
{
if (v->value_size == 1 || v->value_size == 2 ||
v->value_size == 4 || v->value_size == 8) {
- uint32_t bits = 8 * v->value_size, i;
+ __u32 bits = 8 * v->value_size, i;
const struct btf_type *t = NULL;
bool is_signed = false;
- uint64_t sum = 0;
+ __u64 sum = 0;
if (v->btf && v->value_type_id) {
- uint32_t id = btf_unwrap(v->btf, v->value_type_id);
+ __u32 id = btf_unwrap(v->btf, v->value_type_id);
t = btf__type_by_id(v->btf, id);
}
if (t && BTF_INFO_KIND(t->info) == BTF_KIND_INT) {
- uint32_t enc = *(const uint32_t *)(t + 1);
+ __u32 enc = *(const __u32 *)(t + 1);
is_signed = BTF_INT_ENCODING(enc) & BTF_INT_SIGNED;
}
for (i = 0; i < v->n_cpus; i++) {
- uint64_t one = 0;
+ __u64 one = 0;
memcpy(&one, (const char *)values_per_cpu +
(size_t)i * shirudo_upatch_map_value_stride(v),
v->value_size);
/* A negative per-CPU count must not add as 2^n - 1. */
if (is_signed && bits < 64 &&
- (one & ((uint64_t)1 << (bits - 1))))
- one |= ~(((uint64_t)1 << bits) - 1);
+ (one & ((__u64)1 << (bits - 1))))
+ one |= ~(((__u64)1 << bits) - 1);
sum += one;
}
- return is_signed ? fmt_alloc("%" PRId64, (int64_t)sum) :
- fmt_alloc("%" PRIu64, sum);
+ return is_signed ? fmt_alloc("%lld", (__s64)sum) :
+ fmt_alloc("%llu", sum);
}
(void)btf_type;
return xstrdup("(unsupported width)");
@@ -728,7 +729,7 @@ static int open_map_pin(const char *upatch, const char *map_name,
{
char path[PATH_MAX];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
- uint32_t info_len = sizeof(*info_out);
+ __u32 info_len = sizeof(*info_out);
int fd;
if (shirudo_upatch_bpffs_name(bpffs_name, sizeof(bpffs_name), upatch))
@@ -751,7 +752,7 @@ static int open_map_pin(const char *upatch, const char *map_name,
return 0;
}
-static struct btf *try_load_btf(uint32_t btf_id)
+static struct btf *try_load_btf(__u32 btf_id)
{
if (!btf_id)
return NULL;
@@ -776,14 +777,14 @@ static int open_serializer_pin(const char *upatch, const char *map_name)
return fd;
}
-int shirudo_prog_test_run(int prog_fd, void *ctx, uint32_t ctx_sz,
- uint32_t *retval)
+int shirudo_prog_test_run(int prog_fd, void *ctx, __u32 ctx_sz,
+ __u32 *retval)
{
union bpf_attr attr = {};
long ret;
attr.test.prog_fd = prog_fd;
- attr.test.ctx_in = (uint64_t)(uintptr_t)ctx;
+ attr.test.ctx_in = (__u64)(uintptr_t)ctx;
attr.test.ctx_size_in = ctx_sz;
ret = syscall(__NR_bpf, BPF_PROG_TEST_RUN, &attr, sizeof(attr));
if (ret < 0)
@@ -796,7 +797,7 @@ static int validate_serial_map(const struct bpf_map_info *info)
{
if (info->type != BPF_MAP_TYPE_ARRAY)
return -EINVAL;
- if (info->key_size != sizeof(uint32_t))
+ if (info->key_size != sizeof(__u32))
return -EINVAL;
if (info->value_size != sizeof(struct shirudo_serial_chunk))
return -EINVAL;
@@ -809,7 +810,7 @@ static int validate_serial_map(const struct bpf_map_info *info)
static int clear_serial_map(int fd, const struct bpf_map_info *info)
{
void *zero;
- uint32_t i;
+ __u32 i;
zero = xcalloc(1, info->value_size);
if (!zero)
@@ -843,15 +844,15 @@ static int append_serial_page(struct shirudo_map_view *out,
static int read_serial_page(int serial_fd,
const struct bpf_map_info *serial_info,
- uint32_t want_page_index, uint64_t want_cursor,
- uint64_t invocation_id,
+ __u32 want_page_index, __u64 want_cursor,
+ __u64 invocation_id,
struct shirudo_serial_page *page)
{
struct shirudo_serial_chunk *chunk;
- uint32_t key, total_chunks, bytes_total, copied = 0;
- uint32_t page_index, flags;
- uint64_t cursor, next_cursor;
- unsigned char *data = NULL;
+ __u32 key, total_chunks, bytes_total, copied = 0;
+ __u32 page_index, flags;
+ __u64 cursor, next_cursor;
+ __u8 *data = NULL;
int rc;
chunk = xmalloc(sizeof(*chunk));
@@ -945,11 +946,11 @@ out_chunk:
}
static int try_read_serialized(const char *upatch, const char *map,
- uint32_t flags, struct shirudo_map_view *out)
+ __u32 flags, struct shirudo_map_view *out)
{
struct bpf_map_info serial_info = {};
- uint64_t cursor = 0, bytes_total = 0, invocation_id;
- uint32_t page_index;
+ __u64 cursor = 0, bytes_total = 0, invocation_id;
+ __u32 page_index;
int prog_fd = -1, serial_fd = -1, lock_fd = -1, rc;
if (strcmp(map, SHIRUDO_SERIAL_MAP_NAME) == 0)
@@ -986,7 +987,7 @@ static int try_read_serialized(const char *upatch, const char *map,
page_index++) {
struct shirudo_serialize_ctx ctx = {};
struct shirudo_serial_page page = {};
- uint32_t retval = 0;
+ __u32 retval = 0;
rc = clear_serial_map(serial_fd, &serial_info);
if (rc)
@@ -1005,8 +1006,8 @@ static int try_read_serialized(const char *upatch, const char *map,
rc = shirudo_prog_test_run(prog_fd, &ctx, sizeof(ctx), &retval);
if (rc)
goto out;
- if ((int32_t)retval < 0) {
- rc = (int32_t)retval;
+ if ((__s32)retval < 0) {
+ rc = (__s32)retval;
goto out;
}
if (ctx.magic != SHIRUDO_SERIAL_MAGIC ||
@@ -1058,7 +1059,7 @@ static int try_read_serialized(const char *upatch, const char *map,
out:
if (rc < 0 && out->serial_pages) {
- uint32_t i;
+ __u32 i;
for (i = 0; i < out->n_serial_pages; i++)
xfree(out->serial_pages[i].data,
@@ -1079,12 +1080,12 @@ out:
}
static int read_entries(int fd, struct shirudo_map_view *v,
- uint32_t cap)
+ __u32 cap)
{
size_t value_count = shirudo_upatch_map_is_percpu(v->type) ?
v->n_cpus : 1;
size_t value_alloc;
- uint32_t i;
+ __u32 i;
int err;
if (shirudo_upatch_map_value_stride(v) != 0 &&
@@ -1098,7 +1099,7 @@ static int read_entries(int fd, struct shirudo_map_view *v,
v->n_truncated_at = cap;
if (is_array_type(v->type)) {
- uint32_t take = v->max_entries;
+ __u32 take = v->max_entries;
size_t bytes;
if (cap && take > cap) {
@@ -1198,7 +1199,7 @@ static int read_entries(int fd, struct shirudo_map_view *v,
void shirudo_map_view_free(struct shirudo_map_view *v)
{
size_t value_alloc;
- uint32_t i;
+ __u32 i;
if (!v)
return;
@@ -1231,7 +1232,7 @@ void shirudo_map_view_free(struct shirudo_map_view *v)
}
int shirudo_upatch_maps_read(const char *upatch, const char *map,
- uint32_t max_entries_cap,
+ __u32 max_entries_cap,
struct shirudo_map_view *out)
{
return shirudo_upatch_maps_read_flags(upatch, map, max_entries_cap, 0,
@@ -1239,7 +1240,7 @@ int shirudo_upatch_maps_read(const char *upatch, const char *map,
}
int shirudo_upatch_maps_read_flags(const char *upatch, const char *map,
- uint32_t max_entries_cap, uint32_t flags,
+ __u32 max_entries_cap, __u32 flags,
struct shirudo_map_view *out)
{
struct bpf_map_info info = {};
@@ -1263,15 +1264,15 @@ int shirudo_upatch_maps_read_flags(const char *upatch, const char *map,
out->value_stride = map_value_stride(info.type, info.value_size);
out->max_entries = info.max_entries;
out->n_cpus = shirudo_upatch_map_is_percpu(info.type) ?
- (uint32_t)nproc_possible() : 1;
+ (__u32)nproc_possible() : 1;
resolve_map_location(out);
if (!shirudo_upatch_bpffs_name(bpffs_name, sizeof(bpffs_name), upatch) &&
(size_t)snprintf(upatch_dir, sizeof(upatch_dir), "%s/%s",
SHIRUDO_BPFFS_UPATCHES_DIR, bpffs_name) <
sizeof(upatch_dir) && stat(upatch_dir, &upatch_st) == 0)
out->lifetime_start_ns =
- (uint64_t)upatch_st.st_ctim.tv_sec * 1000000000ULL +
- (uint64_t)upatch_st.st_ctim.tv_nsec;
+ (__u64)upatch_st.st_ctim.tv_sec * 1000000000ULL +
+ (__u64)upatch_st.st_ctim.tv_nsec;
rc = try_read_serialized(upatch, map, flags, out);
if (rc < 0) {
@@ -1371,7 +1372,7 @@ static void scan_upatch_maps(const char *upatch,
one.type_name = shirudo_map_type_name(info.type);
one.max_entries = info.max_entries;
one.n_cpus = shirudo_upatch_map_is_percpu(info.type) ?
- (uint32_t)nproc_possible() : 1;
+ (__u32)nproc_possible() : 1;
summary_append(list, n, cap, &one);
}
closedir(d);
@@ -1447,8 +1448,8 @@ void shirudo_upatch_maps_print_summary(const struct shirudo_map_summary *list,
}
char *shirudo_upatch_map_format_cell(const struct shirudo_map_view *v,
- uint32_t type_id, const void *bytes,
- uint32_t bytes_size,
+ __u32 type_id, const void *bytes,
+ __u32 bytes_size,
const char *btf_name)
{
char *s = NULL;
@@ -1460,7 +1461,7 @@ char *shirudo_upatch_map_format_cell(const struct shirudo_map_view *v,
return s;
}
-static void print_type_def(const char *label, const char *def, uint32_t size)
+static void print_type_def(const char *label, const char *def, __u32 size)
{
const char *p = def;
@@ -1479,9 +1480,9 @@ static void print_type_def(const char *label, const char *def, uint32_t size)
printf(" (%u B)\n", size);
}
-static void print_serialized_hex(const unsigned char *data, uint32_t len)
+static void print_serialized_hex(const __u8 *data, __u32 len)
{
- uint32_t i;
+ __u32 i;
for (i = 0; i < len; i++) {
if (i % 16 == 0)
@@ -1496,7 +1497,7 @@ static void print_serialized_hex(const unsigned char *data, uint32_t len)
void shirudo_upatch_maps_print_detail(const struct shirudo_map_view *v)
{
- uint32_t i;
+ __u32 i;
printf("upatch: %s\n", v->upatch);
printf("Map: %s\n", v->name);
@@ -1512,20 +1513,20 @@ void shirudo_upatch_maps_print_detail(const struct shirudo_map_view *v)
return;
}
if (v->serialized) {
- uint32_t p;
+ __u32 p;
printf("Status: serialized\n");
printf("Serialized pages: %u\n", v->n_serial_pages);
- printf("Serialized bytes: %" PRIu64 "\n",
+ printf("Serialized bytes: %llu\n",
v->serial_bytes_total);
for (p = 0; p < v->n_serial_pages; p++) {
const struct shirudo_serial_page *page =
&v->serial_pages[p];
printf("\nPage %u:\n", page->page_index);
- printf(" Cursor: %" PRIu64 "\n",
+ printf(" Cursor: %llu\n",
page->cursor);
- printf(" Next cursor: %" PRIu64 "\n",
+ printf(" Next cursor: %llu\n",
page->next_cursor);
printf(" Done: %s\n",
page->done ? "true" : "false");
@@ -1610,7 +1611,7 @@ void shirudo_upatch_maps_print_detail(const struct shirudo_map_view *v)
void shirudo_upatch_maps_emit_json_fields(struct shirudo_json_emit *e,
const struct shirudo_map_view *v)
{
- uint32_t i, j;
+ __u32 i, j;
if (!v)
return;
@@ -1621,7 +1622,7 @@ void shirudo_upatch_maps_emit_json_fields(struct shirudo_json_emit *e,
if (shirudo_upatch_map_is_percpu(v->type))
shirudo_json_kv_int(e, "n_cpus", v->n_cpus);
if (v->serialized) {
- uint32_t p;
+ __u32 p;
shirudo_json_kv_bool(e, "serialized", true);
shirudo_json_kv_int(e, "serialized_pages",
diff --git a/util/upatch_maps.h b/util/upatch_maps.h
index e394d021..58d75f55 100644
--- a/util/upatch_maps.h
+++ b/util/upatch_maps.h
@@ -11,8 +11,8 @@
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include "upatch_resolve.h"
@@ -41,12 +41,12 @@ struct shirudo_map_entry {
struct btf;
struct shirudo_serial_page {
- uint32_t page_index;
- uint64_t cursor;
- uint64_t next_cursor;
- uint32_t flags;
- uint32_t chunks;
- uint32_t bytes;
+ __u32 page_index;
+ __u64 cursor;
+ __u64 next_cursor;
+ __u32 flags;
+ __u32 chunks;
+ __u32 bytes;
bool done;
void *data;
};
@@ -55,20 +55,20 @@ struct shirudo_map_view {
char upatch[SHIRUDO_UPATCH_NAME_MAX + 1];
char name[SHIRUDO_MAP_NAME_MAX + 1];
char location[SHIRUDO_UPATCH_LOCATION_MAX];
- uint32_t type; /* BPF_MAP_TYPE_* */
+ __u32 type; /* BPF_MAP_TYPE_* */
const char *type_name; /* "percpu_array" / "hash" / ... */
- uint32_t key_size;
- uint32_t value_size; /* per-CPU value size (not multiplied) */
- uint32_t value_stride; /* per-CPU kernel ABI stride; else value_size */
- uint32_t max_entries;
- uint32_t n_entries; /* live entries iterated */
- uint32_t n_cpus; /* 1 for non-percpu types */
- uint64_t lifetime_start_ns; /* pin-dir ctime; metric reset boundary */
+ __u32 key_size;
+ __u32 value_size; /* per-CPU value size (not multiplied) */
+ __u32 value_stride; /* per-CPU kernel ABI stride; else value_size */
+ __u32 max_entries;
+ __u32 n_entries; /* live entries iterated */
+ __u32 n_cpus; /* 1 for non-percpu types */
+ __u64 lifetime_start_ns; /* pin-dir ctime; metric reset boundary */
bool truncated;
- uint32_t n_truncated_at;
+ __u32 n_truncated_at;
bool serialized;
- uint32_t n_serial_pages;
- uint64_t serial_bytes_total;
+ __u32 n_serial_pages;
+ __u64 serial_bytes_total;
struct shirudo_serial_page *serial_pages;
bool skipped;
const char *skipped_reason;
@@ -76,8 +76,8 @@ struct shirudo_map_view {
char btf_value_type[SHIRUDO_MAP_TYPENAME_MAX];
char key_type_def[SHIRUDO_MAP_TYPEDEF_MAX];
char value_type_def[SHIRUDO_MAP_TYPEDEF_MAX];
- uint32_t key_type_id;
- uint32_t value_type_id;
+ __u32 key_type_id;
+ __u32 value_type_id;
struct btf *btf; /* heap; owned by the view */
struct shirudo_map_entry *entries; /* n_entries long */
};
@@ -88,10 +88,10 @@ void shirudo_map_view_free(struct shirudo_map_view *v);
struct shirudo_map_summary {
char upatch[SHIRUDO_UPATCH_NAME_MAX + 1];
char name[SHIRUDO_MAP_NAME_MAX + 1];
- uint32_t type;
+ __u32 type;
const char *type_name;
- uint32_t max_entries;
- uint32_t n_cpus;
+ __u32 max_entries;
+ __u32 n_cpus;
};
int shirudo_upatch_maps_list_all(struct shirudo_map_summary **out,
@@ -107,7 +107,7 @@ void shirudo_map_summary_free(struct shirudo_map_summary *list, size_t n);
* default, 0 for unlimited (--show-full). Returns 0/-errno.
*/
int shirudo_upatch_maps_read(const char *upatch, const char *map,
- uint32_t max_entries_cap,
+ __u32 max_entries_cap,
struct shirudo_map_view *out);
/*
@@ -118,7 +118,7 @@ int shirudo_upatch_maps_read(const char *upatch, const char *map,
#define SHIRUDO_MAP_READ_PEEK (1u << 0)
int shirudo_upatch_maps_read_flags(const char *upatch, const char *map,
- uint32_t max_entries_cap, uint32_t flags,
+ __u32 max_entries_cap, __u32 flags,
struct shirudo_map_view *out);
void shirudo_upatch_maps_print_summary(const struct shirudo_map_summary *list,
@@ -131,17 +131,17 @@ void shirudo_upatch_maps_emit_json_fields(struct shirudo_json_emit *e,
void shirudo_upatch_maps_emit_json_object(struct shirudo_json_emit *e,
const struct shirudo_map_view *v);
-bool shirudo_upatch_map_is_percpu(uint32_t type);
-uint32_t shirudo_upatch_map_value_stride(const struct shirudo_map_view *v);
+bool shirudo_upatch_map_is_percpu(__u32 type);
+__u32 shirudo_upatch_map_value_stride(const struct shirudo_map_view *v);
char *shirudo_upatch_map_format_cell(const struct shirudo_map_view *v,
- uint32_t type_id, const void *value,
- uint32_t value_size,
+ __u32 type_id, const void *value,
+ __u32 value_size,
const char *btf_type);
char *shirudo_upatch_map_format_percpu_sum(const struct shirudo_map_view *v,
const void *values_per_cpu,
const char *btf_type);
-int shirudo_prog_test_run(int prog_fd, void *ctx, uint32_t ctx_sz,
- uint32_t *retval);
+int shirudo_prog_test_run(int prog_fd, void *ctx, __u32 ctx_sz,
+ __u32 *retval);
#endif /* SHIRUDO_UTIL_UPATCH_MAPS_H */
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 580e04dd..a47e1925 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -10,7 +10,6 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
-#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
@@ -25,6 +24,7 @@
#include <linux/bpf.h>
#include <linux/btf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include <bpf/btf.h>
@@ -62,7 +62,7 @@
struct otel_response {
char buf[OTEL_RESPONSE_MAX];
size_t len;
- unsigned long retry_after;
+ __u64 retry_after;
};
struct otel_state {
@@ -83,9 +83,9 @@ struct otel_state {
struct s3_snapshot {
FILE *file;
- uint64_t json_bytes;
- uint64_t object_bytes;
- unsigned char sha256[32];
+ __u64 json_bytes;
+ __u64 object_bytes;
+ __u8 sha256[32];
char sha256_hex[65];
char sha256_b64[45];
};
@@ -108,11 +108,11 @@ struct otel_s3_state {
struct metric_type {
bool is_signed;
- uint32_t width;
+ __u32 width;
};
static const struct btf_type *unwrap_btf_type(const struct btf *btf,
- uint32_t id)
+ __u32 id)
{
const struct btf_type *t;
int i;
@@ -194,11 +194,11 @@ static void otlp_kv_string(struct shirudo_json_emit *e, const char *key,
}
static void otlp_kv_int(struct shirudo_json_emit *e, const char *key,
- int64_t value)
+ __s64 value)
{
char num[32];
- snprintf(num, sizeof(num), "%" PRId64, value);
+ snprintf(num, sizeof(num), "%lld", value);
shirudo_json_obj_open(e);
shirudo_json_kv_string(e, "key", key);
shirudo_json_obj_open_key(e, "value");
@@ -282,12 +282,12 @@ static void otlp_array_kvlist_end(struct shirudo_json_emit *e)
struct json_anyvalue_ctx {
const char *p;
const char *end;
- unsigned int depth;
+ __u32 depth;
};
static void json_anyvalue_skip_ws(struct json_anyvalue_ctx *c)
{
- while (c->p < c->end && isspace((unsigned char)*c->p))
+ while (c->p < c->end && isspace((__u8)*c->p))
c->p++;
}
@@ -300,7 +300,7 @@ static int json_anyvalue_string(struct json_anyvalue_ctx *c,
return -EINVAL;
p = c->p++;
while (c->p < c->end) {
- unsigned char ch = (unsigned char)*c->p++;
+ __u8 ch = (__u8)*c->p++;
if (ch == '"') {
*start = p;
@@ -310,14 +310,14 @@ static int json_anyvalue_string(struct json_anyvalue_ctx *c,
if (ch == '\\') {
if (c->p >= c->end)
return -EINVAL;
- ch = (unsigned char)*c->p++;
+ ch = (__u8)*c->p++;
if (ch == 'u') {
int i;
if ((size_t)(c->end - c->p) < 4)
return -EINVAL;
for (i = 0; i < 4; i++) {
- if (!isxdigit((unsigned char)c->p[i]))
+ if (!isxdigit((__u8)c->p[i]))
return -EINVAL;
}
c->p += 4;
@@ -355,20 +355,20 @@ static int json_anyvalue_number(struct json_anyvalue_ctx *c,
if (c->p < c->end && *c->p == '-')
c->p++;
- if (c->p >= c->end || !isdigit((unsigned char)*c->p))
+ if (c->p >= c->end || !isdigit((__u8)*c->p))
return -EINVAL;
if (*c->p == '0') {
c->p++;
} else {
- while (c->p < c->end && isdigit((unsigned char)*c->p))
+ while (c->p < c->end && isdigit((__u8)*c->p))
c->p++;
}
if (c->p < c->end && *c->p == '.') {
fraction = true;
c->p++;
- if (c->p >= c->end || !isdigit((unsigned char)*c->p))
+ if (c->p >= c->end || !isdigit((__u8)*c->p))
return -EINVAL;
- while (c->p < c->end && isdigit((unsigned char)*c->p))
+ while (c->p < c->end && isdigit((__u8)*c->p))
c->p++;
}
if (c->p < c->end && (*c->p == 'e' || *c->p == 'E')) {
@@ -376,9 +376,9 @@ static int json_anyvalue_number(struct json_anyvalue_ctx *c,
c->p++;
if (c->p < c->end && (*c->p == '+' || *c->p == '-'))
c->p++;
- if (c->p >= c->end || !isdigit((unsigned char)*c->p))
+ if (c->p >= c->end || !isdigit((__u8)*c->p))
return -EINVAL;
- while (c->p < c->end && isdigit((unsigned char)*c->p))
+ while (c->p < c->end && isdigit((__u8)*c->p))
c->p++;
}
len = (size_t)(c->p - start);
@@ -602,24 +602,24 @@ static void emit_resource_attributes(struct shirudo_json_emit *e)
shirudo_json_arr_close(e);
}
-static uint64_t realtime_ns(void)
+static __u64 realtime_ns(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
return 0;
- return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
+ return (__u64)ts.tv_sec * 1000000000ULL + (__u64)ts.tv_nsec;
}
-static void u64_decimal(uint64_t value, char out[32])
+static void u64_decimal(__u64 value, char out[32])
{
- snprintf(out, 32, "%" PRIu64, value);
+ snprintf(out, 32, "%llu", value);
}
static __attribute__((unused)) void emit_log_map_data(struct shirudo_json_emit *e,
const struct shirudo_map_view *v)
{
- uint32_t i, j;
+ __u32 i, j;
otlp_kv_string(e, "name", v->name);
otlp_kv_string(e, "type", v->type_name);
@@ -628,12 +628,12 @@ static __attribute__((unused)) void emit_log_map_data(struct shirudo_json_emit *
if (shirudo_upatch_map_is_percpu(v->type))
otlp_kv_int(e, "n_cpus", v->n_cpus);
if (v->serialized) {
- uint32_t p;
+ __u32 p;
otlp_kv_bool(e, "serialized", true);
otlp_kv_int(e, "serialized_pages", v->n_serial_pages);
otlp_kv_int(e, "serialized_bytes",
- (int64_t)v->serial_bytes_total);
+ (__s64)v->serial_bytes_total);
otlp_array_begin(e, "pages");
for (p = 0; p < v->n_serial_pages; p++) {
const struct shirudo_serial_page *page = &v->serial_pages[p];
@@ -642,9 +642,9 @@ static __attribute__((unused)) void emit_log_map_data(struct shirudo_json_emit *
otlp_array_kvlist_begin(e);
otlp_kv_int(e, "page_index", page->page_index);
- otlp_kv_int(e, "cursor", (int64_t)page->cursor);
+ otlp_kv_int(e, "cursor", (__s64)page->cursor);
otlp_kv_int(e, "next_cursor",
- (int64_t)page->next_cursor);
+ (__s64)page->next_cursor);
otlp_kv_bool(e, "done", page->done);
otlp_kv_int(e, "flags", page->flags);
otlp_kv_int(e, "chunks", page->chunks);
@@ -812,20 +812,20 @@ int shirudo_upatch_maps_otel_build_log_file(const char *path,
static int read_integer(const void *p, const struct metric_type *mt,
__int128 *signed_out, unsigned __int128 *unsigned_out)
{
- uint64_t u = 0;
- int64_t s = 0;
+ __u64 u = 0;
+ __s64 s = 0;
memcpy(&u, p, mt->width);
if (mt->is_signed) {
switch (mt->width) {
case 1:
- s = (int8_t)u;
+ s = (__s8)u;
break;
case 2:
- s = (int16_t)u;
+ s = (__s16)u;
break;
case 4:
- s = (int32_t)u;
+ s = (__s32)u;
break;
case 8:
memcpy(&s, p, 8);
@@ -842,15 +842,15 @@ static int read_integer(const void *p, const struct metric_type *mt,
static int entry_metric_value(const struct shirudo_map_view *v,
const struct shirudo_map_entry *en,
- const struct metric_type *mt, int64_t *out)
+ const struct metric_type *mt, __s64 *out)
{
- unsigned int count = shirudo_upatch_map_is_percpu(v->type) ?
+ __u32 count = shirudo_upatch_map_is_percpu(v->type) ?
v->n_cpus : 1;
__int128 signed_sum = 0;
unsigned __int128 unsigned_sum = 0;
- uint32_t stride = shirudo_upatch_map_value_stride(v);
- unsigned int i;
+ __u32 stride = shirudo_upatch_map_value_stride(v);
+ __u32 i;
for (i = 0; i < count; i++) {
__int128 s = 0;
@@ -869,11 +869,11 @@ static int entry_metric_value(const struct shirudo_map_view *v,
if (mt->is_signed) {
if (signed_sum < INT64_MIN || signed_sum > INT64_MAX)
return -ERANGE;
- *out = (int64_t)signed_sum;
+ *out = (__s64)signed_sum;
} else {
if (unsigned_sum > INT64_MAX)
return -ERANGE;
- *out = (int64_t)unsigned_sum;
+ *out = (__s64)unsigned_sum;
}
return 0;
}
@@ -882,13 +882,13 @@ static bool valid_metric_name(const char *name)
{
size_t i, n;
- if (!name || !isalpha((unsigned char)name[0]))
+ if (!name || !isalpha((__u8)name[0]))
return false;
n = strlen(name);
if (n == 0 || n > 255)
return false;
for (i = 1; i < n; i++) {
- unsigned char c = (unsigned char)name[i];
+ __u8 c = (__u8)name[i];
if (!isalnum(c) && c != '_' && c != '.' && c != '-' && c != '/')
return false;
@@ -912,7 +912,7 @@ static int metric_name(const struct otel_state *st,
if (n < 0 || n >= 256)
return -ENAMETOOLONG;
for (i = 0; out[i]; i++) {
- unsigned char c = (unsigned char)out[i];
+ __u8 c = (__u8)out[i];
if (!isalnum(c) && c != '_' && c != '.' && c != '-' && c != '/')
out[i] = '_';
@@ -920,7 +920,7 @@ static int metric_name(const struct otel_state *st,
return 0;
}
-static int map_start_ns(const struct shirudo_map_view *v, uint64_t *out)
+static int map_start_ns(const struct shirudo_map_view *v, __u64 *out)
{
char path[PATH_MAX];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
@@ -938,8 +938,8 @@ static int map_start_ns(const struct shirudo_map_view *v, uint64_t *out)
return -ENAMETOOLONG;
if (stat(path, &st) != 0)
return -errno;
- *out = (uint64_t)st.st_ctim.tv_sec * 1000000000ULL +
- (uint64_t)st.st_ctim.tv_nsec;
+ *out = (__u64)st.st_ctim.tv_sec * 1000000000ULL +
+ (__u64)st.st_ctim.tv_nsec;
return 0;
}
@@ -969,9 +969,9 @@ static __attribute__((unused)) int build_metric_payload(const struct otel_state
struct shirudo_json_emit e = {};
struct metric_type mt;
char name[256], now[32], start[32];
- uint64_t now_ns, start_ns = 0;
+ __u64 now_ns, start_ns = 0;
char *buf;
- uint32_t i;
+ __u32 i;
int rc;
rc = validate_metric_view(st, v, &mt);
@@ -1031,14 +1031,14 @@ static __attribute__((unused)) int build_metric_payload(const struct otel_state
const struct shirudo_map_entry *en = &v->entries[i];
char value[32];
char *key;
- int64_t point;
+ __s64 point;
rc = entry_metric_value(v, en, &mt, &point);
if (rc) {
xfree(buf, SHIRUDO_JSON_MAX_BYTES);
return rc;
}
- snprintf(value, sizeof(value), "%" PRId64, point);
+ snprintf(value, sizeof(value), "%lld", point);
key = shirudo_upatch_map_format_cell(v, v->key_type_id,
en->key, v->key_size, v->btf_key_type);
shirudo_json_obj_open(&e);
@@ -1093,7 +1093,7 @@ static int build_metric_decoded_payload(const char *path,
return rc;
decoded_buf = decoded;
decoded_alloc_len = decoded_len;
- while (decoded_len && isspace((unsigned char)decoded[0])) {
+ while (decoded_len && isspace((__u8)decoded[0])) {
decoded++;
decoded_len--;
}
@@ -1303,7 +1303,7 @@ int shirudo_upatch_maps_otel_build_metric_file(const char *path,
static int parse_timeout(const char *value, long *out)
{
char *end;
- unsigned long n;
+ __u64 n;
if (!value) {
*out = OTEL_DEFAULT_TIMEOUT_MS;
@@ -1388,12 +1388,12 @@ static int add_configured_headers(struct otel_state *st, const char *input)
*eq = '\0';
name = pair;
value = eq + 1;
- while (isspace((unsigned char)*name))
+ while (isspace((__u8)*name))
name++;
- while (isspace((unsigned char)*value))
+ while (isspace((__u8)*value))
value++;
for (char *p = name + strlen(name);
- p > name && isspace((unsigned char)p[-1]); p--)
+ p > name && isspace((__u8)p[-1]); p--)
p[-1] = '\0';
tenant = strcasecmp(name, "x-tenant-id") == 0;
if (!name[0] || reserved_header(name) ||
@@ -1503,9 +1503,9 @@ static size_t response_header(char *ptr, size_t size, size_t nmemb, void *ctx)
strncasecmp(ptr, prefix, sizeof(prefix) - 1) == 0) {
char *p = ptr + sizeof(prefix) - 1;
char *end;
- unsigned long value;
+ __u64 value;
- while (p < ptr + n && isspace((unsigned char)*p))
+ while (p < ptr + n && isspace((__u8)*p))
p++;
errno = 0;
value = strtoul(p, &end, 10);
@@ -1516,11 +1516,11 @@ static size_t response_header(char *ptr, size_t size, size_t nmemb, void *ctx)
}
static int gzip_data(const char *in, size_t in_len,
- unsigned char **out, size_t *out_len)
+ __u8 **out, size_t *out_len)
{
z_stream z = {};
size_t cap = compressBound(in_len) + 32;
- unsigned char *buf;
+ __u8 *buf;
int zr;
buf = xmalloc(cap);
@@ -1532,7 +1532,7 @@ static int gzip_data(const char *in, size_t in_len,
xfree(buf, cap);
return -EIO;
}
- z.next_in = (unsigned char *)in;
+ z.next_in = (__u8 *)in;
z.avail_in = (uInt)in_len;
z.next_out = buf;
z.avail_out = (uInt)cap;
@@ -1591,7 +1591,7 @@ static int check_partial_success(enum shirudo_otel_signal signal,
{ "partialSuccess", SHIRUDO_JSON_T_OBJECT, NULL, 0, 0,
partial, ARRAY_SIZE(partial) },
};
- int64_t rejected = 0;
+ __s64 rejected = 0;
int rc;
if (len == 0)
@@ -1618,7 +1618,7 @@ 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 %" PRId64
+ 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);
@@ -1627,26 +1627,26 @@ static int check_partial_success(enum shirudo_otel_signal signal,
return 0;
}
-static unsigned int random_below(unsigned int limit)
+static __u32 random_below(__u32 limit)
{
- uint32_t value;
+ __u32 value;
if (limit == 0)
return 0;
if (getrandom(&value, sizeof(value), 0) != sizeof(value))
- value = (uint32_t)time(NULL) ^ (uint32_t)getpid();
+ value = (__u32)time(NULL) ^ (__u32)getpid();
return value % limit;
}
-static void retry_wait(unsigned int retry, unsigned long retry_after)
+static void retry_wait(__u32 retry, __u64 retry_after)
{
struct timespec ts;
- unsigned int cap_ms = 250U << (retry > 4 ? 4 : retry);
- unsigned int delay_ms;
+ __u32 cap_ms = 250U << (retry > 4 ? 4 : retry);
+ __u32 delay_ms;
if (cap_ms > 5000)
cap_ms = 5000;
- delay_ms = retry_after ? (unsigned int)retry_after * 1000U :
+ delay_ms = retry_after ? (__u32)retry_after * 1000U :
random_below(cap_ms + 1);
ts.tv_sec = delay_ms / 1000;
ts.tv_nsec = (long)(delay_ms % 1000) * 1000000L;
@@ -1656,10 +1656,10 @@ static void retry_wait(unsigned int retry, unsigned long retry_after)
static int otel_post(struct otel_state *st, const char *json, size_t json_len)
{
- unsigned char *compressed = NULL;
+ __u8 *compressed = NULL;
const void *body = json;
size_t body_len = json_len, compressed_cap = 0;
- unsigned int attempt, max_attempts = st->opts.retries + 1;
+ __u32 attempt, max_attempts = st->opts.retries + 1;
int rc = -EIO;
if (!st->s3_reference && st->opts.otlp_max_bytes &&
@@ -1871,7 +1871,7 @@ static int build_generic_log_payload(const char *scope_name,
int severity_number,
const char *severity_text,
const char *status, const char *version_id,
- int64_t tamper_attempts,
+ __s64 tamper_attempts,
const char *body_json, size_t body_len,
char **out, size_t *out_len,
size_t *alloc_len)
@@ -1953,7 +1953,7 @@ int shirudo_otel_send_log_json(const struct shirudo_otel_opts *opts,
const char *severity_text,
const char *status,
const char *version_id,
- int64_t tamper_attempts,
+ __s64 tamper_attempts,
const char *body_json, size_t body_len)
{
char *payload = NULL;
@@ -1984,8 +1984,8 @@ struct snapshot_writer {
z_stream z;
gz_header header;
struct shirudo_sha256_ctx md;
- uint64_t json_bytes;
- uint64_t object_bytes;
+ __u64 json_bytes;
+ __u64 object_bytes;
bool deflate_ready;
};
@@ -2030,7 +2030,7 @@ static int snapshot_temp_file(FILE **out)
}
static int snapshot_write_compressed(struct snapshot_writer *w,
- const unsigned char *data, size_t len)
+ const __u8 *data, size_t len)
{
if (UINT64_MAX - w->object_bytes < len)
return -EOVERFLOW;
@@ -2074,7 +2074,7 @@ static void snapshot_writer_abort(struct snapshot_writer *w)
static int snapshot_json_sink(void *ctx, const char *data, size_t len)
{
struct snapshot_writer *w = ctx;
- unsigned char out[S3_GZIP_BUFFER_SIZE];
+ __u8 out[S3_GZIP_BUFFER_SIZE];
if (UINT64_MAX - w->json_bytes < len)
return -EOVERFLOW;
@@ -2082,7 +2082,7 @@ static int snapshot_json_sink(void *ctx, const char *data, size_t len)
while (len) {
uInt chunk = len > UINT_MAX ? UINT_MAX : (uInt)len;
- w->z.next_in = (unsigned char *)data;
+ w->z.next_in = (__u8 *)data;
w->z.avail_in = chunk;
while (w->z.avail_in) {
size_t produced;
@@ -2107,7 +2107,7 @@ static int snapshot_json_sink(void *ctx, const char *data, size_t len)
static int snapshot_writer_finish(struct snapshot_writer *w,
struct s3_snapshot *snapshot)
{
- unsigned char out[S3_GZIP_BUFFER_SIZE];
+ __u8 out[S3_GZIP_BUFFER_SIZE];
int zr, rc;
do {
@@ -2195,11 +2195,11 @@ err_writer:
static bool valid_bucket(const char *bucket, size_t len)
{
if (len < 3 || len > S3_BUCKET_MAX ||
- !isalnum((unsigned char)bucket[0]) ||
- !isalnum((unsigned char)bucket[len - 1]))
+ !isalnum((__u8)bucket[0]) ||
+ !isalnum((__u8)bucket[len - 1]))
return false;
for (size_t i = 0; i < len; i++) {
- unsigned char c = bucket[i];
+ __u8 c = bucket[i];
if (!(c >= 'a' && c <= 'z') && !isdigit(c) && c != '-' &&
c != '.')
@@ -2212,7 +2212,7 @@ static bool valid_region(const char *region)
{
if (!region || !region[0] || strlen(region) >= S3_REGION_MAX)
return false;
- for (const unsigned char *p = (const unsigned char *)region; *p; p++) {
+ for (const __u8 *p = (const __u8 *)region; *p; p++) {
if (!isalnum(*p) && *p != '-')
return false;
}
@@ -2243,7 +2243,7 @@ static int parse_s3_spec(struct otel_s3_state *st, const char *spec)
if (prefix_len > S3_PREFIX_MAX)
return -ENAMETOOLONG;
for (size_t i = 0; i < prefix_len; i++) {
- if ((unsigned char)prefix[i] < 0x20 || prefix[i] == '?' ||
+ if ((__u8)prefix[i] < 0x20 || prefix[i] == '?' ||
prefix[i] == '#')
return -EINVAL;
}
@@ -2356,7 +2356,7 @@ out:
static void safe_key_segment(char *segment)
{
- for (unsigned char *p = (unsigned char *)segment; *p; p++) {
+ for (__u8 *p = (__u8 *)segment; *p; p++) {
if (!isalnum(*p) && *p != '-' && *p != '_' && *p != '.')
*p = '_';
}
@@ -2367,7 +2367,7 @@ static int escape_s3_path(const char *path, char *out, size_t out_sz)
static const char hex[] = "0123456789ABCDEF";
size_t n = 0;
- for (const unsigned char *p = (const unsigned char *)path; *p; p++) {
+ for (const __u8 *p = (const __u8 *)path; *p; p++) {
bool plain = isalnum(*p) || *p == '-' || *p == '_' || *p == '.' ||
*p == '~' || *p == '/';
@@ -2498,7 +2498,7 @@ static int s3_put(struct otel_s3_state *st,
{
struct curl_slist *headers = NULL;
char sigv4[128];
- unsigned int max_attempts = st->opts.retries + 1;
+ __u32 max_attempts = st->opts.retries + 1;
int rc;
*already_present = false;
@@ -2512,7 +2512,7 @@ static int s3_put(struct otel_s3_state *st,
rc = -ENAMETOOLONG;
goto out;
}
- for (unsigned int attempt = 0; attempt < max_attempts; attempt++) {
+ for (__u32 attempt = 0; attempt < max_attempts; attempt++) {
struct otel_response response = {};
CURL *curl = curl_easy_init();
CURLcode cc;
@@ -2643,9 +2643,9 @@ static int build_s3_reference(const struct shirudo_map_view *v,
"application/json");
otlp_kv_string(&e, "shirudo.map.snapshot.content_encoding", "gzip");
otlp_kv_int(&e, "shirudo.map.snapshot.size_bytes",
- (int64_t)snapshot->object_bytes);
+ (__s64)snapshot->object_bytes);
otlp_kv_int(&e, "shirudo.map.snapshot.uncompressed_size_bytes",
- (int64_t)snapshot->json_bytes);
+ (__s64)snapshot->json_bytes);
otlp_kv_string(&e, "shirudo.map.snapshot.sha256",
snapshot->sha256_hex);
shirudo_json_arr_close(&e);
@@ -2747,7 +2747,7 @@ static int otel_s3_emit(void *state, const struct shirudo_map_view *v)
rc = s3_put(st, v, &snapshot, url, &already_present);
if (rc)
goto out;
- fprintf(stderr, "otel.s3: %s %s (%" PRIu64 " bytes)\n",
+ fprintf(stderr, "otel.s3: %s %s (%llu bytes)\n",
already_present ? "found" : "uploaded", uri,
snapshot.object_bytes);
rc = build_s3_reference(v, &snapshot, uri, &payload, &payload_len);
diff --git a/util/upatch_maps_otel.h b/util/upatch_maps_otel.h
index adbf0bbb..9c2810f9 100644
--- a/util/upatch_maps_otel.h
+++ b/util/upatch_maps_otel.h
@@ -7,6 +7,8 @@
#include <stdbool.h>
#include <stddef.h>
+#include <shirudo/misc.h>
+
#include "upatch_maps.h"
enum shirudo_otel_signal {
@@ -32,9 +34,9 @@ struct shirudo_otel_opts {
const char *tenant_id;
const char *s3_uri;
bool allow_insecure;
- unsigned int retries;
- unsigned int log_record_max_bytes;
- unsigned int otlp_max_bytes;
+ __u32 retries;
+ __u32 log_record_max_bytes;
+ __u32 otlp_max_bytes;
enum shirudo_otel_metric_kind metric_kind;
bool metric_kind_set;
const char *metric_name;
@@ -77,7 +79,7 @@ int shirudo_otel_send_log_json(const struct shirudo_otel_opts *opts,
const char *severity_text,
const char *status,
const char *version_id,
- int64_t tamper_attempts,
+ __s64 tamper_attempts,
const char *body_json, size_t body_len);
#endif /* SHIRUDO_UTIL_UPATCH_MAPS_OTEL_H */
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 6bb20051..e47bc34e 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -20,6 +20,7 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
#include <shirudo/upatch.h>
#include "malloc.h"
@@ -63,7 +64,7 @@ int shirudo_upatch_open_first_link(const char *name)
return errno == ENOENT ? -first_errno : -errno;
}
-static const char *loc_prefix(uint32_t prog_type, uint32_t attach_type)
+static const char *loc_prefix(__u32 prog_type, __u32 attach_type)
{
if (prog_type == BPF_PROG_TYPE_LSM)
return "lsm";
@@ -94,10 +95,10 @@ int shirudo_upatch_resolve_location(struct btf *vbtf, const char *name,
struct bpf_link_info linfo = {};
struct bpf_prog_info info = {};
const char *attach_name = NULL;
- uint32_t attach_type = 0;
int link_fd, prog_fd, rc;
+ __u32 attach_type = 0;
const char *prefix;
- uint32_t len;
+ __u32 len;
out[0] = '\0';
link_fd = shirudo_upatch_open_first_link(name);
@@ -111,8 +112,8 @@ int shirudo_upatch_resolve_location(struct btf *vbtf, const char *name,
}
if (linfo.type == BPF_LINK_TYPE_UPROBE_MULTI) {
struct bpf_link_info detail = {};
- uint32_t count = linfo.uprobe_multi.count;
- uint32_t path_size = linfo.uprobe_multi.path_size;
+ __u32 count = linfo.uprobe_multi.count;
+ __u32 path_size = linfo.uprobe_multi.path_size;
char *path;
if (!path_size || path_size > PATH_MAX) {
@@ -124,7 +125,7 @@ int shirudo_upatch_resolve_location(struct btf *vbtf, const char *name,
close(link_fd);
return -ENOMEM;
}
- detail.uprobe_multi.path = (uint64_t)(uintptr_t)path;
+ detail.uprobe_multi.path = (__u64)(uintptr_t)path;
detail.uprobe_multi.path_size = path_size;
len = sizeof(detail);
rc = bpf_link_get_info_by_fd(link_fd, &detail, &len);
@@ -210,7 +211,7 @@ void shirudo_upatch_payload_sha256(const char *name, char *out, size_t cap)
{
char dir[PATH_MAX];
char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];
- uint8_t raw[32];
+ __u8 raw[32];
ssize_t n;
out[0] = '\0';
diff --git a/util/upatch_table.c b/util/upatch_table.c
index 46de039a..aff7d26c 100644
--- a/util/upatch_table.c
+++ b/util/upatch_table.c
@@ -9,11 +9,13 @@
#include <string.h>
#include <time.h>
+#include <shirudo/misc.h>
+
#include "json.h"
#include "timefmt.h"
#include "upatch_table.h"
-int64_t shirudo_upatch_age_sec(const char *ts)
+__s64 shirudo_upatch_age_sec(const char *ts)
{
struct tm tm = {};
@@ -21,7 +23,7 @@ int64_t shirudo_upatch_age_sec(const char *ts)
return -1;
if (!strptime(ts, "%Y-%m-%dT%H:%M:%SZ", &tm))
return -1;
- return (int64_t)(time(NULL) - timegm(&tm));
+ return (__s64)(time(NULL) - timegm(&tm));
}
void shirudo_upatch_table_print(const struct shirudo_upatch_table_row *rows,
@@ -62,7 +64,7 @@ void shirudo_upatch_table_emit_json(struct shirudo_json_emit *e,
shirudo_json_kv_string(e, "location", r->location);
shirudo_json_kv_string(e, "status",
r->status[0] ? r->status : "success");
- shirudo_json_kv_int(e, "n_maps", (int64_t)r->n_maps);
+ shirudo_json_kv_int(e, "n_maps", (__s64)r->n_maps);
shirudo_json_kv_string(e, "loaded_at", r->loaded_at);
if (r->age_sec >= 0)
shirudo_json_kv_int(e, "age_sec", r->age_sec);
diff --git a/util/upatch_table.h b/util/upatch_table.h
index 7e06fe71..556a0853 100644
--- a/util/upatch_table.h
+++ b/util/upatch_table.h
@@ -9,7 +9,8 @@
#define SHIRUDO_UTIL_UPATCH_TABLE_H
#include <stddef.h>
-#include <stdint.h>
+
+#include <shirudo/misc.h>
#include "upatch_resolve.h"
@@ -20,8 +21,8 @@ struct shirudo_upatch_table_row {
char location[SHIRUDO_UPATCH_LOCATION_MAX]; /* SEC()-style */
char loaded_at[32]; /* ISO-8601 UTC, "" if unknown */
char status[64]; /* success or a tree skip reason */
- uint32_t n_maps;
- int64_t age_sec; /* derived from loaded_at; -1 if unknown */
+ __u32 n_maps;
+ __s64 age_sec; /* derived from loaded_at; -1 if unknown */
char payload_sha256[65]; /* "" if unavailable; JSON only */
};
@@ -32,6 +33,6 @@ void shirudo_upatch_table_emit_json(struct shirudo_json_emit *e,
const struct shirudo_upatch_table_row *rows,
size_t n);
-int64_t shirudo_upatch_age_sec(const char *ts);
+__s64 shirudo_upatch_age_sec(const char *ts);
#endif /* SHIRUDO_UTIL_UPATCH_TABLE_H */