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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/verifier_helper_access_var_len.c
Extension
.c
Size
20811 bytes
Lines
826
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

struct test_val {
	unsigned int index;
	int foo[MAX_ENTRIES];
};

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__uint(max_entries, 1);
	__type(key, long long);
	__type(value, struct test_val);
} map_hash_48b SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__uint(max_entries, 1);
	__type(key, long long);
	__type(value, long long);
} map_hash_8b SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_RINGBUF);
	__uint(max_entries, 4096);
} map_ringbuf SEC(".maps");

SEC("tracepoint")
__description("helper access to variable memory: stack, bitwise AND + JMP, correct bounds")
__success
__naked void bitwise_and_jmp_correct_bounds(void)
{
	asm volatile ("					\
	r1 = r10;					\
	r1 += -64;					\
	r0 = 0;						\
	*(u64*)(r10 - 64) = r0;				\
	*(u64*)(r10 - 56) = r0;				\
	*(u64*)(r10 - 48) = r0;				\
	*(u64*)(r10 - 40) = r0;				\
	*(u64*)(r10 - 32) = r0;				\
	*(u64*)(r10 - 24) = r0;				\
	*(u64*)(r10 - 16) = r0;				\
	*(u64*)(r10 - 8) = r0;				\
	r2 = 16;					\
	*(u64*)(r1 - 128) = r2;				\
	r2 = *(u64*)(r1 - 128);				\
	r2 &= 64;					\
	r4 = 0;						\
	if r4 >= r2 goto l0_%=;				\
	r3 = 0;						\
	call %[bpf_probe_read_kernel];			\
l0_%=:	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_probe_read_kernel)
	: __clobber_all);
}

SEC("socket")
__description("helper access to variable memory: stack, bitwise AND, zero included")
/* in privileged mode reads from uninitialized stack locations are permitted */
__success __failure_unpriv
__msg_unpriv("invalid read from stack R2 off -64+0 size 64")
__retval(0)
__naked void stack_bitwise_and_zero_included(void)
{
	asm volatile ("					\
	/* set max stack size */			\
	r6 = 0;						\
	*(u64*)(r10 - 128) = r6;			\
	/* set r3 to a random value */			\
	call %[bpf_get_prandom_u32];			\
	r3 = r0;					\
	/* use bitwise AND to limit r3 range to [0, 64] */\
	r3 &= 64;					\
	r1 = %[map_ringbuf] ll;				\
	r2 = r10;					\
	r2 += -64;					\
	r4 = 0;						\
	/* Call bpf_ringbuf_output(), it is one of a few helper functions with\
	 * ARG_CONST_SIZE_OR_ZERO parameter allowed in unpriv mode.\
	 * For unpriv this should signal an error, because memory at &fp[-64] is\
	 * not initialized.				\
	 */						\
	call %[bpf_ringbuf_output];			\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32),
	  __imm(bpf_ringbuf_output),
	  __imm_addr(map_ringbuf)
	: __clobber_all);
}

Annotation

Implementation Notes