tools/testing/selftests/bpf/progs/verifier_sock.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/verifier_sock.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/verifier_sock.c
Extension
.c
Size
29794 bytes
Lines
1175
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

SEC("cgroup/sock_create")
__description("sk->src_port [word load]")
__failure __msg("invalid bpf_context access off=44 size=2")
__naked void sock_create_read_src_port(void)
{
	asm volatile ("					\
	r6 = r1;					\
	r7 = *(u16*)(r6 + %[bpf_sock_src_port]);	\
	r0 = 1;						\
	exit;						\
"	:
	: __imm_const(bpf_sock_src_port, offsetof(struct bpf_sock, src_port))
	: __clobber_all);
}

__noinline
long skb_pull_data2(struct __sk_buff *sk, __u32 len)
{
	return bpf_skb_pull_data(sk, len);
}

__noinline
long skb_pull_data1(struct __sk_buff *sk, __u32 len)
{
	return skb_pull_data2(sk, len);
}

/* global function calls bpf_skb_pull_data(), which invalidates packet
 * pointers established before global function call.
 */
SEC("tc")
__failure __msg("invalid mem access")
int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk)
{
	int *p = (void *)(long)sk->data;

	if ((void *)(p + 1) > (void *)(long)sk->data_end)
		return TCX_DROP;
	skb_pull_data1(sk, 0);
	*p = 42; /* this is unsafe */
	return TCX_PASS;
}

__noinline
long xdp_pull_data2(struct xdp_md *x, __u32 len)
{
	return bpf_xdp_pull_data(x, len);
}

__noinline
long xdp_pull_data1(struct xdp_md *x, __u32 len)
{
	return xdp_pull_data2(x, len);
}

/* global function calls bpf_xdp_pull_data(), which invalidates packet
 * pointers established before global function call.
 */
SEC("xdp")
__failure __msg("invalid mem access")
int invalidate_xdp_pkt_pointers_from_global_func(struct xdp_md *x)
{
	int *p = (void *)(long)x->data;

	if ((void *)(p + 1) > (void *)(long)x->data_end)
		return XDP_DROP;
	xdp_pull_data1(x, 0);
	*p = 42; /* this is unsafe */
	return XDP_PASS;
}

/* XDP packet changing kfunc calls invalidate packet pointers */
SEC("xdp")
__failure __msg("invalid mem access")
int invalidate_xdp_pkt_pointers(struct xdp_md *x)
{
	int *p = (void *)(long)x->data;

	if ((void *)(p + 1) > (void *)(long)x->data_end)
		return XDP_DROP;
	bpf_xdp_pull_data(x, 0);
	*p = 42; /* this is unsafe */
	return XDP_PASS;
}

__noinline
int tail_call(struct __sk_buff *sk)
{
	bpf_tail_call_static(sk, &jmp_table, 0);
	return 0;

Annotation

Implementation Notes