Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2571790pxb; Tue, 9 Mar 2021 06:06:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJwdjZqSePQ8NgtnEgjL+Cl31SLmShEMdBs/zvYjW8MLgxSQES0ZRAB+7aJ2prQE6EXBAJqp X-Received: by 2002:a19:3fca:: with SMTP id m193mr14260827lfa.609.1615298759645; Tue, 09 Mar 2021 06:05:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615298759; cv=none; d=google.com; s=arc-20160816; b=RuWhKBMVJdXxDFfTvdJI1lHtmGPm17huCIUNmKayVeEwpD8KXMoSp4UnG9C6DN19xf o3lX5PZCa1gmPkSuN1do2YeOkEWEeDwV8RMM2AaaAq/jUqdF+4Ukd7Mv09o8HgwEuXVg a6CNqYbOJeCksrzA4+wwW7WMnOX7p1CZK7eeZqNNZjI5/9SVOgOnQXTU3hGJeJAq29Pj NrcatPAplSP7hFWDIys3FqizjfEMoVvXWzgKseRehToIoscL3GQXl4F0xRBn8HTx1tyn 4pzFOHMvBK+FfwKoZDssHeHvE9tcUBr2MXPHMJIHywD6Y4Uydtab7B65DSqNHfcZ7GQQ gkmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=4ft5rd560a0W9wxTzYwRNovTAUDlXaQrOqyKdLfMHLs=; b=SQ68PmkTItC/e7p+c04gT3HLzEFsk0PhqR4B8qK7g9HjT/wdoXRyufX0GLcpU4M0GT On9L3H0Gu+dPbnwZ8HSI3H2hgkrwa7gsjqMeUwKPrhEK4Kmzwf3IHuyvfaSwK+ki/xv3 ZCAyGuubP8B0Fby62jlF3vgdpIju45pX6pCNcVo5gzIAbYwOK5dG2bPWyqZH+gNb+Hmw rY0ClfgPSSk9VKdkapd3Kn28mnKf2OT3sEMWgM+qbeTgrLU0G1FWTJjjQX196cPhGsnO 3F5BRKIEOuRX+UJZHcIjCejjLZSuJOgYIQRjJBYllhoSd1r+/4XQ1kM/LBrN7/+lCzEV oztw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="gUJlyg/5"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x9si9666716eje.662.2021.03.09.06.05.35; Tue, 09 Mar 2021 06:05:59 -0800 (PST) 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=@gmail.com header.s=20161025 header.b="gUJlyg/5"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230147AbhCIOEF (ORCPT + 99 others); Tue, 9 Mar 2021 09:04:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230403AbhCIODk (ORCPT ); Tue, 9 Mar 2021 09:03:40 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2AB7C06174A for ; Tue, 9 Mar 2021 06:03:39 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id mj10so27953578ejb.5 for ; Tue, 09 Mar 2021 06:03:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=4ft5rd560a0W9wxTzYwRNovTAUDlXaQrOqyKdLfMHLs=; b=gUJlyg/5pLeWaXd2N9NNQZ8NbrIXx8As9ZuRUg03D0jpMX1dggNIiX81+CrM0dvfqV bETGkoHFu0y2s4uJr/WA/HDXUhVapWWNEIUrv71y8RnvHYt/ICGyDwlTeZ1CIIo6oOgf fbGwCWvn2RGvBtFKMRJIOga4IzaEWmx3GYa/VrBQCV2+EJdRrFPVU3FBHgAjZOOXMxNp P28+xHRk2mbnUGGro5CC4U5AvAjRz9sjseiZxvMA9cTsjnULKebfvYlAS6ChIaFPqTNC FVqUFtOIUUJoMrVQreOP7k5rAgliJ737LyGM/ozq6GVnx1qF9hBMlNDuw7ikpxEGbayK vT9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=4ft5rd560a0W9wxTzYwRNovTAUDlXaQrOqyKdLfMHLs=; b=qirSqHL0VLQ75lmVvG2E6Wa3i9x0/xvvpmDDKfg5qEmb0UslarDckmZdNkEp5OjBXC 0e3a3p6l8klxfvfOE67WeY0hw+gPHG+TziduemLlLyVoVt35PBdYcg9aL++a4v5pXF9v Mfd5nlWG+3cinJryN1CFfn3jtj7jc+a4kK2WMTBheCv3Igm7Fr9iiYzId/cGhWTsAzaq D6C3ztrojdVAmM7yHYn2Vk4qZn4u+iB8tD/Fdo/TIiLMSGaM0AWy+IqwnmNNM2QHHj2W MY9+5OI8AU2ki6fCaH3UjbqHhh4gtYWhBYULG9+DRcftlv38ew9bQ/yTbLy2z3XpER++ vveQ== X-Gm-Message-State: AOAM533BQVluYozLDoWxgHRid2YSHKdTi4b6TPErng3aLlArroOkAkoP +loIq2OkTVLldnOOT5KLkmcKFi+Tk50= X-Received: by 2002:a17:907:94cc:: with SMTP id dn12mr20486439ejc.177.1615298618364; Tue, 09 Mar 2021 06:03:38 -0800 (PST) Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com. [209.85.128.53]) by smtp.gmail.com with ESMTPSA id bx2sm9037191edb.80.2021.03.09.06.03.37 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Mar 2021 06:03:37 -0800 (PST) Received: by mail-wm1-f53.google.com with SMTP id c76-20020a1c9a4f0000b029010c94499aedso6278041wme.0 for ; Tue, 09 Mar 2021 06:03:37 -0800 (PST) X-Received: by 2002:a05:600c:2053:: with SMTP id p19mr4202330wmg.87.1615298612543; Tue, 09 Mar 2021 06:03:32 -0800 (PST) MIME-Version: 1.0 References: <8f2cb8f8614d86bba02df73c1a0665179583f1c3.1615199056.git.bnemeth@redhat.com> <20210309062116-mutt-send-email-mst@kernel.org> In-Reply-To: <20210309062116-mutt-send-email-mst@kernel.org> From: Willem de Bruijn Date: Tue, 9 Mar 2021 09:02:52 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct To: "Michael S. Tsirkin" Cc: Balazs Nemeth , Network Development , linux-kernel , Jason Wang , David Miller , virtualization@lists.linux-foundation.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 9, 2021 at 6:26 AM Michael S. Tsirkin wrote: > > On Mon, Mar 08, 2021 at 11:31:25AM +0100, Balazs Nemeth wrote: > > For gso packets, virtio_net_hdr_set_proto sets the protocol (if it isn't > > set) based on the type in the virtio net hdr, but the skb could contain > > anything since it could come from packet_snd through a raw socket. If > > there is a mismatch between what virtio_net_hdr_set_proto sets and > > the actual protocol, then the skb could be handled incorrectly later > > on. > > > > An example where this poses an issue is with the subsequent call to > > skb_flow_dissect_flow_keys_basic which relies on skb->protocol being set > > correctly. A specially crafted packet could fool > > skb_flow_dissect_flow_keys_basic preventing EINVAL to be returned. > > > > Avoid blindly trusting the information provided by the virtio net header > > by checking that the protocol in the packet actually matches the > > protocol set by virtio_net_hdr_set_proto. Note that since the protocol > > is only checked if skb->dev implements header_ops->parse_protocol, > > packets from devices without the implementation are not checked at this > > stage. > > > > Fixes: 9274124f023b ("net: stricter validation of untrusted gso packets") > > Signed-off-by: Balazs Nemeth > > --- > > include/linux/virtio_net.h | 8 +++++++- > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h > > index e8a924eeea3d..6c478eee0452 100644 > > --- a/include/linux/virtio_net.h > > +++ b/include/linux/virtio_net.h > > @@ -79,8 +79,14 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, > > if (gso_type && skb->network_header) { > > struct flow_keys_basic keys; > > > > - if (!skb->protocol) > > + if (!skb->protocol) { > > + const struct ethhdr *eth = skb_eth_hdr(skb); > > + __be16 etype = dev_parse_header_protocol(skb); > > + > > virtio_net_hdr_set_proto(skb, hdr); > > + if (etype && etype != skb->protocol) > > + return -EINVAL; > > + } > > > Well the protocol in the header is an attempt at an optimization to > remove need to parse the packet ... any data on whether this > affecs performance? This adds a branch and reading a cacheline that is inevitably read not much later. It shouldn't be significant. And this branch is only taken if skb->protocol is not set. So the cost can easily be avoided by passing the information. But you raise a good point, because TUNTAP does set it, but only after the call to virtio_net_hdr_to_skb. That should perhaps be inverted (in a separate net-next patch).