Kui-Feng Lee 5416c9aea8 bpf: Fix the kernel crash caused by bpf_setsockopt().
The kernel crash was caused by a BPF program attached to the
"lsm_cgroup/socket_sock_rcv_skb" hook, which performed a call to
`bpf_setsockopt()` in order to set the TCP_NODELAY flag as an
example. Flags like TCP_NODELAY can prompt the kernel to flush a
socket's outgoing queue, and this hook
"lsm_cgroup/socket_sock_rcv_skb" is frequently triggered by
softirqs. The issue was that in certain circumstances, when
`tcp_write_xmit()` was called to flush the queue, it would also allow
BH (bottom-half) to run. This could lead to our program attempting to
flush the same socket recursively, which caused a `skbuff` to be
unlinked twice.

`security_sock_rcv_skb()` is triggered by `tcp_filter()`. This occurs
before the sock ownership is checked in `tcp_v4_rcv()`. Consequently,
if a bpf program runs on `security_sock_rcv_skb()` while under softirq
conditions, it may not possess the lock needed for `bpf_setsockopt()`,
thus presenting an issue.

The patch fixes this issue by ensuring that a BPF program attached to
the "lsm_cgroup/socket_sock_rcv_skb" hook is not allowed to call
`bpf_setsockopt()`.

The differences from v1 are
 - changing commit log to explain holding the lock of the sock,
 - emphasizing that TCP_NODELAY is not the only flag, and
 - adding the fixes tag.

v1: https://lore.kernel.org/bpf/20230125000244.1109228-1-kuifeng@meta.com/

Signed-off-by: Kui-Feng Lee <kuifeng@meta.com>
Fixes: 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")
Link: https://lore.kernel.org/r/20230127001732.4162630-1-kuifeng@meta.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
2023-01-26 23:26:40 -08:00
..
2022-12-13 19:29:45 -08:00
2022-12-02 11:25:00 +01:00
2022-12-21 14:31:52 -08:00
2022-12-13 14:05:39 -08:00
2022-12-19 07:13:33 -06:00
2022-12-21 07:59:57 -08:00
2022-08-29 19:47:03 -04:00
2022-01-11 13:08:21 -08:00
2022-09-26 10:13:13 -07:00
2022-12-17 14:06:53 -06:00
2022-11-15 00:42:02 -08:00
2022-12-23 12:00:24 -08:00
2022-12-19 12:33:32 -06:00
2022-12-23 12:00:24 -08:00
2022-12-16 03:54:54 -08:00
2022-06-29 13:07:16 +02:00
2022-12-16 03:54:54 -08:00
2022-12-14 12:20:00 -08:00
2022-10-10 12:49:34 -07:00
2022-02-25 09:36:06 +01:00
2022-12-13 19:29:45 -08:00
2022-08-06 16:38:17 -07:00