Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3511909pxf; Mon, 15 Mar 2021 11:10:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9O1qAPZFjVrzGR/Hm254hUTmy/LbK0p7B6sd9CuuDxOFVa79U3v7Dh24A49rC/UZrjbXT X-Received: by 2002:aa7:c654:: with SMTP id z20mr20282598edr.377.1615831814605; Mon, 15 Mar 2021 11:10:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615831814; cv=none; d=google.com; s=arc-20160816; b=FQsZTVijegCQEk9NmZOLYIrgi5QZOWB2wxLvcepSfpndKfjqhf+8g94Xq4L75iW4jb InIBmvU9P1pWNY7opdCOyEka+iyG3F6ZsN1cTV+eb+S5p4GnSfIQOc+nlAYZ0V1QBYcC ZK35FtHiyudW7CwIZZsDYKuwjZh86QMg7S9ApFiIQ8vTQYhQvnYQH6BYeWqHsm0SDM/X 0knbR/jilcl0as6RLFAJfeULJXdzWgXPQ0GNJ/isfEqBmwjEr2hdX7efhoHA3hpwM8FO sCPDG65DksQ9VW88fNkbQs3qLFLdUjH4ZLLuU8AVCXprF4B4ld7eWvfost3kuwHGoAfv 5jvA== 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=ohuSsb2e6FDwdV9jjZFRqRvRjAqF1iGvDvLw05Njjc0=; b=FCr4NvKo4RCf9Tf2zeTzLQ/YUirjl137tEbWclCz7OaK6USpBkRntAmeK1ndO5ax+Q OKRqdfGVuE/QJcpLAhKFPGxHWmqeqw851HyE0QZdHFJvGZskFbX9Sd+uIQHznfMSW8IP YSrm7WKrZzB6ltaKOFgfuc61/bmPkRGJqnCPflUKT2yAxL+37tNDe9+vW7kNo/LfWSaG O+5cmiphyjENPJhqTifpN30wpVJsE5q5YreAXtVrx59v6V9yXS68xQfo9Rg4brMSJTS8 zO4KVqAusezctcNBPk2DVpX5rtenzqeyvB2+BOzPcV0Hart5UOGWdXb60htNwf8Jof1/ DyPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JyRrKysM; 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 k12si11656640ejq.734.2021.03.15.11.09.49; Mon, 15 Mar 2021 11:10:14 -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=JyRrKysM; 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 S235697AbhCOOKQ (ORCPT + 99 others); Mon, 15 Mar 2021 10:10:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:37540 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232287AbhCON6W (ORCPT ); Mon, 15 Mar 2021 09:58:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 053B364F16; Mon, 15 Mar 2021 13:58:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816697; bh=cWYpPuK4cVYNfdhQeETw55m2OhHl72Tn093ipU2PKkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JyRrKysMOC5WE9Mq+HN2YyQO/JuCvIzy7ULBxW9HEtGKLFWzhMohJiATYx9y5IzJK ke/0w1GRN3d7ppr2G46tdIIvyiRp8srYw0M7hZ7SCD04/TZX1dQ6AetqfnRzNOGT+B 1/5vv4kxSLzToEstGBHUcb8GdPOu4m3L4rWJMGZg= 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 4.19 013/120] tcp: add sanity tests to TCP_QUEUE_SEQ Date: Mon, 15 Mar 2021 14:56:04 +0100 Message-Id: <20210315135720.451315222@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135720.002213995@linuxfoundation.org> References: <20210315135720.002213995@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 [ Upstream commit 8811f4a9836e31c14ecdf79d9f3cb7c5d463265d ] 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 @@ -2886,16 +2886,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: