Skip to content

eBPF-based security platform

builtin: move the helpers every builtin re-wrote into util

Daniel Borkmann4f6a2e79d206
diff --git a/Makefile.shirudo b/Makefile.shirudo
index aa70f902..5f16c915 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -547,7 +547,7 @@ testing/helper/test-chain: testing/helper/test-chain.c $(REGISTER_OBJS)
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
testing/helper/test-tpm-wire: testing/helper/test-tpm-wire.c util/tpm_wire.o util/hex.o \
- util/crypto_openssl.o util/malloc.o
+ util/crypto_openssl.o util/malloc.o util/log.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ -lcrypto -lssl
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 59fd9ddd..a67fc999 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -275,11 +275,19 @@ static int save_source(const struct bpm_source *s)
static int with_bpm_lock(void)
{
- int fd;
- { int rc = bpm_mkdir_p(SHIRUDO_LOCKS_DIR, 0700); if (rc) return rc; }
+ int fd, rc;
+
+ rc = shirudo_seal_mkdir_p(SHIRUDO_LOCKS_DIR, 0700);
+ if (rc)
+ return rc;
fd = shirudo_seal_open(SHIRUDO_BPM_LOCK_PATH, O_RDWR | O_CREAT, 0600);
- if (fd < 0) return fd;
- if (flock(fd, LOCK_EX)) { int rc = -errno; close(fd); return rc; }
+ if (fd < 0)
+ return fd;
+ if (flock(fd, LOCK_EX)) {
+ rc = -errno;
+ close(fd);
+ return rc;
+ }
return fd;
}
@@ -369,7 +377,11 @@ static int extract_archive(const char *archive, const char *dest)
int rc = 0;
if (!gz) return -EINVAL;
- if ((rc = bpm_mkdir_p(dest, 0700))) { gzclose(gz); return rc; }
+ rc = shirudo_seal_mkdir_p(dest, 0700);
+ if (rc) {
+ gzclose(gz);
+ return rc;
+ }
for (;;) {
int n = gzread(gz, h, sizeof(h));
uint64_t size, remain, pad;
@@ -392,12 +404,20 @@ static int extract_archive(const char *archive, const char *dest)
if ((!*rel && type != '5') || (*rel && !bpm_archive_rel_ok(rel))) { rc = -EINVAL; break; }
if (snprintf(path, sizeof(path), "%s/%s", dest, rel) >= (int)sizeof(path)) { rc = -ENAMETOOLONG; break; }
if (type == '5') {
- if ((rc = bpm_mkdir_p(path, 0700))) break;
+ rc = shirudo_seal_mkdir_p(path, 0700);
+ if (rc)
+ break;
} else if (type == '0') {
char parent[PATH_MAX];
FILE *fp;
snprintf(parent, sizeof(parent), "%s", path);
- slash = strrchr(parent, '/'); if (slash) { *slash = 0; if ((rc = bpm_mkdir_p(parent, 0700))) break; }
+ slash = strrchr(parent, '/');
+ if (slash) {
+ *slash = 0;
+ rc = shirudo_seal_mkdir_p(parent, 0700);
+ if (rc)
+ break;
+ }
fp = fopen(path, "wb"); if (!fp) { rc = -errno; break; }
remain = size;
while (remain) {
@@ -461,7 +481,7 @@ static int install_key_file(const char *src)
rc = key_file_fingerprint(src, hex);
if (rc)
return rc;
- rc = bpm_mkdir_p(SHIRUDO_KEYS_DIR, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_KEYS_DIR, 0700);
if (rc)
return rc;
active = lstat(SHIRUDO_CERT_PATH, &st) == 0;
@@ -683,7 +703,7 @@ static int state_write(const struct bpm_state_entry *st, int n)
shirudo_json_arr_close(&e);
shirudo_json_obj_close(&e);
if (e.err) return -E2BIG;
- rc = bpm_mkdir_p(SHIRUDO_BPM_VAR_DIR, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_BPM_VAR_DIR, 0700);
if (!rc)
rc = shirudo_seal_write_file(SHIRUDO_BPM_STATE_PATH, buf, e.len,
0600);
@@ -770,7 +790,9 @@ static int repo_update_one(const char *name)
if (bpm_index_parse(index.data, index.len, checked, &checked_n)) { rc = -EINVAL; goto out; }
}
if (snprintf(dir, sizeof(dir), "%s/%s", SHIRUDO_BPM_REPOS_DIR, name) >= (int)sizeof(dir)) { rc = -ENAMETOOLONG; goto out; }
- if ((rc = bpm_mkdir_p(dir, 0700))) goto out;
+ rc = shirudo_seal_mkdir_p(dir, 0700);
+ if (rc)
+ goto out;
if ((rc = cache_path(name, cache, sizeof(cache)))) goto out;
rc = shirudo_seal_write_file(cache, index.data, index.len, 0600);
out:
@@ -827,9 +849,13 @@ static int package_install(const struct bpm_source *s, const struct bpm_index_en
if (entry->package_size > 0 && entry->package_size != layers[i].size) { rc = -EKEYREJECTED; goto out; }
if ((rc = bpm_oci_url(&c, entry->repository[0] ? entry->repository : entry->name, "blobs", layers[i].digest, url, sizeof(url)))) goto out;
if (snprintf(dir, sizeof(dir), "%s/%s", SHIRUDO_BPM_PACKAGES_DIR, entry->name) >= (int)sizeof(dir)) { rc = -ENAMETOOLONG; goto out; }
- if ((rc = bpm_mkdir_p(dir, 0700))) goto out;
+ rc = shirudo_seal_mkdir_p(dir, 0700);
+ if (rc)
+ goto out;
if (snprintf(package_dir, sizeof(package_dir), "%s/%s", dir, entry->version) >= (int)sizeof(package_dir)) { rc = -ENAMETOOLONG; goto out; }
- if ((rc = bpm_mkdir_p(SHIRUDO_BPM_STAGING_DIR, 0700))) goto out;
+ rc = shirudo_seal_mkdir_p(SHIRUDO_BPM_STAGING_DIR, 0700);
+ if (rc)
+ goto out;
if (snprintf(archive, sizeof(archive), "%s/%s-%s.bee", SHIRUDO_BPM_STAGING_DIR, entry->name, entry->version) >= (int)sizeof(archive)) { rc = -ENAMETOOLONG; goto out; }
rc = bpm_oci_get_file(&c, url, BPM_PACKAGE_MEDIA, archive); if (rc) goto out;
if (sha256_file_hex(archive, digest, sizeof(digest)) || !bpm_digest_matches(layers[i].digest, digest)) { rc = -EKEYREJECTED; goto out; }
@@ -990,7 +1016,9 @@ static int cmd_bpm_config(int argc, const char **argv)
if (!key || shirudo_cert_read_file(key, &cert)) { shirudo_cert_free(&cert); pr_err("bpm: register add requires a valid oci:// endpoint and --key-file certificate"); return 2; }
if (!bpm_name_ok(s.name) || bpm_oci_parse_endpoint(s.endpoint, &(struct bpm_oci_client){}) ) { shirudo_cert_free(&cert); pr_err("bpm: register add requires a valid oci:// endpoint and --key-file certificate"); return 2; }
shirudo_cert_free(&cert);
- if (bpm_mkdir_p(SHIRUDO_BPM_REGISTRIES_DIR, 0700) || bpm_mkdir_p(SHIRUDO_BPM_KEYS_DIR, 0700)) return 1;
+ if (shirudo_seal_mkdir_p(SHIRUDO_BPM_REGISTRIES_DIR, 0700) ||
+ shirudo_seal_mkdir_p(SHIRUDO_BPM_KEYS_DIR, 0700))
+ return 1;
if (snprintf(managed, sizeof(managed), "%s/%s.der", SHIRUDO_BPM_KEYS_DIR, s.name) >= (int)sizeof(managed)) return 1;
if (copy_file(key, managed, 0644)) return 1;
if (strlen(managed) >= sizeof(s.key_file))
@@ -1176,10 +1204,9 @@ static int tree_pull_artifact(const struct shirudo_tree_operation *op,
rc = -EKEYREJECTED;
if (rc)
goto out;
- if (bpm_mkdir_p(SHIRUDO_BPM_TREE_ARTIFACTS_DIR, 0700)) {
- rc = -errno;
+ rc = shirudo_seal_mkdir_p(SHIRUDO_BPM_TREE_ARTIFACTS_DIR, 0700);
+ if (rc)
goto out;
- }
rc = shirudo_seal_write_file(cache, blob.data, blob.len, 0600);
if (rc)
goto out;
@@ -1238,7 +1265,7 @@ static int tree_write_pending(const struct shirudo_tree_operation *op)
shirudo_json_obj_close(&e);
if (e.err)
return e.err;
- if (bpm_mkdir_p(SHIRUDO_BPM_TREE_VAR_DIR, 0700))
+ if (shirudo_seal_mkdir_p(SHIRUDO_BPM_TREE_VAR_DIR, 0700))
return -errno;
return shirudo_seal_write_file(SHIRUDO_BPM_TREE_PENDING_PATH, buf,
e.len, 0600);
@@ -1251,7 +1278,7 @@ static int tree_status_write(const char *id, const char *status)
if (!id || !shirudo_upatch_name_valid(id) || !status || !status[0])
return -EINVAL;
- rc = bpm_mkdir_p(SHIRUDO_BPM_TREE_STATUS_DIR, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_BPM_TREE_STATUS_DIR, 0700);
if (rc)
return rc;
if (snprintf(path, sizeof(path), "%s/%s",
diff --git a/builtin-genkey.c b/builtin-genkey.c
index 50a4a847..a13e2c22 100644
--- a/builtin-genkey.c
+++ b/builtin-genkey.c
@@ -27,7 +27,9 @@
#include "util/help.h"
#include "util/log.h"
#include "util/macros.h"
+#include "util/crypto.h"
#include "util/paths.h"
+#include "util/seal.h"
#define VALIDITY_DAYS (10L * 365L)
#define CERT_CN "shirudo"
@@ -41,84 +43,18 @@
*/
#define GENKEY_ALG "ML-DSA-87"
-static void openssl_complain(const char *where)
-{
- unsigned long e = ERR_get_error();
- char buf[256];
-
- ERR_error_string_n(e, buf, sizeof(buf));
- pr_err("openssl: %s: %s", where, buf);
-}
-
-static int mkdir_p_mode(const char *path, mode_t mode)
+static int ensure_keys_dir(const char *keys_dir)
{
- struct stat st;
- char tmp[PATH_MAX];
- size_t len;
- int n;
-
- if (!path || !*path) {
- pr_err("mkdir: empty path");
- return -1;
- }
-
- n = snprintf(tmp, sizeof(tmp), "%s", path);
- if (n < 0 || (size_t)n >= sizeof(tmp)) {
- pr_err("mkdir %s: path too long", path);
- return -1;
- }
+ int rc;
- len = strlen(tmp);
- while (len > 1 && tmp[len - 1] == '/')
- tmp[--len] = '\0';
- if (strcmp(tmp, "/") == 0) {
+ if (!strcmp(keys_dir, "/")) {
pr_err("keys dir cannot be /");
- return -1;
+ return -EINVAL;
}
-
- for (char *p = tmp + 1; *p; p++) {
- if (*p != '/')
- continue;
- *p = '\0';
- if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
- pr_err("mkdir %s: %s", tmp, strerror(errno));
- return -1;
- }
- *p = '/';
- while (p[1] == '/')
- p++;
- }
-
- if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
- pr_err("mkdir %s: %s", tmp, strerror(errno));
- return -1;
- }
- if (stat(tmp, &st) != 0) {
- pr_err("stat %s: %s", tmp, strerror(errno));
- return -1;
- }
- if (!S_ISDIR(st.st_mode)) {
- pr_err("keys dir %s is not a directory", tmp);
- return -1;
- }
- /* Defeat umask on the final key directory. */
- if (chmod(tmp, mode) != 0) {
- pr_err("chmod %s: %s", tmp, strerror(errno));
- return -1;
- }
- return 0;
-}
-
-static int ensure_keys_dir(const char *keys_dir)
-{
- return mkdir_p_mode(keys_dir, 0700);
-}
-
-static int path_exists(const char *p)
-{
- struct stat st;
-
- return stat(p, &st) == 0;
+ rc = shirudo_seal_mkdir_p(keys_dir, 0700);
+ if (rc)
+ pr_err("mkdir %s: %s", keys_dir, strerror(-rc));
+ return rc;
}
static EVP_PKEY *gen_keypair(void)
@@ -126,7 +62,7 @@ static EVP_PKEY *gen_keypair(void)
EVP_PKEY *k = EVP_PKEY_Q_keygen(NULL, NULL, GENKEY_ALG);
if (!k) {
- openssl_complain("EVP_PKEY_Q_keygen");
+ shirudo_openssl_complain("EVP_PKEY_Q_keygen");
if (OPENSSL_version_major() < 3 ||
(OPENSSL_version_major() == 3 &&
OPENSSL_version_minor() < 5))
@@ -144,11 +80,11 @@ static int set_subject_and_issuer(X509 *c)
if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8,
(const unsigned char *)CERT_CN,
-1, -1, 0) != 1) {
- openssl_complain("X509_NAME_add_entry_by_txt");
+ shirudo_openssl_complain("X509_NAME_add_entry_by_txt");
return -1;
}
if (X509_set_issuer_name(c, name) != 1) {
- openssl_complain("X509_set_issuer_name");
+ shirudo_openssl_complain("X509_set_issuer_name");
return -1;
}
return 0;
@@ -165,13 +101,13 @@ static int add_ext(X509 *c, int nid, const char *value)
ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
if (!ex) {
- openssl_complain("X509V3_EXT_conf_nid");
+ shirudo_openssl_complain("X509V3_EXT_conf_nid");
return -1;
}
rc = X509_add_ext(c, ex, -1);
X509_EXTENSION_free(ex);
if (rc != 1) {
- openssl_complain("X509_add_ext");
+ shirudo_openssl_complain("X509_add_ext");
return -1;
}
return 0;
@@ -180,7 +116,7 @@ static int add_ext(X509 *c, int nid, const char *value)
static int build_serial(X509 *c)
{
if (ASN1_INTEGER_set(X509_get_serialNumber(c), 1) != 1) {
- openssl_complain("ASN1_INTEGER_set");
+ shirudo_openssl_complain("ASN1_INTEGER_set");
return -1;
}
return 0;
@@ -189,12 +125,12 @@ static int build_serial(X509 *c)
static int build_validity(X509 *c)
{
if (!X509_gmtime_adj(X509_getm_notBefore(c), 0)) {
- openssl_complain("X509_gmtime_adj notBefore");
+ shirudo_openssl_complain("X509_gmtime_adj notBefore");
return -1;
}
if (!X509_gmtime_adj(X509_getm_notAfter(c),
VALIDITY_DAYS * 24L * 60L * 60L)) {
- openssl_complain("X509_gmtime_adj notAfter");
+ shirudo_openssl_complain("X509_gmtime_adj notAfter");
return -1;
}
return 0;
@@ -204,7 +140,7 @@ static int build_validity(X509 *c)
static int build_cert(X509 *c, EVP_PKEY *pkey)
{
if (X509_set_version(c, 2) != 1) { /* X.509 v3 */
- openssl_complain("X509_set_version");
+ shirudo_openssl_complain("X509_set_version");
return -1;
}
if (build_serial(c) != 0)
@@ -212,7 +148,7 @@ static int build_cert(X509 *c, EVP_PKEY *pkey)
if (build_validity(c) != 0)
return -1;
if (X509_set_pubkey(c, pkey) != 1) {
- openssl_complain("X509_set_pubkey");
+ shirudo_openssl_complain("X509_set_pubkey");
return -1;
}
if (set_subject_and_issuer(c) != 0)
@@ -228,7 +164,7 @@ static int build_cert(X509 *c, EVP_PKEY *pkey)
return -1;
if (X509_sign(c, pkey, NULL) == 0) {
- openssl_complain("X509_sign");
+ shirudo_openssl_complain("X509_sign");
return -1;
}
return 0;
@@ -255,7 +191,7 @@ static int write_private_pem(EVP_PKEY *pkey, const char *path)
}
fd = -1; /* now owned by fp */
if (PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL) != 1) {
- openssl_complain("PEM_write_PrivateKey");
+ shirudo_openssl_complain("PEM_write_PrivateKey");
goto out;
}
if (fflush(fp) != 0 || fsync(fileno(fp)) != 0) {
@@ -289,11 +225,11 @@ static int write_cert_der(X509 *c, const char *path)
}
bio = BIO_new_fd(fd, BIO_NOCLOSE);
if (!bio) {
- openssl_complain("BIO_new_fd");
+ shirudo_openssl_complain("BIO_new_fd");
goto out;
}
if (i2d_X509_bio(bio, c) != 1) {
- openssl_complain("i2d_X509_bio");
+ shirudo_openssl_complain("i2d_X509_bio");
goto out;
}
if (fsync(fd) != 0) {
@@ -393,7 +329,8 @@ int cmd_genkey(int argc, const char **argv)
rc = 1;
if (!force) {
- if (path_exists(pkey_path) || path_exists(cert_path)) {
+ if (shirudo_path_exists(pkey_path) ||
+ shirudo_path_exists(cert_path)) {
pr_err("genkey: %s or %s already exists; pass --force",
pkey_path, cert_path);
return 1;
@@ -409,7 +346,7 @@ int cmd_genkey(int argc, const char **argv)
goto out;
cert = X509_new();
if (!cert) {
- openssl_complain("X509_new");
+ shirudo_openssl_complain("X509_new");
goto out;
}
if (build_cert(cert, pkey) != 0)
diff --git a/builtin-init.c b/builtin-init.c
index 0529dfd5..ad5acb0c 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -450,28 +450,16 @@ static int is_bpffs_mounted(const char *path)
return found;
}
-static int mkdir_p_mode(const char *path, mode_t mode)
-{
- if (mkdir(path, mode) == 0) {
- if (chmod(path, mode) != 0)
- return -errno;
- return 0;
- }
- if (errno == EEXIST)
- return 0;
- return -errno;
-}
-
static int init_bpffs(void)
{
int rc;
- rc = mkdir_p_mode(SHIRUDO_VAR_LIB, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_VAR_LIB, 0700);
if (rc) {
pr_err("mkdir %s: %s", SHIRUDO_VAR_LIB, strerror(-rc));
return rc;
}
- rc = mkdir_p_mode(SHIRUDO_RUN_DIR, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_RUN_DIR, 0700);
if (rc) {
pr_err("mkdir %s: %s", SHIRUDO_RUN_DIR, strerror(-rc));
return rc;
@@ -480,7 +468,7 @@ static int init_bpffs(void)
rc = shirudo_seal_label(SHIRUDO_RUN_DIR);
if (rc)
pr_warn("seal: label %s: %s", SHIRUDO_RUN_DIR, strerror(-rc));
- rc = mkdir_p_mode(SHIRUDO_BPFFS_DIR, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_BPFFS_DIR, 0700);
if (rc) {
pr_err("mkdir %s: %s",
SHIRUDO_BPFFS_DIR, strerror(-rc));
@@ -1086,13 +1074,6 @@ out:
#define HEXLEN(n) ((n) * 2 + 1)
-static int select_upatch(const struct dirent *e)
-{
- size_t n = strlen(e->d_name);
-
- return n > 7 && strcmp(e->d_name + n - 7, ".upatch") == 0;
-}
-
/*
* State an older shirudo may have left behind for features this
* version no longer has: the trusted-root mode record, the Java agent
@@ -1169,7 +1150,8 @@ static void remove_stale_state(void)
if (is_javaagent_env())
unlink_stale(STALE_JAVAAGENT_ENV);
- n = scandir(SHIRUDO_UPATCHES_DIR, &names, select_upatch, alphasort);
+ n = scandir(SHIRUDO_UPATCHES_DIR, &names, shirudo_upatch_select,
+ alphasort);
for (i = 0; i < n; i++) {
char path[PATH_MAX];
@@ -1199,7 +1181,8 @@ static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)
*out_set = NULL;
*out_n = 0;
- n = scandir(SHIRUDO_UPATCHES_DIR, &names, select_upatch, alphasort);
+ n = scandir(SHIRUDO_UPATCHES_DIR, &names, shirudo_upatch_select,
+ alphasort);
if (n < 0) {
if (errno == ENOENT)
return 0;
@@ -1519,25 +1502,6 @@ static void usage(void)
fprintf(stderr, "usage: %s\n", init_help.usage);
}
-static int mkdir_parents(const char *path, mode_t mode)
-{
- char buf[PATH_MAX], *p;
-
- if (snprintf(buf, sizeof(buf), "%s", path) >= (int)sizeof(buf))
- return -ENAMETOOLONG;
- for (p = buf + 1; *p; p++) {
- if (*p != '/')
- continue;
- *p = '\0';
- if (mkdir(buf, mode) != 0 && errno != EEXIST)
- return -errno;
- *p = '/';
- }
- if (mkdir(buf, mode) != 0 && errno != EEXIST)
- return -errno;
- return 0;
-}
-
/*
* The initrd phase reads the root's state tree: the archived upatches
* to restore, the eventlog to continue, the keys to compare. A
@@ -1562,7 +1526,7 @@ static int initrd_enter(void)
"there (x-initrd.mount)", src, shirudo_sysroot());
return -ENOENT;
}
- rc = mkdir_parents(SHIRUDO_VAR_LIB, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_VAR_LIB, 0700);
if (rc) {
pr_err("initrd: mkdir %s: %s", SHIRUDO_VAR_LIB, strerror(-rc));
return rc;
diff --git a/builtin-keys.c b/builtin-keys.c
index 011031af..085e743b 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -42,22 +42,6 @@
#include "util/seal.h"
#include "util/upatch.h"
-static void openssl_complain(const char *where)
-{
- unsigned long e = ERR_get_error();
- char buf[256];
-
- ERR_error_string_n(e, buf, sizeof(buf));
- pr_err("openssl: %s: %s", where, buf);
-}
-
-static int path_exists(const char *p)
-{
- struct stat st;
-
- return stat(p, &st) == 0;
-}
-
static X509 *read_cert_der(const char *path)
{
BIO *bio;
@@ -65,13 +49,13 @@ static X509 *read_cert_der(const char *path)
bio = BIO_new_file(path, "rb");
if (!bio) {
- openssl_complain("BIO_new_file");
+ shirudo_openssl_complain("BIO_new_file");
return NULL;
}
c = d2i_X509_bio(bio, NULL);
BIO_free(bio);
if (!c)
- openssl_complain("d2i_X509_bio");
+ shirudo_openssl_complain("d2i_X509_bio");
return c;
}
@@ -88,7 +72,7 @@ static EVP_PKEY *read_priv_pem(const char *path)
k = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
if (!k)
- openssl_complain("PEM_read_PrivateKey");
+ shirudo_openssl_complain("PEM_read_PrivateKey");
return k;
}
@@ -138,7 +122,7 @@ static int compute_cert_fpr(X509 *c, unsigned char fpr[SHA256_DIGEST_LENGTH])
unsigned int n = SHA256_DIGEST_LENGTH;
if (X509_digest(c, EVP_sha256(), fpr, &n) != 1) {
- openssl_complain("X509_digest");
+ shirudo_openssl_complain("X509_digest");
return -1;
}
return 0;
@@ -155,12 +139,12 @@ static int compute_spki_fpr(EVP_PKEY *pkey,
der_len = i2d_PUBKEY(pkey, &der);
if (der_len <= 0) {
- openssl_complain("i2d_PUBKEY");
+ shirudo_openssl_complain("i2d_PUBKEY");
return -1;
}
if (EVP_Digest(der, (size_t)der_len, fpr, &n,
EVP_sha256(), NULL) != 1) {
- openssl_complain("EVP_Digest");
+ shirudo_openssl_complain("EVP_Digest");
goto out;
}
rc = 0;
@@ -242,7 +226,7 @@ static int dump_signing_key(const char *pkey_path,
EVP_PKEY *priv;
int rc = 0;
- if (!path_exists(pkey_path)) {
+ if (!shirudo_path_exists(pkey_path)) {
printf("Signing key (%s):\n", pkey_path);
printf(" %-22s %s\n", "Present:",
"no (target host -- private key lives on the operator)");
@@ -741,13 +725,6 @@ static int measure_revoke(const struct shirudo_trusted_cert *c,
return 0;
}
-static int select_upatch(const struct dirent *e)
-{
- size_t n = strlen(e->d_name);
-
- return n > 7 && strcmp(e->d_name + n - 7, ".upatch") == 0;
-}
-
/*
* Every archived upatch the revoked certificate signed goes: the kernel
* would refuse to restore it at the next boot, and keeping it loaded
@@ -764,7 +741,8 @@ static int purge_signed_by(const unsigned char *der, size_t der_sz,
*unloaded = 0;
if (shirudo_cert_read_der(der, der_sz, &cert))
return -EINVAL;
- n = scandir(SHIRUDO_UPATCHES_DIR, &names, select_upatch, alphasort);
+ n = scandir(SHIRUDO_UPATCHES_DIR, &names, shirudo_upatch_select,
+ alphasort);
if (n < 0) {
shirudo_cert_free(&cert);
return errno == ENOENT ? 0 : -errno;
@@ -1114,7 +1092,7 @@ static int keys_show(int argc, const char **argv)
* rather than picking one mode: the on-disk cert + signing key,
* then the kernel keyring once `shirudo init` has populated one.
*/
- if (path_exists(cert_path)) {
+ if (shirudo_path_exists(cert_path)) {
if (keys_dump_disk_material(cert_path, pkey_path))
rc = 1;
shown = true;
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 32afb8b6..3f2ac614 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -44,11 +44,6 @@ void bpm_copy_string(char *dst, size_t dst_sz, const char *src)
dst[len] = '\0';
}
-int bpm_mkdir_p(const char *path, mode_t mode)
-{
- return shirudo_seal_mkdir_p(path, mode);
-}
-
static bool bpm_component_ok(const char *s, bool plus)
{
const unsigned char *p = (const unsigned char *)s;
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index eb56d5f6..9b59e5b8 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -102,8 +102,6 @@ int bpm_oci_url(const struct bpm_oci_client *c, const char *repo,
/* 2xx fills out; 404 is -ENOENT, other failures -EACCES. */
int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
const char *accept, struct bpm_buffer *out);
-/* mkdir -p under the state tree: born sealed as the parent is. */
-int bpm_mkdir_p(const char *path, mode_t mode);
int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
const char *accept, const char *path);
int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
diff --git a/util/crypto.h b/util/crypto.h
index 54d11fb6..7a076837 100644
--- a/util/crypto.h
+++ b/util/crypto.h
@@ -128,4 +128,7 @@ int shirudo_cert_pubkey(const struct shirudo_cert *cert,
int shirudo_cert_verify_chain(const struct shirudo_cert *cert,
const char *bundle_path, const char **reason);
+/* Log libcrypto's last error under where. */
+void shirudo_openssl_complain(const char *where);
+
#endif /* SHIRUDO_CRYPTO_H */
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index 17fc5b3c..be83f767 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -25,6 +25,7 @@
#include <openssl/x509_vfy.h>
#include "crypto.h"
+#include "log.h"
#include "malloc.h"
static X509 *read_x509(const char *path)
@@ -578,3 +579,11 @@ out:
X509_STORE_free(store);
return rc;
}
+
+void shirudo_openssl_complain(const char *where)
+{
+ char buf[256];
+
+ ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
+ pr_err("openssl: %s: %s", where, buf);
+}
diff --git a/util/paths.c b/util/paths.c
index 0a32972c..1941ee43 100644
--- a/util/paths.c
+++ b/util/paths.c
@@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
#include "paths.h"
@@ -54,3 +55,10 @@ int shirudo_key_paths(const char *keys_dir,
return shirudo_key_path(cert_path, cert_sz, keys_dir,
SHIRUDO_CERT_FILE);
}
+
+bool shirudo_path_exists(const char *path)
+{
+ struct stat st;
+
+ return stat(path, &st) == 0;
+}
diff --git a/util/paths.h b/util/paths.h
index 8f479317..315646b1 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -8,6 +8,8 @@
#ifndef SHIRUDO_PATHS_H
#define SHIRUDO_PATHS_H
+#include <stdbool.h>
+
#include <errno.h>
#include <stddef.h>
#include <string.h>
@@ -172,4 +174,6 @@ static inline int shirudo_upatch_name_from_bpffs(char *out, size_t out_sz,
return 0;
}
+bool shirudo_path_exists(const char *path);
+
#endif /* SHIRUDO_PATHS_H */
diff --git a/util/upatch.c b/util/upatch.c
index f247c52c..a831d1ec 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -375,3 +375,10 @@ bool shirudo_upatch_name_valid(const char *name)
}
return true;
}
+
+int shirudo_upatch_select(const struct dirent *e)
+{
+ size_t n = strlen(e->d_name);
+
+ return n > 7 && strcmp(e->d_name + n - 7, ".upatch") == 0;
+}
diff --git a/util/upatch.h b/util/upatch.h
index c74de307..1521f37c 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -9,6 +9,7 @@
#define SHIRUDO_UTIL_UPATCH_H
#include <shirudo/upatch.h>
+#include <dirent.h>
#include <stdbool.h>
#include <stddef.h>
@@ -65,4 +66,7 @@ int shirudo_upatch_decoder_view(const struct shirudo_upatch *u, uint32_t idx,
*/
bool shirudo_upatch_name_valid(const char *name);
+/* A scandir() filter for the archived .upatch files. */
+int shirudo_upatch_select(const struct dirent *e);
+
#endif /* SHIRUDO_UTIL_UPATCH_H */