Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4822071ybl; Wed, 22 Jan 2020 05:24:23 -0800 (PST) X-Google-Smtp-Source: APXvYqzJiuzoIfVyqccjsvxIOg3SymNu1WwDlpnhoem9m/GioS3uOpZhCP+hWoMq/CvEIRoCEQdd X-Received: by 2002:aca:d6c8:: with SMTP id n191mr7001245oig.103.1579699463033; Wed, 22 Jan 2020 05:24:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579699463; cv=none; d=google.com; s=arc-20160816; b=ln3Od9NMNlMSnELpeUyujXx872P6mkVFbTWTA2i7W5rpsdjvkW1f32O1IwhGg/w1vT tFcga3eEm8L98kTfexfh0BISZ+06Ocv79EBOQ942FFW61YPDKRqaWXLyUy/1N5xFXsHH sB3qdzt7GleZcNCuyg0gNnTrqrJG0YZgJArQJwGOBV/KoKcdt93Rjroo9CHHJ/qGzLUl jdL5KTYSo+mxLySAqFX5QQrsMnl1v2Oh2gabUiG5H7jLLfI4SH7HChChVt3OZDj+D/tt LK22wV/et9L3dvbtoZKM81xQshhkdOsHH+ldoITm2GG8nhxcOGP9/roLhYFQFyIwA3wS ucZw== 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=Uvx5HeI27yvaCeGaDz6uz/GixZK7VqKLAm4xEBzBE5Q=; b=OK1Ld5CXpm9o8ZCNkRjEjVI8wRFSiLf+mgzM4x+/6idgvIzSyhBcWIVlRS6TQWOrwT LP0O5s6YTE7jyCm2skC/GpWp3iuH5TOHtKFLgnDjitkKjCNUFe6EGKelmOhl+HGr/M1B mS1p20R2XhurIhs2F2x1t1JfeWBdbKLKV9JLBofczToyPpcAcejm8bDnluZdxmOCCQmK xtXxcJMkFLGqHINz3+YZev+ZuTPpQwpf7eremebKwy7QbDd4sVLJ9MxzyvV7h4lBVj8Q ZZ0NlY8GZE6m0XTz/wIIg6qhda7l5YjJOkzwbnE9Sylc/A0ODIa5J49oJ5K6aKmldEqu yOGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=g2vcqi8P; 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 t142si20984665oih.242.2020.01.22.05.24.11; Wed, 22 Jan 2020 05:24:23 -0800 (PST) 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=g2vcqi8P; 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 S1730704AbgAVNXK (ORCPT + 99 others); Wed, 22 Jan 2020 08:23:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:41146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730693AbgAVNXH (ORCPT ); Wed, 22 Jan 2020 08:23:07 -0500 Received: from localhost (unknown [84.241.205.26]) (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 CA472205F4; Wed, 22 Jan 2020 13:23:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579699386; bh=Ty9RH+RLqMXgUyZ5xZoWxctM3JahlJeZFqmLUuI2JFQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g2vcqi8P2uaK3X3G+Rw/mqI3GXfrHZrLKPROK/dHL+r27VDTUhJCaH/XLjcvBTAio QQs9FhHMP/9+SAsrG4QDy1pVchcRPWwaVDsnvpWoETZrmvsZvTjvCZuzhaJuCq1Ymk DHHe7vBMf7S1fUSFKDDhYkVts6oYN7ILWFxr0a2o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Fastabend , Daniel Borkmann , Song Liu Subject: [PATCH 5.4 111/222] bpf: Sockmap, skmsg helper overestimates push, pull, and pop bounds Date: Wed, 22 Jan 2020 10:28:17 +0100 Message-Id: <20200122092841.682573648@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122092833.339495161@linuxfoundation.org> References: <20200122092833.339495161@linuxfoundation.org> User-Agent: quilt/0.66 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 From: John Fastabend commit 6562e29cf6f0ddd368657d97a8d484ffc30df5ef upstream. In the push, pull, and pop helpers operating on skmsg objects to make data writable or insert/remove data we use this bounds check to ensure specified data is valid, /* Bounds checks: start and pop must be inside message */ if (start >= offset + l || last >= msg->sg.size) return -EINVAL; The problem here is offset has already included the length of the current element the 'l' above. So start could be past the end of the scatterlist element in the case where start also points into an offset on the last skmsg element. To fix do the accounting slightly different by adding the length of the previous entry to offset at the start of the iteration. And ensure its initialized to zero so that the first iteration does nothing. Fixes: 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface") Fixes: 6fff607e2f14b ("bpf: sk_msg program helper bpf_msg_push_data") Fixes: 7246d8ed4dcce ("bpf: helper to pop data from messages") Signed-off-by: John Fastabend Signed-off-by: Daniel Borkmann Acked-by: Song Liu Cc: stable@vger.kernel.org Link: https://lore.kernel.org/bpf/20200111061206.8028-5-john.fastabend@gmail.com Signed-off-by: Greg Kroah-Hartman --- net/core/filter.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) --- a/net/core/filter.c +++ b/net/core/filter.c @@ -2231,10 +2231,10 @@ BPF_CALL_4(bpf_msg_pull_data, struct sk_ /* First find the starting scatterlist element */ i = msg->sg.start; do { + offset += len; len = sk_msg_elem(msg, i)->length; if (start < offset + len) break; - offset += len; sk_msg_iter_var_next(i); } while (i != msg->sg.end); @@ -2346,7 +2346,7 @@ BPF_CALL_4(bpf_msg_push_data, struct sk_ u32, len, u64, flags) { struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge; - u32 new, i = 0, l, space, copy = 0, offset = 0; + u32 new, i = 0, l = 0, space, copy = 0, offset = 0; u8 *raw, *to, *from; struct page *page; @@ -2356,11 +2356,11 @@ BPF_CALL_4(bpf_msg_push_data, struct sk_ /* First find the starting scatterlist element */ i = msg->sg.start; do { + offset += l; l = sk_msg_elem(msg, i)->length; if (start < offset + l) break; - offset += l; sk_msg_iter_var_next(i); } while (i != msg->sg.end); @@ -2506,7 +2506,7 @@ static void sk_msg_shift_right(struct sk BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start, u32, len, u64, flags) { - u32 i = 0, l, space, offset = 0; + u32 i = 0, l = 0, space, offset = 0; u64 last = start + len; int pop; @@ -2516,11 +2516,11 @@ BPF_CALL_4(bpf_msg_pop_data, struct sk_m /* First find the starting scatterlist element */ i = msg->sg.start; do { + offset += l; l = sk_msg_elem(msg, i)->length; if (start < offset + l) break; - offset += l; sk_msg_iter_var_next(i); } while (i != msg->sg.end);