bpf: sk_msg, improve offset chk in _is_valid_access
authorJohn Fastabend <john.fastabend@gmail.com>
Thu, 20 Dec 2018 19:35:30 +0000 (11:35 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 20 Dec 2018 22:47:08 +0000 (23:47 +0100)
commitbc1b4f013b5029b4c8b63fb9ba8d084119486d7b
tree6489e3b3f26160d6154037e3e1d1bef77006460b
parent9ee79a65d176e5815877aa148acc956e4cc90c53
bpf: sk_msg, improve offset chk in _is_valid_access

The check for max offset in sk_msg_is_valid_access uses sizeof()
which is incorrect because it would allow accessing possibly
past the end of the struct in the padded case. Further, it doesn't
preclude accessing any padding that may be added in the middle of
a struct. All told this makes it fragile to rely on.

To fix this explicitly check offsets with fields using the
bpf_ctx_range() and bpf_ctx_range_till() macros.

For reference the current structure layout looks as follows (reported
by pahole)

struct sk_msg_md {
union {
void *             data;                 /*           8 */
};                                               /*     0     8 */
union {
void *             data_end;             /*           8 */
};                                               /*     8     8 */
__u32                      family;               /*    16     4 */
__u32                      remote_ip4;           /*    20     4 */
__u32                      local_ip4;            /*    24     4 */
__u32                      remote_ip6[4];        /*    28    16 */
__u32                      local_ip6[4];         /*    44    16 */
__u32                      remote_port;          /*    60     4 */
/* --- cacheline 1 boundary (64 bytes) --- */
__u32                      local_port;           /*    64     4 */
__u32                      size;                 /*    68     4 */

/* size: 72, cachelines: 2, members: 10 */
/* last cacheline: 8 bytes */
};

So there should be no padding at the moment but fixing this now
prevents future errors.

Reported-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
net/core/filter.c