eBPF-based security platform
config: wait for the operation lock instead of failing at once
Daniel Borkmann6fbcec0dd26d
diff --git a/Documentation/operations.rst b/Documentation/operations.rstindex d81ab9cb..c5192960 100644--- a/Documentation/operations.rst+++ b/Documentation/operations.rst@@ -429,7 +429,8 @@ success, ``1`` on any refusal or failure and ``2`` on a usage error;Use ``shirudo image status`` to inspect the active config root hash, PCR,TPM reset and restart counts, event sequence and pending state. Ordinary target commandshold the config operation lock and use one held config directory for the-operation. They refuse a pending activation or PCR/history inconsistency;+operation; a command waits up to 30 seconds for another one to release+it. They refuse a pending activation or PCR/history inconsistency;attestation follows the same rule.Activate a complete locally staged bundle or pull it by HTTPS prefix::diff --git a/testing/helper/test-config-update.c b/testing/helper/test-config-update.cindex 4eb2213a..a73c4238 100644--- a/testing/helper/test-config-update.c+++ b/testing/helper/test-config-update.c@@ -283,6 +283,7 @@ int main(int argc, char **argv)struct shirudo_config_update second = { .dir_fd = -1, .lock_fd = -1 };struct shirudo_config_update update = { .dir_fd = -1, .lock_fd = -1 };struct test_context ctx = { .update = &update };+ struct shirudo_config_update_ops ops;struct shirudo_config_update_target target;char hex[65];__u32 pcr = 15;@@ -320,7 +321,11 @@ int main(int argc, char **argv)if (rc)goto out;}- rc = shirudo_config_update_open(argv[1], &test_ops, &ctx, &update);+ ops = test_ops;+ if (getenv("SHIRUDO_TEST_CONFIG_LOCK_WAIT_MS"))+ ops.lock_wait_ms = strtoul(getenv("SHIRUDO_TEST_CONFIG_LOCK_WAIT_MS"),+ NULL, 10);+ rc = shirudo_config_update_open(argv[1], &ops, &ctx, &update);if (rc)goto out;if (!strcmp(argv[2], "boot")) {diff --git a/testing/t0046-config-update.sh b/testing/t0046-config-update.shindex d6dfcafd..f8ec7ac1 100755--- a/testing/t0046-config-update.sh+++ b/testing/t0046-config-update.sh@@ -208,6 +208,29 @@ test_expect_success 'a second writer cannot acquire the same operation lock' '[ "$(field "$WORK/locked.out" rc)" = -11 ]'+test_expect_success 'a command waits for the operation lock another one holds' '+ command -v flock >/dev/null 2>&1 || exit 0;+ fresh waiting &&+ { flock "$WORK/waiting/lock" sleep 1 & holder=$!; } &&+ sleep 0.3 &&+ SHIRUDO_TEST_CONFIG_LOCK_WAIT_MS=10000 "$H" "$WORK/waiting" boot A \+ >"$WORK/waiting.out" 2>"$WORK/waiting.err" &&+ wait "$holder" &&+ [ "$(field "$WORK/waiting.out" rc)" = 0 ]+'++test_expect_success 'the wait for the operation lock ends with the lock still held' '+ command -v flock >/dev/null 2>&1 || exit 0;+ fresh held &&+ { flock "$WORK/held/lock" sleep 3 & holder=$!; } &&+ sleep 0.3 &&+ ! SHIRUDO_TEST_CONFIG_LOCK_WAIT_MS=500 "$H" "$WORK/held" boot A \+ >"$WORK/held.out" 2>"$WORK/held.err" &&+ [ "$(field "$WORK/held.out" rc)" = -11 ] &&+ grep -q "still holds the config operation lock" "$WORK/held.err" &&+ wait "$holder"+'+test_expect_success 'seal failures prevent trusted journal creation' 'mkdir -m 700 "$WORK/no-seal" &&! SHIRUDO_TEST_CONFIG_SEAL_FAIL=1 "$H" "$WORK/no-seal" boot A >/dev/null &&diff --git a/util/config_update.c b/util/config_update.cindex a82775a5..fddaddd7 100644--- a/util/config_update.c+++ b/util/config_update.c@@ -12,6 +12,7 @@#include <string.h>#include <sys/file.h>#include <sys/stat.h>+#include <time.h>#include <unistd.h>#include "chain.h"@@ -141,6 +142,33 @@ static int checkpoint(struct shirudo_config_update *update,0;}+/*+ * Every target command holds the lock for its whole run, and the keepalive+ * and map-export timers start one every few seconds, so a short wait lets+ * them take turns instead of failing each other.+ */+static int lock_wait(int fd, __u32 wait_ms)+{+ struct timespec pause = { .tv_nsec = 100 * 1000 * 1000 };+ bool told = false;+ __u32 waited = 0;+ int err;++ for (;;) {+ if (!flock(fd, LOCK_EX | LOCK_NB))+ return 0;+ err = errno;+ if (err != EWOULDBLOCK || waited >= wait_ms)+ return -err;+ if (!told && waited >= 1000) {+ pr_info("image: waiting for another shirudo command to release the config operation lock");+ told = true;+ }+ nanosleep(&pause, NULL);+ waited += 100;+ }+}+int shirudo_config_update_open(const char *directory,const struct shirudo_config_update_ops *ops,void *ctx, struct shirudo_config_update *out)@@ -185,8 +213,11 @@ int shirudo_config_update_open(const char *directory,rc = regular_file(out->lock_fd);if (rc)goto fail;- if (flock(out->lock_fd, LOCK_EX | LOCK_NB)) {- rc = -errno;+ rc = lock_wait(out->lock_fd, ops->lock_wait_ms);+ if (rc) {+ if (rc == -EWOULDBLOCK)+ pr_err("image: another shirudo command still holds the config operation lock %s/lock after %u seconds",+ directory, ops->lock_wait_ms / 1000);goto fail;}rc = ops->seal(ctx, out->lock_fd);diff --git a/util/config_update.h b/util/config_update.hindex 2ee75beb..78b4f3c0 100644--- a/util/config_update.h+++ b/util/config_update.h@@ -55,6 +55,8 @@ struct shirudo_config_update_ops {int (*seal)(void *ctx, int fd);int (*checkpoint)(void *ctx,enum shirudo_config_update_checkpoint checkpoint);+ /* How long to wait for another command's operation lock; 0 fails at once. */+ __u32 lock_wait_ms;};typedef int (*shirudo_config_cb)(const struct shirudo_config_update_record *r,diff --git a/util/image_runtime.c b/util/image_runtime.cindex 5a95c0b8..cc626f8d 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -640,6 +640,7 @@ static const struct shirudo_config_update_ops update_ops = {.prepare = prepare_update,.activate = activate_update,.seal = seal_fd,+ .lock_wait_ms = 30 * 1000,};static const struct shirudo_config_update_ops boot_ops = {