Skip to content

Commit 2443ca6

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Handle memory acct if skb_verdict prog redirects to self
If the skb_verdict_prog redirects an skb knowingly to itself, fix your BPF program this is not optimal and an abuse of the API please use SK_PASS. That said there may be cases, such as socket load balancing, where picking the socket is hashed based or otherwise picks the same socket it was received on in some rare cases. If this happens we don't want to confuse userspace giving them an EAGAIN error if we can avoid it. To avoid double accounting in these cases. At the moment even if the skb has already been charged against the sockets rcvbuf and forward alloc we check it again and do set_owner_r() causing it to be orphaned and recharged. For one this is useless work, but more importantly we can have a case where the skb could be put on the ingress queue, but because we are under memory pressure we return EAGAIN. The trouble here is the skb has already been accounted for so any rcvbuf checks include the memory associated with the packet already. This rolls up and can result in unnecessary EAGAIN errors in userspace read() calls. Fix by doing an unlikely check and skipping checks if skb->sk == sk. Fixes: 5119940 ("bpf: skb_verdict, support SK_PASS on RX BPF path") Signed-off-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com> Link: https://lore.kernel.org/bpf/160556574804.73229.11328201020039674147.stgit@john-XPS-13-9370
1 parent 6fa9201 commit 2443ca6

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

net/core/skmsg.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,19 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
442442
return copied;
443443
}
444444

445+
static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb);
446+
445447
static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
446448
{
447449
struct sock *sk = psock->sk;
448450
struct sk_msg *msg;
449451

452+
/* If we are receiving on the same sock skb->sk is already assigned,
453+
* skip memory accounting and owner transition seeing it already set
454+
* correctly.
455+
*/
456+
if (unlikely(skb->sk == sk))
457+
return sk_psock_skb_ingress_self(psock, skb);
450458
msg = sk_psock_create_ingress_msg(sk, skb);
451459
if (!msg)
452460
return -EAGAIN;

0 commit comments

Comments
 (0)