eBPF-based security platform
upatch: classify a failed pin open in one place
John Fastabend8464b3d04245
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rstindex 07067aad..b6d32fd3 100644--- a/Documentation/reference/failure-categories.rst+++ b/Documentation/reference/failure-categories.rst@@ -102,13 +102,14 @@ Categoriesconfig, see :doc:`/operate/maintain/keys`.* - ``tampered``- ``pre_flight``, ``tpm_extend``- - none, ``EPERM``, ``EACCES``+ - none, ``EPERM``, ``EACCES``, ``ENOENT``, ``ESTALE``- The ``.bpf`` keyring is not the frozen anchor (no errno), the- ``lsm_link`` pin cannot be opened, the register differs from+ ``lsm_link`` pin is missing (``ENOENT``), the register differs from``--expect-chain`` or cannot be read (``EPERM``), or the registeris not the one ``init`` bound (``EACCES``, the load is undone). From``upatch reconcile``: the archive of a loaded XDP upatch differs- from what the records say was loaded, or its program pin is missing.+ from what the records say was loaded, or its program pin is missing+ (``ENOENT``) or holds no XDP program (``ESTALE``).- Do not retry. Keep the eventlog and the journal and handle thetarget as an incident: :doc:`/operate/troubleshoot/incident-tampered`.* - ``not_initialized``@@ -183,10 +184,13 @@ Categories- Build the kernel with ``CONFIG_CRYPTO_MLDSA``, see:doc:`/operate/plan/system-requirements`.* - ``lsm_denied``- - ``target_verify``, ``signed_loader_load``, ``link_create``+ - ``pre_flight``, ``target_verify``, ``signed_loader_load``,+ ``link_create``- ``EACCES``, ``EPERM``- The kernel refused an operation with ``EACCES`` or ``EPERM``,- normally an LSM, shirudo's own or another one.+ normally an LSM, shirudo's own or another one. In ``pre_flight``+ it refused to open the ``lsm_link`` pin or, from ``upatch+ reconcile``, an XDP program pin.- Check the tamper counters in ``shirudo status`` and the otherLSMs' audit records.* - ``verifier_rejected``@@ -252,9 +256,9 @@ Categories- Any other failure: out of memory, a failed hash of the loader, anunclassified attach error, a payload that cannot be recorded on thepin directory or a failed archive copy (the load is undone for- both). From ``upatch reconcile``, an XDP program pin that cannot- be opened for a reason that says nothing about the pin, such as- ``EMFILE`` or ``ENOMEM``.+ both). In ``pre_flight``, the ``lsm_link`` pin or, from ``upatch+ reconcile``, an XDP program pin that cannot be opened for a reason+ that says nothing about the pin, such as ``EMFILE`` or ``ENOMEM``.- Read ``message`` and the journal. For ``persist``, check the spaceon the file system that holds ``/var/lib/shirudo``.diff --git a/builtin-upatch.c b/builtin-upatch.cindex e84e6e19..7dd57b65 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -113,6 +113,21 @@ static const char *cat_from_load_errno(int e)}}+/* Only a pin that is gone or holds another object says what the pin is. */+static const char *cat_from_pin_errno(int e)+{+ switch (e) {+ case EACCES:+ case EPERM:+ return "lsm_denied";+ case ENOENT:+ case ESTALE:+ return "tampered";+ default:+ return "unknown";+ }+}+static const char *flag_str(const struct upatch *u){return upatch_is_unload(u) ? "UNLOAD-directive" : "LOAD";@@ -2342,6 +2357,17 @@ int upatch_restore(const char *path)return __upatch_apply(path, false, NULL);}+static int preflight_lsm_pin_failed(int err, struct op_report *rep)+{+ const char *cat = cat_from_pin_errno(err);++ pr_err("upatch: pre-flight refused (category: %s%s): open %s: %s", cat,+ err == ENOENT ? ", message: lsm_detached" : "", LSM_LINK_PIN,+ strerror(err));+ rep_fail(rep, "pre_flight", cat, err, "cannot open the LSM link pin");+ return -1;+}+/** The tamper gate before any BPF_PROG_LOAD: a concurrent operator who* advanced the register, a detached LSM or a missing anchor surface@@ -2369,12 +2395,8 @@ static int upatch_preflight(const char *expect_chain,return -1;fd = bpf_obj_get(LSM_LINK_PIN);- if (fd < 0) {- pr_err("upatch load: pre-flight refused (category: tampered, "- "message: lsm_detached): %s not pinned",- LSM_LINK_PIN);- return -1;- }+ if (fd < 0)+ return preflight_lsm_pin_failed(errno, rep);close(fd);if (!expect_chain)@@ -4396,16 +4418,8 @@ static int reconcile_prog_failed(const char *name, int err,{pr_err("upatch reconcile: an XDP program of %s: %s", name,strerror(-err));- if (err == -EACCES || err == -EPERM)- rep_fail(rep, "pre_flight", "lsm_denied", err,- "the guards refused the XDP program pin");- /* Only these say what the pin is; EMFILE or ENOMEM say nothing. */- else if (err == -ENOENT || err == -ESTALE)- rep_fail(rep, "pre_flight", "tampered", err,- "an XDP program pin is missing or not XDP");- else- rep_fail(rep, "pre_flight", "unknown", err,- "cannot open an XDP program pin");+ rep_fail(rep, "pre_flight", cat_from_pin_errno(-err), err,+ "cannot open an XDP program pin");return 1;}