Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1450554pxb; Thu, 14 Apr 2022 06:34:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXO/kczUqHK9pi2bkCP+MIY7RH+LkfzF9aXGYKGiE5tTt82fo7km4TKNUmfr/kUVeG+vOP X-Received: by 2002:a17:90a:b307:b0:1bd:37f3:f0fc with SMTP id d7-20020a17090ab30700b001bd37f3f0fcmr4370950pjr.132.1649943242646; Thu, 14 Apr 2022 06:34:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649943242; cv=none; d=google.com; s=arc-20160816; b=kSXF9L9ogRGpXO+xz6TGU/YjkiIipKP4sJyZSDm/DqpVspJtwVcjSnViujkTr5Hhn5 pXut9uzsvQ9QJjoo28G0RXqyB76R1EFI6hrdfMBFadiJUJm/tB4q0lKzhYNK1ppQYoxo 5APHzxJzcc/9qCKHBsx0m7gNOT0Z6+d9k0LK0WAbrqtlcUOFXwodw6PBq0q7uWb/g487 iCGm7hc4srGg1k8GtMYUeFmmWirF3Jf/qTK/96+mc6H6jNYQvhddMJt8QkgJJca0YbJr yccRpbzb8Qc0JDt4fzTfhNH1qdLj6cJOc6+FWs1jSThvlzAmrAL5UdFa/E5bBAQsGGjD qNPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uX50zrEMcunrp6nLIwZJ5dpGlndEZgBupE5oS9UHVBw=; b=SJR3h4TD7XjHzkyhNSatrfy93huncns+s78a2OVgFBNriU6rqJzwpDZHw9urroKB+o Tuq+tia7eYjFOxt4XvsVLm6g2zMMxZKu8HVx9YleToqEUHhp9q/0F9Y5fnLbIdx8y0lu R3GUQXIVNNJcjFWbw6mtaCbMeFe4kdyw1BOA1tAUx0PtN+90BqANI0CCCEICD/kPFIBg vh5fYxfLvsLqGVXK0xSZbdxonLCiWg4JF10HFNQv21uZLqSbaQn7rUMGU1R9sw91hou4 TJMPkqpdiF7khiNjQPolCv0gAGToqQDIvzVKoZUj1/1oX0fil4nyKadHtVn9FPV8ZAp/ foJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TWL9gpNo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y15-20020a170902d64f00b00156aa846117si15763783plh.622.2022.04.14.06.33.48; Thu, 14 Apr 2022 06:34:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TWL9gpNo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S238738AbiDMUuk (ORCPT + 99 others); Wed, 13 Apr 2022 16:50:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236542AbiDMUu1 (ORCPT ); Wed, 13 Apr 2022 16:50:27 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DF0F6BDFD; Wed, 13 Apr 2022 13:48:05 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id c6so3878660edn.8; Wed, 13 Apr 2022 13:48:04 -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=TWL9gpNoJEn+V3lGRa+0fR4At7GciihC0gb78RaA0RoWmfVHi1tXmtW7DcBqBe9yqq 2SPKbI4tntfHK1tyGMLHSLJ9NUROyqCAqhElxEB0yaPxLgIIFiX2wYSau4QvlQ6RONXB ak8Z3+Wzuh7XnctzCH/+a8XI9SnGfTbXK9Y7KLZ5sgw7MNRQnq6Gnqag8Y3vkGajf39r PG6QavaDM9GuvYU583HpozI7yM9CRr9/bobeXKdnzs8cS/v0PB9XjDZ3lVa45VCIkj8j NQoKQrDYbxhldh7E016l71JTlH4p1VVvvARJMcxDniOFRu9t1ojZnrL2TWEI3asYszM7 DJdw== 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=JDpIXmdvklZaqpLJsTSKxZ561cx1mRhqUiwT2yaRgFIbUpLOAIRmGIvLMbZKhiZCoH lCPVdKcaaAimdFQjkTbohtDmyDIa/XcOLaWMr9aLfVgc8rZnSLMPvr9bJGRDwuEyNL3z CU42YO5aZhQVgviXLd09UX7+YVH9LZ/tX3L0i5WqIf7PcGhlxZafN+iXF1OVdJCeGkQr cH/wJe6TCNhjjQzW/iGbJT6AYkZLEtd1ibbpTZl6QjxPZFxgdpIp9sKNE14sFcShup9H oLeGlPSkJLTcN4fhgyF0CoxaBM9cUzp2WOIHUqs9pt6GXG6PaaevFPfZUduKV3FGfwCp FGdA== X-Gm-Message-State: AOAM531WduXZzYFVTaQoIrovyKiRKb2C08MVZL9OSYsL/ZQOBIOGVDRS eXM7xYU1ZL+vplZGhS9FXnQ= X-Received: by 2002:a05:6402:d62:b0:41d:79e6:1617 with SMTP id ec34-20020a0564020d6200b0041d79e61617mr18485763edb.378.1649882883512; Wed, 13 Apr 2022 13:48:03 -0700 (PDT) Received: from anparri.mshome.net (host-79-52-64-69.retail.telecomitalia.it. [79.52.64.69]) by smtp.gmail.com with ESMTPSA id u6-20020a170906408600b006e87d654270sm5021ejj.44.2022.04.13.13.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 13:48:02 -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: [RFC PATCH 3/6] hv_sock: Add validation for untrusted Hyper-V values Date: Wed, 13 Apr 2022 22:47:39 +0200 Message-Id: <20220413204742.5539-4-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220413204742.5539-1-parri.andrea@gmail.com> References: <20220413204742.5539-1-parri.andrea@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.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) --- 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) -- 2.25.1