A socket in a sockmap can't get TLS keys since commit 460e6486617c ("tls: reject the combination of TLS and sockmap"), but it can still get the TLS ULP. When the socket then leaves the map, sk_write_space is not restored. If the map has an sk_skb program, sk_prot isn't either, and close() spins in sock_map_close() forever. Reject the ULP with -EBUSY on a socket with a psock. A BPF program can add the socket to a sockmap without lock_sock(), so set icsk_ulp_ops before ->init(), under sk_callback_lock and only if there is no psock. sk_psock_init() checks for a ULP under the same lock. Remove tls_update(), tcp_update_ulp() and the psock check in TLS, which are now dead. Fixes: e34a07c0ae39 ("sock: redo the psock vs ULP protection check") Suggested-by: Alexei Starovoitov Signed-off-by: Sechang Lim --- include/net/tcp.h | 5 ----- net/ipv4/tcp_bpf.c | 16 +++------------- net/ipv4/tcp_ulp.c | 29 ++++++++++++++++++----------- net/tls/tls_main.c | 30 ------------------------------ 4 files changed, 21 insertions(+), 59 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index c5348b358290..d2a791810e65 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2881,9 +2881,6 @@ struct tcp_ulp_ops { /* initialize ulp */ int (*init)(struct sock *sk); - /* update ulp */ - void (*update)(struct sock *sk, struct proto *p, - void (*write_space)(struct sock *sk)); /* cleanup ulp */ void (*release)(struct sock *sk); /* diagnostic */ @@ -2901,8 +2898,6 @@ void tcp_unregister_ulp(struct tcp_ulp_ops *type); int tcp_set_ulp(struct sock *sk, const char *name); void tcp_get_available_ulp(char *buf, size_t len); void tcp_cleanup_ulp(struct sock *sk); -void tcp_update_ulp(struct sock *sk, struct proto *p, - void (*write_space)(struct sock *sk)); static inline bool tcp_kcm_attached(const struct sock *sk) { diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index 788ce08eef4d..2fd10b003ec3 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -794,19 +794,9 @@ int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) } if (restore) { - if (inet_csk_has_ulp(sk)) { - /* TLS does not have an unhash proto in SW cases, - * but we need to ensure we stop using the sock_map - * unhash routine because the associated psock is being - * removed. So use the original unhash handler. - */ - WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash); - tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space); - } else { - WRITE_ONCE(sk->sk_write_space, psock->saved_write_space); - /* Pairs with lockless read in sk_clone_lock() */ - sock_replace_proto(sk, psock->sk_proto); - } + WRITE_ONCE(sk->sk_write_space, psock->saved_write_space); + /* Pairs with lockless read in sk_clone_lock() */ + sock_replace_proto(sk, psock->sk_proto); return 0; } diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c index b58045df101e..b50ca0576352 100644 --- a/net/ipv4/tcp_ulp.c +++ b/net/ipv4/tcp_ulp.c @@ -12,6 +12,7 @@ #include #include #include +#include #include static DEFINE_SPINLOCK(tcp_ulp_list_lock); @@ -100,15 +101,6 @@ void tcp_get_available_ulp(char *buf, size_t maxlen) rcu_read_unlock(); } -void tcp_update_ulp(struct sock *sk, struct proto *proto, - void (*write_space)(struct sock *sk)) -{ - struct inet_connection_sock *icsk = inet_csk(sk); - - if (icsk->icsk_ulp_ops->update) - icsk->icsk_ulp_ops->update(sk, proto, write_space); -} - void tcp_cleanup_ulp(struct sock *sk) { struct inet_connection_sock *icsk = inet_csk(sk); @@ -147,11 +139,26 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops) if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN) goto out_err; - err = ulp_ops->init(sk); + /* A ULP and a psock exclude each other. sk_psock_init() checks + * icsk_ulp_ops and sets the psock under the same lock. + */ + err = -EBUSY; + write_lock_bh(&sk->sk_callback_lock); + rcu_read_lock(); + if (!sk_psock(sk)) { + icsk->icsk_ulp_ops = ulp_ops; + err = 0; + } + rcu_read_unlock(); + write_unlock_bh(&sk->sk_callback_lock); if (err) goto out_err; - icsk->icsk_ulp_ops = ulp_ops; + err = ulp_ops->init(sk); + if (err) { + icsk->icsk_ulp_ops = NULL; + goto out_err; + } return 0; out_err: module_put(ulp_ops->owner); diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c index d24e86e7f42d..ac729b569180 100644 --- a/net/tls/tls_main.c +++ b/net/tls/tls_main.c @@ -631,17 +631,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval, int rc = 0; int conf; - /* TLS and sockmap are mutually exclusive. A socket already in a - * sockmap (i.e. with a psock attached) cannot be upgraded to TLS. - * sockmap rejects TLS sockets already (see sk_psock_init()). - */ - rcu_read_lock(); - if (sk_psock(sk)) { - rcu_read_unlock(); - return -EINVAL; - } - rcu_read_unlock(); - if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info))) return -EINVAL; @@ -1087,24 +1076,6 @@ static int tls_init(struct sock *sk) return rc; } -static void tls_update(struct sock *sk, struct proto *p, - void (*write_space)(struct sock *sk)) -{ - struct tls_context *ctx; - - WARN_ON_ONCE(sk->sk_prot == p); - - ctx = tls_get_ctx(sk); - if (likely(ctx)) { - ctx->sk_write_space = write_space; - ctx->sk_proto = p; - } else { - /* Pairs with lockless read in sk_clone_lock(). */ - WRITE_ONCE(sk->sk_prot, p); - sk->sk_write_space = write_space; - } -} - static u16 tls_user_config(struct tls_context *ctx, bool tx) { u16 config = tx ? ctx->tx_conf : ctx->rx_conf; @@ -1234,7 +1205,6 @@ static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = { .name = "tls", .owner = THIS_MODULE, .init = tls_init, - .update = tls_update, .get_info = tls_get_info, .get_info_size = tls_get_info_size, }; -- 2.56.1