Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp467486pxb; Thu, 7 Apr 2022 10:05:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZyu3MTMhuuBs1i/K4gYQpo8eQL9gen9KB4DA8LCrQ7M0kQ9mlh9x/EnCV+AohsbauXyfL X-Received: by 2002:a17:90b:314b:b0:1c7:4a4f:6740 with SMTP id ip11-20020a17090b314b00b001c74a4f6740mr16960178pjb.145.1649351104332; Thu, 07 Apr 2022 10:05:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649351104; cv=none; d=google.com; s=arc-20160816; b=UKxG66gRHkKIXcSXcFOmn62yM32lFj5HkMntIVRDr99errTKDIGv/TOkHZ/3t+Y3nB NRZafBct30207TM6pafXScydDn+AtHtUUpTfrNJ2fQnXSg9Y1/qSS/HD45htpR9UfBa+ d606z9C6WBIPTGN4a8DbFl64H4X1RHY2ZYZ93T4qB2UBhdqcToIrLs3sRfkq/8emaGEk 1rtA9nY5dFbin42GMdbQnw+JxQZcCDkP9956lDXBU9flpe2++Ok8AsGcBixKH2cTxX0X bxkc2NEQQ6JwCrkxFdcoLOqj8RI6KjlM+f1fyyDeL5sIwLO9R8NQNj+Sv5t6mECGHo67 x/Bw== 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=PLk82bq3hxGeZAsu9PKoYX7IPjIjTIMBZgaXITeK+Oo=; b=dWcvAEwwKC40RllHJGtkt2PMbNKaFgN0RD4/O7KRHERAYS39GZGBzvCHNA5El3ABJE CYwbeSRuU43F3OGn5z7DyR4z+yCz+LdWBUivtAi+oQuL3cfLrRHT+YvyOl0jEzhfK+Y+ moHA7Y24Y1+W6c9CqJaGtN69vRrGmMVwAUHrI3+/JjLKe4xyA67td5nSG7eVkALVYY8b ZS+L0MEBaZuQxUBPhGBbagvhJYYJJIIwfVHsDJGYnDHHEy+eERCSktzWbdLds/Ei52XG ZRJ4hL/2sX1uMvGTPHUdaCPP2/VxIg7Sj+KHP3XtwQ+2oSghCFXa74GOm2FJMFB1uOC2 yWwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=FgBomAD9; 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 q11-20020a170902bd8b00b0015620aa1c68si145819pls.451.2022.04.07.10.04.05; Thu, 07 Apr 2022 10:05:04 -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=FgBomAD9; 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 S240693AbiDGEeb (ORCPT + 99 others); Thu, 7 Apr 2022 00:34:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240656AbiDGEeP (ORCPT ); Thu, 7 Apr 2022 00:34:15 -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 7D7041E6E8F; Wed, 6 Apr 2022 21:32:13 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id bq8so8276209ejb.10; Wed, 06 Apr 2022 21:32:13 -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=PLk82bq3hxGeZAsu9PKoYX7IPjIjTIMBZgaXITeK+Oo=; b=FgBomAD9uMBxBhvAxcGsyN8xGpx6a4yc7XbtSZF0jBwzC3SxUwv9d1sucKUMJQt14W p6RBZbhkhyBeQ6srNiXAw6b3PP6rAcp8GfwZSqRyogyhKhzcf+imd7HBVV+lN2Y5oed+ M6V2lB6gWpouN051rJsiURiw3SA8U8aSjGtvw4Dzgzcq3L9t+Pt4xYL+Gad40UwBDUyS iRZzFSGvGF822WBLdeORC7hVGAId5/eEdrB0VMw896z0IRdGONoyBn40b2RleNmahIcG SNMSJGIHB4N6t02/jRH84aTyKmwez0wxDSV9rOgWmjSE9FAu21uqugOtm/gYZnqAjHax oPQw== 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=PLk82bq3hxGeZAsu9PKoYX7IPjIjTIMBZgaXITeK+Oo=; b=knQR2a8PSM2neMWN8ThE/XZHLM67AADZWJEbZ1+InP7KuxB67EajM3r9QKp6rLdMJw 1n9dBBOv5s0nJMeLnNiFmwBt8UJbDTBDERUREVHwXCigyMs3J7wHGQFxGlN5/2d2TKXw UuKNaQYElQfyrr8OCi1Pkdt72w8+9QwOVD22HXPQKpAYpxMQ9PcuwPTi7obMtmA5HF7O JK9t+8WaXrc6VL3Jtn/IgfVM1xy3vHADB3RWuSsravNTiP1/XttBFm0o2wx+wFBrGR2e emxYaIKfMFhN0X6uePeZMkKOJTslgSh7Lr5++D41PB3cPegaD5wjjOCeM0m8+leXELPD evDg== X-Gm-Message-State: AOAM532DEtssh5WJh/9/M4xu5wplhqilkfQw5PgdJ3TOYzQSrluTm186 wnLyrRpVdKGOmT2fIaklC50= X-Received: by 2002:a17:907:6d2a:b0:6df:e513:5410 with SMTP id sa42-20020a1709076d2a00b006dfe5135410mr11302460ejc.544.1649305931920; Wed, 06 Apr 2022 21:32:11 -0700 (PDT) Received: from anparri.mshome.net (host-87-11-75-174.retail.telecomitalia.it. [87.11.75.174]) by smtp.gmail.com with ESMTPSA id ke11-20020a17090798eb00b006e7fbf53398sm3531341ejc.129.2022.04.06.21.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 21:32:11 -0700 (PDT) From: "Andrea Parri (Microsoft)" To: KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Wei Hu , Lorenzo Pieralisi , Rob Herring , Krzysztof Wilczynski , Bjorn Helgaas Cc: linux-hyperv@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, "Andrea Parri (Microsoft)" Subject: [PATCH 2/6] PCI: hv: Use vmbus_requestor to generate transaction IDs for VMbus hardening Date: Thu, 7 Apr 2022 06:30:24 +0200 Message-Id: <20220407043028.379534-3-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220407043028.379534-1-parri.andrea@gmail.com> References: <20220407043028.379534-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 Currently, pointers to guest memory are passed to Hyper-V as transaction IDs in hv_pci. In the face of errors or malicious behavior in Hyper-V, hv_pci should not expose or trust the transaction IDs returned by Hyper-V to be valid guest memory addresses. Instead, use small integers generated by vmbus_requestor as request (transaction) IDs. Suggested-by: Michael Kelley Signed-off-by: Andrea Parri (Microsoft) --- drivers/pci/controller/pci-hyperv.c | 39 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c index 88b3b56d05228..c1322ac37cda9 100644 --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -91,6 +91,13 @@ static enum pci_protocol_version_t pci_protocol_versions[] = { /* space for 32bit serial number as string */ #define SLOT_NAME_SIZE 11 +/* + * Size of requestor for VMbus; the value is based on the observation + * that having more than one request outstanding is 'rare', and so 64 + * should be generous in ensuring that we don't ever run out. + */ +#define HV_PCI_RQSTOR_SIZE 64 + /* * Message Types */ @@ -1407,7 +1414,7 @@ static void hv_int_desc_free(struct hv_pci_dev *hpdev, int_pkt->wslot.slot = hpdev->desc.win_slot.slot; int_pkt->int_desc = *int_desc; vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), - (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0); + 0, VM_PKT_DATA_INBAND, 0); kfree(int_desc); } @@ -2649,7 +2656,7 @@ static void hv_eject_device_work(struct work_struct *work) ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; vmbus_sendpacket(hbus->hdev->channel, ejct_pkt, - sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt, + sizeof(*ejct_pkt), 0, VM_PKT_DATA_INBAND, 0); /* For the get_pcichild() in hv_pci_eject_device() */ @@ -2696,8 +2703,9 @@ static void hv_pci_onchannelcallback(void *context) const int packet_size = 0x100; int ret; struct hv_pcibus_device *hbus = context; + struct vmbus_channel *chan = hbus->hdev->channel; u32 bytes_recvd; - u64 req_id; + u64 req_id, req_addr; struct vmpacket_descriptor *desc; unsigned char *buffer; int bufferlen = packet_size; @@ -2715,8 +2723,8 @@ static void hv_pci_onchannelcallback(void *context) return; while (1) { - ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer, - bufferlen, &bytes_recvd, &req_id); + ret = vmbus_recvpacket_raw(chan, buffer, bufferlen, + &bytes_recvd, &req_id); if (ret == -ENOBUFS) { kfree(buffer); @@ -2743,11 +2751,14 @@ static void hv_pci_onchannelcallback(void *context) switch (desc->type) { case VM_PKT_COMP: - /* - * The host is trusted, and thus it's safe to interpret - * this transaction ID as a pointer. - */ - comp_packet = (struct pci_packet *)req_id; + req_addr = chan->request_addr_callback(chan, req_id); + if (req_addr == VMBUS_RQST_ERROR) { + dev_warn_ratelimited(&hbus->hdev->device, + "Invalid transaction ID %llx\n", + req_id); + break; + } + comp_packet = (struct pci_packet *)req_addr; response = (struct pci_response *)buffer; comp_packet->completion_func(comp_packet->compl_ctxt, response, @@ -3428,6 +3439,10 @@ static int hv_pci_probe(struct hv_device *hdev, goto free_dom; } + hdev->channel->next_request_id_callback = vmbus_next_request_id; + hdev->channel->request_addr_callback = vmbus_request_addr; + hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; + ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, hv_pci_onchannelcallback, hbus); if (ret) @@ -3758,6 +3773,10 @@ static int hv_pci_resume(struct hv_device *hdev) hbus->state = hv_pcibus_init; + hdev->channel->next_request_id_callback = vmbus_next_request_id; + hdev->channel->request_addr_callback = vmbus_request_addr; + hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; + ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, hv_pci_onchannelcallback, hbus); if (ret) -- 2.25.1