eBPF-based security platform
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
/*
* Copyright (c) 2026 Authors of shirudo
*
* CVE-2026-31431 precise mitigation. The algif_aead copy-failure path
* writes back into its source scatter-gather pages; the bug is reachable
* only when those pages are foreign ones aliased into the AF_ALG socket
* by a zero-copy splice (MSG_SPLICE_PAGES), never when the data was
* copied in by an ordinary sendmsg. This refuses exactly that: a
* MSG_SPLICE_PAGES send to an AF_ALG socket. Every copying use of
* AF_ALG -- aead, skcipher, hash and rng, encrypt and decrypt -- is left
* working, unlike the socket_create block that takes AF_ALG away
* wholesale.
*
* The op (encrypt/decrypt) and the algorithm type live in the crypto
* module's private af_alg_ctx, which carries no BTF, so a shield cannot
* read them without hard-coded offsets. The zero-copy precondition is in
* the generic struct sock and struct msghdr, which do, so this is the
* narrowest condition a CO-RE shield can enforce.
*/
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#define AF_ALG 38
#define EACCES 13
/* include/linux/socket.h: MSG_SPLICE_PAGES is BIT(27), kernel-internal. */
#define MSG_SPLICE_PAGES 0x8000000
char _license[] SEC("license") = "Dual BSD/GPL";
SEC("lsm/socket_sendmsg")
int BPF_PROG(cve_2026_31431_alg_splice, struct socket *sock,
struct msghdr *msg, int size)
{
if (BPF_CORE_READ(sock, sk, __sk_common.skc_family) != AF_ALG)
return 0;
if (BPF_CORE_READ(msg, msg_flags) & MSG_SPLICE_PAGES)
return -EACCES;
return 0;
}