Skip to content

eBPF-based security platform

3.1 KB
Raw Blame
// 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;
}