Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3925571imm; Mon, 8 Oct 2018 11:47:36 -0700 (PDT) X-Google-Smtp-Source: ACcGV63zmgDcw0dJCy9Z+ta1nkuK9jLl4EbzUIi6bBWiLz09JFEcdAqR5m8KjUvWKuUROei3/FwV X-Received: by 2002:a62:9316:: with SMTP id b22-v6mr27193633pfe.193.1539024456251; Mon, 08 Oct 2018 11:47:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539024456; cv=none; d=google.com; s=arc-20160816; b=Nm2dmN5ft2f4LEaC00HaA/O/PWKqH48Xy/uBCEfvbCx6zt6/+8lPnc5JgqSAMjYxx7 nlR4zM+2QtTb/+AA3cCPsdhEDKfoTTzulScYF/DzacvKzBUfZgSCXo4CE7uVoOW83Zw6 QvY12U2w/ym85bPSemRqozAvIrl3yXBxbEMImU3APGGu07CqvI+hLepk4mFvKjEIOeSI +Uyvs1mjT7qWtKzZKMMjrGxHHurohub7E6Lzrr89ASl1ykvWNpQIXnDZcvi5EbnskYIL 9PBq/CGivzrIa+i771Wr2lXDso6Db4zjpWvF7HDzco29Ih+ehD26iQf+ZSyrCKcKVp3S 8YDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1z+Eg1QN2PQH1GYCSd0Q2BkO9PxZnv3q7QamZbGiWyY=; b=0gEc9TaEmWA/pP/nZ71470JQlw6jzXpFHbzrn9IV9TjywAgZUL1D51Nd7SeTqSPqLe AdgZ9/SC9ljX4PtIKxv3dUs7UPm8KaFnPdGKmh7K/KuXEM32hlS83wXrRfc5pXPApLs3 3yBDQ1Zya3nT5XvBxQuCdQTyNr6PFa4ai+Mfb3O1x7QgBDvqEepUxYOj6VPfQeBVTbnp 3TNHkcSTcqZrnUbktHBPsnvnpKA2cA5PhzwxuBKU/6N5VuULu0GEOUDAcQJg8MBImHko lzjpdA7Qi7SC/C5GwvDhcJ2B7y8aFRSI/Dif9FkWs4WQI2H75MtqNe9uPZutenEjblW/ szIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lNMLInWw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i1-v6si17762921plt.108.2018.10.08.11.47.21; Mon, 08 Oct 2018 11:47:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lNMLInWw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730940AbeJIB75 (ORCPT + 99 others); Mon, 8 Oct 2018 21:59:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:48252 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727510AbeJIB74 (ORCPT ); Mon, 8 Oct 2018 21:59:56 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7E9B42087D; Mon, 8 Oct 2018 18:46:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539024410; bh=ooIXQuIFSSmFtWLm6cbCKyRpf/mXx/WAxqDIL0KpLHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lNMLInWwjXNnpv/rYEwIWDJdm9OgjxuNf+R5LmsFDf/U0O14Tj0WTuORrf7lnozl2 wquQACnsOOq1HeUKv3Hjsa2Ru5zCXI7rwcTksN4nK/eDGT0T/pz7avcdC9VBAz/ste R1pjSesr2ma1UPVAR3DNr9F2yA8XU4Q+jW8cDzfc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , John Fastabend , Alexei Starovoitov , Sasha Levin Subject: [PATCH 4.18 029/168] bpf: fix shift upon scatterlist ring wrap-around in bpf_msg_pull_data Date: Mon, 8 Oct 2018 20:30:09 +0200 Message-Id: <20181008175621.153149806@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181008175620.043587728@linuxfoundation.org> References: <20181008175620.043587728@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Daniel Borkmann [ Upstream commit 2e43f95dd8ee62bc8bf57f2afac37fbd70c8d565 ] If first_sg and last_sg wraps around in the scatterlist ring, then we need to account for that in the shift as well. E.g. crafting such msgs where this is the case leads to a hang as shift becomes negative. E.g. consider the following scenario: first_sg := 14 |=> shift := -12 msg->sg_start := 10 last_sg := 3 | msg->sg_end := 5 round 1: i := 15, move_from := 3, sg[15] := sg[ 3] round 2: i := 0, move_from := -12, sg[ 0] := sg[-12] round 3: i := 1, move_from := -11, sg[ 1] := sg[-11] round 4: i := 2, move_from := -10, sg[ 2] := sg[-10] [...] round 13: i := 11, move_from := -1, sg[ 2] := sg[ -1] round 14: i := 12, move_from := 0, sg[ 2] := sg[ 0] round 15: i := 13, move_from := 1, sg[ 2] := sg[ 1] round 16: i := 14, move_from := 2, sg[ 2] := sg[ 2] round 17: i := 15, move_from := 3, sg[ 2] := sg[ 3] [...] This means we will loop forever and never hit the msg->sg_end condition to break out of the loop. When we see that the ring wraps around, then the shift should be MAX_SKB_FRAGS - first_sg + last_sg - 1. Meaning, the remainder slots from the tail of the ring and the head until last_sg combined. Fixes: 015632bb30da ("bpf: sk_msg program helper bpf_sk_msg_pull_data") Signed-off-by: Daniel Borkmann Acked-by: John Fastabend Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- net/core/filter.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/net/core/filter.c +++ b/net/core/filter.c @@ -2360,7 +2360,10 @@ BPF_CALL_4(bpf_msg_pull_data, * had a single entry though we can just replace it and * be done. Otherwise walk the ring and shift the entries. */ - shift = last_sg - first_sg - 1; + WARN_ON_ONCE(last_sg == first_sg); + shift = last_sg > first_sg ? + last_sg - first_sg - 1 : + MAX_SKB_FRAGS - first_sg + last_sg - 1; if (!shift) goto out;