net/flow_dissector: correctly cap nhoff and thoff in case of BPF
authorStanislav Fomichev <sdf@google.com>
Thu, 6 Dec 2018 04:40:48 +0000 (20:40 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 7 Dec 2018 21:38:29 +0000 (13:38 -0800)
We want to make sure that the following condition holds:
0 <= nhoff <= thoff <= skb->len

BPF program can set out-of-bounds nhoff and thoff, which is dangerous, see
recent commit d0c081b49137 ("flow_dissector: properly cap thoff field")'.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
net/core/flow_dissector.c

index ff5556d80570000e5d77e4140c743f32b4ace03d..af68207ee56c34324939e2a0ca0b0e6a87dee2fc 100644 (file)
@@ -791,9 +791,12 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
                /* Restore state */
                memcpy(cb, &cb_saved, sizeof(cb_saved));
 
+               flow_keys.nhoff = clamp_t(u16, flow_keys.nhoff, 0, skb->len);
+               flow_keys.thoff = clamp_t(u16, flow_keys.thoff,
+                                         flow_keys.nhoff, skb->len);
+
                __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
                                         target_container);
-               key_control->thoff = min_t(u16, key_control->thoff, skb->len);
                rcu_read_unlock();
                return result == BPF_OK;
        }