Skip to content

Commit 9937fa6

Browse files
andrea-parriliuw
authored andcommitted
PCI: hv: Add validation for untrusted Hyper-V values
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 hv_pci_onchannelcallback(). While at it, remove a redundant validation in hv_pci_generic_compl(): hv_pci_onchannelcallback() already ensures that all processed incoming packets are "at least as large as [in fact larger than] a response". Signed-off-by: Andrea Parri (Microsoft) <parri.andrea@gmail.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Acked-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> Link: https://lore.kernel.org/r/20220511223207.3386-2-parri.andrea@gmail.com Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent a2bad84 commit 9937fa6

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

drivers/pci/controller/pci-hyperv.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -981,11 +981,7 @@ static void hv_pci_generic_compl(void *context, struct pci_response *resp,
981981
{
982982
struct hv_pci_compl *comp_pkt = context;
983983

984-
if (resp_packet_size >= offsetofend(struct pci_response, status))
985-
comp_pkt->completion_status = resp->status;
986-
else
987-
comp_pkt->completion_status = -1;
988-
984+
comp_pkt->completion_status = resp->status;
989985
complete(&comp_pkt->host_event);
990986
}
991987

@@ -1606,8 +1602,13 @@ static void hv_pci_compose_compl(void *context, struct pci_response *resp,
16061602
struct pci_create_int_response *int_resp =
16071603
(struct pci_create_int_response *)resp;
16081604

1605+
if (resp_packet_size < sizeof(*int_resp)) {
1606+
comp_pkt->comp_pkt.completion_status = -1;
1607+
goto out;
1608+
}
16091609
comp_pkt->comp_pkt.completion_status = resp->status;
16101610
comp_pkt->int_desc = int_resp->int_desc;
1611+
out:
16111612
complete(&comp_pkt->comp_pkt.host_event);
16121613
}
16131614

@@ -2291,12 +2292,14 @@ static void q_resource_requirements(void *context, struct pci_response *resp,
22912292
struct q_res_req_compl *completion = context;
22922293
struct pci_q_res_req_response *q_res_req =
22932294
(struct pci_q_res_req_response *)resp;
2295+
s32 status;
22942296
int i;
22952297

2296-
if (resp->status < 0) {
2298+
status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
2299+
if (status < 0) {
22972300
dev_err(&completion->hpdev->hbus->hdev->device,
22982301
"query resource requirements failed: %x\n",
2299-
resp->status);
2302+
status);
23002303
} else {
23012304
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
23022305
completion->hpdev->probed_bar[i] =
@@ -2848,7 +2851,8 @@ static void hv_pci_onchannelcallback(void *context)
28482851
case PCI_BUS_RELATIONS:
28492852

28502853
bus_rel = (struct pci_bus_relations *)buffer;
2851-
if (bytes_recvd <
2854+
if (bytes_recvd < sizeof(*bus_rel) ||
2855+
bytes_recvd <
28522856
struct_size(bus_rel, func,
28532857
bus_rel->device_count)) {
28542858
dev_err(&hbus->hdev->device,
@@ -2862,7 +2866,8 @@ static void hv_pci_onchannelcallback(void *context)
28622866
case PCI_BUS_RELATIONS2:
28632867

28642868
bus_rel2 = (struct pci_bus_relations2 *)buffer;
2865-
if (bytes_recvd <
2869+
if (bytes_recvd < sizeof(*bus_rel2) ||
2870+
bytes_recvd <
28662871
struct_size(bus_rel2, func,
28672872
bus_rel2->device_count)) {
28682873
dev_err(&hbus->hdev->device,
@@ -2876,6 +2881,11 @@ static void hv_pci_onchannelcallback(void *context)
28762881
case PCI_EJECT:
28772882

28782883
dev_message = (struct pci_dev_incoming *)buffer;
2884+
if (bytes_recvd < sizeof(*dev_message)) {
2885+
dev_err(&hbus->hdev->device,
2886+
"eject message too small\n");
2887+
break;
2888+
}
28792889
hpdev = get_pcichild_wslot(hbus,
28802890
dev_message->wslot.slot);
28812891
if (hpdev) {
@@ -2887,6 +2897,11 @@ static void hv_pci_onchannelcallback(void *context)
28872897
case PCI_INVALIDATE_BLOCK:
28882898

28892899
inval = (struct pci_dev_inval_block *)buffer;
2900+
if (bytes_recvd < sizeof(*inval)) {
2901+
dev_err(&hbus->hdev->device,
2902+
"invalidate message too small\n");
2903+
break;
2904+
}
28902905
hpdev = get_pcichild_wslot(hbus,
28912906
inval->wslot.slot);
28922907
if (hpdev) {

0 commit comments

Comments
 (0)