eBPF-based security platform
image: verify and measure config updates before using them
KP Singh7f70e40189e3
diff --git a/builtin-image.c b/builtin-image.cindex cb31c5b3..8813007b 100644--- a/builtin-image.c+++ b/builtin-image.c@@ -16,6 +16,7 @@ static void image_help(void)fprintf(stderr,"usage: shirudo image build [image builder options]\n"" shirudo image verify <prefix> --cert <certificate>\n"+ " shirudo image update <prefix-or-https-prefix>\n"" shirudo image status\n");}@@ -24,7 +25,7 @@ int cmd_image(int argc, const char **argv)int rc;if (argc == 2 && !strcmp(argv[1], "--list-cmds")) {- puts("build verify status");+ puts("build verify update status");return 0;}if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {@@ -48,7 +49,9 @@ int cmd_image(int argc, const char **argv)xfree(helper, bytes);return rc;}- if (!strcmp(argv[1], "status") && argc == 2) {+ if (!strcmp(argv[1], "update") && argc == 3) {+ rc = shirudo_image_update(argv[2]);+ } else if (!strcmp(argv[1], "status") && argc == 2) {rc = shirudo_image_status();} else {image_help();diff --git a/builtin-keys.c b/builtin-keys.cindex b37e3f58..c18c1e74 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -826,12 +826,12 @@ static int purge_signed_by(const __u8 *der, size_t der_sz,int shirudo_keys_revoke_image(const __u8 *der, size_t size, bool boot){+ struct shirudo_trusted_cert cert = {};key_serial_t ring, key;+ char fingerprint[65];size_t unloaded;int rc;- if (!boot)- return -EOPNOTSUPP;ring = keyring_bpf_serial();if (ring < 0)return ring;@@ -842,6 +842,16 @@ int shirudo_keys_revoke_image(const __u8 *der, size_t size, bool boot)rc = keyring_unlink_key(ring, key);if (rc)return rc;+ if (!boot) {+ rc = keys_digest_der(der, size, cert.sha256,+ cert.fingerprint);+ if (rc)+ return rc;+ hex_encode(cert.fingerprint, 32, fingerprint);+ rc = measure_revoke(&cert, fingerprint);+ if (rc)+ return rc;+ }}rc = purge_signed_by(der, size, &unloaded);return rc < 0 ? rc : 0;diff --git a/util/config_update.c b/util/config_update.cindex dfe7f909..a82775a5 100644--- a/util/config_update.c+++ b/util/config_update.c@@ -598,6 +598,27 @@ static int begin_event(struct shirudo_config_update *update,return complete(update);}+int shirudo_config_update_begin(struct shirudo_config_update *update,+ const struct shirudo_config_update_target *t,+ __u32 pcr)+{+ struct shirudo_config_update_record previous;+ int rc;++ if (!update || update->dir_fd < 0 || target_valid(t) ||+ shirudo_config_measure_pcr_valid(pcr))+ return -EINVAL;+ rc = shirudo_config_update_check(update, &previous);+ if (rc)+ return rc;+ if (pcr != previous.pcr)+ return -EPERM;+ if (!memcmp(t->root_hash, previous.target.root_hash, 32))+ return 0;+ return begin_event(update, t, &previous, pcr,+ SHIRUDO_CONFIG_EVENT_UPDATE);+}+int shirudo_config_update_boot(struct shirudo_config_update *update,const struct shirudo_config_update_target *t,__u32 pcr)diff --git a/util/config_update.h b/util/config_update.hindex 8ef6003f..2ee75beb 100644--- a/util/config_update.h+++ b/util/config_update.h@@ -75,6 +75,9 @@ int shirudo_config_update_open(const char *directory,void shirudo_config_update_close(struct shirudo_config_update *update);int shirudo_config_update_read(struct shirudo_config_update *update,struct shirudo_config_update_record *out);+int shirudo_config_update_begin(struct shirudo_config_update *update,+ const struct shirudo_config_update_target *t,+ __u32 pcr);/* Boot leaves intent pending until upatch restoration and NV init succeed. */int shirudo_config_update_boot(struct shirudo_config_update *update,const struct shirudo_config_update_target *t,diff --git a/util/image_runtime.c b/util/image_runtime.cindex f9504d9f..00cc56a9 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -1015,3 +1015,233 @@ void shirudo_image_leave(void)image.certificate_fd = -1;explicit_bzero(&image.bootstrap_config, sizeof(image.bootstrap_config));}++struct image_download {+ int fd;+ __u64 limit;+ __u64 written;+};++static size_t download_write(char *data, size_t size, size_t count, void *ctx)+{+ struct image_download *download = ctx;+ size_t bytes;++ if (size && count > SIZE_MAX / size)+ return 0;+ bytes = size * count;+ if (bytes > download->limit - download->written ||+ write_complete(download->fd, data, bytes))+ return 0;+ download->written += bytes;+ return bytes;+}++static int copy_file(const char *source, const char *destination, __u64 max)+{+ struct image_download download;+ CURL *curl = NULL;+ char buffer[65536];+ CURLcode result;+ struct stat st;+ ssize_t n;+ int input = -1, output, rc = 0;++ output = open(destination,+ O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW,+ 0600);+ if (output < 0)+ return -errno;+ rc = seal_fd(NULL, output);+ download = (struct image_download){ .fd = output, .limit = max };+ if (rc)+ goto out;+ if (!strncmp(source, "https://", 8)) {+ curl = curl_easy_init();+ if (!curl) {+ rc = -ENOMEM;+ goto out;+ }+ rc = tls_curl_policy(curl, NULL);+ if (rc)+ goto out;+ curl_easy_setopt(curl, CURLOPT_URL, source);+ curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, "https");+ curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "https");+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);+ curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);+ curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);+ curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1024L);+ curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60L);+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_write);+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &download);+ result = curl_easy_perform(curl);+ if (result != CURLE_OK)+ rc = -EIO;+ } else {+ input = open(source, O_RDONLY | O_CLOEXEC | O_NOFOLLOW |+ O_NONBLOCK);+ if (input < 0) {+ rc = -errno;+ goto out;+ }+ if (fstat(input, &st)) {+ rc = -errno;+ goto out;+ }+ if (!S_ISREG(st.st_mode) || st.st_size < 0 ||+ (__u64)st.st_size > max) {+ rc = -EINVAL;+ goto out;+ }+ while ((n = read(input, buffer, sizeof(buffer))) != 0) {+ if (n < 0) {+ if (errno == EINTR)+ continue;+ rc = -errno;+ break;+ }+ if (download_write(buffer, 1, n, &download) !=+ (size_t)n) {+ rc = -EFBIG;+ break;+ }+ }+ }+ if (!rc && fsync(output))+ rc = -errno;+out:+ if (curl)+ curl_easy_cleanup(curl);+ if (input >= 0)+ close(input);+ close(output);+ return rc;+}++/* Verify downloads before publishing their root-hash-named cache files. */+static int verify_incoming(const struct shirudo_config_image_open *opened,+ const char *staging)+{+ struct shirudo_dm_verity device = {+ .data_loop_fd = -1,+ .hash_loop_fd = -1,+ };+ int rc, cleanup;+ char name[128];++ snprintf(name, sizeof(name), "shirudo-verify-%s",+ strrchr(staging, '/') + 1);+ rc = shirudo_dm_verity_create(opened, name, &device);+ if (rc)+ return rc;+ rc = shirudo_dm_verity_verify(&device, opened);+ cleanup = shirudo_dm_verity_remove(&device, true);+ if (cleanup)+ pr_warn("image: cannot remove incoming mapping: %s",+ strerror(-cleanup));+ shirudo_dm_verity_close(&device);+ return rc ? rc : cleanup;+}++static int cache_image(const char *source,+ struct shirudo_config_update_target *target)+{+ static const char *const extensions[] = {+ ".roothash",+ ".p7s",+ ".img",+ ".verity",+ };+ struct shirudo_config_image_open opened;+ char staging[] = SHIRUDO_IMAGE_CACHE_DIR "/incoming-XXXXXX";+ char incoming[PATH_MAX], from[PATH_MAX], to[PATH_MAX];+ char hex[65];+ int dir = -1, rc;+ size_t i;++ rc = mkdir_owned(SHIRUDO_IMAGE_CACHE_DIR);+ if (rc)+ return rc;+ if (!mkdtemp(staging))+ return -errno;+ rc = seal_label(staging);+ if (rc)+ goto out;+ snprintf(incoming, sizeof(incoming), "%s/image", staging);+ for (i = 0; i < ARRAY_SIZE(extensions); i++) {+ __u64 max = i == 0 ? 65 :+ i == 1 ? SHIRUDO_CONFIG_IMAGE_SIG_MAX :+ SHIRUDO_CONFIG_IMAGE_MAX_BYTES;++ if (snprintf(from, sizeof(from), "%s%s", source,+ extensions[i]) >= (int)sizeof(from) ||+ snprintf(to, sizeof(to), "%s%s", incoming, extensions[i]) >=+ (int)sizeof(to)) {+ rc = -ENAMETOOLONG;+ goto out;+ }+ rc = copy_file(from, to, max);+ if (rc)+ goto out;+ }+ rc = shirudo_config_image_open(incoming, &opened);+ if (rc)+ goto out;+ rc = verify_incoming(&opened, staging);+ if (!rc) {+ memcpy(target->root_hash, opened.image.root_hash, 32);+ hex_encode(target->root_hash, 32, hex);+ snprintf(target->prefix, sizeof(target->prefix), "%s/%s",+ SHIRUDO_IMAGE_CACHE_DIR, hex);+ }+ shirudo_config_image_close(&opened);+ if (rc)+ goto out;+ for (i = 0; i < ARRAY_SIZE(extensions); i++) {+ snprintf(from, sizeof(from), "%s%s", incoming, extensions[i]);+ snprintf(to, sizeof(to), "%s%s", target->prefix, extensions[i]);+ if (link(from, to) && errno != EEXIST) {+ rc = -errno;+ goto out;+ }+ }+ dir = open(SHIRUDO_IMAGE_CACHE_DIR,+ O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);+ if (dir < 0 || fsync(dir)) {+ rc = -errno;+ goto out;+ }+ rc = shirudo_config_image_open(target->prefix, &opened);+ if (!rc) {+ if (memcmp(opened.image.root_hash, target->root_hash, 32))+ rc = -EKEYREJECTED;+ shirudo_config_image_close(&opened);+ }+out:+ if (dir >= 0)+ close(dir);+ for (i = 0; i < ARRAY_SIZE(extensions); i++) {+ snprintf(to, sizeof(to), "%s/image%s", staging, extensions[i]);+ unlink(to);+ }+ rmdir(staging);+ return rc;+}++int shirudo_image_update(const char *prefix)+{+ struct shirudo_config_update_target target = {};+ int rc;++ if (!image.locked)+ return -ENODEV;+ rc = current_config();+ if (!rc)+ rc = cache_image(prefix, &target);+ if (!rc)+ rc = shirudo_config_update_begin(&image.update, &target,+ image.bootstrap.pcr);+ return rc;+}diff --git a/util/image_runtime.h b/util/image_runtime.hindex 220fdd6c..bb4b99a9 100644--- a/util/image_runtime.h+++ b/util/image_runtime.h@@ -20,6 +20,7 @@ int shirudo_image_boot(void);int shirudo_image_boot_complete(void);int shirudo_image_enter(const char *command, int argc, const char **argv);void shirudo_image_leave(void);+int shirudo_image_update(const char *prefix);int shirudo_image_status(void);#endif /* SHIRUDO_IMAGE_RUNTIME_H */