Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3322467pxf; Mon, 15 Mar 2021 07:07:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDBv3aqN9tnDEaHQt4mnGNF33eWHqGH9VkCmAIfAxIrKci+E6jmHgGXSDdGQQi0zHh2n7Q X-Received: by 2002:a17:906:2dda:: with SMTP id h26mr23516391eji.163.1615817249157; Mon, 15 Mar 2021 07:07:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615817249; cv=none; d=google.com; s=arc-20160816; b=CsX8Ggh3ZKAIx5fNDz34bIA6IzVvKotYugKBP6uvi4Ek52jN3rndURj7we6nbcRWTu jTj4NDL5BF3qv/oX703jsyEXETcCjSkh3NHR4VpYUJ1PHVh3MoPTaxC6ASjX7AplKd3E UpEsLFmqCOyJmTodHDQ6CesfuKZD4VOG2DaNRV+qCOd4f+CpFCIKXrb2H8vnDJBahtUq UWdXRwxorkAuw5/ACuBr10NDe+Vy3RQHZ9QH63OipYo97ReFxrwWHTgUTTymz1slrc3v mb/4K1gCSWl4EpX8H+MXIXSopMqe7X5LquzNh2SZOPncrosfjOichWxm6qVzbu1vpnpu Vrqg== 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=88Zh/3pKhBNDTUQ4EYz8djEctYMdo1fX1c36m7JpELA=; b=IE8uobAnL2zqtGRgsic5nCYnximLHIqOPVCwxLopWWbs4QSVMgxfg1640uBbkZLK5B 9w45gMaYxw4bZ1ZnT4Eb44V24E0mWUUoyNpJ4lEVbdeWC9KH+L7t2+B4lyMUKuELw82g hEX6u3J9vHOUqwCPowxa7KP+dPBGMLcT3++wTsAGAQ4sLq7Kw9uaN+q8NdEqfPQrDt3x f0/CuC6mXrzGDqJxmJbfwqARUZeGBbKmBettJRK3MuK6uYWeDGZSiordUTtm3QEO5bVK l2rHWhQxcpMJ4nx8Ka/bn/YJzoy+/xfXyHaijKMu6b/5ntEtucrOY57wKz4EjzWm5PIe UjSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="RE2Zg5X/"; 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 j12si11142797edp.565.2021.03.15.07.07.05; Mon, 15 Mar 2021 07:07:29 -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="RE2Zg5X/"; 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 S233351AbhCOOD6 (ORCPT + 99 others); Mon, 15 Mar 2021 10:03:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:34096 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231807AbhCON5O (ORCPT ); Mon, 15 Mar 2021 09:57:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7799964F05; Mon, 15 Mar 2021 13:56:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816619; bh=/+tUiVkxhsq5nvMtpV03ucQMrUlxNoCwFo1NCUVR0HM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RE2Zg5X//lmBZ0TZ/EdysH24qOBImVCtperUFB5l0LghtA+/In7iYWpmS3NTILX5r 0yGIhMSsUtKa9vm+STESWFNuUOJmXOj2VbEI68myyMFBI3FijxO/U+MVrLc4mCiSXv 678y3oRHEzboihCqI5uzPVAIjsjFcbBGL7KV71d8= 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.11 024/306] tcp: add sanity tests to TCP_QUEUE_SEQ Date: Mon, 15 Mar 2021 14:51:27 +0100 Message-Id: <20210315135508.445397367@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135507.611436477@linuxfoundation.org> References: <20210315135507.611436477@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 @@ -3431,16 +3431,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: