Skip to content

eBPF-based security platform

bpm: give a failed gzopen() an errno and read BPM_OCI_CA_FILE once

Daniel Borkmanna1e05638730e
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 92eb863e..2180f713 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -362,7 +362,7 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
int bpm_oci_parse_endpoint(const char *endpoint, struct bpm_oci_client *c)
{
- const char *p, *scheme = getenv("BPM_OCI_SCHEME");
+ const char *p, *ca_file, *scheme = getenv("BPM_OCI_SCHEME");
char transport[16] = "https";
if (!endpoint || strncmp(endpoint, "oci://", 6) != 0)
@@ -381,8 +381,8 @@ int bpm_oci_parse_endpoint(const char *endpoint, struct bpm_oci_client *c)
return -ENAMETOOLONG;
if (strsprintf(c->base_url, sizeof(c->base_url), "%s://%s", transport, p) < 0)
return -ENAMETOOLONG;
- if (getenv("BPM_OCI_CA_FILE")) {
- const char *ca_file = getenv("BPM_OCI_CA_FILE");
+ ca_file = getenv("BPM_OCI_CA_FILE");
+ if (ca_file) {
struct cert ca = {};
if (strlen(ca_file) >= sizeof(c->ca_file))
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 80605c5a..091e0413 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -401,6 +401,21 @@ static gzFile package_gzdopen(int fd)
return gz;
}
+/*
+ * A gzip stream of the file at path. zlib sets errno where a call it
+ * makes fails and leaves it as it was where it refuses on its own.
+ */
+static gzFile package_gzopen(const char *path)
+{
+ gzFile gz;
+
+ errno = 0;
+ gz = gzopen(path, "rb");
+ if (!gz && !errno)
+ errno = ENOMEM;
+ return gz;
+}
+
/* Takes gz over, and its open's errno when it is NULL. */
static int read_member_gz(gzFile gz, const char *member,
const char *package_dir, char *found_dir,
@@ -526,7 +541,7 @@ int bpm_pkg_read_member_in(const char *archive, const char *member,
size_t found_dir_sz, __u8 **out,
size_t *out_len, size_t max)
{
- return read_member_gz(gzopen(archive, "rb"), member, package_dir,
+ return read_member_gz(package_gzopen(archive), member, package_dir,
found_dir, found_dir_sz, out, out_len, max);
}