bpf: pass sk to helper functions

BPF helper functions access socket fields through skb->sk. This is not
set in ingress cgroup and socket filters. The association is only made
in skb_set_owner_r once the filter has accepted the packet. Sk is
available as socket lookup has taken place.

Temporarily set skb->sk to sk in these cases.

Signed-off-by: Willem de Bruijn <willemb@google.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Willem de Bruijn 2017-04-11 14:08:08 -04:00 коммит произвёл David S. Miller
Родитель cb6bf9cfdb
Коммит 8f917bba00
2 изменённых файлов: 10 добавлений и 2 удалений

Просмотреть файл

@ -154,7 +154,7 @@ int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent,
/** /**
* __cgroup_bpf_run_filter_skb() - Run a program for packet filtering * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
* @sk: The socken sending or receiving traffic * @sk: The socket sending or receiving traffic
* @skb: The skb that is being sent or received * @skb: The skb that is being sent or received
* @type: The type of program to be exectuted * @type: The type of program to be exectuted
* *
@ -189,10 +189,13 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk,
prog = rcu_dereference(cgrp->bpf.effective[type]); prog = rcu_dereference(cgrp->bpf.effective[type]);
if (prog) { if (prog) {
unsigned int offset = skb->data - skb_network_header(skb); unsigned int offset = skb->data - skb_network_header(skb);
struct sock *save_sk = skb->sk;
skb->sk = sk;
__skb_push(skb, offset); __skb_push(skb, offset);
ret = bpf_prog_run_save_cb(prog, skb) == 1 ? 0 : -EPERM; ret = bpf_prog_run_save_cb(prog, skb) == 1 ? 0 : -EPERM;
__skb_pull(skb, offset); __skb_pull(skb, offset);
skb->sk = save_sk;
} }
rcu_read_unlock(); rcu_read_unlock();

Просмотреть файл

@ -92,8 +92,13 @@ int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
rcu_read_lock(); rcu_read_lock();
filter = rcu_dereference(sk->sk_filter); filter = rcu_dereference(sk->sk_filter);
if (filter) { if (filter) {
unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb); struct sock *save_sk = skb->sk;
unsigned int pkt_len;
skb->sk = sk;
pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM; err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
skb->sk = save_sk;
} }
rcu_read_unlock(); rcu_read_unlock();