Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3320430pxf; Mon, 15 Mar 2021 07:05:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJymwt/FMUzF5YXMbd4LvdnRWQdm0ZTA/6UYrXnJYl84cY1MV4go+VyzcihLcYvGQUBujdMO X-Received: by 2002:aa7:d2d5:: with SMTP id k21mr29653310edr.216.1615817128275; Mon, 15 Mar 2021 07:05:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615817128; cv=none; d=google.com; s=arc-20160816; b=Ke/VCh4jVRDClY3HDBWNe+Kkj9gdIuqH2XkAJgTpIuXPDqfrF0qau/JDSyml7LNN6b JfM5npcGl2+lPRQcoE8/aYP8lXNIFAdKhebLL1G3OvvjXXdMea7a7TKbb6mb86RB/PFp SUTr3TC9loE3lba2JjZEa7EzE7/X4qnqXO0akV9R4UnV47lajPZJrg1uEzUgGSlf0Riu 6ra05nvc/ptJHJ7tp5iBDnF/LUOI2jcZ/hr9KxMFW8bu5yLDbd6tH96tRP9jlKSs+Ztd XGN6iX391Yg4yCPz1RmI7eJvoUiMw6UplxlIV+y49jfLjZ9ikIn/bIS5+vIReQwELKJg ixwg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/62cmGDVMppl7uekuSIZavCcW6uC8D+js8f00XB/6hY=; b=W+/vlfWsakk5Vz1qSjiKMkEUWx2FsmvSIfZShM9NPuFg53KywpdKUfYyr9oi+XnaTz 8R1Ny/7TziONN81fbmcr0wlumKbqrr53RtS5aUfDQQtgW2hcxgkihbyE3h8X4//acG3q v2+iyRnryunFLbTvpfnW6ep/BSVaPQglHp3z7kskRsFbp1+uvfMHfFkBWCSYx792ieI8 4L/r2sYyzHoeB+8ptv21ennCbZvBCDqdlvX3sanNjjyHX2QgXkncodOoI+DuJGbyabHw BEGKvw0HWsBbHtyoI80/5gf8DoOB00pARFU23BWeFVRZbLYXgp/hmDBE3NqeaTFzPonC ssFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Wv71est7; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s10si11837479edi.563.2021.03.15.07.05.04; Mon, 15 Mar 2021 07:05:28 -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=@linuxfoundation.org header.s=korg header.b=Wv71est7; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232308AbhCOODw (ORCPT + 99 others); Mon, 15 Mar 2021 10:03:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:34124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231802AbhCON5O (ORCPT ); Mon, 15 Mar 2021 09:57:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0701E64F00; Mon, 15 Mar 2021 13:56:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816620; bh=Tj752T5KVH6A+ShCPQIpgLAwmmH7EX4UNPwrp+k8CT8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wv71est7MDgz7NTGxheOTR0jMXGnN2ayxqJqZwCiMqPcrp2C5at1syt/lotBHqCgv HZCF+dR/NkFyZWhdTLn+f+6Xrz4Th8WXyQBCrKxXJ0PAlRdAsc0jldl57rNjcLYQag 2bI4Lmm7YSyvduXYBen6uxCwjbkAMoSjqwc0xfQg= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Pavel Emelyanov , Qingyu Li , "David S. Miller" Subject: [PATCH 5.10 017/290] tcp: add sanity tests to TCP_QUEUE_SEQ Date: Mon, 15 Mar 2021 14:51:50 +0100 Message-Id: <20210315135542.521992931@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135541.921894249@linuxfoundation.org> References: <20210315135541.921894249@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Eric Dumazet commit 8811f4a9836e31c14ecdf79d9f3cb7c5d463265d upstream. 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 Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/ipv4/tcp.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3164,16 +3164,23 @@ static int do_tcp_setsockopt(struct sock 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: