Commit 46f8bc9
bpf: Add a bpf_sock pointer to __sk_buff and a bpf_sk_fullsock helper
In kernel, it is common to check "skb->sk && sk_fullsock(skb->sk)"
before accessing the fields in sock. For example, in __netdev_pick_tx:
static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
struct net_device *sb_dev)
{
/* ... */
struct sock *sk = skb->sk;
if (queue_index != new_index && sk &&
sk_fullsock(sk) &&
rcu_access_pointer(sk->sk_dst_cache))
sk_tx_queue_set(sk, new_index);
/* ... */
return queue_index;
}
This patch adds a "struct bpf_sock *sk" pointer to the "struct __sk_buff"
where a few of the convert_ctx_access() in filter.c has already been
accessing the skb->sk sock_common's fields,
e.g. sock_ops_convert_ctx_access().
"__sk_buff->sk" is a PTR_TO_SOCK_COMMON_OR_NULL in the verifier.
Some of the fileds in "bpf_sock" will not be directly
accessible through the "__sk_buff->sk" pointer. It is limited
by the new "bpf_sock_common_is_valid_access()".
e.g. The existing "type", "protocol", "mark" and "priority" in bpf_sock
are not allowed.
The newly added "struct bpf_sock *bpf_sk_fullsock(struct bpf_sock *sk)"
can be used to get a sk with all accessible fields in "bpf_sock".
This helper is added to both cg_skb and sched_(cls|act).
int cg_skb_foo(struct __sk_buff *skb) {
struct bpf_sock *sk;
sk = skb->sk;
if (!sk)
return 1;
sk = bpf_sk_fullsock(sk);
if (!sk)
return 1;
if (sk->family != AF_INET6 || sk->protocol != IPPROTO_TCP)
return 1;
/* some_traffic_shaping(); */
return 1;
}
(1) The sk is read only
(2) There is no new "struct bpf_sock_common" introduced.
(3) Future kernel sock's members could be added to bpf_sock only
instead of repeatedly adding at multiple places like currently
in bpf_sock_ops_md, bpf_sock_addr_md, sk_reuseport_md...etc.
(4) After "sk = skb->sk", the reg holding sk is in type
PTR_TO_SOCK_COMMON_OR_NULL.
(5) After bpf_sk_fullsock(), the return type will be in type
PTR_TO_SOCKET_OR_NULL which is the same as the return type of
bpf_sk_lookup_xxx().
However, bpf_sk_fullsock() does not take refcnt. The
acquire_reference_state() is only depending on the return type now.
To avoid it, a new is_acquire_function() is checked before calling
acquire_reference_state().
(6) The WARN_ON in "release_reference_state()" is no longer an
internal verifier bug.
When reg->id is not found in state->refs[], it means the
bpf_prog does something wrong like
"bpf_sk_release(bpf_sk_fullsock(skb->sk))" where reference has
never been acquired by calling "bpf_sk_fullsock(skb->sk)".
A -EINVAL and a verbose are done instead of WARN_ON. A test is
added to the test_verifier in a later patch.
Since the WARN_ON in "release_reference_state()" is no longer
needed, "__release_reference_state()" is folded into
"release_reference_state()" also.
Acked-by: Alexei Starovoitov <[email protected]>
Signed-off-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>1 parent 5f45664 commit 46f8bc9
4 files changed
Lines changed: 157 additions & 41 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
197 | 198 | | |
198 | 199 | | |
199 | 200 | | |
| |||
256 | 257 | | |
257 | 258 | | |
258 | 259 | | |
| 260 | + | |
| 261 | + | |
259 | 262 | | |
260 | 263 | | |
261 | 264 | | |
| |||
920 | 923 | | |
921 | 924 | | |
922 | 925 | | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
923 | 929 | | |
924 | 930 | | |
925 | 931 | | |
| |||
928 | 934 | | |
929 | 935 | | |
930 | 936 | | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
931 | 943 | | |
932 | 944 | | |
933 | 945 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2329 | 2329 | | |
2330 | 2330 | | |
2331 | 2331 | | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
2332 | 2340 | | |
2333 | 2341 | | |
2334 | 2342 | | |
| |||
2425 | 2433 | | |
2426 | 2434 | | |
2427 | 2435 | | |
2428 | | - | |
| 2436 | + | |
| 2437 | + | |
2429 | 2438 | | |
2430 | 2439 | | |
2431 | 2440 | | |
| |||
2545 | 2554 | | |
2546 | 2555 | | |
2547 | 2556 | | |
| 2557 | + | |
2548 | 2558 | | |
2549 | 2559 | | |
2550 | 2560 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
334 | 340 | | |
335 | 341 | | |
336 | 342 | | |
337 | | - | |
| 343 | + | |
| 344 | + | |
338 | 345 | | |
339 | 346 | | |
340 | 347 | | |
| |||
377 | 384 | | |
378 | 385 | | |
379 | 386 | | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
380 | 393 | | |
381 | 394 | | |
382 | 395 | | |
| |||
392 | 405 | | |
393 | 406 | | |
394 | 407 | | |
| 408 | + | |
| 409 | + | |
395 | 410 | | |
396 | 411 | | |
397 | 412 | | |
| |||
618 | 633 | | |
619 | 634 | | |
620 | 635 | | |
621 | | - | |
| 636 | + | |
622 | 637 | | |
623 | 638 | | |
624 | 639 | | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | 640 | | |
629 | 641 | | |
630 | 642 | | |
| |||
636 | 648 | | |
637 | 649 | | |
638 | 650 | | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | | - | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
647 | | - | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
| 651 | + | |
654 | 652 | | |
655 | 653 | | |
656 | 654 | | |
| |||
1209 | 1207 | | |
1210 | 1208 | | |
1211 | 1209 | | |
| 1210 | + | |
| 1211 | + | |
1212 | 1212 | | |
1213 | 1213 | | |
1214 | 1214 | | |
| |||
1647 | 1647 | | |
1648 | 1648 | | |
1649 | 1649 | | |
| 1650 | + | |
1650 | 1651 | | |
1651 | 1652 | | |
1652 | 1653 | | |
1653 | 1654 | | |
1654 | 1655 | | |
1655 | 1656 | | |
1656 | 1657 | | |
1657 | | - | |
1658 | | - | |
1659 | | - | |
1660 | | - | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
| 1662 | + | |
| 1663 | + | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
1661 | 1667 | | |
1662 | 1668 | | |
1663 | | - | |
1664 | 1669 | | |
1665 | | - | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
1666 | 1680 | | |
1667 | 1681 | | |
1668 | 1682 | | |
| |||
1688 | 1702 | | |
1689 | 1703 | | |
1690 | 1704 | | |
1691 | | - | |
1692 | | - | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
1693 | 1713 | | |
1694 | 1714 | | |
1695 | 1715 | | |
| |||
1800 | 1820 | | |
1801 | 1821 | | |
1802 | 1822 | | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
1803 | 1826 | | |
1804 | 1827 | | |
1805 | 1828 | | |
| |||
2003 | 2026 | | |
2004 | 2027 | | |
2005 | 2028 | | |
2006 | | - | |
| 2029 | + | |
2007 | 2030 | | |
2008 | | - | |
| 2031 | + | |
2009 | 2032 | | |
2010 | 2033 | | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
2011 | 2037 | | |
2012 | 2038 | | |
2013 | 2039 | | |
| |||
2053 | 2079 | | |
2054 | 2080 | | |
2055 | 2081 | | |
2056 | | - | |
| 2082 | + | |
2057 | 2083 | | |
2058 | | - | |
| 2084 | + | |
| 2085 | + | |
2059 | 2086 | | |
2060 | 2087 | | |
2061 | 2088 | | |
| |||
2102 | 2129 | | |
2103 | 2130 | | |
2104 | 2131 | | |
2105 | | - | |
| 2132 | + | |
| 2133 | + | |
2106 | 2134 | | |
2107 | 2135 | | |
2108 | 2136 | | |
| |||
2369 | 2397 | | |
2370 | 2398 | | |
2371 | 2399 | | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
2372 | 2405 | | |
2373 | 2406 | | |
2374 | 2407 | | |
| |||
2783 | 2816 | | |
2784 | 2817 | | |
2785 | 2818 | | |
2786 | | - | |
| 2819 | + | |
2787 | 2820 | | |
2788 | 2821 | | |
2789 | 2822 | | |
| |||
3049 | 3082 | | |
3050 | 3083 | | |
3051 | 3084 | | |
3052 | | - | |
| 3085 | + | |
| 3086 | + | |
| 3087 | + | |
3053 | 3088 | | |
| 3089 | + | |
3054 | 3090 | | |
3055 | 3091 | | |
3056 | 3092 | | |
| |||
3099 | 3135 | | |
3100 | 3136 | | |
3101 | 3137 | | |
3102 | | - | |
3103 | | - | |
3104 | | - | |
3105 | 3138 | | |
3106 | 3139 | | |
3107 | | - | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
| 3143 | + | |
| 3144 | + | |
| 3145 | + | |
| 3146 | + | |
| 3147 | + | |
| 3148 | + | |
| 3149 | + | |
| 3150 | + | |
3108 | 3151 | | |
3109 | 3152 | | |
3110 | 3153 | | |
| |||
3364 | 3407 | | |
3365 | 3408 | | |
3366 | 3409 | | |
| 3410 | + | |
| 3411 | + | |
3367 | 3412 | | |
3368 | 3413 | | |
3369 | 3414 | | |
| |||
4597 | 4642 | | |
4598 | 4643 | | |
4599 | 4644 | | |
| 4645 | + | |
| 4646 | + | |
4600 | 4647 | | |
4601 | 4648 | | |
4602 | 4649 | | |
| |||
4621 | 4668 | | |
4622 | 4669 | | |
4623 | 4670 | | |
4624 | | - | |
| 4671 | + | |
4625 | 4672 | | |
4626 | 4673 | | |
4627 | 4674 | | |
| |||
5790 | 5837 | | |
5791 | 5838 | | |
5792 | 5839 | | |
| 5840 | + | |
| 5841 | + | |
5793 | 5842 | | |
5794 | 5843 | | |
5795 | 5844 | | |
| |||
6110 | 6159 | | |
6111 | 6160 | | |
6112 | 6161 | | |
| 6162 | + | |
| 6163 | + | |
6113 | 6164 | | |
6114 | 6165 | | |
6115 | 6166 | | |
| |||
7112 | 7163 | | |
7113 | 7164 | | |
7114 | 7165 | | |
| 7166 | + | |
7115 | 7167 | | |
7116 | 7168 | | |
7117 | 7169 | | |
| |||
0 commit comments