Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3978512pxv; Mon, 5 Jul 2021 10:20:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylRkS3Q7ZrKsa/aZWenUezkUMDQolVoGNhBcGBMBfqTdE/fpMriOIS/n/3jzpcFR4C9vMV X-Received: by 2002:a17:906:d14a:: with SMTP id br10mr7221212ejb.405.1625505608514; Mon, 05 Jul 2021 10:20:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625505608; cv=none; d=google.com; s=arc-20160816; b=jtmCRx2dcmzPbBw676WSi6ssNtJOa+1Y2DKRboq8DB/4tIgIFBQ8DkrJy9fY+wliR0 WKvQkbR/d89jCvBluGudVYflua/sx/R0kJ3u6pBpYvXxXkRhfDO+jKEW5jM79TKjmCA0 4ay4gwF3TSJ5OfZqsayex97hJGyxcnZyLPX8l1eAulumZGzaDLuYBGLbbdSNzQ6olSI7 a5OcyHgS/CvBdpCe2vWzT2TVeK4MS9js+ZpXwFA49B8lRFBmzTZYEKOcePgP3/670zAa zUtcKqSFtc4ZnJrgY0yR7LkaEBnbRQstBkLTtG+EdUCJF4kXiRimZYO5e7BxrgHESpuO kVsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=8AsOPg4uvziFNGy4LwJbOa0Julbheb6cKC2U9CBvyi8=; b=gqKLiDb8Wh/2D7HS9/DIAeeCcrec/4C2iEhu9BR4Ri+xpPAP8OWPVXpo6VLGyNWHNF 4Ep2Y6NXCorEloM2jwp6BzKjxTCJ2dnDATa62QEurG6sFFrIPF7/h+SPmh7T6rFpHyWB IzkATUPC/N9xcv4HgvBQGADFFXipuZlfJNewIBkAqgDMcm7luRAJtGAqIXqH03yHl0Y3 lmSwNlu12lS+lJGChSdKTxiGnJq5/EOpBDMK4Ds5TbMMMQB46MS/CJh1fPEHiPKzVIiT BXrUuUK/9Nf0s4RHY1KUEWoFpn5H8JZktNO+EtJOKytNn4MBosA2pRRAil+Dw8kTJRLI dW7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=upNQc2Cc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m19si4627019edr.133.2021.07.05.10.19.41; Mon, 05 Jul 2021 10:20:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=upNQc2Cc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbhGERVU (ORCPT + 99 others); Mon, 5 Jul 2021 13:21:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229686AbhGERVT (ORCPT ); Mon, 5 Jul 2021 13:21:19 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B1D9C061574; Mon, 5 Jul 2021 10:18:42 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id y17so18846003pgf.12; Mon, 05 Jul 2021 10:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8AsOPg4uvziFNGy4LwJbOa0Julbheb6cKC2U9CBvyi8=; b=upNQc2CcpFBftWiYE6Px2uScC9wQC67aHGAdWpMcWEUpbU+a9XaRFECmqLlUbxDM9m uP6wB+P6LsRPHlGn+k4pZtEx/R5+oTlbl9Y5VF+a+fyos7kwxCm1RD6yNutR/P+IFiyn 4TGigeKDNE6KCBnH0PfR8LpOW/uYhBUny3CcQHmODyuZUa9qM9X8QugLAl6UD5X1TtJF y+V8mD9x/ciCVuqqzYAFx8AV2J0iD/3vJM9wnDGBICaRyRmFCweTbkkINDU3NHDEltzg /lFSS53bYMQvFfxyeTuvBxa3aIw7FBn1DwzjRersxWRKyJOiQxFhofUjqX/pOvZMpCja ELSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8AsOPg4uvziFNGy4LwJbOa0Julbheb6cKC2U9CBvyi8=; b=IJFRBLW/3N1ke0IGS+5sG8PGK9DdeLG8lYvOY/EjwI9POLaVQzk9Qm1uU6ewjec3Xq cAaq3WJ3sF116/aDy+UtG0O9WSlwJzs47DO309Pl3VKUCxpee0GVyJ6hCKBBFhtXZ5/Y vBs8USkwPogJ/BT1BFaPvuKtM4TbEwiDhEvNTXn8Aw8Xqi93OxWZais0FV30K2OJek8i zu17o+/Rt9bkjdZpLJdwV6uzSbOpS5S79m0pdlOlWNQmvCeYoEpX4tPBKrophVwL1/Vu C49N+S90cnrnZuq9lnbDE0QCqSE+ZSFNsgNo7b6j+FAQHNLhgDvbpk2xcGr1nUoITnB8 b5gQ== X-Gm-Message-State: AOAM530QoCH73oe+PEekuxnUy8SIoToSxzuzapuphu48lUEaufGFgSJR 9TdQ0IZizrIhdgEQ+gfYjUo= X-Received: by 2002:a63:e214:: with SMTP id q20mr15107616pgh.437.1625505521847; Mon, 05 Jul 2021 10:18:41 -0700 (PDT) Received: from pn-hyperv.lan (bb42-60-144-185.singnet.com.sg. [42.60.144.185]) by smtp.gmail.com with ESMTPSA id w123sm14059300pff.152.2021.07.05.10.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 10:18:41 -0700 (PDT) From: Nguyen Dinh Phi To: yhs@fb.com, edumazet@google.com, davem@davemloft.net, yoshfuji@linux-ipv6.org, dsahern@kernel.org, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, ycheng@google.com, ncardwell@google.com, yyd@google.com Cc: Nguyen Dinh Phi , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, syzbot+f1e24a0594d4e3a895d3@syzkaller.appspotmail.com Subject: [PATCH v5] tcp: fix tcp_init_transfer() to not reset icsk_ca_initialized Date: Tue, 6 Jul 2021 01:18:23 +0800 Message-Id: <20210705171823.524171-1-phind.uet@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This commit fixes a bug (found by syzkaller) that could cause spurious double-initializations for congestion control modules, which could cause memory leaks orother problems for congestion control modules (like CDG) that allocate memory in their init functions. The buggy scenario constructed by syzkaller was something like: (1) create a TCP socket (2) initiate a TFO connect via sendto() (3) while socket is in TCP_SYN_SENT, call setsockopt(TCP_CONGESTION), which calls: tcp_set_congestion_control() -> tcp_reinit_congestion_control() -> tcp_init_congestion_control() (4) receive ACK, connection is established, call tcp_init_transfer(), set icsk_ca_initialized=0 (without first calling cc->release()), call tcp_init_congestion_control() again. Note that in this sequence tcp_init_congestion_control() is called twice without a cc->release() call in between. Thus, for CC modules that allocate memory in their init() function, e.g, CDG, a memory leak may occur. The syzkaller tool managed to find a reproducer that triggered such a leak in CDG. The bug was introduced when that commit 8919a9b31eb4 ("tcp: Only init congestion control if not initialized already") introduced icsk_ca_initialized and set icsk_ca_initialized to 0 in tcp_init_transfer(), missing the possibility for a sequence like the one above, where a process could call setsockopt(TCP_CONGESTION) in state TCP_SYN_SENT (i.e. after the connect() or TFO open sendmsg()), which would call tcp_init_congestion_control(). It did not intend to reset any initialization that the user had already explicitly made; it just missed the possibility of that particular sequence (which syzkaller managed to find). Fixes: 8919a9b31eb4 (tcp: Only init congestion control if not initialized already) Reported-by: syzbot+f1e24a0594d4e3a895d3@syzkaller.appspotmail.com Signed-off-by: Nguyen Dinh Phi --- V2: - Modify the Subject line. - Adjust the commit message. - Add Fixes: tag. V3: - Fix netdev/verify_fixes format error. V4: - Add blamed authors to receiver list. V5: - Add comment about the congestion control initialization. net/ipv4/tcp_input.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 7d5e59f688de..84c70843b404 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5922,8 +5922,8 @@ void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb) tp->snd_cwnd = tcp_init_cwnd(tp, __sk_dst_get(sk)); tp->snd_cwnd_stamp = tcp_jiffies32; - icsk->icsk_ca_initialized = 0; bpf_skops_established(sk, bpf_op, skb); + /* Initialize congestion control unless BPF initialized it already: */ if (!icsk->icsk_ca_initialized) tcp_init_congestion_control(sk); tcp_init_buffer_space(sk); -- 2.25.1