From patchwork Thu Apr 28 14:51:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12830840 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 2AD21C4332F for ; Thu, 28 Apr 2022 14:51:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235966AbiD1Oyt (ORCPT ); Thu, 28 Apr 2022 10:54:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234686AbiD1Oyo (ORCPT ); Thu, 28 Apr 2022 10:54:44 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A0E75C853; Thu, 28 Apr 2022 07:51:29 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id l18so10048174ejc.7; Thu, 28 Apr 2022 07:51:29 -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=vuIsff87ALefijT05zxCKdqr80dXhi87dosPw80x4gc=; b=PstODrS9isMX6NHMixerP3v6KE5/4KrHAUXQKcTvx5lkKrEVb/g2ukG4fHldglpVun rTVT9xe6GxBlc8KDvz5SLHuiKFZ5bSevL7Zh6fdIY6j3GXCI0t8ivIZvE9tyttg055jh KHDGYaypWpdMQeFXAwLFi0jmHUsfBcEfE4perTsJq8OmRO6XBsS+nDAapDQkfCoUtSIK QoiEF0ErqgmotE7DKu02YgEH6BUouVqQQCSHZCuXnhhj6T4RORamSymNyqBzB4PtmdbB ECVPm9y+KBogeetoaQWScoJQfPDuZrBTlE7awUW4wMbkRsYa2NhvSHp95eUOfFkaufnf ykkw== 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=vuIsff87ALefijT05zxCKdqr80dXhi87dosPw80x4gc=; b=xS92vSnMt3ioYAju0bCHCdMfdXf8mb4rgzXw0EFTuuazspBAbgi1AeTd6VRv3ZD6YL 0wWe4XA6UduViyOQIWgvmI5HscSaQ4W6CqpZH5aTQbkyJqaj6TV6bJ6a3ncWhqKE4Ho3 /jGYXx07jbvBT/Ujgz1CmxSOO4Ewe96yDzPHNZYwjqePmXtyWkuRjZXO++d9VFigSSJX MHO+nNCqb15XtxYIDL958B71C3yL1+kRWbdfGEKB3DTs3J6CxAQNcRgZaFOfpFTXCVYI f3DCqG64EwB1yoXhKQvTwrsWjomXX7PMogAWSM09VdTqLwAhA1Hj7PcFE6uBcJI6tO47 ZRpg== X-Gm-Message-State: AOAM532U6Jl+ue0tHayeQdRy4hPEQdYSdsWP4SRdGamI8wuqnvb1xVnt /uI7oFamcJdNNZ6q1C8l8o0= X-Google-Smtp-Source: ABdhPJyWKoDRWtnsnCacA/1k6ig5YP6Iu6E9yfNfRPhRAc2DeNw/Ayi7I+PHkRNxj32Zb91VlUymtw== X-Received: by 2002:a17:906:1ec3:b0:6cf:d118:59e2 with SMTP id m3-20020a1709061ec300b006cfd11859e2mr32140363ejj.767.1651157487582; Thu, 28 Apr 2022 07:51:27 -0700 (PDT) Received: from anparri.mshome.net (host-79-49-65-106.retail.telecomitalia.it. [79.49.65.106]) by smtp.gmail.com with ESMTPSA id x18-20020a170906b09200b006e8baac3a09sm61616ejy.157.2022.04.28.07.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 07:51:27 -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 hyperv-next v2 1/5] hv_sock: Check hv_pkt_iter_first_raw()'s return value Date: Thu, 28 Apr 2022 16:51:03 +0200 Message-Id: <20220428145107.7878-2-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220428145107.7878-1-parri.andrea@gmail.com> References: <20220428145107.7878-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 Thu Apr 28 14:51:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12830841 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 4437FC43217 for ; Thu, 28 Apr 2022 14:51:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235963AbiD1Oyu (ORCPT ); Thu, 28 Apr 2022 10:54:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235947AbiD1Oyr (ORCPT ); Thu, 28 Apr 2022 10:54:47 -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 561775C76D; Thu, 28 Apr 2022 07:51:31 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id y3so10016867ejo.12; Thu, 28 Apr 2022 07:51:31 -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=xnoykz5SfHywGsuFJryTcQ62jy0T4q8fF6j2ty0MvCU=; b=huCiHGgumyOnAt2uMrgxcQyx9XZheJ3jb/h+VzM0kr0EfYqyP4IiiWYnEiFIkzDSeU 4eAghBKsEp8QPDSD5jC8Uri3YrHM008eG2IyPZtP/r5ZIm/PFB+XCC1m9ONELiAsF/qZ df4fCvvtWrDpy+W2+N1dqx6eiuI5L/H//J2rxjvwrF6m5+Sbt+6eomz/oBtjncxRsVJY s3ss2Wes+oh4YDihCnCp+T/UaA9f4//CPyeoN1XzFRRLjqn6B+bE1cy6Aqit0MF/gBsV p0I1thKEpdOsfar58FA41hzWxkUNt6fsBKZ9hInVSKdlN6N50DPnWF0FJJQCmy7U0E61 Yhxw== 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=xnoykz5SfHywGsuFJryTcQ62jy0T4q8fF6j2ty0MvCU=; b=tqGjG+tJIrn/7QRQyiZpUXXeqpiEJ6zSExJSIgu2tZD+jjZfTRIRdauBi+ZPXIC79J 9bIAge7Vbf+WONGFZ/LP5Qazz1FJayg5WN6JCkhgU0SfI3Zv28h8WpxpIfLZjjYeapTq HDMezHXAa06BfviqVhVys+arsjx4Dv4kFuNeV5z/cAr62KlNBv80OQXuUalo+kfvSLPI VAASn9N4S1WnY4VXjYM4ioks4RClgpRTe05l6WM1aU73/V6zDe7antG7KchXwSNVqhvy J5/jZrZowKzwyzS5QNwQesnCoAG2vXROrtNZYgGiGexeRuSoqDY5E4txkmWIrUscTgBg a9+Q== X-Gm-Message-State: AOAM532ENbFjKUC4ZAHARPszYD5cYANDNYo/kozOEauvBwC+tvmUfwTo ckCRbxq/LRaR5tIEXQ8b44k= X-Google-Smtp-Source: ABdhPJwuEZqtU95uxdJubkh8VZ+ZIfDWFNbLbnFkksVAEGmQd/oiVGp5cHf6ZPaBQEHlR2+beDb5+g== X-Received: by 2002:a17:907:1c8d:b0:6f2:eb2:1cd6 with SMTP id nb13-20020a1709071c8d00b006f20eb21cd6mr24177811ejc.568.1651157489831; Thu, 28 Apr 2022 07:51:29 -0700 (PDT) Received: from anparri.mshome.net (host-79-49-65-106.retail.telecomitalia.it. [79.49.65.106]) by smtp.gmail.com with ESMTPSA id x18-20020a170906b09200b006e8baac3a09sm61616ejy.157.2022.04.28.07.51.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 07:51:29 -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 hyperv-next v2 2/5] hv_sock: Copy packets sent by Hyper-V out of the ring buffer Date: Thu, 28 Apr 2022 16:51:04 +0200 Message-Id: <20220428145107.7878-3-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220428145107.7878-1-parri.andrea@gmail.com> References: <20220428145107.7878-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}(). Use HVS_PKT_LEN(HVS_MTU_SIZE) to initialize the buffer which holds the copies of the incoming packets. In this way, the packet can no longer be modified by the host. Signed-off-by: Andrea Parri (Microsoft) Reviewed-by: Michael Kelley Reviewed-by: Stefano Garzarella --- 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 Thu Apr 28 14:51:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12830843 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 D1260C433F5 for ; Thu, 28 Apr 2022 14:51:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348585AbiD1Oyw (ORCPT ); Thu, 28 Apr 2022 10:54:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231589AbiD1Oys (ORCPT ); Thu, 28 Apr 2022 10:54:48 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C7485C746; Thu, 28 Apr 2022 07:51:33 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id i27so10056501ejd.9; Thu, 28 Apr 2022 07:51:33 -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=RIN8gOawCXDxnuKuymLvtjAI0BjsxNFPgbemmO0+RTI=; b=k9qd4rs+kos4p9fuFrcrhEaTVurYbQu5JAB5TlvpM1zhEvKT+R76kG9wVl/juFq0Wp vPSw7lsWKQ82Gv71hZte+YG+/2NSk9LIqPmLLSUlVrcfWH/8tFSC5k4s/yFeIDCVknbC vnvZFmxHdNKCL9GoyPA3RLovjT5SQAXbXbJpfOgcPRkQZR1KYa0aWlV/2inf89s2PDuX 7ZQDQxZuKSQ/KktfUMAfrrLbpai6I1p9t02jwzIElprS1pYAPW2thVQPdZWuUYtoNq4X R+c967GpLQXEEXrNZzuxgoeVwIW4cCm+cpYZWcAfD1bmfoNcesP8CJH2zATQlL86n6Rz 8mbA== 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=RIN8gOawCXDxnuKuymLvtjAI0BjsxNFPgbemmO0+RTI=; b=7ngw+/wo1dMatLPSN+GNlPVepD6PVGEXNoa8XhNQO2G1yFZbVbJQgMTiZURWpEgAu1 fW5PIXuFOA/2kpW2hg5mf2EgDlew+uD0OdhICAIXP710KolySjO9Y38W+4nQf5qFix2S yE+3xZUhf1JWllqYzH5ReyRzY3p2DtSysqiH+DkxcbZub64xuhlWGtPQT37eVs7+Zakm beveIKVNdCnVSumGQVXZDlXzegVae6vOm27J5/WVjBBItqKXbV7NtHuSX3vXQ+FIHSey 8goimv8Ob3n4CWiW6FCCu7o21tge6fhH4RsgSvXo8gzydlSOpY2V9Nlv9eAYyyF0MghN enbg== X-Gm-Message-State: AOAM532DHpbbo/zu+sV6XbWw5LC/sDxd8SiUQFfRFIfgiFweCl7iaFA0 ehZWPZWPDSccHIlJ9wjYHSE= X-Google-Smtp-Source: ABdhPJy5lViZ6CLcBoESqVW5JlYJbBvqY0hkae/NMV5Se8ItjCNa8wWSnHEG8wkXUKWqx1nZcI9atg== X-Received: by 2002:a17:907:3e1e:b0:6ef:ebf8:4059 with SMTP id hp30-20020a1709073e1e00b006efebf84059mr32126466ejc.657.1651157492013; Thu, 28 Apr 2022 07:51:32 -0700 (PDT) Received: from anparri.mshome.net (host-79-49-65-106.retail.telecomitalia.it. [79.49.65.106]) by smtp.gmail.com with ESMTPSA id x18-20020a170906b09200b006e8baac3a09sm61616ejy.157.2022.04.28.07.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 07:51:31 -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 hyperv-next v2 3/5] hv_sock: Add validation for untrusted Hyper-V values Date: Thu, 28 Apr 2022 16:51:05 +0200 Message-Id: <20220428145107.7878-4-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220428145107.7878-1-parri.andrea@gmail.com> References: <20220428145107.7878-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 Reviewed-by: Stefano Garzarella --- include/linux/hyperv.h | 5 +++++ net/vmw_vsock/hyperv_transport.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 460a716f47486..a01c9fd0a3348 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1696,6 +1696,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..fd98229e3db30 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -577,12 +577,18 @@ 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); + + 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) + if (payload_len > pkt_len - HVS_HEADER_LEN || + payload_len > HVS_MTU_SIZE) return -EIO; if (payload_len == 0) From patchwork Thu Apr 28 14:51:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12830842 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 EFAC8C433EF for ; Thu, 28 Apr 2022 14:51:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348598AbiD1Oyx (ORCPT ); Thu, 28 Apr 2022 10:54:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229518AbiD1Oyu (ORCPT ); Thu, 28 Apr 2022 10:54:50 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A9B85C844; Thu, 28 Apr 2022 07:51:35 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id r13so10083167ejd.5; Thu, 28 Apr 2022 07:51:35 -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=emDG9f3LFc4UN/7AhW/W0+ckar3Ug+oqa5fA2UJ5tjg=; b=JndLe74SQfBVIJ2WpPhe5Wbx5xE7RqcorW45jPQ8Q3RK8izOuZ62/BUTNkGDR4Y0cM b9/D2ZWzWTr9AZVJnvuVlEc7hv0Anzjx0aYJNeBEsKU9DyLEBwQsudDz22hdve+EBNeS 9u3K3IBOvAsvYy5l5UEMQ4fzq5BZnaIUIv7vN8HX4fZIZqjAFHxgxDtb3FKLvo92HeLe pbpURa1z9zZbU8npF5Ni22aBAo0HCT6kBsfyg5tavA23gelCR0yWrcl2akRSmZv3BTkm cATdUwRQAibEt08Rq7JDIHnloAkK6wJ2jGKolJLbZhugt7KFeCilUCLAW7TfW8y5sdCy XdCQ== 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=emDG9f3LFc4UN/7AhW/W0+ckar3Ug+oqa5fA2UJ5tjg=; b=mDKWQekC3nGvOez4uSwq37aFQ08PYNqocCjQEuzDwQnTg+JShfg5RhpOUNipoCWDLm DAJULZsAzR/HKEnlgF1hYH+Mdfu8Ufm+iv1q6LdQanEdpo9kobPAXpqF6g/p7EWhXpmx 9EST2gXH7h3YQLzSqZ/u9h4Dy6NLC4JAKdPQQqbk925DvgieXDBoZZZi+n5W2neqZ8U4 PiFoZSafOYbJVrcoKdQGsZYJg2oiBAkdOdPesBmfOY1T37pnzeBT/PqEtVLel7kn3CEZ QOxPW0vWI3XG9CHCiRtIKwOguUZk6qeCXrNppdQPCQimDdqR3oZEj89UZduP3nBRZglo hmjQ== X-Gm-Message-State: AOAM53124AYlDIpn5kRxnhNT4qPecBzRWhnBlLN5Q0/KDTdO8e6OVZDf 8Ra2oSibr9Mf1p/PjxPTlU0= X-Google-Smtp-Source: ABdhPJwHp94hcTTfBsMBV3fZO22PEKNX1A9156t3tyfdZ8cjd1oOUTGNvy8rd3aUXXz+KLsR0mFWfA== X-Received: by 2002:a17:907:9622:b0:6f3:9797:8eaf with SMTP id gb34-20020a170907962200b006f397978eafmr20337071ejc.96.1651157494095; Thu, 28 Apr 2022 07:51:34 -0700 (PDT) Received: from anparri.mshome.net (host-79-49-65-106.retail.telecomitalia.it. [79.49.65.106]) by smtp.gmail.com with ESMTPSA id x18-20020a170906b09200b006e8baac3a09sm61616ejy.157.2022.04.28.07.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 07:51:33 -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 hyperv-next v2 4/5] Drivers: hv: vmbus: Accept hv_sock offers in isolated guests Date: Thu, 28 Apr 2022 16:51:06 +0200 Message-Id: <20220428145107.7878-5-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220428145107.7878-1-parri.andrea@gmail.com> References: <20220428145107.7878-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 50fea31f019f6..0db1b775e0139 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 a01c9fd0a3348..b028905d8334e 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1064,10 +1064,14 @@ u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id, 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 Thu Apr 28 14:51:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 12830844 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 B212AC433EF for ; Thu, 28 Apr 2022 14:51:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348631AbiD1OzC (ORCPT ); Thu, 28 Apr 2022 10:55:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348524AbiD1Oy4 (ORCPT ); Thu, 28 Apr 2022 10:54:56 -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 CF6765C87D; Thu, 28 Apr 2022 07:51:40 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id g6so10095830ejw.1; Thu, 28 Apr 2022 07:51:40 -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=sHYM7FZ7flhs082HViFIxV2vfk2Us4kPGD+KKmAh1Vs=; b=L79K8paf+nvvtoCzl3IMFopubCwaAVSOX3w0q5lEk2wc22QRuWcJRAjFTEag8Rsh4w 4jefT6tpnzbZgxdTbH+FSj6KsuLDEiYr13Bx7PWH4Zo6nTGCSWyEt7UDLPdTGvXqKhn9 r84g3+jq7XgQtY+M5dYDhKpmIgdAouhG/0v1AgIrzjVWrUqmL/4X/BgMINpZHJbFPv0q A4yRePrvk49eWAi0Cn3ULXflMZZdZByC1WZCMQM6LA885hrq+JmHbKYluRmeCdrb1zyh nwqDW7GXUBbCli3mI/rsfAXa3tUn9VteXVCRAuI3BdAPacv1AEiCQ/aGPdt8LmfWDWoy WwEQ== 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=sHYM7FZ7flhs082HViFIxV2vfk2Us4kPGD+KKmAh1Vs=; b=aAKMOQCU3jZvlsvOvCYPnHBuhxBpdEN0Pxinao5k4K5skjlPBsXai5fSqx30vAbD0D DKLf/tIbkR4HazuHkugdetfLp4E7NMry/YkCVsxt69XvfELU6dSZPaFl3NjYa8TYZYKZ QNTldJ2nFFTHUgwtmQIo/e+B93LeSD3wtNxcWjgZ8h0tfzvnNGc8p1TDldN15d6VNbWr FQbtXxm6XoXje4+kLs4qYI/fuJUcCb396FFZl0+zhlIq2fmpi1lZGpaQM4TV1Oc37jen 6Atm0z1JPqkI4rs9VaK0FaM8GOVKam9sNApheVxuajMGM2AkCETCe7sI79/5GUEx3fb9 9NlQ== X-Gm-Message-State: AOAM531+GUJ46cftzOU4/TYmDoZHtP1O4cS/lVBq3cAFY5zo1jOacvXp 620iKMNqGz+hpiVVY4hJ47A= X-Google-Smtp-Source: ABdhPJzWlV9lwW74NqP3RKApB0jNXyJnJAnRmcJUAc6xTX6qIRNoSWWC/I+7tYeq1iZNZkqRgi7cWA== X-Received: by 2002:a17:907:7fac:b0:6ef:e068:f5aa with SMTP id qk44-20020a1709077fac00b006efe068f5aamr25543538ejc.238.1651157499364; Thu, 28 Apr 2022 07:51:39 -0700 (PDT) Received: from anparri.mshome.net (host-79-49-65-106.retail.telecomitalia.it. [79.49.65.106]) by smtp.gmail.com with ESMTPSA id x18-20020a170906b09200b006e8baac3a09sm61616ejy.157.2022.04.28.07.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 07:51:39 -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 hyperv-next v2 5/5] Drivers: hv: vmbus: Refactor the ring-buffer iterator functions Date: Thu, 28 Apr 2022 16:51:07 +0200 Message-Id: <20220428145107.7878-6-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220428145107.7878-1-parri.andrea@gmail.com> References: <20220428145107.7878-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 e101b11f95e5d..59a4aa86d1f35 100644 --- a/drivers/hv/ring_buffer.c +++ b/drivers/hv/ring_buffer.c @@ -429,7 +429,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); @@ -464,22 +464,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 * @@ -491,11 +475,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 @@ -542,8 +529,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; @@ -556,7 +542,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 b028905d8334e..c440c45887c26 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1706,55 +1706,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))