Skip to content

eBPF-based security platform

bpm: require the directive of every load member at uninstall

KP Singhb82c6dd2be21
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index fa1bfb32..f98e89e3 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -556,12 +556,12 @@ runs the timer.
``uninstall <name>`` applies the package's shipped unload directives in
reverse name order. BPM opens the directives from the installed version
-and reads each from the descriptor it checked. A missing directive for
-an active load stops uninstall. An interrupted uninstall can finish if
-the remaining load members are inactive or the directory is already
-empty. Successful uninstall removes the whole package directory,
-including archives and older versions left by upgrades. Uninstall and
-failed-install cleanup refuse to cross mounts below the state root.
+and reads each from the descriptor it checked. A load member without its
+directive stops uninstall. Cleanup removes load members before their
+directives, so an interrupted uninstall can finish on a retry. Successful
+uninstall removes the whole package directory, including archives and
+older versions left by upgrades. Uninstall and failed-install cleanup
+refuse to cross mounts below the state root.
``list`` prints the installed packages under the heading ``Installed
packages:`` as a table with the columns ``NAME``, ``VERSION`` and
diff --git a/builtin-upatch.c b/builtin-upatch.c
index aa4d1d79..ed45ba73 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -4048,34 +4048,6 @@ static int purge_is_loaded(const char *name, __u32 type)
return ret ? ret : 1;
}
-/* The load member of an older package may have no unload directive. */
-int upatch_member_active_fd(int fd)
-{
- char name[UPATCH_NAME_MAX + 1];
- struct upatch u;
- int ret;
-
- ret = upatch_open_fd(fd, &u);
- if (ret)
- return ret;
- upatch_header_name(&u, name);
- if (upatch_is_unload(&u) || !upatch_name_signed(&u) || !upatch_name_valid(name)) {
- ret = -EINVAL;
- } else if (u.hdr.type == UPATCH_TYPE_JAVA) {
- ret = purge_is_loaded(name, u.hdr.type);
- } else {
- ret = open_upatch_pin_dir(name);
- if (ret >= 0) {
- close(ret);
- ret = 1;
- } else if (ret == -ENOENT) {
- ret = 0;
- }
- }
- upatch_close(&u);
- return ret;
-}
-
static int upatch_purge_locked(const char *name, const struct cert *signer,
const char *reason)
{
diff --git a/testing/unit/bpm_cleanup.c b/testing/unit/bpm_cleanup.c
index bee041b2..71678f74 100644
--- a/testing/unit/bpm_cleanup.c
+++ b/testing/unit/bpm_cleanup.c
@@ -18,14 +18,6 @@
static int calls;
static char order[4];
static bool refuse;
-static int active;
-
-int upatch_member_active_fd(int fd)
-{
- char byte;
-
- return pread(fd, &byte, 1, 0) == 1 && byte == 'l' ? active : -EINVAL;
-}
int upatch_remove_fd(const char *path, int fd)
{
@@ -101,20 +93,21 @@ static void uninstall_retires_all_versions_after_detach(void)
finish_package(&p);
}
-static void unpaired_load_must_be_inactive(void)
+static void load_without_directive_stops_uninstall(void)
{
struct package p = {};
new_package(&p);
- put_file(p.fd, "sample/1/upatches/load.upatch", 'l');
- active = 1;
+ put_file(p.fd, "sample/1/upatches/a.upatch", 'l');
+ put_file(p.fd, "sample/1/upatches/a.unload.upatch", 'a');
+ put_file(p.fd, "sample/1/upatches/b.upatch", 'l');
EXPECT_EQ(bpm_uninstall_at(p.fd, &p.st), -ENODATA);
- active = -EIO;
- EXPECT_EQ(bpm_uninstall_at(p.fd, &p.st), -EIO);
- EXPECT_EQ(faccessat(p.fd, "sample/1/upatches/load.upatch", F_OK, 0), 0);
- active = 0;
- ASSERT_EQ(bpm_uninstall_at(p.fd, &p.st), 0);
EXPECT_EQ(calls, 0);
+ EXPECT_EQ(faccessat(p.fd, "sample/1/upatches/a.upatch", F_OK, 0), 0);
+ EXPECT_EQ(faccessat(p.fd, "sample/1/upatches/b.upatch", F_OK, 0), 0);
+ put_file(p.fd, "sample/1/upatches/b.unload.upatch", 'b');
+ ASSERT_EQ(bpm_uninstall_at(p.fd, &p.st), 0);
+ EXPECT_STREQ(order, "ba");
finish_package(&p);
}
@@ -152,7 +145,7 @@ static void cleanup_requires_a_single_child_directory(void)
static const struct unit_case cases[] = {
UNIT_CASE(uninstall_retires_all_versions_after_detach),
- UNIT_CASE(unpaired_load_must_be_inactive),
+ UNIT_CASE(load_without_directive_stops_uninstall),
UNIT_CASE(retry_finishes_a_partly_removed_package),
UNIT_CASE(cleanup_requires_a_single_child_directory),
{}
diff --git a/util/bpm_cleanup.c b/util/bpm_cleanup.c
index f1efcf2b..33e3fd70 100644
--- a/util/bpm_cleanup.c
+++ b/util/bpm_cleanup.c
@@ -100,7 +100,8 @@ static int directive_compare_desc(const void *left, const void *right)
return strcmp(b->path, a->path);
}
-static int check_unpaired_load(int dir, const char *name)
+/* A package ships each load member with the directive that undoes it. */
+static int check_load_directive(int dir, const char *name)
{
char pair[NAME_MAX + 1];
int fd, ret;
@@ -109,22 +110,14 @@ static int check_unpaired_load(int dir, const char *name)
if (ret)
return ret;
fd = seal_open_child(dir, pair, O_RDONLY | O_NONBLOCK);
- if (fd >= 0) {
- close(fd);
- return 0;
+ if (fd == -ENOENT) {
+ pr_err("bpm uninstall: %s is missing %s", name, pair);
+ return -ENODATA;
}
- if (fd != -ENOENT)
- return fd;
- fd = seal_open_child(dir, name, O_RDONLY | O_NONBLOCK);
if (fd < 0)
return fd;
- ret = upatch_member_active_fd(fd);
close(fd);
- if (ret > 0) {
- pr_err("bpm uninstall: active %s is missing %s", name, pair);
- return -ENODATA;
- }
- return ret;
+ return 0;
}
/* Keep the files we checked open through signature verification. */
@@ -144,7 +137,7 @@ static int open_directives(DIR *dir, const char *version_path,
return -errno;
kind = bpm_upatch_member(entry->d_name, strlen(entry->d_name));
if (kind == BPM_UPATCH_LOAD) {
- ret = check_unpaired_load(dirfd(dir), entry->d_name);
+ ret = check_load_directive(dirfd(dir), entry->d_name);
if (ret)
return ret;
}
diff --git a/util/upatch.h b/util/upatch.h
index e27f236d..c9224ac2 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -234,8 +234,6 @@ int upatch_restore(const char *path);
int upatch_install(const char *path);
int upatch_remove(const char *path);
int upatch_remove_fd(const char *path, int fd);
-/* 1 when the load member is active, 0 when absent, or -errno. */
-int upatch_member_active_fd(int fd);
/* A missing optional kprobe module permits tree projection advancement. */
#define UPATCH_INSTALL_TARGET_MISSING 2