From patchwork Mon Mar 1 18:29:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12110471 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49338C43381 for ; Mon, 1 Mar 2021 18:33:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 20E4164F52 for ; Mon, 1 Mar 2021 18:33:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239996AbhCASc5 (ORCPT ); Mon, 1 Mar 2021 13:32:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239826AbhCASaH (ORCPT ); Mon, 1 Mar 2021 13:30:07 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 237F2C061788 for ; Mon, 1 Mar 2021 10:29:23 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id e9so125675pjj.0 for ; Mon, 01 Mar 2021 10:29:23 -0800 (PST) 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=LAvQgxDafwb7Ugh60Y/bOqRllkB+AWtaculCCpPGN78=; b=iC7pPSiSkWiy6Jv5wqth7KUuJ0WOBVFS5mNlAS0GQxJNTJSyA/gntjbhRsOzE4sox+ Jcz+Nf+Hsfk5ot0duB2abJlFksHI0H5NE4VzJzxhtw76FYe53mfXtCz337nUli04a9PC Vb+llGH5mkbxmNxqzz12uu5HR//h1uYMgGuAa1gsSQ32w2JKlSo7WOnrswSUeA+dZzb/ 3H9GVoPk/86/wDPUqmPQor2GURxI93cJWZ52c+EjxHAvXBEN5VOS1ZZBYZkqJJ/y0G0I E2LQ3jrkFvG9K2jSUYN8/ArzL9ALFxGmDRpKOTP1QOVvj6cALd9q2nTHo3l+ClRxZGI+ 8sQw== 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=LAvQgxDafwb7Ugh60Y/bOqRllkB+AWtaculCCpPGN78=; b=chMUnSTA1yaX2yjmGC32VShQsKo+XDgHF54l5H30dZgJE0KPzmjnjR0+TNxcDRaF93 aHPwt1RR/5e0hPrDJG6i3DoXVTyp7P1ajbGZeSXBDrU7AqlLhUqKuQfSKBV2WiorkeW8 PnM9H87Nts5x0pNXm1wR+Dv7KN+uSWRO/u1zHvbmgaIMCdbQRL0fA+jKUOMZmIDsQeWu dlIiaSc1hI+t++lD/2SMdAYlRs36sA6zzyI2VQEB29ETHG1YOM6hKvDTzooq6nP85LpL PRVIeKFvgdfhPf4tgjn0Iid622dntzBJ5TlddDlGoIG6ZKemrZHxEBH/rnqZRxQZsyks JUXg== X-Gm-Message-State: AOAM533PUN6C4SRj3yOYHFKla5ylzbu1vrlv2cjIHIdzPOp55MUdHkyZ GZsc5pS0RjFZm7MQ2Z2MRnM= X-Google-Smtp-Source: ABdhPJwjjnW/C7e274Jjr7+0kI+HJisPH5Ajr973+MS3XypPbTygU74jVM0dzYgY7eYnjcNrfL6UyQ== X-Received: by 2002:a17:90a:bf0e:: with SMTP id c14mr252412pjs.38.1614623362737; Mon, 01 Mar 2021 10:29:22 -0800 (PST) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:45b6:ff74:c40e:478a]) by smtp.gmail.com with ESMTPSA id g8sm19513662pfu.13.2021.03.01.10.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Mar 2021 10:29:22 -0800 (PST) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski Cc: netdev , Eric Dumazet , Eric Dumazet , Pavel Emelyanov , Qingyu Li Subject: [PATCH net] tcp: add sanity tests to TCP_QUEUE_SEQ Date: Mon, 1 Mar 2021 10:29:17 -0800 Message-Id: <20210301182917.1844037-1-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet Qingyu Li reported a syzkaller bug where the repro changes RCV SEQ _after_ restoring data in the receive queue. mprotect(0x4aa000, 12288, PROT_READ) = 0 mmap(0x1ffff000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x1ffff000 mmap(0x20000000, 16777216, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x20000000 mmap(0x21000000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x21000000 socket(AF_INET6, SOCK_STREAM, IPPROTO_IP) = 3 setsockopt(3, SOL_TCP, TCP_REPAIR, [1], 4) = 0 connect(3, {sa_family=AF_INET6, sin6_port=htons(0), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_scope_id=0}, 28) = 0 setsockopt(3, SOL_TCP, TCP_REPAIR_QUEUE, [1], 4) = 0 sendmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="0x0000000000000003\0\0", iov_len=20}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 20 setsockopt(3, SOL_TCP, TCP_REPAIR, [0], 4) = 0 setsockopt(3, SOL_TCP, TCP_QUEUE_SEQ, [128], 4) = 0 recvfrom(3, NULL, 20, 0, NULL, NULL) = -1 ECONNRESET (Connection reset by peer) syslog shows: [ 111.205099] TCP recvmsg seq # bug 2: copied 80, seq 0, rcvnxt 80, fl 0 [ 111.207894] WARNING: CPU: 1 PID: 356 at net/ipv4/tcp.c:2343 tcp_recvmsg_locked+0x90e/0x29a0 This should not be allowed. TCP_QUEUE_SEQ should only be used when queues are empty. This patch fixes this case, and the tx path as well. Fixes: ee9952831cfd ("tcp: Initial repair mode") Signed-off-by: Eric Dumazet Cc: Pavel Emelyanov Link: https://bugzilla.kernel.org/show_bug.cgi?id=212005 Reported-by: Qingyu Li --- net/ipv4/tcp.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index dfb6f286c1de92aa505ba3554018ecaa3b94fc40..de7cc8445ac035ff2f7d17b93acabd7342144da8 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3469,16 +3469,23 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, break; case TCP_QUEUE_SEQ: - if (sk->sk_state != TCP_CLOSE) + if (sk->sk_state != TCP_CLOSE) { err = -EPERM; - else if (tp->repair_queue == TCP_SEND_QUEUE) - WRITE_ONCE(tp->write_seq, val); - else if (tp->repair_queue == TCP_RECV_QUEUE) { - WRITE_ONCE(tp->rcv_nxt, val); - WRITE_ONCE(tp->copied_seq, val); - } - else + } else if (tp->repair_queue == TCP_SEND_QUEUE) { + if (!tcp_rtx_queue_empty(sk)) + err = -EPERM; + else + WRITE_ONCE(tp->write_seq, val); + } else if (tp->repair_queue == TCP_RECV_QUEUE) { + if (tp->rcv_nxt != tp->copied_seq) { + err = -EPERM; + } else { + WRITE_ONCE(tp->rcv_nxt, val); + WRITE_ONCE(tp->copied_seq, val); + } + } else { err = -EINVAL; + } break; case TCP_REPAIR_OPTIONS: