eBPF-based security platform
attest: close the output once, read errno first, give base64 its room
Daniel Borkmann5dc1d689e645
diff --git a/builtin-attest.c b/builtin-attest.cindex 2df4ba04..6d576967 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -231,8 +231,10 @@ static int cert_digest(const char *path, __u8 out[32])fd = open(path, O_RDONLY | O_CLOEXEC);if (fd < 0) {- pr_err("attest: open %s: %s", path, strerror(errno));- return -errno;+ int err = -errno;++ pr_err("attest: open %s: %s", path, strerror(-err));+ return err;}if (fstat(fd, &st) != 0 || st.st_size <= 0 ||st.st_size > 1024 * 1024) {@@ -444,8 +446,10 @@ static int read_evidence(const char *path, char **out, size_t *len)fd = open(path, O_RDONLY | O_CLOEXEC);if (fd < 0) {- pr_err("attest verify: open %s: %s", path, strerror(errno));- return -errno;+ int err = -errno;++ pr_err("attest verify: open %s: %s", path, strerror(-err));+ return err;}if (fstat(fd, &st) != 0 || st.st_size <= 0 ||st.st_size > (off_t)EVIDENCE_MAX) {@@ -745,11 +749,13 @@ static int cmd_attest_challenge(int argc, const char **argv)ret = 1;goto out;}- if (write_complete(fd, doc, len) ||- write_complete(fd, "\n", 1) || close(fd)) {+ rc = write_complete(fd, doc, len) ||+ write_complete(fd, "\n", 1) ? -EIO : 0;+ if (close(fd) && !rc)+ rc = -EIO;+ if (rc) {pr_err("attest challenge: write %s: %s", out_path,strerror(errno));- close(fd);ret = 1;goto out;}@@ -842,12 +848,15 @@ static int cmd_attest_evidence(int argc, const char **argv)if (fd < 0) {pr_err("attest: open %s: %s", out_path, strerror(-fd));rc = fd;- } else if (write_complete(fd, doc, len) ||- write_complete(fd, "\n", 1) || close(fd)) {- pr_err("attest: write %s: %s", out_path,- strerror(errno));- close(fd);- rc = -EIO;+ } else {+ if (write_complete(fd, doc, len) ||+ write_complete(fd, "\n", 1))+ rc = -EIO;+ if (close(fd) && !rc)+ rc = -EIO;+ if (rc)+ pr_err("attest: write %s: %s", out_path,+ strerror(errno));}} else if (fwrite(doc, 1, len, stdout) != len ||fputc('\n', stdout) == EOF) {diff --git a/util/crypto_openssl.c b/util/crypto_openssl.cindex a7d45be3..cb9accc0 100644--- a/util/crypto_openssl.c+++ b/util/crypto_openssl.c@@ -506,7 +506,7 @@ int base64_decode(const char *in, size_t in_len, __u8 *out,EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();int n = 0, fin = 0, rc = -EINVAL;- if (!ctx || in_len > INT_MAX || cap < 3 * (in_len / 4) + 3)+ if (!ctx || in_len > INT_MAX || cap < 3 * ((in_len + 3) / 4))goto out;EVP_DecodeInit(ctx);if (EVP_DecodeUpdate(ctx, out, &n, (const __u8 *)in,diff --git a/util/tpm.c b/util/tpm.cindex b3b778e0..a988e5c3 100644--- a/util/tpm.c+++ b/util/tpm.c@@ -372,14 +372,18 @@ int tpm_nv_undefine(struct shirudo_tpm *t, __u32 index,if (err)return err;err = owner_auth_set(t, owner_auth, owner_auth_len);- if (err)+ if (err) {+ Esys_TR_Close(t->esys, &nv);return err;+ }rc = Esys_NV_UndefineSpace(t->esys, ESYS_TR_RH_OWNER, nv,ESYS_TR_PASSWORD, ESYS_TR_NONE,ESYS_TR_NONE);owner_auth_clear(t);- if (rc != TSS2_RC_SUCCESS)+ if (rc != TSS2_RC_SUCCESS) {+ Esys_TR_Close(t->esys, &nv);return owner_auth_error("NV_UndefineSpace", index, rc);+ }return 0;}