linux/tools/testing/selftests
Gianluca Borello 5722569bb9 bpf: allow helpers access to map element values
Enable helpers to directly access a map element value by passing a
register type PTR_TO_MAP_VALUE (or PTR_TO_MAP_VALUE_ADJ) to helper
arguments ARG_PTR_TO_STACK or ARG_PTR_TO_RAW_STACK.

This enables several use cases. For example, a typical tracing program
might want to capture pathnames passed to sys_open() with:

struct trace_data {
	char pathname[PATHLEN];
};

SEC("kprobe/sys_open")
void bpf_sys_open(struct pt_regs *ctx)
{
	struct trace_data data;
	bpf_probe_read(data.pathname, sizeof(data.pathname), ctx->di);

	/* consume data.pathname, for example via
	 * bpf_trace_printk() or bpf_perf_event_output()
	 */
}

Such a program could easily hit the stack limit in case PATHLEN needs to
be large or more local variables need to exist, both of which are quite
common scenarios. Allowing direct helper access to map element values,
one could do:

struct bpf_map_def SEC("maps") scratch_map = {
	.type = BPF_MAP_TYPE_PERCPU_ARRAY,
	.key_size = sizeof(u32),
	.value_size = sizeof(struct trace_data),
	.max_entries = 1,
};

SEC("kprobe/sys_open")
int bpf_sys_open(struct pt_regs *ctx)
{
	int id = 0;
	struct trace_data *p = bpf_map_lookup_elem(&scratch_map, &id);
	if (!p)
		return;
	bpf_probe_read(p->pathname, sizeof(p->pathname), ctx->di);

	/* consume p->pathname, for example via
	 * bpf_trace_printk() or bpf_perf_event_output()
	 */
}

And wouldn't risk exhausting the stack.

Code changes are loosely modeled after commit 6841de8b0d ("bpf: allow
helpers access the packet directly"). Unlike with PTR_TO_PACKET, these
changes just work with ARG_PTR_TO_STACK and ARG_PTR_TO_RAW_STACK (not
ARG_PTR_TO_MAP_KEY, ARG_PTR_TO_MAP_VALUE, ...): adding those would be
trivial, but since there is not currently a use case for that, it's
reasonable to limit the set of changes.

Also, add new tests to make sure accesses to map element values from
helpers never go out of boundary, even when adjusted.

Signed-off-by: Gianluca Borello <g.borello@gmail.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-01-09 16:56:26 -05:00
..
bpf bpf: allow helpers access to map element values 2017-01-09 16:56:26 -05:00
breakpoints selftests: arm64: add test for unaligned/inexact watchpoint handling 2016-11-18 17:26:15 +00:00
capabilities selftests/capabilities: clean up for Makefile 2015-11-23 13:20:10 -07:00
cpu-hotplug selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
drivers/gpu kselftests: Exercise hw-independent mock tests for i915.ko 2016-12-08 09:34:13 -07:00
efivarfs efi: Make efivarfs entries immutable by default 2016-02-10 16:25:52 +00:00
exec selftests/exec: Makefile is a run-time dependency, add it to the install list 2016-06-15 13:35:55 -06:00
filesystems selftests: move dnotify_test from Documentation/filesystems 2016-09-20 09:09:00 -06:00
firmware selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
ftrace selftests: ftrace: Shift down default message verbosity 2016-12-13 11:28:10 -05:00
futex docs: fix locations of several documents that got moved 2016-10-24 08:12:35 -02:00
gpio selftest/gpio: add gpio test case 2016-12-13 07:26:37 -07:00
ia64 selftests: move ia64 tests from Documentation/ia64 2016-09-20 09:58:12 -06:00
intel_pstate tools: testing: define the _GNU_SOURCE macro 2016-05-16 09:06:17 -06:00
ipc selftests: add missing .gitignore file or entry 2016-02-25 13:16:36 -07:00
kcmp
lib selftests/lib: set printf.sh executable 2016-06-27 13:17:14 -06:00
media_tests selftests: media_tests add a new video device test 2016-07-26 09:59:30 -06:00
membarrier
memfd selftests: Make scripts executable 2015-11-03 16:54:57 -07:00
memory-hotplug selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
mount selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
mqueue selftests: Add missing #include directives 2015-11-03 16:53:53 -07:00
net tools: psock_tpacket: block Rx until socket filter has been added and socket has been bound to loopback. 2017-01-05 15:03:41 -05:00
networking/timestamping selftests: Move networking/timestamping from Documentation 2016-09-20 09:59:50 -06:00
nsfs selftests: add missing gitignore files/dirs 2016-12-08 08:51:32 -07:00
ntb ntb_test: Add a selftest script for the NTB subsystem 2016-08-05 10:21:08 -04:00
powerpc selftests/powerpc: Add ptrace tests for TM SPR registers 2016-11-17 17:11:52 +11:00
prctl selftests: move prctl tests from Documentation/prctl 2016-09-20 09:09:09 -06:00
pstore selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
ptp selftests: move ptp tests from Documentation/ptp 2016-09-20 09:54:38 -06:00
ptrace add ptrace/.gitignore 2016-01-07 13:38:11 -07:00
rcutorture torture: Prevent jitter from delaying build-only runs 2016-11-14 10:48:59 -08:00
seccomp seccomp: add tests for ptrace hole 2016-06-14 10:54:38 -07:00
sigaltstack selftests: add missing gitignore files/dirs 2016-12-08 08:51:32 -07:00
size
static_keys selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
sync selftest: sync: improve assert() failure message 2016-12-13 07:24:34 -07:00
sysctl
timers linux-kselftest-4.10-rc1-update 2016-12-15 14:17:32 -08:00
user selftests: create test-specific kconfig fragments 2016-02-25 09:47:52 -07:00
vDSO selftests: move vDSO tests from Documentation/vDSO 2016-09-20 09:58:04 -06:00
vm selftests: expanding more mlock selftest 2016-10-07 18:46:28 -07:00
watchdog selftests: move watchdog tests from Documentation/watchdog 2016-09-20 09:58:34 -06:00
x86 selftests/x86: Add test_vdso to test getcpu() 2016-11-17 08:31:22 +01:00
zram selftests/zram: replace ZRAM_LZ4_COMPRESS 2016-09-20 09:00:01 -06:00
.gitignore selftests: add missing gitignore files/dirs 2016-12-08 08:51:32 -07:00
gen_kselftest_tar.sh
kselftest_install.sh
kselftest.h
lib.mk
Makefile linux-kselftest-4.10-rc1-update 2016-12-15 14:17:32 -08:00