From patchwork Wed Apr 20 20:07:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12820759 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CE8AC433EF for ; Wed, 20 Apr 2022 20:07:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382022AbiDTUKc (ORCPT ); Wed, 20 Apr 2022 16:10:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382013AbiDTUKa (ORCPT ); Wed, 20 Apr 2022 16:10:30 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6F7345AD7; Wed, 20 Apr 2022 13:07:43 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id c64so3726359edf.11; Wed, 20 Apr 2022 13:07:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ya9mQeUeUB7WEvwDQ80duVGGhzEqxRrCrwLr6kr2uRY=; b=ZKS2ydxS6tFvgbDiBJy7q6iN9T3Q+XE4WVaeTcv7ov/eSt1NMkiVNfgJML6tC8RQHn urwVbyW3xxUn3ATkvgMWcRubNcX9QeyW/lyav3Mt29EzY/jDP9boNSDx2B4lF2QZgoAJ QqaSRg/5F+dkNaoUeadx52EPZqrWwJtbXu/Fyt4h2P50of1eVJxhwWEK4wBDn0c2Fw3Y lMtfK8LwM2gbVlNmzKPH1xPASPv5k+Z6Cbb8ZV7NyMxBKkfLS1d/Zovy23Invx8X8iWu tiTfqtIs5uSXdM4s2JL0C0S30OnNjwKirCxEGWvtkgSHUGvuFaPddRkk3dX/vwcYiMe3 ahew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ya9mQeUeUB7WEvwDQ80duVGGhzEqxRrCrwLr6kr2uRY=; b=W2hy4lckk/F13Y5gY0vAWxVTCG/DaeOxWQk92JoNlxcavkPIqHdCJzeVoZa128L28D zFCP/9VjtztOYf1VGy3RFpMUMe4sIJEf/6rIUStoo0AS9Hpm39HXzsap42A6gLtgVUkg KgkEc8vdleknpCDYQlEIh4/Y2cypG3zEDD5ynWvsKnhFBq5q5bwqwKOOXkF9qHqBJsSG umX8iMF8Xz9cGrO7KYoZY1+qtlMVNt4+Oz0JlD7LsDCA05HVjP1Pv7tGpOuQLFEmHApI Zr/v5lzESxSHKOUV/DI3OQ6Qg0vyzjSazQHfNod0gYlTXVboAv+j316A2UQYfCd/DfCz Vbnw== X-Gm-Message-State: AOAM533L2B9m5zkSmkG2aeuYcZpHD1JyDaGlvkEGE91jSBuIPt6JqOvl Z8ksmcyPW8cTT+EJQYm0yU8= X-Google-Smtp-Source: ABdhPJytSsdi2FdWcWY6CE3Rnp2Uru41znI43WCdX8sm1eWT0BlveISbbim0Sap/Sp4S/LYDvTL3CQ== X-Received: by 2002:a50:cc9e:0:b0:41d:7123:d3ba with SMTP id q30-20020a50cc9e000000b0041d7123d3bamr25412837edi.296.1650485262173; Wed, 20 Apr 2022 13:07:42 -0700 (PDT) Received: from anparri.mshome.net (host-82-53-3-95.retail.telecomitalia.it. [82.53.3.95]) by smtp.gmail.com with ESMTPSA id gy10-20020a170906f24a00b006e894144707sm7126853ejb.53.2022.04.20.13.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 13:07:41 -0700 (PDT) From: "Andrea Parri (Microsoft)" To: KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Stefano Garzarella , David Miller , Jakub Kicinski , Paolo Abeni Cc: linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Andrea Parri (Microsoft)" Subject: [PATCH 1/5] hv_sock: Check hv_pkt_iter_first_raw()'s return value Date: Wed, 20 Apr 2022 22:07:16 +0200 Message-Id: <20220420200720.434717-2-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420200720.434717-1-parri.andrea@gmail.com> References: <20220420200720.434717-1-parri.andrea@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org The function returns NULL if the ring buffer doesn't contain enough readable bytes to constitute a packet descriptor. The ring buffer's write_index is in memory which is shared with the Hyper-V host, an erroneous or malicious host could thus change its value and overturn the result of hvs_stream_has_data(). Signed-off-by: Andrea Parri (Microsoft) Reviewed-by: Michael Kelley Reviewed-by: Stefano Garzarella --- net/vmw_vsock/hyperv_transport.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index e111e13b66604..943352530936e 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -603,6 +603,8 @@ static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg, if (need_refill) { hvs->recv_desc = hv_pkt_iter_first_raw(hvs->chan); + if (!hvs->recv_desc) + return -ENOBUFS; ret = hvs_update_recv_data(hvs); if (ret) return ret; From patchwork Wed Apr 20 20:07:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12820760 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8106EC433EF for ; Wed, 20 Apr 2022 20:07:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382060AbiDTUKl (ORCPT ); Wed, 20 Apr 2022 16:10:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381936AbiDTUKc (ORCPT ); Wed, 20 Apr 2022 16:10:32 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC1414615B; Wed, 20 Apr 2022 13:07:45 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id z12so3774363edl.2; Wed, 20 Apr 2022 13:07:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=U/urPSU6OV2SvWMA9AXFeQZujMJHrY6/s23lqylxLSk=; b=fSAfQxUOiGpYIwnO2RX01LmcZYwvxv+GsN73AoQIBLNMoEbgziTYl3qjUbemn0u/+V lHofybrqnh5Hu5PyEo+H3xDDAgu05QXclhbAwArt0Ip9RLja86CauCXVDv+X1B6yu6NG RHHWLV43YKZNnPfBgAUE0okFjRLhAs78Re5Qv5ooBdAv4XhHUVT+rXVPUDJ6fZK5sXjq MX5jObDumj+P2+4+wdfm01UC4NxDeu7nZFDvs/S/PWBr3zQ1RoRLslc8acxKK14aaTkk OOuDWkDqoOqnhpkwugmBK/yL6lBtCF+nEQwrvrLbPfhLZFIV5yPqLYhKykbEYKgQK7N2 OmsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=U/urPSU6OV2SvWMA9AXFeQZujMJHrY6/s23lqylxLSk=; b=uL0bktjky0axDXyiBpCApLEqsCW58iwZna7ugHd7u8fjo4NB3Se0aq6/p7/CXPtWpK G4+MP7PJwxktwKhbYHbPbdVcA4KL+o/WuuZ2wPaxkiWfQlDldV4A7XLK5GenNl3PrSm8 /+pJPcJ+Xy67OUmX6lVYFgLf6t1ybHH0DwCCK1HWuuMR+9F/axf2UFzKRUKL8Jm1g347 NdKPuiAgKrZqvh3nJIlBeenhx29fXITUkrU2Dh+kSTvY3Ps7D5b+UXuuOK9iXx5SRleY vaG/fskmVqX1L+FNJcStHLJDqUtMNqWXE3efJZ8ESBM62oaK+ASNus0dHEkPf5yz+NiG yhuw== X-Gm-Message-State: AOAM530Z6AGE5TNOatOORtCystZLgfODCrpc4GhmrBGol0KhpRevCPrd PJsePSYLRRDmezhyuT/fPa0= X-Google-Smtp-Source: ABdhPJyYzdti6UzhNDf5r8bN+Q2rloKA6TmD6TBwWrD56I/rfOtkiQjGzM+VeaXJoPsstZXbThRG5A== X-Received: by 2002:a05:6402:34b:b0:41d:7026:d9e3 with SMTP id r11-20020a056402034b00b0041d7026d9e3mr25405100edw.168.1650485264346; Wed, 20 Apr 2022 13:07:44 -0700 (PDT) Received: from anparri.mshome.net (host-82-53-3-95.retail.telecomitalia.it. [82.53.3.95]) by smtp.gmail.com with ESMTPSA id gy10-20020a170906f24a00b006e894144707sm7126853ejb.53.2022.04.20.13.07.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 13:07:43 -0700 (PDT) From: "Andrea Parri (Microsoft)" To: KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Stefano Garzarella , David Miller , Jakub Kicinski , Paolo Abeni Cc: linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Andrea Parri (Microsoft)" Subject: [PATCH 2/5] hv_sock: Copy packets sent by Hyper-V out of the ring buffer Date: Wed, 20 Apr 2022 22:07:17 +0200 Message-Id: <20220420200720.434717-3-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420200720.434717-1-parri.andrea@gmail.com> References: <20220420200720.434717-1-parri.andrea@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Pointers to VMbus packets sent by Hyper-V are used by the hv_sock driver within the guest VM. Hyper-V can send packets with erroneous values or modify packet fields after they are processed by the guest. To defend against these scenarios, copy the incoming packet after validating its length and offset fields using hv_pkt_iter_{first,next}(). In this way, the packet can no longer be modified by the host. Signed-off-by: Andrea Parri (Microsoft) Reviewed-by: Michael Kelley --- net/vmw_vsock/hyperv_transport.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index 943352530936e..8c37d07017fc4 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -78,6 +78,9 @@ struct hvs_send_buf { ALIGN((payload_len), 8) + \ VMBUS_PKT_TRAILER_SIZE) +/* Upper bound on the size of a VMbus packet for hv_sock */ +#define HVS_MAX_PKT_SIZE HVS_PKT_LEN(HVS_MTU_SIZE) + union hvs_service_id { guid_t srv_id; @@ -378,6 +381,8 @@ static void hvs_open_connection(struct vmbus_channel *chan) rcvbuf = ALIGN(rcvbuf, HV_HYP_PAGE_SIZE); } + chan->max_pkt_size = HVS_MAX_PKT_SIZE; + ret = vmbus_open(chan, sndbuf, rcvbuf, NULL, 0, hvs_channel_cb, conn_from_host ? new : sk); if (ret != 0) { @@ -602,7 +607,7 @@ static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg, return -EOPNOTSUPP; if (need_refill) { - hvs->recv_desc = hv_pkt_iter_first_raw(hvs->chan); + hvs->recv_desc = hv_pkt_iter_first(hvs->chan); if (!hvs->recv_desc) return -ENOBUFS; ret = hvs_update_recv_data(hvs); @@ -618,7 +623,7 @@ static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg, hvs->recv_data_len -= to_read; if (hvs->recv_data_len == 0) { - hvs->recv_desc = hv_pkt_iter_next_raw(hvs->chan, hvs->recv_desc); + hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc); if (hvs->recv_desc) { ret = hvs_update_recv_data(hvs); if (ret) From patchwork Wed Apr 20 20:07:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12820761 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04CDBC433F5 for ; Wed, 20 Apr 2022 20:08:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382062AbiDTUKn (ORCPT ); Wed, 20 Apr 2022 16:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382044AbiDTUKk (ORCPT ); Wed, 20 Apr 2022 16:10:40 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1087E4667A; Wed, 20 Apr 2022 13:07:47 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id g13so5799403ejb.4; Wed, 20 Apr 2022 13:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uX50zrEMcunrp6nLIwZJ5dpGlndEZgBupE5oS9UHVBw=; b=aSGzqeRdozxRjHTlADzcwmPwGfL+rDDtfxZI+Tm38J/JsfArG7yiFPH+6TKTf3gYkN oEZPjIGwEddLFEzFZ4vWsRdgBwizMZJ/rcLx+3pDsc0udmBBI3/fPc8YL1kQaRQsjIs+ YfDn+mHsEcJvkz8SZ0PWRCJ9VCgzM0jAR92j0lNOIE3zNVxMPiFlq9brmK8u0b/3gy6o XwHWYqpMm/cYBN0Jw25au5dW7V/i1KcyS4ZA8rVRBW+KywG08GyBPCc6hi3+5YZwI1qt gyh3f+u0TJMNWJB3vXHCuQhaDweEMPLPmM6OKLf5jr/ttWEEvs10xf/vq/deCJQfXJlW 8r8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uX50zrEMcunrp6nLIwZJ5dpGlndEZgBupE5oS9UHVBw=; b=lXdfy4OBhdjrOtHr7hiTLA3OEbmLGm6IPhmods9wcAe8bHDuNIb2Yo2x3oWKv7baIe +Cj7OYfSW8Z6mToftA3buZHJzcCQafU4KXFl4V9YAyEiDfsk7XqRskI9j4bgW9DCl3O/ WwzRDNHtMwDHAN70BVNgRonpSC9zI7EwwqJasOU9S9xQ1wDedZi5szulDlv5dDQr9beN /w6a+M2ucb6o+zCR8f3YKlKpPbE9eVYNeJrahh1a3du1wvGhANbn8YordaabsOo9Detp rSxFxSa0AKZKXdR69emMaRkw3xfgadeW2lSPeEg3zQktlOvRHcuEj/Gnuw9weXU2Rq91 0aEw== X-Gm-Message-State: AOAM530HDgKrAKVi5aobGaRTs1D3WlPnzfsAUPGs0005sNLSoE7G68mB 6Y5wYkPqFW7HB+qAGx8HaNM= X-Google-Smtp-Source: ABdhPJz4tqa3SfnB+LvR0FE25Hb7iOYwxK7ciw+lS7083/dNExCdZYtRlKTqFEgGnFpnXTU5TkV/eg== X-Received: by 2002:a17:906:b102:b0:6db:1487:e73 with SMTP id u2-20020a170906b10200b006db14870e73mr19190797ejy.474.1650485266406; Wed, 20 Apr 2022 13:07:46 -0700 (PDT) Received: from anparri.mshome.net (host-82-53-3-95.retail.telecomitalia.it. [82.53.3.95]) by smtp.gmail.com with ESMTPSA id gy10-20020a170906f24a00b006e894144707sm7126853ejb.53.2022.04.20.13.07.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 13:07:46 -0700 (PDT) From: "Andrea Parri (Microsoft)" To: KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Stefano Garzarella , David Miller , Jakub Kicinski , Paolo Abeni Cc: linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Andrea Parri (Microsoft)" Subject: [PATCH 3/5] hv_sock: Add validation for untrusted Hyper-V values Date: Wed, 20 Apr 2022 22:07:18 +0200 Message-Id: <20220420200720.434717-4-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420200720.434717-1-parri.andrea@gmail.com> References: <20220420200720.434717-1-parri.andrea@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org For additional robustness in the face of Hyper-V errors or malicious behavior, validate all values that originate from packets that Hyper-V has sent to the guest in the host-to-guest ring buffer. Ensure that invalid values cannot cause data being copied out of the bounds of the source buffer in hvs_stream_dequeue(). Signed-off-by: Andrea Parri (Microsoft) Reviewed-by: Michael Kelley --- include/linux/hyperv.h | 5 +++++ net/vmw_vsock/hyperv_transport.c | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index fe2e0179ed51e..55478a6810b60 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1663,6 +1663,11 @@ static inline u32 hv_pkt_datalen(const struct vmpacket_descriptor *desc) return (desc->len8 << 3) - (desc->offset8 << 3); } +/* Get packet length associated with descriptor */ +static inline u32 hv_pkt_len(const struct vmpacket_descriptor *desc) +{ + return desc->len8 << 3; +} struct vmpacket_descriptor * hv_pkt_iter_first_raw(struct vmbus_channel *channel); diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index 8c37d07017fc4..092cadc2c866d 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -577,12 +577,19 @@ static bool hvs_dgram_allow(u32 cid, u32 port) static int hvs_update_recv_data(struct hvsock *hvs) { struct hvs_recv_buf *recv_buf; - u32 payload_len; + u32 pkt_len, payload_len; + + pkt_len = hv_pkt_len(hvs->recv_desc); + + /* Ensure the packet is big enough to read its header */ + if (pkt_len < HVS_HEADER_LEN) + return -EIO; recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1); payload_len = recv_buf->hdr.data_size; - if (payload_len > HVS_MTU_SIZE) + /* Ensure the packet is big enough to read its payload */ + if (payload_len > pkt_len - HVS_HEADER_LEN || payload_len > HVS_MTU_SIZE) return -EIO; if (payload_len == 0) From patchwork Wed Apr 20 20:07:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12820762 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3276C433EF for ; Wed, 20 Apr 2022 20:08:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382069AbiDTUKr (ORCPT ); Wed, 20 Apr 2022 16:10:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382043AbiDTUKk (ORCPT ); Wed, 20 Apr 2022 16:10:40 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17E9947047; Wed, 20 Apr 2022 13:07:50 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id ks6so5827811ejb.1; Wed, 20 Apr 2022 13:07:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gqny/vtMMTZYJnMuIZbRZTkwYXwooOfkrPfqMnie+pg=; b=RoRN/H4aK8ddwxHumTI6VWwDp2gXNMIkho/pNo6w8BIqPI5pub97EksV82dB1yjqAe wgGIHXgAVdiaBZyuWjdqSRoh8GB5+2J6ID/gp0SSudXi3aXki53J2khC+IeiEzz7OdBy oOitUxPNa9i5Zg/RAoQd0wwxDSy8OhP8kxBRUemC6g7lNRjcnhYoQHGNwXIsHJd0uymm Eq2SlCWgeiHI6oiBhIP99UP+tyYBwJMnzXELixpPQY2+UsStIVw8qVHPEsmnyXXuZ32w eBtVUu1huG85Y10uGoKF1/ImvP6cxJQGx28HZw5CMxltNaBsw+CD41n30Md41zFbZHEE OU0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gqny/vtMMTZYJnMuIZbRZTkwYXwooOfkrPfqMnie+pg=; b=wx3hqV8C3mj5KZ8ziV/RsmCpBBJMKEjHyUZ/mk2nJrIsBLCpwn1l/pbg6Cb3zPbxSk 5pWx/jQdxi9/efT7gOH+jEp4qcEIuH99bjn1QkaIucOCvkDFzAMN+PYj+xqHcwNWYyNY NRx6EPNwC+3/flbBqC0R+9ylnkMhFaqR1a6Mz3BsEpcmsvxspUU9/onxAK7CrCNlK+fk aQHxQratJjchTAg7c1kCP7wcrHTcQ++i1cz5Q/0sjMcQSgqqtqzFp/ZKr4NL7GYR8ftJ tASISkAw8zRMsGjPP1CflQkO3fiH3GotxFn8QKtZZZ3AwAxsrEuoP1IScRpc//Rw+mOi Dl/Q== X-Gm-Message-State: AOAM530c6URuHZfZio8LZzT7bzj/qHT1w2yB30mrclE4tQg0J140Ib4F uBxSOLK76RDC1iQZwdvqsXk= X-Google-Smtp-Source: ABdhPJyZkdpK/O7qghqShfdT23WGzRmhcv73TTRaVeeq0UBB1njFxL6qqyF7N5cv3558WjxMZZlcLw== X-Received: by 2002:a17:907:168a:b0:6df:ad44:3009 with SMTP id hc10-20020a170907168a00b006dfad443009mr20298321ejc.176.1650485268654; Wed, 20 Apr 2022 13:07:48 -0700 (PDT) Received: from anparri.mshome.net (host-82-53-3-95.retail.telecomitalia.it. [82.53.3.95]) by smtp.gmail.com with ESMTPSA id gy10-20020a170906f24a00b006e894144707sm7126853ejb.53.2022.04.20.13.07.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 13:07:48 -0700 (PDT) From: "Andrea Parri (Microsoft)" To: KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Stefano Garzarella , David Miller , Jakub Kicinski , Paolo Abeni Cc: linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Andrea Parri (Microsoft)" Subject: [PATCH 4/5] Drivers: hv: vmbus: Accept hv_sock offers in isolated guests Date: Wed, 20 Apr 2022 22:07:19 +0200 Message-Id: <20220420200720.434717-5-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420200720.434717-1-parri.andrea@gmail.com> References: <20220420200720.434717-1-parri.andrea@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org So that isolated guests can communicate with the host via hv_sock channels. Signed-off-by: Andrea Parri (Microsoft) Reviewed-by: Michael Kelley --- drivers/hv/channel_mgmt.c | 8 ++++++-- include/linux/hyperv.h | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/hv/channel_mgmt.c b/drivers/hv/channel_mgmt.c index 67be81208a2d9..d800220ee54f4 100644 --- a/drivers/hv/channel_mgmt.c +++ b/drivers/hv/channel_mgmt.c @@ -976,13 +976,17 @@ find_primary_channel_by_offer(const struct vmbus_channel_offer_channel *offer) return channel; } -static bool vmbus_is_valid_device(const guid_t *guid) +static bool vmbus_is_valid_offer(const struct vmbus_channel_offer_channel *offer) { + const guid_t *guid = &offer->offer.if_type; u16 i; if (!hv_is_isolation_supported()) return true; + if (is_hvsock_offer(offer)) + return true; + for (i = 0; i < ARRAY_SIZE(vmbus_devs); i++) { if (guid_equal(guid, &vmbus_devs[i].guid)) return vmbus_devs[i].allowed_in_isolated; @@ -1004,7 +1008,7 @@ static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) trace_vmbus_onoffer(offer); - if (!vmbus_is_valid_device(&offer->offer.if_type)) { + if (!vmbus_is_valid_offer(offer)) { pr_err_ratelimited("Invalid offer %d from the host supporting isolation\n", offer->child_relid); atomic_dec(&vmbus_connection.offer_in_progress); diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 55478a6810b60..1112c5cf894e6 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1044,10 +1044,14 @@ struct vmbus_channel { u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr); u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id); +static inline bool is_hvsock_offer(const struct vmbus_channel_offer_channel *o) +{ + return !!(o->offer.chn_flags & VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER); +} + static inline bool is_hvsock_channel(const struct vmbus_channel *c) { - return !!(c->offermsg.offer.chn_flags & - VMBUS_CHANNEL_TLNPI_PROVIDER_OFFER); + return is_hvsock_offer(&c->offermsg); } static inline bool is_sub_channel(const struct vmbus_channel *c) From patchwork Wed Apr 20 20:07:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12820767 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CBB8C433EF for ; Wed, 20 Apr 2022 20:08:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382096AbiDTULU (ORCPT ); Wed, 20 Apr 2022 16:11:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382066AbiDTUKo (ORCPT ); Wed, 20 Apr 2022 16:10:44 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 252594738F; Wed, 20 Apr 2022 13:07:52 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id f17so3759819edt.4; Wed, 20 Apr 2022 13:07:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jgbU6+UKMrFaCyV2cRMHk+0IULMTYKeEHlBySELDljA=; b=k4l0mt+CPkmxIn2s/XBG5DM9kpYWuVHWRLSOarEzjqOPxZwGfBONSzl5nSNPsrXwzh wvg6/oO2H9ZW9qy3y9i4hc0/TPlmIOjf/ssPlGwtY9RAre7q5xSLqzWgGBxEtMJHQM5Y 55kmmKFiM6J77fQuT8vWRy0J8B+IbtxKAZG4oGgc4ywFAzlpcSGDuMKY4dWqoambEIIU n/XsTQ7ktiYYHy88ZwaFBlj08dCPA77bpyvf0jBZ5886XJBVSo0t+eHFuLCGYPdmk6fs lGXX9to3SE0myYhpLyD9hdXa7ecs25IPVJepGXvAtn9vQmeLFBN3Ps568marx9PzyqsV vWug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jgbU6+UKMrFaCyV2cRMHk+0IULMTYKeEHlBySELDljA=; b=unQw4HLyUPhhS82gCO3szf1tN/+NeK2CTWl2GG/3jM6JSYsIBR9gmR8VS0Sx6Ym3Lz 5h1CqOlfFWzVf7FNyGoyxNZmCqqvAy+71AmvlmNcRyqnLXb9wT5bQb80lAtDka7BLxwo yfQT1vVQYwzWITUR8BISm1wsJG0uKZKUFOLyXG0FsOvYbLF1P+IWU3PfFuZNCY9o5uUF wS+GlF2TI2Mz1tjXZt5/6TPiKfB6i9GYLcfxBWpLI7PobjfUnKQa29xdyLOCGp0kOyBZ XolUA3E28Om6NrzTGJgHjUJ1HquOeXhjIxSkSs4FAYmNZXPukRB3bOrPYWdzTe0ukSaV g+/Q== X-Gm-Message-State: AOAM533UUhWsUAbI8SDTFmu6CSj3l2b4YE4B9c9RBYI2miTpzTGuGn3D olrOU9CMMqhJpoMiktl2DOc2kQQuwALhvsJd X-Google-Smtp-Source: ABdhPJyRPMuB76L1g7iwvZj7xk/t0HTeEYFXgzyK0fRO/y7PZhA4FyYJe4doPA/3rcCveWfsIF3tVQ== X-Received: by 2002:aa7:d047:0:b0:41d:57cf:d588 with SMTP id n7-20020aa7d047000000b0041d57cfd588mr24916641edo.172.1650485270544; Wed, 20 Apr 2022 13:07:50 -0700 (PDT) Received: from anparri.mshome.net (host-82-53-3-95.retail.telecomitalia.it. [82.53.3.95]) by smtp.gmail.com with ESMTPSA id gy10-20020a170906f24a00b006e894144707sm7126853ejb.53.2022.04.20.13.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 13:07:50 -0700 (PDT) From: "Andrea Parri (Microsoft)" To: KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Stefano Garzarella , David Miller , Jakub Kicinski , Paolo Abeni Cc: linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Andrea Parri (Microsoft)" Subject: [PATCH 5/5] Drivers: hv: vmbus: Refactor the ring-buffer iterator functions Date: Wed, 20 Apr 2022 22:07:20 +0200 Message-Id: <20220420200720.434717-6-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420200720.434717-1-parri.andrea@gmail.com> References: <20220420200720.434717-1-parri.andrea@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org With no users of hv_pkt_iter_next_raw() and no "external" users of hv_pkt_iter_first_raw(), the iterator functions can be refactored and simplified to remove some indirection/code. Signed-off-by: Andrea Parri (Microsoft) Reviewed-by: Michael Kelley --- drivers/hv/ring_buffer.c | 32 +++++++++----------------------- include/linux/hyperv.h | 35 ++++------------------------------- 2 files changed, 13 insertions(+), 54 deletions(-) diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c index 3d215d9dec433..fa98b3a91206a 100644 --- a/drivers/hv/ring_buffer.c +++ b/drivers/hv/ring_buffer.c @@ -421,7 +421,7 @@ int hv_ringbuffer_read(struct vmbus_channel *channel, memcpy(buffer, (const char *)desc + offset, packetlen); /* Advance ring index to next packet descriptor */ - __hv_pkt_iter_next(channel, desc, true); + __hv_pkt_iter_next(channel, desc); /* Notify host of update */ hv_pkt_iter_close(channel); @@ -456,22 +456,6 @@ static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi) return (rbi->ring_datasize - priv_read_loc) + write_loc; } -/* - * Get first vmbus packet without copying it out of the ring buffer - */ -struct vmpacket_descriptor *hv_pkt_iter_first_raw(struct vmbus_channel *channel) -{ - struct hv_ring_buffer_info *rbi = &channel->inbound; - - hv_debug_delay_test(channel, MESSAGE_DELAY); - - if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor)) - return NULL; - - return (struct vmpacket_descriptor *)(hv_get_ring_buffer(rbi) + rbi->priv_read_index); -} -EXPORT_SYMBOL_GPL(hv_pkt_iter_first_raw); - /* * Get first vmbus packet from ring buffer after read_index * @@ -483,11 +467,14 @@ struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel) struct vmpacket_descriptor *desc, *desc_copy; u32 bytes_avail, pkt_len, pkt_offset; - desc = hv_pkt_iter_first_raw(channel); - if (!desc) + hv_debug_delay_test(channel, MESSAGE_DELAY); + + bytes_avail = hv_pkt_iter_avail(rbi); + if (bytes_avail < sizeof(struct vmpacket_descriptor)) return NULL; + bytes_avail = min(rbi->pkt_buffer_size, bytes_avail); - bytes_avail = min(rbi->pkt_buffer_size, hv_pkt_iter_avail(rbi)); + desc = (struct vmpacket_descriptor *)(hv_get_ring_buffer(rbi) + rbi->priv_read_index); /* * Ensure the compiler does not use references to incoming Hyper-V values (which @@ -534,8 +521,7 @@ EXPORT_SYMBOL_GPL(hv_pkt_iter_first); */ struct vmpacket_descriptor * __hv_pkt_iter_next(struct vmbus_channel *channel, - const struct vmpacket_descriptor *desc, - bool copy) + const struct vmpacket_descriptor *desc) { struct hv_ring_buffer_info *rbi = &channel->inbound; u32 packetlen = desc->len8 << 3; @@ -548,7 +534,7 @@ __hv_pkt_iter_next(struct vmbus_channel *channel, rbi->priv_read_index -= dsize; /* more data? */ - return copy ? hv_pkt_iter_first(channel) : hv_pkt_iter_first_raw(channel); + return hv_pkt_iter_first(channel); } EXPORT_SYMBOL_GPL(__hv_pkt_iter_next); diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 1112c5cf894e6..370adc9971d3e 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1673,55 +1673,28 @@ static inline u32 hv_pkt_len(const struct vmpacket_descriptor *desc) return desc->len8 << 3; } -struct vmpacket_descriptor * -hv_pkt_iter_first_raw(struct vmbus_channel *channel); - struct vmpacket_descriptor * hv_pkt_iter_first(struct vmbus_channel *channel); struct vmpacket_descriptor * __hv_pkt_iter_next(struct vmbus_channel *channel, - const struct vmpacket_descriptor *pkt, - bool copy); + const struct vmpacket_descriptor *pkt); void hv_pkt_iter_close(struct vmbus_channel *channel); static inline struct vmpacket_descriptor * -hv_pkt_iter_next_pkt(struct vmbus_channel *channel, - const struct vmpacket_descriptor *pkt, - bool copy) +hv_pkt_iter_next(struct vmbus_channel *channel, + const struct vmpacket_descriptor *pkt) { struct vmpacket_descriptor *nxt; - nxt = __hv_pkt_iter_next(channel, pkt, copy); + nxt = __hv_pkt_iter_next(channel, pkt); if (!nxt) hv_pkt_iter_close(channel); return nxt; } -/* - * Get next packet descriptor without copying it out of the ring buffer - * If at end of list, return NULL and update host. - */ -static inline struct vmpacket_descriptor * -hv_pkt_iter_next_raw(struct vmbus_channel *channel, - const struct vmpacket_descriptor *pkt) -{ - return hv_pkt_iter_next_pkt(channel, pkt, false); -} - -/* - * Get next packet descriptor from iterator - * If at end of list, return NULL and update host. - */ -static inline struct vmpacket_descriptor * -hv_pkt_iter_next(struct vmbus_channel *channel, - const struct vmpacket_descriptor *pkt) -{ - return hv_pkt_iter_next_pkt(channel, pkt, true); -} - #define foreach_vmbus_pkt(pkt, channel) \ for (pkt = hv_pkt_iter_first(channel); pkt; \ pkt = hv_pkt_iter_next(channel, pkt))