eBPF-based security platform
bpm: refuse a registry auth file that holds a NUL
Daniel Borkmannd40d7df503a6
diff --git a/Documentation/reference/environment.rst b/Documentation/reference/environment.rstindex 67498ab4..c920fdca 100644--- a/Documentation/reference/environment.rst+++ b/Documentation/reference/environment.rst@@ -136,7 +136,8 @@ Honored on a target.* - ``DOCKER_CONFIG``- Directory holding ``config.json``, read when``REGISTRY_AUTH_FILE`` is unset. Without either,- ``$HOME/.docker/config.json`` is read.+ ``$HOME/.docker/config.json`` is read. An auth file that holds a+ NUL byte is refused, wherever it is read from.Telemetry export----------------diff --git a/testing/fuzz/fuzz-registry-auth.c b/testing/fuzz/fuzz-registry-auth.cindex 5312d940..497b95cd 100644--- a/testing/fuzz/fuzz-registry-auth.c+++ b/testing/fuzz/fuzz-registry-auth.c@@ -2,10 +2,13 @@/** Copyright (c) 2026 Authors of shirudo*- * The registry credentials in a container auth file, REGISTRY_AUTH_FILE.+ * The registry credentials in a container auth file, REGISTRY_AUTH_FILE:+ * a file that holds a NUL gives none, as the search for them would end+ * there.*/#include <stdlib.h>+#include <string.h>#include "bpm_oci.h"#include "fuzz.h"@@ -22,13 +25,15 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){struct bpm_oci_client c = {};char path[64];- int fd;+ int fd, ret;fd = fuzz_file(data, size, path, sizeof(path));if (fd < 0)return 0;setenv("REGISTRY_AUTH_FILE", path, 1);- bpm_oci_parse_endpoint("oci://registry.example:5000", &c);+ ret = bpm_oci_parse_endpoint("oci://registry.example:5000", &c);+ if (memchr(data, '\0', size) && (!ret || c.username[0]))+ abort();close(fd);return 0;}diff --git a/testing/unit/bpm_oci.c b/testing/unit/bpm_oci.cindex b5bfa363..1ed8e500 100644--- a/testing/unit/bpm_oci.c+++ b/testing/unit/bpm_oci.c@@ -2,11 +2,15 @@/* Copyright (c) 2026 Authors of shirudo */#include <errno.h>+#include <fcntl.h>+#include <stdlib.h>#include <string.h>+#include <unistd.h>#include <shirudo/string.h>#include "bpm_oci.h"+#include "io.h"#include "unit.h"#define DIGEST "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"@@ -65,9 +69,58 @@ static void digest_names_a_blob_as_a_registry_does(void)EXPECT_EQ(bpm_sha256_digest("abc", 3, out, 71), -ENAMETOOLONG);}+#define REGISTRY "registry.example:5000"+/* "user:pass" */+#define AUTHS "{\"auths\":{\"" REGISTRY "\":{\"auth\":\"dXNlcjpwYXNz\"}}}"++/* The credentials shirudo takes for REGISTRY from an auth file of len bytes. */+static int auth_file(const char *text, size_t len, struct bpm_oci_client *c)+{+ const char *tmp = getenv("TMPDIR");+ char path[4096];+ int fd, ret;++ ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/shirudo-unit-auth-XXXXXX",+ tmp && tmp[0] ? tmp : "/tmp") > 0);+ fd = mkstemp(path);+ ASSERT_TRUE(fd >= 0);+ ASSERT_EQ(write_complete(fd, text, len), 0);+ ASSERT_EQ(close(fd), 0);+ ASSERT_EQ(setenv("REGISTRY_AUTH_FILE", path, 1), 0);+ memset(c, 0, sizeof(*c));+ ret = bpm_oci_parse_endpoint("oci://" REGISTRY, c);+ ASSERT_EQ(unsetenv("REGISTRY_AUTH_FILE"), 0);+ ASSERT_EQ(unlink(path), 0);+ return ret;+}++/*+ * The file is searched as a string. A NUL ahead of the registry's entry+ * left the request without credentials and without a word, and one+ * behind it took the entry from a file that is no text.+ */+static void auth_file_with_a_nul_is_refused(void)+{+ static const char ahead[] = "\0" AUTHS;+ static const char behind[] = AUTHS "\0{";+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};++ ASSERT_EQ(unsetenv("BPM_OCI_USERNAME"), 0);+ ASSERT_EQ(unsetenv("BPM_OCI_SCHEME"), 0);+ ASSERT_EQ(unsetenv("BPM_OCI_CA_FILE"), 0);+ ASSERT_EQ(auth_file(AUTHS, sizeof(AUTHS) - 1, &c), 0);+ EXPECT_STREQ(c.username, "user");+ EXPECT_STREQ(c.password, "pass");+ EXPECT_EQ(auth_file(ahead, sizeof(ahead) - 1, &c), -EINVAL);+ EXPECT_STREQ(c.username, "");+ EXPECT_EQ(auth_file(behind, sizeof(behind) - 1, &c), -EINVAL);+ EXPECT_STREQ(c.username, "");+}+static const struct unit_case cases[] = {UNIT_CASE(manifest_layer_is_a_whole_descriptor),UNIT_CASE(digest_names_a_blob_as_a_registry_does),+ UNIT_CASE(auth_file_with_a_nul_is_refused),{}};diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex f8cfcf2a..ab54106d 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -322,6 +322,10 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)ret = read_file(authfile, 4u * 1024u * 1024u, &body, &body_len);if (ret)return ret == -ENOENT ? 0 : ret;+ if (span_to_str((char *)body, body_len + 1, body, body_len)) {+ xfree(body, body_len + 1);+ return -EINVAL;+ }/** The auth file is local input; require the registry key and auth* member to occur in the same JSON object before accepting