Skip to content

eBPF-based security platform

upatches/kernel-cve: add a CVE-2026-31431 copy-failure detector

John Fastabend05cc7c8081f8
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index c19615b8..d49791de 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -313,6 +313,13 @@ signed config image.
``BROKEN``), is refused (``BLOCKED``) while the mitigation is
loaded with no ``AF_UNIX`` socket refused, and reaches the path
again after the unload
+ * - ``t3117-copyfail-detector``
+ - The CVE-2026-31431 copy-failure detector of ``upatches/kernel-cve``,
+ built from its shipped source: ``socket_sendmsg`` marks an ``AF_ALG``
+ socket whose send carried a zero-copy splice, ``aead_recvmsg`` counts
+ every op as ``activity`` and, for a marked socket whose op failed, a
+ ``hit``. A spliced failing decrypt moves both counters; an ordinary
+ copying failing decrypt moves only ``activity``; the unload removes it
``tpm`` (4)
~~~~~~~~~~~
diff --git a/testing/functional/upatch/t3117-copyfail-detector.sh b/testing/functional/upatch/t3117-copyfail-detector.sh
new file mode 100755
index 00000000..8dbfdea3
--- /dev/null
+++ b/testing/functional/upatch/t3117-copyfail-detector.sh
@@ -0,0 +1,171 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t3117 -- the CVE-2026-31431 copy-failure detector of upatches/kernel-cve.
+# socket_sendmsg marks an AF_ALG socket whose send carried a zero-copy
+# splice; aead_recvmsg counts every op as the activity denominator and, for
+# a marked socket whose op failed, a hit: the foreign-page writeback. A
+# spliced failing decrypt moves hits; an ordinary copying failing decrypt
+# moves only activity, so the detector separates the two.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+NAME=cve-2026-31431-copyfail-detector
+LOAD="$FIXTURES/$NAME.upatch"
+UNLOAD="$FIXTURES/$NAME.unload.upatch"
+PROBE="$FIXTURES/cve-2026-31431-splice-probe.py"
+PIN=/run/shirudo/bpffs/upatches/$NAME/link_0
+PIN1=/run/shirudo/bpffs/upatches/$NAME/link_1
+WORK=$(mktemp -d)
+
+cleanup() {
+ if [ -e "$PIN" ]; then
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck \
+ >/dev/null 2>&1 || true
+ fi
+ rm -rf "$WORK"
+}
+trap cleanup EXIT
+
+[ -s "$LOAD" ] || test_skip_all "missing copyfail-detector fixture"
+[ -s "$UNLOAD" ] || test_skip_all "missing copyfail-detector unload fixture"
+[ -s "$PROBE" ] || test_skip_all "missing CVE-2026-31431 splice probe"
+
+# The probe reaches a zero-copy splice into an AF_ALG aead socket only
+# where the kernel offers that path; without it there is nothing to count.
+[ "$(python3 "$PROBE" 2>/dev/null </dev/null)" = SPLICE_OK ] ||
+ test_skip_all "kernel has no AF_ALG aead splice path to exercise"
+
+# Drive one failing aead decrypt, its ciphertext delivered either by a
+# zero-copy splice (foreign pages, the detector's hit) or by an ordinary
+# copying send (the activity denominator only).
+cat >"$WORK/drive.py" <<'PY'
+import os
+import socket
+import struct
+import sys
+
+AF_ALG = getattr(socket, "AF_ALG", 38)
+SOL_ALG = 279
+ALG_SET_KEY = 1
+ALG_SET_IV = 2
+ALG_SET_OP = 3
+ALG_SET_AEAD_ASSOCLEN = 4
+ALG_SET_AEAD_AUTHSIZE = 5
+ALG_OP_DECRYPT = 0
+
+
+def authenc_key(authkey, enckey):
+ # struct crypto_authenc_key_param in an rtattr, then the two keys.
+ param = struct.pack(">I", len(enckey))
+ rta_len = 4 + len(param)
+ rtattr = struct.pack("HH", rta_len, 1) + param
+ rtattr += b"\x00" * ((-rta_len) % 4)
+ return rtattr + authkey + enckey
+
+
+try:
+ with open("/proc/sys/crypto/af_alg_restrict", "w") as f:
+ f.write("0")
+except OSError:
+ pass
+
+try:
+ alg = socket.socket(AF_ALG, socket.SOCK_SEQPACKET, 0)
+except OSError:
+ print("UNSUPPORTED")
+ sys.exit(0)
+with alg:
+ try:
+ alg.bind(("aead", "authencesn(hmac(sha256),cbc(aes))"))
+ alg.setsockopt(SOL_ALG, ALG_SET_AEAD_AUTHSIZE, None, 16)
+ alg.setsockopt(SOL_ALG, ALG_SET_KEY,
+ authenc_key(b"\x00" * 32, b"\x00" * 16))
+ op, _ = alg.accept()
+ except OSError:
+ print("UNSUPPORTED")
+ sys.exit(0)
+ with op:
+ aad = b"\x00" * 16
+ try:
+ op.sendmsg(
+ [aad],
+ [(SOL_ALG, ALG_SET_OP, struct.pack("I", ALG_OP_DECRYPT)),
+ (SOL_ALG, ALG_SET_IV, struct.pack("I", 16) + b"\x00" * 16),
+ (SOL_ALG, ALG_SET_AEAD_ASSOCLEN, struct.pack("I", len(aad)))],
+ socket.MSG_MORE,
+ )
+ except OSError:
+ print("UNSUPPORTED")
+ sys.exit(0)
+ if sys.argv[1] == "splice":
+ r_pipe, w_pipe = os.pipe()
+ try:
+ os.write(w_pipe, b"\x00" * 32)
+ try:
+ os.splice(r_pipe, op.fileno(), 32)
+ except (OSError, AttributeError):
+ print("UNSUPPORTED")
+ sys.exit(0)
+ finally:
+ os.close(r_pipe)
+ os.close(w_pipe)
+ else:
+ try:
+ op.sendmsg([b"\x00" * 32])
+ except OSError:
+ print("UNSUPPORTED")
+ sys.exit(0)
+ try:
+ op.recv(4096)
+ except OSError:
+ pass
+ print("OK")
+PY
+
+# The per-cpu counters are summed across CPUs under "sum" in the map JSON.
+read_count() {
+ "$SHIRUDO_BIN" upatch get "$NAME" --json |
+ python3 -c 'import json, sys
+doc = json.load(sys.stdin)
+m = next(x for x in doc["upatch"]["maps"] if x["name"] == sys.argv[1])
+print(int(m["entries"][0]["sum"], 0))' "$1"
+}
+
+test_expect_success 'the detector loads with both of its programs pinned' '
+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&
+ [ -e "$PIN" ] &&
+ [ -e "$PIN1" ]
+'
+
+test_expect_success 'a spliced failing decrypt moves hits and activity' '
+ hits_before=$(read_count hits) &&
+ activity_before=$(read_count activity) &&
+ [ "$(python3 "$WORK/drive.py" splice)" = OK ] &&
+ hits_after=$(read_count hits) &&
+ activity_after=$(read_count activity) &&
+ [ "$hits_after" -gt "$hits_before" ] &&
+ [ "$activity_after" -gt "$activity_before" ]
+'
+
+test_expect_success 'an ordinary copying failing decrypt moves only activity' '
+ hits_before=$(read_count hits) &&
+ activity_before=$(read_count activity) &&
+ [ "$(python3 "$WORK/drive.py" copy)" = OK ] &&
+ hits_after=$(read_count hits) &&
+ activity_after=$(read_count activity) &&
+ [ "$hits_after" = "$hits_before" ] &&
+ [ "$activity_after" -gt "$activity_before" ]
+'
+
+test_expect_success 'the detector unloads' '
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&
+ [ ! -e "$PIN" ]
+'
+
+test_done
diff --git a/testing/vm/build-bundle-payload.sh b/testing/vm/build-bundle-payload.sh
index 3639663d..a6bd2dde 100755
--- a/testing/vm/build-bundle-payload.sh
+++ b/testing/vm/build-bundle-payload.sh
@@ -754,6 +754,24 @@ build_alg_splice_block_fixture()
)
}
+build_copyfail_detector_fixture()
+{
+ local source_dir="$REPO_ROOT/upatches/kernel-cve/sources/cve-2026-31431-crypto-algif-aead-copy-fail"
+ local keys_dir="$1"
+
+ (
+ cd "$REPO_ROOT"
+ "$REPO_ROOT/tools/shirudo-build/shirudo-build" \
+ "$source_dir/observability/cve-2026-31431-copyfail-detector.bpf.c" \
+ -o "$TMP_DIR/fixtures/cve-2026-31431-copyfail-detector.upatch" \
+ --name cve-2026-31431-copyfail-detector \
+ --vmlinux "$REPO_ROOT/vendor/linux/vmlinux.h" \
+ --extra-cflags "-I$REPO_ROOT/vendor/libbpf" \
+ --key "$keys_dir/signing_key.pem" \
+ --cert "$keys_dir/cert.der"
+ )
+}
+
build_policy_labels_fixture()
{
local source_dir="$FIXTURE_WORK/policy-labels"
@@ -850,6 +868,7 @@ queue_fixture build_policy_exec_graph_fixture
queue_fixture build_obs_fixture "$FIXTURE_WORK/trusted"
queue_fixture build_af_alg_block_fixture "$FIXTURE_WORK/trusted"
queue_fixture build_alg_splice_block_fixture "$FIXTURE_WORK/trusted"
+queue_fixture build_copyfail_detector_fixture "$FIXTURE_WORK/trusted"
queue_fixture build_java_fixture
wait_fixture_jobs
@@ -904,6 +923,9 @@ af_alg_block_payload="$($REPO_ROOT/shirudo upatch info \
alg_splice_block_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/cve-2026-31431-alg-splice-block.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+copyfail_detector_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/cve-2026-31431-copyfail-detector.upatch" --json |
+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
observability_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/kernel-obs.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
@@ -961,6 +983,7 @@ shirudo-test-arena-tree $arena_tree_payload shirudo-test-arena-tree.unload.upatc
kernel-obs $observability_payload kernel-obs.unload.upatch
cve-2026-31431-af-alg-block $af_alg_block_payload cve-2026-31431-af-alg-block.unload.upatch
cve-2026-31431-alg-splice-block $alg_splice_block_payload cve-2026-31431-alg-splice-block.unload.upatch
+cve-2026-31431-copyfail-detector $copyfail_detector_payload cve-2026-31431-copyfail-detector.unload.upatch
kernel-obs $observability_anomaly_payload kernel-obs-cred-anomaly.unload.upatch
shirudo-test-uprobe-multi $uprobe_payload shirudo-test-uprobe-multi.unload.upatch
shirudo-test-uretprobe-multi $uretprobe_payload shirudo-test-uretprobe-multi.unload.upatch
diff --git a/upatches/kernel-cve/sources/cve-2026-31431-crypto-algif-aead-copy-fail/observability/cve-2026-31431-copyfail-detector.bpf.c b/upatches/kernel-cve/sources/cve-2026-31431-crypto-algif-aead-copy-fail/observability/cve-2026-31431-copyfail-detector.bpf.c
new file mode 100644
index 00000000..6b17c87e
--- /dev/null
+++ b/upatches/kernel-cve/sources/cve-2026-31431-crypto-algif-aead-copy-fail/observability/cve-2026-31431-copyfail-detector.bpf.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * CVE-2026-31431 source-derived detector. The algif_aead copy-failure
+ * path scrubs its source scatter-gather pages, which corrupts memory it
+ * does not own only when a zero-copy splice aliased foreign pages into
+ * the AF_ALG socket. socket_sendmsg marks a socket whose send carried
+ * MSG_SPLICE_PAGES; aead_recvmsg counts those marked sockets whose op
+ * then failed, the exact foreign-page writeback. The op and algorithm
+ * live in the BTF-less af_alg_ctx, so the splice mark carried through
+ * the socket's own storage is the signal a CO-RE detector can read.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+#define AF_ALG 38
+/* include/linux/socket.h: MSG_SPLICE_PAGES is BIT(27), kernel-internal. */
+#define MSG_SPLICE_PAGES 0x8000000
+
+/*
+ * One byte per AF_ALG socket: set when a MSG_SPLICE_PAGES send aliased
+ * foreign pages into its send list, cleared when the op consumes them.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_SK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, __u8);
+} spliced SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} hits SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} activity SEC(".maps");
+
+static __always_inline void count_event(void *map)
+{
+ __u32 zero = 0;
+ __u64 *count = bpf_map_lookup_elem(map, &zero);
+
+ if (count)
+ (*count)++;
+}
+
+/*
+ * The send side runs before the crypto op, where the foreign-page
+ * precondition is still legible: a splice into an AF_ALG socket is the
+ * only way a page the socket does not own reaches its send list. The
+ * mark is carried to the recvmsg side rather than acted on here.
+ */
+SEC("lsm/socket_sendmsg")
+int BPF_PROG(cve_2026_31431_mark, struct socket *sock, struct msghdr *msg,
+ int size)
+{
+ struct sock *sk = sock->sk;
+ __u8 *mark;
+
+ if (!sk || sk->__sk_common.skc_family != AF_ALG)
+ return 0;
+ if (!(msg->msg_flags & MSG_SPLICE_PAGES))
+ return 0;
+ mark = bpf_sk_storage_get(&spliced, sk, NULL,
+ BPF_SK_STORAGE_GET_F_CREATE);
+ if (mark)
+ *mark = 1;
+ return 0;
+}
+
+/*
+ * aead_recvmsg runs and consumes the send list under the socket lock, so
+ * its mark cannot change under this hook. activity counts every aead op
+ * as the exposure denominator; a hit is a marked socket whose op failed,
+ * the copy-failure path writing back into foreign pages. fexit sees the
+ * error return, not which cleanup branch ran, so a hit over-approximates
+ * to any error on a splice-fed op, which is the narrowest the generic
+ * return value allows.
+ */
+SEC("fexit/aead_recvmsg")
+int BPF_PROG(cve_2026_31431_copyfail, struct socket *sock, struct msghdr *msg,
+ __u64 ignored, int flags, int ret)
+{
+ struct sock *sk = sock->sk;
+ __u8 *mark;
+
+ count_event(&activity);
+ if (!sk)
+ return 0;
+ mark = bpf_sk_storage_get(&spliced, sk, NULL, 0);
+ if (mark && *mark) {
+ if (ret < 0)
+ count_event(&hits);
+ *mark = 0;
+ }
+ return 0;
+}