remarkable-linux/tools/testing/selftests
Gianluca Borello 06c1c04972 bpf: allow helpers access to variable memory
Currently, helpers that read and write from/to the stack can do so using
a pair of arguments of type ARG_PTR_TO_STACK and ARG_CONST_STACK_SIZE.
ARG_CONST_STACK_SIZE accepts a constant register of type CONST_IMM, so
that the verifier can safely check the memory access. However, requiring
the argument to be a constant can be limiting in some circumstances.

Since the current logic keeps track of the minimum and maximum value of
a register throughout the simulated execution, ARG_CONST_STACK_SIZE can
be changed to also accept an UNKNOWN_VALUE register in case its
boundaries have been set and the range doesn't cause invalid memory
accesses.

One common situation when this is useful:

int len;
char buf[BUFSIZE]; /* BUFSIZE is 128 */

if (some_condition)
	len = 42;
else
	len = 84;

some_helper(..., buf, len & (BUFSIZE - 1));

The compiler can often decide to assign the constant values 42 or 48
into a variable on the stack, instead of keeping it in a register. When
the variable is then read back from stack into the register in order to
be passed to the helper, the verifier will not be able to recognize the
register as constant (the verifier is not currently tracking all
constant writes into memory), and the program won't be valid.

However, by allowing the helper to accept an UNKNOWN_VALUE register,
this program will work because the bitwise AND operation will set the
range of possible values for the UNKNOWN_VALUE register to [0, BUFSIZE),
so the verifier can guarantee the helper call will be safe (assuming the
argument is of type ARG_CONST_STACK_SIZE_OR_ZERO, otherwise one more
check against 0 would be needed). Custom ranges can be set not only with
ALU operations, but also by explicitly comparing the UNKNOWN_VALUE
register with constants.

Another very common example happens when intercepting system call
arguments and accessing user-provided data of variable size using
bpf_probe_read(). One can load at runtime the user-provided length in an
UNKNOWN_VALUE register, and then read that exact amount of data up to a
compile-time determined limit in order to fit into the proper local
storage allocated on the stack, without having to guess a suboptimal
access size at compile time.

Also, in case the helpers accepting the UNKNOWN_VALUE register operate
in raw mode, disable the raw mode so that the program is required to
initialize all memory, since there is no guarantee the helper will fill
it completely, leaving possibilities for data leak (just relevant when
the memory used by the helper is the stack, not when using a pointer to
map element value or packet). In other words, ARG_PTR_TO_RAW_STACK will
be treated as ARG_PTR_TO_STACK.

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:27 -05:00
..
bpf bpf: allow helpers access to variable memory 2017-01-09 16:56:27 -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 selftests: Set CC using CROSS_COMPILE once in lib.mk 2015-03-19 15:16:51 -06:00
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 membarrier: clean up selftest 2015-09-22 15:09:53 -07:00
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 selftests: Set CC using CROSS_COMPILE once in lib.mk 2015-03-19 15:16:51 -06:00
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 selftests: Add install target 2015-03-13 15:21:56 -06:00
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 selftests: Add tool to generate kselftest tar archive 2015-03-24 08:43:19 -06:00
kselftest.h kselftest: Add exit code defines 2015-05-26 15:58:08 -06:00
kselftest_install.sh selftests: Add kselftest install tool 2015-03-24 08:43:05 -06:00
lib.mk selftests: change install command to rsync 2015-09-14 16:43:51 -06:00
Makefile linux-kselftest-4.10-rc1-update 2016-12-15 14:17:32 -08:00