From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796101775; b=1J+7b1PMUnZoxY1LJpGGSG1hmGRjAazdQbwBRGhwAMehgRfHrZJf2VeZuprk0vVci9+r RNJ2wCtKUPxfT9dyQudlpvJjZIvrwoK7jhrDLwuPQifl/bby6gOiQOHkNtT1wNnaSgX62 sOo6r/BMDfI3ixW2BhmLJycqkowPq2Kx5h2ZSr0MO3EesCHkAVrEG3bGM5Xfhuh5EURY7 hnGq7ngjnudRLphG7D7WXQfh058lraeohadZOCyse0PZDZBCvVNfzqxje+WUjlM6kF6aK p3yiSmx3BN2WqvcEqXhCsbDqahF4oFBbs2tI8P9tVrXpHZqsf7umYQ7kc8xvotiWMPw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796101775; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Content-Type:Date:Message-Id:Cc:Subject:From:To:In-Reply-To: References:Content-Transfer-Encoding:MIME-Version; bh=R2O1RV2SfXAyYWM03iN3ZTM9CPisSk/IoyZ7pEglTmo=; b=222NeGgC6gDiwvghmX/xP7/aRZ1U1W3AZVn1eunubQWAhCyOlVEio89so4GDjT+3CudM TEMY5ussKQvF9Jj8wzzm7/Nc5GQm2vmbgVRfvCDBEMV/Omtin0cSseLuUAszdXzwXkf8y 0fhj2WF7CGex+IsptVwwNdTdyfQ3JtEkKEoqWrsGGUr6CePwYDdmvZAXJm3d916dH2D2Y 1W6/8rs683gI4sRpmz5JBSAOixdLppwIDn/I6OlC2UzDAPPyC+UGI3Fk3t7/RF4NZY+Zp jRih2cE/PsY2tdjeuC+ZoX8447cYOElpPNe8KML7/Gg+2yiECoBb6jz9L/KgVVnzdRg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Zkm1k0iI; arc=none smtp.remote-ip=209.85.215.181 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A687D606A320 for ; Tue, 1 Dec 2026 05:09:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A687D606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Zkm1k0iI Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-cde1479f76fso6000022a12.0 for ; Mon, 30 Nov 2026 21:09:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796101774; x=1796706574; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:references:in-reply-to:to :from:subject:cc:message-id:date:content-type:from:to:cc:subject :date:message-id:reply-to:content-type; bh=R2O1RV2SfXAyYWM03iN3ZTM9CPisSk/IoyZ7pEglTmo=; b=Zkm1k0iIHKejSki0l//RjyxznXa39qMLDdr1e+kYmN3j1Qy1Xk/gqW09eQPpBsse5j xpZFqSPtaHv6mTvI3fd7DAzlRAo4JIag7Q8zyHXkVDcAbRgOFln5PHD7b3GCJGrScrn2 7tiURtCS+b4JVDFDNtxpdeXLZAEnNQ60KvLpeVvtiNY1v2T7xX5QTrj+75LHz2kbTAaj ki4kE7bzEg9wlGc5YP+JRJqff3hCBl2BEdAucDJqsCWFvA2pCd0N5DDDL5atIhPW7zX/ +k/D/BLvUYM/qhIO7FHsqWOHhWkfmePYJl4D5Ld1Ga7cCBLDNwHecPWdLg2ArlM2WFFT +87A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796101774; x=1796706574; h=mime-version:content-transfer-encoding:references:in-reply-to:to :from:subject:cc:message-id:date:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=R2O1RV2SfXAyYWM03iN3ZTM9CPisSk/IoyZ7pEglTmo=; b=NzhIojCxSYADir0rrdi79Vr7gM8jvXpckLCtogxzs48iHd3PBDtXDjdRemazKnKKCN hMS8YkY6+ayfCU90I/5wXNXyy3B2bct/b4d8p3ECbLh6t7HVfdFlqNa5N5iqCcgQ/OQS T6CTk4mNd1jAp46+V5aU/M0wqdPI6n0RN4x9WUFaUCyxUQNGNVwDbXPYe7nzuMTNBIDj LPTwbkLTHejgr6zZ0fzrBw9P6bqW+/gSs5nTZQY79wx671XtdiyV6iHJ7BC7gEe3T84r isjuEYGwoGrpvsbucDcYQHJSm0GF1Vb7eX1Zuxkq7rLjTAcdb6w71CMl6qg1q8m9q5pq Jd3Q== X-Forwarded-Encrypted: i=1; AL+hErHd8wgX/tXfAmg6sK3c8C9n55WWr/ZlaY2/Ym9pjAUBtFgHH17EUNN+nmFc6kc81+OXzYY=@vger.kernel.org X-Gm-Message-State: APuU4Z+maFgrH+UZUd0Yweo8ENfQueZK6kV3WhhUqHlla+bnKfonoSBy 0B45xruy5x4PUnjEu6Rr/NhoIiU5QYxMmAv3ttx9gHph0apl8/R9LU9xrgEzIQ== X-Gm-Gg: AZFdTZfOk22xz1TJZ9bJu0BhJuamSmZ5qmjaS98+Bl2zyylNC23l9uMaNeRIGVRjIdd 5SQOi9btIlRrBEYhgSLfk2Mu5XfmJon81kKNdNpEVtGTwooM4NRh3d/ddgLKRyx0KHhFHgKskrA aFfHN3RAVmxL0rz+sfdzT/iYBlXtdPcm52X1ji7owGdof1ZLB1l67hMGgPliWmitHOGO/W64SQA yO0FqO41NDGyFz+A/vHGH7b0YpteRk7XZs72LU28vbqtBlo0lBs4lEuVoEcUm/Bj0zGUXiyG/fB eJ2jXQCt+oBd9FF+PS+aLfVwJqyysaQcS6XtBJSKfV95sMf04FEk8MPF3l4Mls61hiRzFXNBlqn z1RfrdOdk082+dEblT5ItywvzuB5o3xhk8UNkVci0j5mfzx+LcBa4UuHEgZRtbbXHIsrxVYrQ8n b0K05eQ6/2io/m/zy1UeDuxl80MliK5HM0n0jcyhWS9+rF4xYLzay7A4f5JnBZHp0Eu+j6J7gMT Lvbv1SQlazQMbTUvFFv5d8SmAR35f7FCjCQtApDsOWGwy5KMQbre4XanFDrCp4q0obcAmbQY3zL l1g3QeZrQoDTgPmrMZibJlIvlZ2y3ZUOohcMTvuXXgXhp+YAP2cQy3+uphhWBY/BrTvL31Zbm9D 4KZpPVCg74JSYKe5y36WYcQvrxaLM85NlojQ= X-Received: by 2002:a17:90b:4983:b0:3b0:97b8:d6b7 with SMTP id 98e67ed59e1d1-3b41c931d29mr37271604a91.42.1796101773609; Mon, 30 Nov 2026 21:09:33 -0800 (PST) Received: from localhost ([153.61.198.253]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b405ecb486sm58422335a91.16.2026.11.30.21.09.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 21:09:33 -0800 (PST) Content-Type: text/plain; charset=UTF-8 Date: Tue, 01 Dec 2026 05:09:32 +0000 Message-Id: Cc: , , , , , , , , , , Subject: Re: [PATCH bpf] bpf, sockmap: Don't replace a ULP's sk_prot when leaving a sockmap From: "Alexei Starovoitov" To: "Sechang Lim" , In-Reply-To: <20261130055650.686483-1-rhkrqnwk98@gmail.com> References: <20261130055650.686483-1-rhkrqnwk98@gmail.com> Content-Transfer-Encoding: 8bit X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 On Sun, Nov 29, 2026 at 09:56 PM Sechang Lim wrote: > A socket can get the TLS ULP after it was added to a sockmap. If the > map has an sk_skb program, removing the socket from it calls > tcp_bpf_update_proto() with restore=false, which puts a tcp_bpf proto > in sk->sk_prot over the TLS one. The first sentence is the bug. Lets fix it there. TLS and sockmap exclude each other since commit 460e6486617c ("tls: reject the combination of TLS and sockmap"). A socket with a psock can't get TLS keys. So the ULP on top of a psock has no use. It only breaks things: where effect ------------------- ------------------------------------------------ sock_map_del_link() tcp_bpf proto over the TLS one, close() spins sk_psock_drop() sk_write_space stays sk_psock_write_space() tls_build_proto() global tls_prots[] is built from tcp_bpf_prots[] This patch fixes the first row only. The second row needs the same steps: sk_skb prog, ULP, delete from the map. sk_psock_write_space() finds no psock and calls nothing. ACKs no longer wake a send() that waits for sndbuf space. Refuse the ULP in __tcp_set_ulp(), like espintcp_init_sk() does. 460e6486617c checks the psock when the keys are set, because of a race. __tcp_set_ulp() sets icsk_ulp_ops after ->init(), outside of sk_callback_lock. sk_psock_init() can run in between and see no ULP. The diff sets icsk_ulp_ops before ->init(), under that lock. User space gets -EBUSY from TCP_ULP, not -EINVAL from TLS_TX and TLS_RX. tls_update(), tcp_update_ulp() and the ULP branch of the restore path go away. The kernel part compiles. I didn't build or run the selftest: diff --git a/include/net/tcp.h b/include/net/tcp.h index 869e780..9917a88 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 788ce08..2fd10b0 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 b58045d..63f30fe 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,25 @@ 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); + if (!sk_psock(sk)) { + icsk->icsk_ulp_ops = ulp_ops; + err = 0; + } + 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 d24e86e..ac729b5 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 @@ out: 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, }; diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 34737e8..749b41b 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -118,12 +118,6 @@ close: static void test_sockmap_ktls_enable_fails_when_in_sockmap(int family, int map) { - struct tls12_crypto_info_aes_gcm_128 crypto = { - .info = { - .version = TLS_1_2_VERSION, - .cipher_type = TLS_CIPHER_AES_GCM_128, - }, - }; struct sockaddr_storage addr = {}; socklen_t len = sizeof(addr); struct sockaddr_in6 *v6; @@ -165,14 +159,8 @@ static void test_sockmap_ktls_enable_fails_when_in_sockmap(int family, int map) if (!ASSERT_OK(err, "sockmap update elem")) goto close; - /* Installing the TLS ULP is allowed, it does not touch the datapath. */ err = setsockopt(s, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls")); - if (!ASSERT_OK(err, "setsockopt(TCP_ULP)")) - goto close; - - /* Enabling the TLS crypto datapath must be rejected. */ - err = setsockopt(s, SOL_TLS, TLS_TX, &crypto, sizeof(crypto)); - ASSERT_ERR(err, "setsockopt(TLS_TX)"); + ASSERT_ERR(err, "setsockopt(TCP_ULP)"); close: close(s); pw-bot: cr