af_unix: Disable MSG_OOB handling for sockets in sockmap/sockhash
Subsystems (5)
PATCH v2 1/1 af_unix: Disable MSG_OOB handling for sockets in sockmap/sockhash
AF_UNIX socket tracks the most recent OOB packet (in its receive queue) with an oob_skb pointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets, oob_skb is left outdated. This results in a single skb that may be accessed from two different sockets.
Take the easy way out: silently drop MSG_OOB data targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb's scm_fp_list (SCM_RIGHTS, SCM_CREDENTIALS).
For symmetry, forbid MSG_OOB in unix_bpf_recvmsg().
---
v2:
- Reduce time under mutex, restructure (Kuniyuki)
- Handle unix_release_sock() race
v1: https://lore.kernel.org/netdev/[email protected]/
net/unix/af_unix.c | 41 ++++++++++++++++++++++++++++++++++++++++- net/unix/unix_bpf.c | 3 +++ 2 files changed, 43 insertions(+), 1 deletion(-)
net/unix/af_unix.c +40 -1
@@ -2653,10 +2653,49 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,static int unix_stream_read_skb (struct sock *sk,skb_read_actor_t recv_actor) { +struct unix_sock *u =unix_sk (sk); +struct sk_buff *skb; +int err; +if (unlikely (READ_ONCE (sk->sk_state) !=TCP_ESTABLISHED ))return -ENOTCONN ; -return unix_read_skb (sk, recv_actor); +mutex_lock (&u->iolock); + skb =skb_recv_datagram (sk,MSG_DONTWAIT , &err); +mutex_unlock (&u->iolock); +if (!skb) +return err; + +#if IS_ENABLED (CONFIG_AF_UNIX_OOB ) +if (unlikely (skb ==READ_ONCE (u->oob_skb))) { +bool drop = false; + +unix_state_lock (sk); + +if (sock_flag (sk,SOCK_DEAD )) { +unix_state_unlock (sk); +kfree_skb (skb); +return -ECONNRESET ; + } + +spin_lock (&sk->sk_receive_queue.lock); +if (likely (skb == u->oob_skb)) { +WRITE_ONCE (u->oob_skb,NULL ); + drop = true; + } +spin_unlock (&sk->sk_receive_queue.lock); + +unix_state_unlock (sk); + +if (drop) { +WARN_ON_ONCE (skb_unref (skb)); +kfree_skb (skb); +return -EAGAIN ; + } + } +#endif + +return recv_actor (sk, skb); } static int unix_stream_read_generic(struct unix_stream_read_state *state,
net/unix/unix_bpf.c +3 -0
@@ -54,6 +54,9 @@ static int unix_bpf_recvmsg(struct sock *sk, struct msghdr *msg,struct sk_psock *psock;int copied; +if (flags &MSG_OOB ) +return -EOPNOTSUPP ; +if (!len)return 0 ;
Signature · 1 line
2.45.1
Jakub Sitnicki
maintainerQuoted 9 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).
Quoted 6 lines
For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
[+CC Cong who authored ->read_skb]
I'm guessing you have a test program that you're developing the fix against. Would you like to extend the test case for sockmap redirect from unix stream [1] to incorporate it?
Sadly unix_inet_redir_to_connected needs a fix first because it hardcodes sotype to SOCK_DGRAM.
Quoted 18 lines · Jakub Sitnicki
Quoted 9 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).Quoted 6 lines
For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
[+CC Cong who authored ->read_skb]
Quoted 8 lines
I'm guessing you have a test program that you're developing the fix against. Would you like to extend the test case for sockmap redirect from unix stream [1] to incorporate it?
Sadly
unix_inet_redir_to_connectedneeds a fix first because it hardcodes sotype toSOCK_DGRAM.
I would like to understand why this is not an issue for TCP as we try to mimic TCP OOB behavior for AF_UNIX sockets. However, I am out of the office till July 8th and can only look at the issue after my return.
Shoaib
Quoted 17 lines · Jakub Sitnicki
Quoted 9 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).Quoted 6 lines
For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
Quoted 8 lines
[+CC Cong who authored ->read_skb]
I'm guessing you have a test program that you're developing the fix against. Would you like to extend the test case for sockmap redirect from unix stream [1] to incorporate it?
Sadly
unix_inet_redir_to_connectedneeds a fix first because it hardcodes sotype toSOCK_DGRAM.
Ugh, my last two replies got silently dropped by vger. Is there any way to tell what went wrong?
So, again, sure, I'll extend the sockmap redirect test.
And regarding Rao's comment, I took a look and I think sockmap'ed TCP OOB does indeed act the same way. I'll try to add that into selftest as well.
Thanks,
Michal
Jakub Sitnicki
maintainerQuoted 24 lines · Michal Luczaj
Quoted 23 lines · Jakub Sitnicki
Quoted 9 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).Quoted 6 lines
For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
[+CC Cong who authored ->read_skb]
I'm guessing you have a test program that you're developing the fix against. Would you like to extend the test case for sockmap redirect from unix stream [1] to incorporate it?
Quoted 5 lines
Quoted 2 lines
Sadly
unix_inet_redir_to_connectedneeds a fix first because it hardcodes sotype toSOCK_DGRAM.Ugh, my last two replies got silently dropped by vger. Is there any way to tell what went wrong?
Not sure if it was vger or lore archive. Your reply hit my inbox but is nowhere to be found in the archive:
https://lore.kernel.org/r/[email protected]
I think we can reach out to Konstantin Ryabitsev at [email protected]. AFAIK he maintains the lore.kernel.org archive.
Quoted 1 line
So, again, sure, I'll extend the sockmap redirect test.
Appreciate the help with adding a regression test, if time allows. Fixes are of course very welcome even without them.
Quoted 2 lines
And regarding Rao's comment, I took a look and I think sockmap'ed TCP OOB does indeed act the same way. I'll try to add that into selftest as well.n
Right, it does sound like we're not clearing the offset kept in tcp_sock::urg_data when skb is redirected.
Quoted 42 lines · Jakub Sitnicki
Quoted 24 lines · Michal Luczaj
Quoted 23 lines · Jakub Sitnicki
Quoted 9 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).Quoted 6 lines
For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
[+CC Cong who authored ->read_skb]
I'm guessing you have a test program that you're developing the fix against. Would you like to extend the test case for sockmap redirect from unix stream [1] to incorporate it?
Quoted 5 lines
Quoted 2 lines
Sadly
unix_inet_redir_to_connectedneeds a fix first because it hardcodes sotype toSOCK_DGRAM.Ugh, my last two replies got silently dropped by vger. Is there any way to tell what went wrong?
Not sure if it was vger or lore archive. Your reply hit my inbox but is nowhere to be found in the archive:
I think we can reach out to Konstantin Ryabitsev at [email protected]. AFAIK he maintains the lore.kernel.org archive.
Quoted 1 line
So, again, sure, I'll extend the sockmap redirect test.
Quoted 9 lines
Appreciate the help with adding a regression test, if time allows. Fixes are of course very welcome even without them.
Quoted 2 lines
And regarding Rao's comment, I took a look and I think sockmap'ed TCP OOB does indeed act the same way. I'll try to add that into selftest as well.n
Right, it does sound like we're not clearing the offset kept in
tcp_sock::urg_datawhen skb is redirected.
I am fine if the behavior is same as TCP. Thanks a lot for looking into this.
Regards,
Shoaib
Quoted 3 lines
Quoted 25 lines · Jakub Sitnicki
Quoted 24 lines · Michal Luczaj
Quoted 23 lines · Jakub Sitnicki
Quoted 9 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).Quoted 6 lines
For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
[+CC Cong who authored ->read_skb]
I'm guessing you have a test program that you're developing the fix against. Would you like to extend the test case for sockmap redirect from unix stream [1] to incorporate it?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Sadly
unix_inet_redir_to_connectedneeds a fix first because it hardcodes sotype toSOCK_DGRAM.Ugh, my last two replies got silently dropped by vger. Is there any way to tell what went wrong?
Not sure if it was vger or lore archive. Your reply hit my inbox but is nowhere to be found in the archive:> [...]
24h later mailer daemon revealed that my SMTP server got (temporarily) on the Spamhaus Blocklist. Oh well.
Quoted 4 lines
Quoted 1 line
So, again, sure, I'll extend the sockmap redirect test.
Appreciate the help with adding a regression test, if time allows. Fixes are of course very welcome even without them.
No problem, fix along with the test sent. Let me know what you think.
Quoted 5 lines
Quoted 2 lines
And regarding Rao's comment, I took a look and I think sockmap'ed TCP OOB does indeed act the same way. I'll try to add that into selftest as well.n
Right, it does sound like we're not clearing the offset kept in
tcp_sock::urg_datawhen skb is redirected.
Yeah, so I also wanted to extend the TCP's redir_to_connected(), but is that code correct? It seems to be testing REDIR_INGRESS, yet prog_stream_verdict() doesn't run bpf_sk_redirect_map() with the BPF_F_INGRESS flag.
Thanks,
Michal
Jakub Sitnicki
maintainerQuoted 19 lines · Michal Luczaj
Quoted 18 lines · Jakub Sitnicki
Quoted 17 lines · Michal Luczaj
Quoted 16 lines · Jakub Sitnicki
Quoted 15 lines · Michal Luczaj
AF_UNIXsocket tracks the most recent OOB packet (in its receive queue) with anoob_skbpointer. BPF redirecting does not account for that: when an OOB packet is moved between sockets,oob_skbis left outdated. This results in a single skb that may be accessed from two different sockets.Take the easy way out: silently drop
MSG_OOBdata targeting any socket that is in a sockmap or a sockhash. Note that such silent drop is akin to the fate of redirected skb'sscm_fp_list(SCM_RIGHTS,SCM_CREDENTIALS).For symmetry, forbid
MSG_OOBinunix_bpf_recvmsg().Suggested-by Kuniyuki Iwashima <[email protected]>Fixes 314001f0bf92 ("af_unix: Add OOB support")Signed-off-by Michal Luczaj <[email protected]>---
[...]
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
And regarding Rao's comment, I took a look and I think sockmap'ed TCP OOB does indeed act the same way. I'll try to add that into selftest as well.n
Right, it does sound like we're not clearing the offset kept in
tcp_sock::urg_datawhen skb is redirected.Yeah, so I also wanted to extend the TCP's
redir_to_connected(), but is that code correct? It seems to be testingREDIR_INGRESS, yetprog_stream_verdict()doesn't runbpf_sk_redirect_map()with theBPF_F_INGRESSflag.
Right, we don't have the ingress-to-local [1] setup covered there.
This test case has outgrown its initial coverage purpose - using sockmap with listening / unconnected sockets - and needs to be split up, IMO.
There are definitely gaps in the coverage of redirect cases, and I'd like to extend it to cover all combinations (supported and unsupported ones) [2].
[1] It's a term I coined to make it easier to communiate
https://github.com/jsitnicki/kubecon-2024-sockmap/blob/main/cheatsheet-sockmap-redirect.png
[2] https://gist.github.com/jsitnicki/578fdd614d181bed2b02922b17972b4e