From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yx1-f43.google.com (mail-yx1-f43.google.com [74.125.224.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA009402BA1 for ; Fri, 24 Jul 2026 10:39:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784889570; cv=none; b=BAqXKSxHXaTNXJ1iyp0PDWuCn4VPOlsie1LdlLkXbKYVCQLtWC9ViNij71meadmLwf34wzw1O2cPk00PyB2jhIMFr0dg/XkX++O+stgqf3YrG9FtcKaoFqRWlri9Ylods86QD3a4vMf+tG2uyf/GRfFV3Ux8fZbOzUp9lAIFf9Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784889570; c=relaxed/simple; bh=eKd9pCQK8yq/2taFlLxedEdPHpo601/nz/qzYpE3TWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O5jcjWyhkQ5BwItGlCcN+AxtUgU8Yxz9uZ4Ibm4mP4SzuOta2pzHGCGjIZaC8WMVPKqhDc4OVSM3kAA514REgORNLvxlEkZlbLLid3FUHfHwtSd0+B6xDI5OJSZOaPBVZ4i4sDHIEcs6b//jsGZg3bkTPR1MqBiFUKNIbOanNv0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fRpfZyOI; arc=none smtp.client-ip=74.125.224.43 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.b="fRpfZyOI" Received: by mail-yx1-f43.google.com with SMTP id 956f58d0204a3-664a5193741so33443d50.1 for ; Fri, 24 Jul 2026 03:39:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784889562; x=1785494362; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=N9RSE/2JZSOuvVzl92N/Hvh+oDgfC4zbCGCsJCtj4yw=; b=fRpfZyOI9xZogkAs2e+FLw/KFHE6GRjMp8hORrC9BV0aYChO5JwYhtuwYzZK13A4Fq FSxdENCJ61EQc+u2QFowdzvbLFLvn+xsvtwVfNZDONGriFO2XOReM6Kbjm7rWIoAmX/q vcS+mZQchYEZmVc9sMGPFqjX0kCKwc6bhk9pmM+L0sN0xAOVQFTU1CqLFHAS12Y8IEi8 Gce23RVayOkKPh+J/kftwSKO+ERkw8o9qvW26sMtakjqFIpj3hnEQ16zKel/plqqXTBQ egDd/JRaeDSh59uwvq2+QS8RBkDrXa5yiHi1iVcGL9NyL6z5gm12pq5WDO8lojPxlnCY 53TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784889562; x=1785494362; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=N9RSE/2JZSOuvVzl92N/Hvh+oDgfC4zbCGCsJCtj4yw=; b=ZHEN8I1fVGPEkXKqrs59rsKjV25/jl+XlYMr9ewrvc1N0xc50GmWZ76f7rW2FJqkGa ga1iBZXACGbkxxXmb1F9/0Ahx04+Aj/dFZMsGmTvdrPbUsv+DI0+CnOAW5IKAuDfcScK GIt5d6kp5DoqROgscO4THH3PbMbLTaf3AyQXz6tvb1vjlEcYluLhzbgDYpovCGMrOmPR Z0KVxUtMuau5L9/R3v2+ZDOG3x9Solmc6drplCbTszwQRbea9bZIZAUc0e/vHOHSYmLf +r06kJdHSoavEtagTnuG5IByKdb/IqKvCtVNOqSrt1NdlFJgqtzLeMvdaLTPU/lrxZ73 MiIQ== X-Forwarded-Encrypted: i=1; AHgh+RoQZeAn7a2yBRnlhxJWl4F4p/DgFlKRQWoLLOZg88/WmLHdrOsZpG5D2aOJUV8zDfe5s0TtisFRURxnxiA=@vger.kernel.org X-Gm-Message-State: AOJu0YxKQcxLVuOfcKRK/vu33WtnuQUb/6XUcOChKP6tZ4O6jpVxstgT v+JyD+ARW8SQujQWzy7WSmhE6FW82dsnobx8gyk19NCnshdUSxKqP1b9 X-Gm-Gg: AR+sD11YcGdeynV2988RZOmDuHmEaYs6pNVeHki9dt4N7F3UhTvhgXbGdmgqLyuSkL6 DzvVaDnF7BSB0Kbj4C4c94X/3Ch5aXnmFuzRNrQshEBLl/u0qtFhhXrl+G/chnftCiKQ31SDMz+ OiIuCMprUJ/lebkJl6XQKuqUiUfKKupmOVX7uIYoRKvNlu5HyMoeqH9zDLNr1b9wGtv3vRkrzfG 5TFlQ1DNputdaA/Cdemw64yUHJ9zzKuMdSNSNP0kD/6u/kuCP7QAdNDdCrfFFQCOPEXira5ht0Q SsQO+LHvx8V0RDELTd9wIL2GZVyo2a9WZTK4iMRLaXmXgAQ69x8tX1TdfTL8nkQ9oOTGBPWUFNU gg0XspO7Bt6dpAKi8OeOTc+lZDSdk4UUAgjXGZsPl6l2l1cLKsyMy+nYyEyuNg+u1KkeU0iZIMV DnxkS58btYuCjOSx/xX+ZXAUXXI9BDp49FAWflqybx6X0WUvgZvVvbJD/aVyFww/bUJjaO X-Received: by 2002:a53:e18e:0:b0:668:99fe:7ba4 with SMTP id 956f58d0204a3-668b2fe1f2cmr632835d50.0.1784889562290; Fri, 24 Jul 2026 03:39:22 -0700 (PDT) Received: from localhost.localdomain (45.78.65.84.16clouds.com. [45.78.65.84]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-66890521642sm4645770d50.3.2026.07.24.03.39.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2026 03:39:21 -0700 (PDT) From: Chengfeng Ye To: Emil Tsalapatis , Eric Dumazet , Neal Cardwell , Kuniyuki Iwashima , John Fastabend , Jakub Sitnicki , Jiayuan Chen , "David S. Miller" , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Chengfeng Ye Subject: [PATCH bpf v4] bpf, sockmap: Fix cork use-after-free in tcp_bpf_sendmsg() Date: Fri, 24 Jul 2026 18:38:56 +0800 Message-ID: <20260724103856.3399001-1-nicoyip.dev@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <87fr18lmzo.fsf@cloudflare.com> References: <87fr18lmzo.fsf@cloudflare.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit tcp_bpf_sendmsg() keeps msg_tx across sk_stream_wait_memory(), which drops and reacquires the socket lock. Its error path tries to decide whether msg_tx names the local temporary message by comparing it with the current value of psock->cork. This comparison is unsafe when two threads send on the same socket: Thread A Thread B msg_tx = psock->cork sk_msg_alloc() fails sk_stream_wait_memory() releases the socket lock acquires the socket lock completes the cork psock->cork = NULL frees the cork reacquires the socket lock msg_tx != psock->cork sk_msg_free(msg_tx) The stale cork is therefore mistaken for the local temporary message and freed again. KASAN reported: BUG: KASAN: slab-use-after-free in sk_msg_free+0x49/0x50 Read of size 4 at addr ffff88810c908800 by task poc/90 Call Trace: sk_msg_free+0x49/0x50 tcp_bpf_sendmsg+0x14f5/0x1cc0 __sys_sendto+0x32c/0x3a0 __x64_sys_sendto+0xdb/0x1b0 Allocated by task 89: __kasan_kmalloc+0x8f/0xa0 tcp_bpf_sendmsg+0x16b3/0x1cc0 Freed by task 91: __kasan_slab_free+0x43/0x70 kfree+0x131/0x3c0 tcp_bpf_sendmsg+0xec3/0x1cc0 msg_tx can only name the stack-local tmp or the shared cork. Check for tmp directly so a changed psock->cork cannot turn a shared message into an apparent local one. Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface") Reviewed-by: Emil Tsalapatis Signed-off-by: Chengfeng Ye --- Changes in v4: - Use the PATCH bpf prefix. - Restore the minimal v1 fix, as suggested by Jakub Sitnicki. - Drop the changes from v2 and v3 for other issues flagged by sashiko. Please queue this fix for stable kernels affected by the Fixes commit. Link: https://lore.kernel.org/bpf/87fr18lmzo.fsf%40cloudflare.com/ Link: https://lore.kernel.org/netdev/20260719161630.2901208-1-nicoyip.dev%40gmail.com/ [v1] net/ipv4/tcp_bpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index 8e905b50dead..a30475afb6f8 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -604,7 +604,7 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) wait_for_memory: err = sk_stream_wait_memory(sk, &timeo); if (err) { - if (msg_tx && msg_tx != psock->cork) + if (msg_tx == &tmp) sk_msg_free(sk, msg_tx); goto out_err; } -- 2.43.0