eBPF-based security platform
shirudo-build: tighten the error paths of the operator tools
Daniel Borkmann59d8cf19219b
diff --git a/builtin-genkey.c b/builtin-genkey.cindex 71099565..565f3d73 100644--- a/builtin-genkey.c+++ b/builtin-genkey.c@@ -24,6 +24,7 @@#include <openssl/x509v3.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "builtin.h"#include "util/help.h"@@ -417,16 +418,37 @@ int cmd_genkey(int argc, const char **argv)goto out;if (force) {- /* Make way for the O_EXCL writes only once a new pair exists. */- unlink(pkey_path);- unlink(cert_path);- }-- if (write_private_pem(pkey, pkey_path) != 0)- goto out;- if (write_cert_der(cert, cert_path) != 0) {- unlink(pkey_path);- goto out;+ char pkey_new[PATH_MAX], cert_new[PATH_MAX];++ /* The old pair goes only once the new one is on disk. */+ if (strsprintf(pkey_new, sizeof(pkey_new), "%s.new",+ pkey_path) < 0 ||+ strsprintf(cert_new, sizeof(cert_new), "%s.new",+ cert_path) < 0) {+ pr_err("genkey: no room for a temporary beside %s",+ pkey_path);+ goto out;+ }+ unlink(pkey_new);+ unlink(cert_new);+ if (write_private_pem(pkey, pkey_new) != 0)+ goto out;+ if (write_cert_der(cert, cert_new) != 0 ||+ rename(pkey_new, pkey_path) != 0 ||+ rename(cert_new, cert_path) != 0) {+ pr_err("genkey: cannot put the new pair in place: %s",+ strerror(errno));+ unlink(pkey_new);+ unlink(cert_new);+ goto out;+ }+ } else {+ if (write_private_pem(pkey, pkey_path) != 0)+ goto out;+ if (write_cert_der(cert, cert_path) != 0) {+ unlink(pkey_path);+ goto out;+ }}pr_info("wrote %s (%s, mode 0600)", pkey_path, GENKEY_ALG);diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 8ad2f223..9154ad63 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -754,8 +754,10 @@ static int build_run_rodata_command(const struct bpm_build_spec *spec,execvp(argv[0], (char *const *)argv);_exit(127);}- if (waitpid(pid, &status, 0) < 0)- return -errno;+ while (waitpid(pid, &status, 0) < 0) {+ if (errno != EINTR)+ return -errno;+ }if (WIFEXITED(status) && WEXITSTATUS(status) == 0)return 0;return -EIO;@@ -1439,6 +1441,8 @@ static const char *build_resolve_keys_dir(const char *arg, char *out, size_t cap{const char *home = getenv("HOME");+ if (cap)+ out[0] = '\0';if (!arg || !*arg)arg = keys_dir_resolve(NULL);if (arg[0] == '~' && (!arg[1] || arg[1] == '/')) {@@ -2047,7 +2051,8 @@ int shirudo_build_bpm_config_update(int argc, char **argv)if (rc)break;if (upatch_is_unload(&parsed) ||- strcmp(parsed.hdr.name, expected_name)) {+ strncmp(parsed.hdr.name, expected_name,+ sizeof(parsed.hdr.name))) {rc = -EINVAL;upatch_close(&parsed);break;diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.cindex 6858c019..1b5d271c 100644--- a/tools/shirudo-build/compile.c+++ b/tools/shirudo-build/compile.c@@ -173,9 +173,11 @@ int shirudo_build_compile(const char *source, const char *output,SHIRUDO_CLANG_PATH, strerror(errno));_exit(127);}- if (waitpid(pid, &status, 0) < 0) {- rc = -errno;- goto out;+ while (waitpid(pid, &status, 0) < 0) {+ if (errno != EINTR) {+ rc = -errno;+ goto out;+ }}if (WIFEXITED(status)) {int es = WEXITSTATUS(status);diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex c6ccec3c..c863a8ec 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -112,6 +112,25 @@ static void pack_header(struct shirudo_upatch_header *h,h->reserved1 = 0;}+/*+ * The inputs stay mapped while the output is written, so an output that+ * is one of them, under whatever name, must be refused before it is+ * truncated.+ */+static bool output_is_input(int fd, const char *original,+ const char *modified)+{+ struct stat out, in;++ if (fstat(fd, &out))+ return true;+ if (!stat(original, &in) && in.st_dev == out.st_dev &&+ in.st_ino == out.st_ino)+ return true;+ return !stat(modified, &in) && in.st_dev == out.st_dev &&+ in.st_ino == out.st_ino;+}+static int package_resign_symbols(const char *original,const char *modified, const char *output,const struct shirudo_data_range *ranges,@@ -131,6 +150,7 @@ static int package_resign_symbols(const char *original,__u32 digest_len;__u64 total, payload_len;size_t i, j;+ bool wrote = false;int fd = -1;int rc;@@ -278,12 +298,17 @@ static int package_resign_symbols(const char *original,sio.data_sz ? data_off : 0, (__u32)sio.data_sz,mod.hdr.log_buf_sz, mod.hdr.rodata_data_off,mod.hdr.nr_maps, mod.hdr.nr_progs);- fd = open(output, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);+ fd = open(output, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);if (fd < 0) {rc = -errno;goto out;}- rc = write_complete(fd, &hdr, sizeof(hdr));+ rc = output_is_input(fd, original, modified) ? -EINVAL : 0;+ if (!rc && ftruncate(fd, 0))+ rc = -errno;+ wrote = !rc;+ if (!rc)+ rc = write_complete(fd, &hdr, sizeof(hdr));if (!rc)rc = write_complete(fd, sio.insns, sio.insns_sz);if (!rc)@@ -297,6 +322,8 @@ static int package_resign_symbols(const char *original,out:if (fd >= 0)close(fd);+ if (rc && wrote)+ unlink(output);if (sig_buf)xfree(sig_buf, SHIRUDO_MAX_SIG_SIZE);if (payload)@@ -332,6 +359,7 @@ int shirudo_build_package_patch_rodata(const char *input, const char *output,const char *symbols[1] = { symbol };__u8 digest[SHIRUDO_SHA256_LEN], *payload = NULL, *candidate = NULL;char modified[] = "/tmp/shirudo-rodata-XXXXXX";+ bool created = false;__u64 payload_len;__u32 digest_len;int fd = -1;@@ -392,6 +420,7 @@ int shirudo_build_package_patch_rodata(const char *input, const char *output,rc = -errno;goto out;}+ created = true;rc = write_complete(fd, candidate, src.map_size);if (!rc && fsync(fd))rc = -errno;@@ -408,7 +437,7 @@ int shirudo_build_package_patch_rodata(const char *input, const char *output,out:if (fd >= 0)close(fd);- if (modified[0] && access(modified, F_OK) == 0)+ if (created)unlink(modified);if (candidate)xfree(candidate, src.map_size);