bpf: permits narrower load from bpf program context fields
commit31fd85816dbe3a714bcc3f67c17c3dd87011f79e
authorYonghong Song <yhs@fb.com>
Tue, 13 Jun 2017 22:52:13 +0000 (13 15:52 -0700)
committerDavid S. Miller <davem@davemloft.net>
Wed, 14 Jun 2017 18:56:25 +0000 (14 14:56 -0400)
treed8c694e4997605254ea96a76c5d633f60ee091cf
parenta88e2676a6cd3352c2f590f872233d83d8db289c
bpf: permits narrower load from bpf program context fields

Currently, verifier will reject a program if it contains an
narrower load from the bpf context structure. For example,
        __u8 h = __sk_buff->hash, or
        __u16 p = __sk_buff->protocol
        __u32 sample_period = bpf_perf_event_data->sample_period
which are narrower loads of 4-byte or 8-byte field.

This patch solves the issue by:
  . Introduce a new parameter ctx_field_size to carry the
    field size of narrower load from prog type
    specific *__is_valid_access validator back to verifier.
  . The non-zero ctx_field_size for a memory access indicates
    (1). underlying prog type specific convert_ctx_accesses
         supporting non-whole-field access
    (2). the current insn is a narrower or whole field access.
  . In verifier, for such loads where load memory size is
    less than ctx_field_size, verifier transforms it
    to a full field load followed by proper masking.
  . Currently, __sk_buff and bpf_perf_event_data->sample_period
    are supporting narrowing loads.
  . Narrower stores are still not allowed as typical ctx stores
    are just normal stores.

Because of this change, some tests in verifier will fail and
these tests are removed. As a bonus, rename some out of bound
__sk_buff->cb access to proper field name and remove two
redundant "skb cb oob" tests.

Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/bpf.h
include/linux/bpf_verifier.h
kernel/bpf/verifier.c
kernel/trace/bpf_trace.c
net/core/filter.c
tools/testing/selftests/bpf/test_verifier.c