Skip to content

eBPF-based security platform

bpm: require enforcement to install, upgrade and uninstall

John Fastabend8274c79b7d2b
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 4e625aa6..f2644007 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -694,7 +694,8 @@ signed config image.
- Covers
* - ``t7001-bpm-cli``
- Subcommands, reference validation before network access, source
- and tree configuration lines
+ and tree configuration lines, ``install``, ``upgrade`` and
+ ``uninstall`` refused without enforcement
* - ``t7002-bpm-info``
- READMEs of local and OCI packages, archive integrity, a download
kept in a memory file rather than in ``/tmp``, one key
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index f98e89e3..55dd6f92 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -526,6 +526,12 @@ Options take the value as the next argument or after ``=``, except
install, upgrade, uninstall and list
------------------------------------
+``install``, ``upgrade`` and ``uninstall`` run only where enforcement is
+active. On a host that ran no ``shirudo init``, or whose ``init``
+failed, each prints ``bpm <action>: enforcement is not active; run
+shirudo init first`` and exits with status 1, before it reads or changes
+anything.
+
``install <source>/<name>[@<version>]`` takes the package from the index
cached by ``repo update``: the given version, else the greatest. ``install
oci://<registry>/<repository>:<tag>`` takes the package directly, with the
diff --git a/builtin-bpm.c b/builtin-bpm.c
index fe6c3371..cccf50db 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -71,13 +71,17 @@ struct bpm_source {
static int sha256_file_hex(const char *path, char *hex, size_t hex_sz);
static int sha256_fd_hex(int fd, char *hex, size_t hex_sz);
+/*
+ * -ENODEV, not -ENOENT, without the identity init pins: callers take
+ * -ENOENT for a package or a version that is not there.
+ */
static int package_store_open(void)
{
int state, bpm, packages;
state = state_root_open();
if (state < 0)
- return state;
+ return state == -ENOENT ? -ENODEV : state;
bpm = seal_open_child(state, "bpm", O_RDONLY | O_DIRECTORY);
close(state);
if (bpm < 0)
@@ -2843,6 +2847,23 @@ static const struct help bpm_operator_help = {
.n_sections = 1,
};
+/*
+ * A package is installed, upgraded and removed on an enforcing target
+ * only. 0 there, else the exit status, after the message.
+ */
+static int bpm_needs_enforcement(const char *action)
+{
+ int ret = enforcement_active();
+
+ if (ret > 0)
+ return 0;
+ if (ret < 0)
+ return bpm_fail(action, ret);
+ pr_err("bpm %s: enforcement is not active; run shirudo init first",
+ action);
+ return 1;
+}
+
static int bpm_finish(int lock, int ret, const char *action)
{
if (lock >= 0) {
@@ -2974,6 +2995,9 @@ static int bpm_install(const char *ref, struct bpm_tables *t)
struct bpm_source source_cfg;
int n_entries, n_state, lock, ret, i;
+ ret = bpm_needs_enforcement("install");
+ if (ret)
+ return ret;
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK)
return bpm_fail("initialize curl", -EIO);
lock = with_bpm_lock();
@@ -3044,6 +3068,9 @@ static int bpm_upgrade(const char *unused, struct bpm_tables *t)
struct bpm_state_entry *state = t->state;
int n_state, lock, ret, wrc, i;
+ ret = bpm_needs_enforcement("upgrade");
+ if (ret)
+ return ret;
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK)
return bpm_fail("initialize curl", -EIO);
lock = with_bpm_lock();
@@ -3508,6 +3535,9 @@ static int bpm_uninstall_cmd(int argc, const char **argv)
return bpm_exit(ret);
if (!bpm_name_ok(package))
return bpm_fail("uninstall", -EINVAL);
+ ret = bpm_needs_enforcement("uninstall");
+ if (ret)
+ return ret;
state = xcalloc(BPM_MAX_STATE, sizeof(*state));
if (!state)
return bpm_fail("uninstall", -ENOMEM);
diff --git a/testing/functional/bpm/t7001-bpm-cli.sh b/testing/functional/bpm/t7001-bpm-cli.sh
index 56f00c89..5377d639 100644
--- a/testing/functional/bpm/t7001-bpm-cli.sh
+++ b/testing/functional/bpm/t7001-bpm-cli.sh
@@ -76,6 +76,20 @@ test_expect_success 'bpm uninstall and list validate their arguments' '
$SHIRUDO bpm uninstall --help 2>&1 | grep -q "usage: shirudo bpm uninstall <package>"
'
+# A host that ran no init has nothing to load a package into. The suite
+# also runs in the target guest, where these commands go on.
+if [ ! -e /run/shirudo/bpffs/lsm_link ]; then
+ test_expect_success 'bpm install, upgrade and uninstall need enforcement' '
+ for cmd in "install example/pkg" upgrade "uninstall pkg"; do
+ rc=0
+ out=$($SHIRUDO bpm $cmd 2>&1) || rc=$?
+ [ "$rc" -eq 1 ] || return 1
+ echo "$out" | grep -q "bpm ${cmd%% *}: enforcement is not active; run shirudo init first" ||
+ return 1
+ done
+ '
+fi
+
test_expect_success 'bpm parses repository source and tree configuration lines' '
out=$($BPM_SOURCE_HELPER) &&
echo "$out" | grep -qx "endpoint=oci://registry.test:5443" &&