Skip to content

eBPF-based security platform

bpm: let a registry neither name nor replace a file on pull

Daniel Borkmann3598fde7bd15
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 90497cd5..df8e1494 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -367,7 +367,9 @@ signed config image.
* - ``t7002-bpm-info``
- READMEs of local and OCI packages, archive integrity
* - ``t7003-bpm-key``
- - ``bpm key`` from files and OCI references, ``bpm pull``
+ - ``bpm key`` from files and OCI references, ``bpm pull``, which
+ replaces no file, not even after a failed digest, and names none
+ after a layer title that is not a package name
* - ``t7004-bpm-repo-update``
- The repository update unit and timer and their package
scriptlets, as text
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index afae31d9..2da5de41 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -245,9 +245,13 @@ compare the result with a fingerprint you obtained out of band.
pull and info
-------------
-``pull`` downloads the package layer of an ``oci://`` reference into the
-current directory, named after the layer title or
-``<name>-<tag>.bee``, and checks its digest against the manifest.
+``pull`` downloads the package layer of an ``oci://`` reference into a
+new file in the current directory and checks its digest against the
+manifest. The registry chooses the layer title without a signature, so
+the file takes that title only when it is a package name ending in
+``.bee``, and is named ``<name>-<tag>.bee`` otherwise. ``pull`` never
+replaces a file: when one of that name exists, it fails with ``bpm
+pull: File exists``, so remove or rename the old one first.
``info`` prints the ``README.md`` of a package. ``info upatches`` lists
its ``upatches/*.upatch`` members, with ``--all`` also the
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 0062a9fc..4a55aad6 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -128,10 +128,24 @@ out:
}
/*
- * Into the file open on fd at destination, or, without one, a file in
- * the working directory named after the package. The name comes from
- * the registry, so it is one component ending in .bee, opened as output
- * root may ask for: never a sealed file nor one reached through a link.
+ * The registry names the layer without a signature, so its title names
+ * the file only when it is a package name ending in .bee: never a path,
+ * a hidden file, an option or a terminal control sequence.
+ */
+static bool bee_title_ok(const char *title)
+{
+ size_t n = strlen(title);
+
+ return n > strlen(".bee") && bpm_package_name_ok(title) &&
+ !strcmp(title + n - strlen(".bee"), ".bee");
+}
+
+/*
+ * Into the file open on fd at destination, or, without one, a new file
+ * in the working directory named after the package. The registry picks
+ * that name, so it is created, never an existing file replaced, and
+ * opened as output root may ask for: never a sealed file nor one
+ * reached through a link.
*/
static int download_package(const char *raw, const char *destination, int fd)
{
@@ -173,9 +187,7 @@ static int download_package(const char *raw, const char *destination, int fd)
title[0] = '\0';
if (destination) {
bpm_copy_string(filename, sizeof(filename), destination);
- } else if (title[0] && !strchr(title, '/') &&
- strlen(title) >= strlen(".bee") &&
- !strcmp(title + strlen(title) - strlen(".bee"), ".bee")) {
+ } else if (bee_title_ok(title)) {
bpm_copy_string(filename, sizeof(filename), title);
} else {
const char *base = strrchr(repository, '/');
@@ -195,7 +207,7 @@ static int download_package(const char *raw, const char *destination, int fd)
}
if (!destination) {
out = seal_output_open(filename,
- O_WRONLY | O_CREAT | O_TRUNC,
+ O_WRONLY | O_CREAT | O_EXCL,
0644);
if (out < 0) {
ret = out;
diff --git a/testing/helper/test-bpm-key.py b/testing/helper/test-bpm-key.py
index 10d7a8e5..13af2b29 100644
--- a/testing/helper/test-bpm-key.py
+++ b/testing/helper/test-bpm-key.py
@@ -126,6 +126,9 @@ def main():
layer["size"] += 1
elif tag == "missing-layer":
layer["mediaType"] = "application/octet-stream"
+ elif tag == "escape-title":
+ layer["annotations"]["org.opencontainers.image.title"] = \
+ "\x1b[2Jdownload.bee"
body = json.dumps(dict(schemaVersion=2, layers=[layer])).encode()
content_type = "application/vnd.oci.image.manifest.v1+json"
elif "/blobs/" in self.path:
@@ -156,11 +159,26 @@ def main():
if isolated:
run("add", reference + tag, ok=False)
assert (keys / "cert.der").read_bytes() == der.read_bytes()
+ def pull(tag):
+ return subprocess.run([binary, "bpm", "pull", reference + tag],
+ cwd=root, env=env, capture_output=True,
+ text=True)
+
assert not (root / "download.bee").exists()
- result = subprocess.run([binary, "bpm", "pull", reference + "valid"],
- cwd=root, env=env, capture_output=True, text=True)
+ result = pull("valid")
assert result.returncode == 0, result
assert (root / "download.bee").read_bytes() == payload
+ # The registry names the file, so a pull never replaces one, nor
+ # removes it when the download then fails its digest.
+ for tag in ("valid", "bad-digest"):
+ result = pull(tag)
+ assert result.returncode != 0, result
+ assert "exists" in result.stderr, result
+ assert (root / "download.bee").read_bytes() == payload
+ result = pull("escape-title")
+ assert result.returncode == 0, result
+ assert (root / "package-escape-title.bee").read_bytes() == payload
+ assert not any("\x1b" in p.name for p in root.iterdir())
print("OCI keys, descriptor rejection, private downloads, and pull: passed")
finally:
server.shutdown()