* Re: [RFC] net: add new socket option SO_SETNETNS [not found] <Y9q8Ec1CJILZz7dj@ip-172-31-38-16.us-west-2.compute.internal> @ 2023-02-02 1:48 ` Hillf Danton 2023-02-02 19:55 ` Alok Tiagi 0 siblings, 1 reply; 8+ messages in thread From: Hillf Danton @ 2023-02-02 1:48 UTC (permalink / raw) To: aloktiagi; +Cc: ebiederm, edumazet, netdev, linux-mm, linux-kernel On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > WRITE_ONCE(sk->sk_txrehash, (u8)val); > break; > > + case SO_SETNETNS: > + { > + struct net *other_ns, *my_ns; > + > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > + ret = -EOPNOTSUPP; > + break; > + } > + > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > + ret = -EOPNOTSUPP; > + break; > + } > + > + other_ns = get_net_ns_by_fd(val); > + if (IS_ERR(other_ns)) { > + ret = PTR_ERR(other_ns); > + break; > + } > + > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > + ret = -EPERM; > + goto out_err; > + } > + > + /* check that the socket has never been connected or recently disconnected */ > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > + ret = -EOPNOTSUPP; > + goto out_err; > + } > + > + /* check that the socket is not bound to an interface*/ > + if (sk->sk_bound_dev_if != 0) { > + ret = -EOPNOTSUPP; > + goto out_err; > + } > + > + my_ns = sock_net(sk); > + sock_net_set(sk, other_ns); > + put_net(my_ns); > + break; cpu 0 cpu 2 --- --- ns = sock_net(sk); my_ns = sock_net(sk); sock_net_set(sk, other_ns); put_net(my_ns); ns is invalid ? > +out_err: > + put_net(other_ns); > + break; > + } > + > default: > ret = -ENOPROTOOPT; > break; ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-02 1:48 ` [RFC] net: add new socket option SO_SETNETNS Hillf Danton @ 2023-02-02 19:55 ` Alok Tiagi 2023-02-02 20:10 ` Eric Dumazet 0 siblings, 1 reply; 8+ messages in thread From: Alok Tiagi @ 2023-02-02 19:55 UTC (permalink / raw) To: Hillf Danton; +Cc: ebiederm, edumazet, netdev, linux-mm, linux-kernel On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > > WRITE_ONCE(sk->sk_txrehash, (u8)val); > > break; > > > > + case SO_SETNETNS: > > + { > > + struct net *other_ns, *my_ns; > > + > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > > + ret = -EOPNOTSUPP; > > + break; > > + } > > + > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > > + ret = -EOPNOTSUPP; > > + break; > > + } > > + > > + other_ns = get_net_ns_by_fd(val); > > + if (IS_ERR(other_ns)) { > > + ret = PTR_ERR(other_ns); > > + break; > > + } > > + > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > > + ret = -EPERM; > > + goto out_err; > > + } > > + > > + /* check that the socket has never been connected or recently disconnected */ > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > > + ret = -EOPNOTSUPP; > > + goto out_err; > > + } > > + > > + /* check that the socket is not bound to an interface*/ > > + if (sk->sk_bound_dev_if != 0) { > > + ret = -EOPNOTSUPP; > > + goto out_err; > > + } > > + > > + my_ns = sock_net(sk); > > + sock_net_set(sk, other_ns); > > + put_net(my_ns); > > + break; > > cpu 0 cpu 2 > --- --- > ns = sock_net(sk); > my_ns = sock_net(sk); > sock_net_set(sk, other_ns); > put_net(my_ns); > ns is invalid ? That is the reason we want the socket to be in an un-connected state. That should help us avoid this situation. > > > +out_err: > > + put_net(other_ns); > > + break; > > + } > > + > > default: > > ret = -ENOPROTOOPT; > > break; ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-02 19:55 ` Alok Tiagi @ 2023-02-02 20:10 ` Eric Dumazet 2023-02-02 23:58 ` Alok Tiagi 0 siblings, 1 reply; 8+ messages in thread From: Eric Dumazet @ 2023-02-02 20:10 UTC (permalink / raw) To: Alok Tiagi; +Cc: Hillf Danton, ebiederm, netdev, linux-mm, linux-kernel On Thu, Feb 2, 2023 at 8:55 PM Alok Tiagi <aloktiagi@gmail.com> wrote: > > On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: > > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > > > WRITE_ONCE(sk->sk_txrehash, (u8)val); > > > break; > > > > > > + case SO_SETNETNS: > > > + { > > > + struct net *other_ns, *my_ns; > > > + > > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > > > + ret = -EOPNOTSUPP; > > > + break; > > > + } > > > + > > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > > > + ret = -EOPNOTSUPP; > > > + break; > > > + } > > > + > > > + other_ns = get_net_ns_by_fd(val); > > > + if (IS_ERR(other_ns)) { > > > + ret = PTR_ERR(other_ns); > > > + break; > > > + } > > > + > > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > > > + ret = -EPERM; > > > + goto out_err; > > > + } > > > + > > > + /* check that the socket has never been connected or recently disconnected */ > > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > > > + ret = -EOPNOTSUPP; > > > + goto out_err; > > > + } > > > + > > > + /* check that the socket is not bound to an interface*/ > > > + if (sk->sk_bound_dev_if != 0) { > > > + ret = -EOPNOTSUPP; > > > + goto out_err; > > > + } > > > + > > > + my_ns = sock_net(sk); > > > + sock_net_set(sk, other_ns); > > > + put_net(my_ns); > > > + break; > > > > cpu 0 cpu 2 > > --- --- > > ns = sock_net(sk); > > my_ns = sock_net(sk); > > sock_net_set(sk, other_ns); > > put_net(my_ns); > > ns is invalid ? > > That is the reason we want the socket to be in an un-connected state. That > should help us avoid this situation. This is not enough.... Another thread might look at sock_net(sk), for example from inet_diag or tcp timers (which can be fired even in un-connected state) Even UDP sockets can receive packets while being un-connected, and they need to deref the net pointer. Currently there is no protection about sock_net(sk) being changed on the fly, and the struct net could disappear and be freed. There are ~1500 uses of sock_net(sk) in the kernel, I do not think you/we want to audit all of them to check what could go wrong... ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-02 20:10 ` Eric Dumazet @ 2023-02-02 23:58 ` Alok Tiagi 2023-02-03 15:09 ` Eric Dumazet 0 siblings, 1 reply; 8+ messages in thread From: Alok Tiagi @ 2023-02-02 23:58 UTC (permalink / raw) To: Eric Dumazet; +Cc: Hillf Danton, ebiederm, netdev, linux-mm, linux-kernel On Thu, Feb 02, 2023 at 09:10:23PM +0100, Eric Dumazet wrote: > On Thu, Feb 2, 2023 at 8:55 PM Alok Tiagi <aloktiagi@gmail.com> wrote: > > > > On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: > > > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > > > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > > > > WRITE_ONCE(sk->sk_txrehash, (u8)val); > > > > break; > > > > > > > > + case SO_SETNETNS: > > > > + { > > > > + struct net *other_ns, *my_ns; > > > > + > > > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > > > > + ret = -EOPNOTSUPP; > > > > + break; > > > > + } > > > > + > > > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > > > > + ret = -EOPNOTSUPP; > > > > + break; > > > > + } > > > > + > > > > + other_ns = get_net_ns_by_fd(val); > > > > + if (IS_ERR(other_ns)) { > > > > + ret = PTR_ERR(other_ns); > > > > + break; > > > > + } > > > > + > > > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > > > > + ret = -EPERM; > > > > + goto out_err; > > > > + } > > > > + > > > > + /* check that the socket has never been connected or recently disconnected */ > > > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > > > > + ret = -EOPNOTSUPP; > > > > + goto out_err; > > > > + } > > > > + > > > > + /* check that the socket is not bound to an interface*/ > > > > + if (sk->sk_bound_dev_if != 0) { > > > > + ret = -EOPNOTSUPP; > > > > + goto out_err; > > > > + } > > > > + > > > > + my_ns = sock_net(sk); > > > > + sock_net_set(sk, other_ns); > > > > + put_net(my_ns); > > > > + break; > > > > > > cpu 0 cpu 2 > > > --- --- > > > ns = sock_net(sk); > > > my_ns = sock_net(sk); > > > sock_net_set(sk, other_ns); > > > put_net(my_ns); > > > ns is invalid ? > > > > That is the reason we want the socket to be in an un-connected state. That > > should help us avoid this situation. > > This is not enough.... > > Another thread might look at sock_net(sk), for example from inet_diag > or tcp timers > (which can be fired even in un-connected state) > > Even UDP sockets can receive packets while being un-connected, > and they need to deref the net pointer. > > Currently there is no protection about sock_net(sk) being changed on the fly, > and the struct net could disappear and be freed. > > There are ~1500 uses of sock_net(sk) in the kernel, I do not think > you/we want to audit all > of them to check what could go wrong... I agree, auditing all the uses of sock_net(sk) is not a feasible option. From my exploration of the usage of sock_net(sk) it appeared that it might be safe to swap a sockets net ns if it had never been connected but I looked at only a subset of such uses. Introducing a ref counting logic to every access of sock_net(sk) may help get around this but invovles a bigger change to increment and decrement the count at every use of sock_net(). Any suggestions if this could be achieved in another way much close to the socket creation time or any comments on our workaround for injecting sockets using seccomp addfd? ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-02 23:58 ` Alok Tiagi @ 2023-02-03 15:09 ` Eric Dumazet 2023-02-03 17:50 ` Alok Tiagi 0 siblings, 1 reply; 8+ messages in thread From: Eric Dumazet @ 2023-02-03 15:09 UTC (permalink / raw) To: Alok Tiagi; +Cc: Hillf Danton, ebiederm, netdev, linux-mm, linux-kernel On Fri, Feb 3, 2023 at 12:59 AM Alok Tiagi <aloktiagi@gmail.com> wrote: > > On Thu, Feb 02, 2023 at 09:10:23PM +0100, Eric Dumazet wrote: > > On Thu, Feb 2, 2023 at 8:55 PM Alok Tiagi <aloktiagi@gmail.com> wrote: > > > > > > On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: > > > > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > > > > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > > > > > WRITE_ONCE(sk->sk_txrehash, (u8)val); > > > > > break; > > > > > > > > > > + case SO_SETNETNS: > > > > > + { > > > > > + struct net *other_ns, *my_ns; > > > > > + > > > > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > > > > > + ret = -EOPNOTSUPP; > > > > > + break; > > > > > + } > > > > > + > > > > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > > > > > + ret = -EOPNOTSUPP; > > > > > + break; > > > > > + } > > > > > + > > > > > + other_ns = get_net_ns_by_fd(val); > > > > > + if (IS_ERR(other_ns)) { > > > > > + ret = PTR_ERR(other_ns); > > > > > + break; > > > > > + } > > > > > + > > > > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > > > > > + ret = -EPERM; > > > > > + goto out_err; > > > > > + } > > > > > + > > > > > + /* check that the socket has never been connected or recently disconnected */ > > > > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > > > > > + ret = -EOPNOTSUPP; > > > > > + goto out_err; > > > > > + } > > > > > + > > > > > + /* check that the socket is not bound to an interface*/ > > > > > + if (sk->sk_bound_dev_if != 0) { > > > > > + ret = -EOPNOTSUPP; > > > > > + goto out_err; > > > > > + } > > > > > + > > > > > + my_ns = sock_net(sk); > > > > > + sock_net_set(sk, other_ns); > > > > > + put_net(my_ns); > > > > > + break; > > > > > > > > cpu 0 cpu 2 > > > > --- --- > > > > ns = sock_net(sk); > > > > my_ns = sock_net(sk); > > > > sock_net_set(sk, other_ns); > > > > put_net(my_ns); > > > > ns is invalid ? > > > > > > That is the reason we want the socket to be in an un-connected state. That > > > should help us avoid this situation. > > > > This is not enough.... > > > > Another thread might look at sock_net(sk), for example from inet_diag > > or tcp timers > > (which can be fired even in un-connected state) > > > > Even UDP sockets can receive packets while being un-connected, > > and they need to deref the net pointer. > > > > Currently there is no protection about sock_net(sk) being changed on the fly, > > and the struct net could disappear and be freed. > > > > There are ~1500 uses of sock_net(sk) in the kernel, I do not think > > you/we want to audit all > > of them to check what could go wrong... > > I agree, auditing all the uses of sock_net(sk) is not a feasible option. From my > exploration of the usage of sock_net(sk) it appeared that it might be safe to > swap a sockets net ns if it had never been connected but I looked at only a > subset of such uses. > > Introducing a ref counting logic to every access of sock_net(sk) may help get > around this but invovles a bigger change to increment and decrement the count at > every use of sock_net(). > > Any suggestions if this could be achieved in another way much close to the > socket creation time or any comments on our workaround for injecting sockets using > seccomp addfd? Maybe the existing BPF hook in inet_create() could be used ? err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); The BPF program might be able to switch the netns, because at this time the new socket is not yet visible from external threads. Although it is not going to catch dual stack uses (open a V6 socket, then use a v4mapped address at bind()/connect()/... ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-03 15:09 ` Eric Dumazet @ 2023-02-03 17:50 ` Alok Tiagi 2023-02-03 21:17 ` Eric W. Biederman 0 siblings, 1 reply; 8+ messages in thread From: Alok Tiagi @ 2023-02-03 17:50 UTC (permalink / raw) To: Eric Dumazet; +Cc: Hillf Danton, ebiederm, netdev, linux-mm, linux-kernel On Fri, Feb 03, 2023 at 04:09:12PM +0100, Eric Dumazet wrote: > On Fri, Feb 3, 2023 at 12:59 AM Alok Tiagi <aloktiagi@gmail.com> wrote: > > > > On Thu, Feb 02, 2023 at 09:10:23PM +0100, Eric Dumazet wrote: > > > On Thu, Feb 2, 2023 at 8:55 PM Alok Tiagi <aloktiagi@gmail.com> wrote: > > > > > > > > On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: > > > > > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > > > > > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > > > > > > WRITE_ONCE(sk->sk_txrehash, (u8)val); > > > > > > break; > > > > > > > > > > > > + case SO_SETNETNS: > > > > > > + { > > > > > > + struct net *other_ns, *my_ns; > > > > > > + > > > > > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > > > > > > + ret = -EOPNOTSUPP; > > > > > > + break; > > > > > > + } > > > > > > + > > > > > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > > > > > > + ret = -EOPNOTSUPP; > > > > > > + break; > > > > > > + } > > > > > > + > > > > > > + other_ns = get_net_ns_by_fd(val); > > > > > > + if (IS_ERR(other_ns)) { > > > > > > + ret = PTR_ERR(other_ns); > > > > > > + break; > > > > > > + } > > > > > > + > > > > > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > > > > > > + ret = -EPERM; > > > > > > + goto out_err; > > > > > > + } > > > > > > + > > > > > > + /* check that the socket has never been connected or recently disconnected */ > > > > > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > > > > > > + ret = -EOPNOTSUPP; > > > > > > + goto out_err; > > > > > > + } > > > > > > + > > > > > > + /* check that the socket is not bound to an interface*/ > > > > > > + if (sk->sk_bound_dev_if != 0) { > > > > > > + ret = -EOPNOTSUPP; > > > > > > + goto out_err; > > > > > > + } > > > > > > + > > > > > > + my_ns = sock_net(sk); > > > > > > + sock_net_set(sk, other_ns); > > > > > > + put_net(my_ns); > > > > > > + break; > > > > > > > > > > cpu 0 cpu 2 > > > > > --- --- > > > > > ns = sock_net(sk); > > > > > my_ns = sock_net(sk); > > > > > sock_net_set(sk, other_ns); > > > > > put_net(my_ns); > > > > > ns is invalid ? > > > > > > > > That is the reason we want the socket to be in an un-connected state. That > > > > should help us avoid this situation. > > > > > > This is not enough.... > > > > > > Another thread might look at sock_net(sk), for example from inet_diag > > > or tcp timers > > > (which can be fired even in un-connected state) > > > > > > Even UDP sockets can receive packets while being un-connected, > > > and they need to deref the net pointer. > > > > > > Currently there is no protection about sock_net(sk) being changed on the fly, > > > and the struct net could disappear and be freed. > > > > > > There are ~1500 uses of sock_net(sk) in the kernel, I do not think > > > you/we want to audit all > > > of them to check what could go wrong... > > > > I agree, auditing all the uses of sock_net(sk) is not a feasible option. From my > > exploration of the usage of sock_net(sk) it appeared that it might be safe to > > swap a sockets net ns if it had never been connected but I looked at only a > > subset of such uses. > > > > Introducing a ref counting logic to every access of sock_net(sk) may help get > > around this but invovles a bigger change to increment and decrement the count at > > every use of sock_net(). > > > > Any suggestions if this could be achieved in another way much close to the > > socket creation time or any comments on our workaround for injecting sockets using > > seccomp addfd? > > Maybe the existing BPF hook in inet_create() could be used ? > > err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); > > The BPF program might be able to switch the netns, because at this > time the new socket is not > yet visible from external threads. > > Although it is not going to catch dual stack uses (open a V6 socket, > then use a v4mapped address at bind()/connect()/... We thought of a similar approach by intercepting the socket() call in seccomp and injecting a new file descritpor much earlier but as you said we run into the issue of handling dual stack sockets since we do not know in advance if its going to be used for a v4mapped address. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-03 17:50 ` Alok Tiagi @ 2023-02-03 21:17 ` Eric W. Biederman 2023-02-04 18:44 ` Alok Tiagi 0 siblings, 1 reply; 8+ messages in thread From: Eric W. Biederman @ 2023-02-03 21:17 UTC (permalink / raw) To: Alok Tiagi; +Cc: Eric Dumazet, Hillf Danton, netdev, linux-mm, linux-kernel Alok Tiagi <aloktiagi@gmail.com> writes: > On Fri, Feb 03, 2023 at 04:09:12PM +0100, Eric Dumazet wrote: >> On Fri, Feb 3, 2023 at 12:59 AM Alok Tiagi <aloktiagi@gmail.com> wrote: >> > >> > On Thu, Feb 02, 2023 at 09:10:23PM +0100, Eric Dumazet wrote: >> > > On Thu, Feb 2, 2023 at 8:55 PM Alok Tiagi <aloktiagi@gmail.com> wrote: >> > > > >> > > > On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: >> > > > > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> >> > > > > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, >> > > > > > WRITE_ONCE(sk->sk_txrehash, (u8)val); >> > > > > > break; >> > > > > > >> > > > > > + case SO_SETNETNS: >> > > > > > + { >> > > > > > + struct net *other_ns, *my_ns; >> > > > > > + >> > > > > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { >> > > > > > + ret = -EOPNOTSUPP; >> > > > > > + break; >> > > > > > + } >> > > > > > + >> > > > > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { >> > > > > > + ret = -EOPNOTSUPP; >> > > > > > + break; >> > > > > > + } >> > > > > > + >> > > > > > + other_ns = get_net_ns_by_fd(val); >> > > > > > + if (IS_ERR(other_ns)) { >> > > > > > + ret = PTR_ERR(other_ns); >> > > > > > + break; >> > > > > > + } >> > > > > > + >> > > > > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { >> > > > > > + ret = -EPERM; >> > > > > > + goto out_err; >> > > > > > + } >> > > > > > + >> > > > > > + /* check that the socket has never been connected or recently disconnected */ >> > > > > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { >> > > > > > + ret = -EOPNOTSUPP; >> > > > > > + goto out_err; >> > > > > > + } >> > > > > > + >> > > > > > + /* check that the socket is not bound to an interface*/ >> > > > > > + if (sk->sk_bound_dev_if != 0) { >> > > > > > + ret = -EOPNOTSUPP; >> > > > > > + goto out_err; >> > > > > > + } >> > > > > > + >> > > > > > + my_ns = sock_net(sk); >> > > > > > + sock_net_set(sk, other_ns); >> > > > > > + put_net(my_ns); >> > > > > > + break; >> > > > > >> > > > > cpu 0 cpu 2 >> > > > > --- --- >> > > > > ns = sock_net(sk); >> > > > > my_ns = sock_net(sk); >> > > > > sock_net_set(sk, other_ns); >> > > > > put_net(my_ns); >> > > > > ns is invalid ? >> > > > >> > > > That is the reason we want the socket to be in an un-connected state. That >> > > > should help us avoid this situation. >> > > >> > > This is not enough.... >> > > >> > > Another thread might look at sock_net(sk), for example from inet_diag >> > > or tcp timers >> > > (which can be fired even in un-connected state) >> > > >> > > Even UDP sockets can receive packets while being un-connected, >> > > and they need to deref the net pointer. >> > > >> > > Currently there is no protection about sock_net(sk) being changed on the fly, >> > > and the struct net could disappear and be freed. >> > > >> > > There are ~1500 uses of sock_net(sk) in the kernel, I do not think >> > > you/we want to audit all >> > > of them to check what could go wrong... >> > >> > I agree, auditing all the uses of sock_net(sk) is not a feasible option. From my >> > exploration of the usage of sock_net(sk) it appeared that it might be safe to >> > swap a sockets net ns if it had never been connected but I looked at only a >> > subset of such uses. >> > >> > Introducing a ref counting logic to every access of sock_net(sk) may help get >> > around this but invovles a bigger change to increment and decrement the count at >> > every use of sock_net(). >> > >> > Any suggestions if this could be achieved in another way much close to the >> > socket creation time or any comments on our workaround for injecting sockets using >> > seccomp addfd? >> >> Maybe the existing BPF hook in inet_create() could be used ? >> >> err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); >> >> The BPF program might be able to switch the netns, because at this >> time the new socket is not >> yet visible from external threads. >> >> Although it is not going to catch dual stack uses (open a V6 socket, >> then use a v4mapped address at bind()/connect()/... > > We thought of a similar approach by intercepting the socket() call in seccomp > and injecting a new file descritpor much earlier but as you said we run into the > issue of handling dual stack sockets since we do not know in advance if its > going to be used for a v4mapped address. I would suggest adding a default ipv4 route from your ipv6 network namespaces to your ipv4 network namespace, but that only works for outbound traffic. The inbound traffic problem is classically solved via nat. That you are not suggesting using nat has me thinking there is something subtle in what you are trying to do that I am missing. Perhaps your userspace can do: previous_netns = open("/proc/self/ns/net"); setns(ipv4_netns); socket(); setns(previous_netns); As the network namespace is per thread this is atomic if you add the logic to block signals around it. Eric ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC] net: add new socket option SO_SETNETNS 2023-02-03 21:17 ` Eric W. Biederman @ 2023-02-04 18:44 ` Alok Tiagi 0 siblings, 0 replies; 8+ messages in thread From: Alok Tiagi @ 2023-02-04 18:44 UTC (permalink / raw) To: Eric W. Biederman Cc: Eric Dumazet, Hillf Danton, netdev, linux-mm, linux-kernel, tycho On Fri, Feb 03, 2023 at 03:17:06PM -0600, Eric W. Biederman wrote: > Alok Tiagi <aloktiagi@gmail.com> writes: > > > On Fri, Feb 03, 2023 at 04:09:12PM +0100, Eric Dumazet wrote: > >> On Fri, Feb 3, 2023 at 12:59 AM Alok Tiagi <aloktiagi@gmail.com> wrote: > >> > > >> > On Thu, Feb 02, 2023 at 09:10:23PM +0100, Eric Dumazet wrote: > >> > > On Thu, Feb 2, 2023 at 8:55 PM Alok Tiagi <aloktiagi@gmail.com> wrote: > >> > > > > >> > > > On Thu, Feb 02, 2023 at 09:48:10AM +0800, Hillf Danton wrote: > >> > > > > On Wed, 1 Feb 2023 19:22:57 +0000 aloktiagi <aloktiagi@gmail.com> > >> > > > > > @@ -1535,6 +1535,52 @@ int sk_setsockopt(struct sock *sk, int level, int optname, > >> > > > > > WRITE_ONCE(sk->sk_txrehash, (u8)val); > >> > > > > > break; > >> > > > > > > >> > > > > > + case SO_SETNETNS: > >> > > > > > + { > >> > > > > > + struct net *other_ns, *my_ns; > >> > > > > > + > >> > > > > > + if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) { > >> > > > > > + ret = -EOPNOTSUPP; > >> > > > > > + break; > >> > > > > > + } > >> > > > > > + > >> > > > > > + if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_DGRAM) { > >> > > > > > + ret = -EOPNOTSUPP; > >> > > > > > + break; > >> > > > > > + } > >> > > > > > + > >> > > > > > + other_ns = get_net_ns_by_fd(val); > >> > > > > > + if (IS_ERR(other_ns)) { > >> > > > > > + ret = PTR_ERR(other_ns); > >> > > > > > + break; > >> > > > > > + } > >> > > > > > + > >> > > > > > + if (!ns_capable(other_ns->user_ns, CAP_NET_ADMIN)) { > >> > > > > > + ret = -EPERM; > >> > > > > > + goto out_err; > >> > > > > > + } > >> > > > > > + > >> > > > > > + /* check that the socket has never been connected or recently disconnected */ > >> > > > > > + if (sk->sk_state != TCP_CLOSE || sk->sk_shutdown & SHUTDOWN_MASK) { > >> > > > > > + ret = -EOPNOTSUPP; > >> > > > > > + goto out_err; > >> > > > > > + } > >> > > > > > + > >> > > > > > + /* check that the socket is not bound to an interface*/ > >> > > > > > + if (sk->sk_bound_dev_if != 0) { > >> > > > > > + ret = -EOPNOTSUPP; > >> > > > > > + goto out_err; > >> > > > > > + } > >> > > > > > + > >> > > > > > + my_ns = sock_net(sk); > >> > > > > > + sock_net_set(sk, other_ns); > >> > > > > > + put_net(my_ns); > >> > > > > > + break; > >> > > > > > >> > > > > cpu 0 cpu 2 > >> > > > > --- --- > >> > > > > ns = sock_net(sk); > >> > > > > my_ns = sock_net(sk); > >> > > > > sock_net_set(sk, other_ns); > >> > > > > put_net(my_ns); > >> > > > > ns is invalid ? > >> > > > > >> > > > That is the reason we want the socket to be in an un-connected state. That > >> > > > should help us avoid this situation. > >> > > > >> > > This is not enough.... > >> > > > >> > > Another thread might look at sock_net(sk), for example from inet_diag > >> > > or tcp timers > >> > > (which can be fired even in un-connected state) > >> > > > >> > > Even UDP sockets can receive packets while being un-connected, > >> > > and they need to deref the net pointer. > >> > > > >> > > Currently there is no protection about sock_net(sk) being changed on the fly, > >> > > and the struct net could disappear and be freed. > >> > > > >> > > There are ~1500 uses of sock_net(sk) in the kernel, I do not think > >> > > you/we want to audit all > >> > > of them to check what could go wrong... > >> > > >> > I agree, auditing all the uses of sock_net(sk) is not a feasible option. From my > >> > exploration of the usage of sock_net(sk) it appeared that it might be safe to > >> > swap a sockets net ns if it had never been connected but I looked at only a > >> > subset of such uses. > >> > > >> > Introducing a ref counting logic to every access of sock_net(sk) may help get > >> > around this but invovles a bigger change to increment and decrement the count at > >> > every use of sock_net(). > >> > > >> > Any suggestions if this could be achieved in another way much close to the > >> > socket creation time or any comments on our workaround for injecting sockets using > >> > seccomp addfd? > >> > >> Maybe the existing BPF hook in inet_create() could be used ? > >> > >> err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); > >> > >> The BPF program might be able to switch the netns, because at this > >> time the new socket is not > >> yet visible from external threads. > >> > >> Although it is not going to catch dual stack uses (open a V6 socket, > >> then use a v4mapped address at bind()/connect()/... > > > > We thought of a similar approach by intercepting the socket() call in seccomp > > and injecting a new file descritpor much earlier but as you said we run into the > > issue of handling dual stack sockets since we do not know in advance if its > > going to be used for a v4mapped address. > > I would suggest adding a default ipv4 route from your ipv6 network > namespaces to your ipv4 network namespace, but that only works for > outbound traffic. The inbound traffic problem is classically solved > via nat. > > That you are not suggesting using nat has me thinking there is something > subtle in what you are trying to do that I am missing. > > Perhaps your userspace can do: > > previous_netns = open("/proc/self/ns/net"); > setns(ipv4_netns); > socket(); > setns(previous_netns); > > > As the network namespace is per thread this is atomic if you add > the logic to block signals around it. > > Eric That is correct, we are not using nat, but we are providing a mechanism for the users of our container platform to move to ipv6 only while keeping egress connectivity to their ipv4 destinations. We are doing this transparently without any change in user code, but by intercept networking syscalls in a container manager running in a dedicated ipv4 only network namespace. Our current solution as described in my original commit message has limitations and we are looking for a way to switch a sockets namespace from the ipv6 only container network namespace to the dedicated ipv4 network namespace which really simplifies our design. Since our userspace is the container workload we have no control over how they instantiate their sockets. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-02-04 18:44 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <Y9q8Ec1CJILZz7dj@ip-172-31-38-16.us-west-2.compute.internal>
2023-02-02 1:48 ` [RFC] net: add new socket option SO_SETNETNS Hillf Danton
2023-02-02 19:55 ` Alok Tiagi
2023-02-02 20:10 ` Eric Dumazet
2023-02-02 23:58 ` Alok Tiagi
2023-02-03 15:09 ` Eric Dumazet
2023-02-03 17:50 ` Alok Tiagi
2023-02-03 21:17 ` Eric W. Biederman
2023-02-04 18:44 ` Alok Tiagi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox