* [PATCH v2 0/2] PCI: hv: Hardening changes
@ 2022-05-11 22:32 Andrea Parri (Microsoft)
2022-05-11 22:32 ` [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values Andrea Parri (Microsoft)
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Andrea Parri (Microsoft) @ 2022-05-11 22:32 UTC (permalink / raw)
To: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Michael Kelley, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczynski, Bjorn Helgaas
Cc: linux-hyperv, linux-pci, linux-kernel, Andrea Parri (Microsoft)
Changes since v1[1]:
- Add validation in q_resource_requirements()
Patch #2 depends on changes in hyperv-next. (Acknowledging that hyperv
is entering EOM, for review.)
Thanks,
Andrea
[1] https://lkml.kernel.org/r/20220504125039.2598-1-parri.andrea@gmail.com
Andrea Parri (Microsoft) (2):
PCI: hv: Add validation for untrusted Hyper-V values
PCI: hv: Fix synchronization between channel callback and
hv_pci_bus_exit()
drivers/pci/controller/pci-hyperv.c | 59 +++++++++++++++++++++--------
1 file changed, 43 insertions(+), 16 deletions(-)
--
2.25.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values
2022-05-11 22:32 [PATCH v2 0/2] PCI: hv: Hardening changes Andrea Parri (Microsoft)
@ 2022-05-11 22:32 ` Andrea Parri (Microsoft)
2022-05-12 15:08 ` Michael Kelley (LINUX)
2022-05-11 22:32 ` [PATCH v2 2/2] PCI: hv: Fix synchronization between channel callback and hv_pci_bus_exit() Andrea Parri (Microsoft)
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Andrea Parri (Microsoft) @ 2022-05-11 22:32 UTC (permalink / raw)
To: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Michael Kelley, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczynski, Bjorn Helgaas
Cc: linux-hyperv, linux-pci, linux-kernel, Andrea Parri (Microsoft)
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>
---
drivers/pci/controller/pci-hyperv.c | 33 +++++++++++++++++++++--------
1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
index e439b810f974b..a06e2cf946580 100644
--- a/drivers/pci/controller/pci-hyperv.c
+++ b/drivers/pci/controller/pci-hyperv.c
@@ -981,11 +981,7 @@ static void hv_pci_generic_compl(void *context, struct pci_response *resp,
{
struct hv_pci_compl *comp_pkt = context;
- if (resp_packet_size >= offsetofend(struct pci_response, status))
- comp_pkt->completion_status = resp->status;
- else
- comp_pkt->completion_status = -1;
-
+ comp_pkt->completion_status = resp->status;
complete(&comp_pkt->host_event);
}
@@ -1606,8 +1602,13 @@ static void hv_pci_compose_compl(void *context, struct pci_response *resp,
struct pci_create_int_response *int_resp =
(struct pci_create_int_response *)resp;
+ if (resp_packet_size < sizeof(*int_resp)) {
+ comp_pkt->comp_pkt.completion_status = -1;
+ goto out;
+ }
comp_pkt->comp_pkt.completion_status = resp->status;
comp_pkt->int_desc = int_resp->int_desc;
+out:
complete(&comp_pkt->comp_pkt.host_event);
}
@@ -2291,12 +2292,14 @@ static void q_resource_requirements(void *context, struct pci_response *resp,
struct q_res_req_compl *completion = context;
struct pci_q_res_req_response *q_res_req =
(struct pci_q_res_req_response *)resp;
+ s32 status;
int i;
- if (resp->status < 0) {
+ status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
+ if (status < 0) {
dev_err(&completion->hpdev->hbus->hdev->device,
"query resource requirements failed: %x\n",
- resp->status);
+ status);
} else {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
completion->hpdev->probed_bar[i] =
@@ -2848,7 +2851,8 @@ static void hv_pci_onchannelcallback(void *context)
case PCI_BUS_RELATIONS:
bus_rel = (struct pci_bus_relations *)buffer;
- if (bytes_recvd <
+ if (bytes_recvd < sizeof(*bus_rel) ||
+ bytes_recvd <
struct_size(bus_rel, func,
bus_rel->device_count)) {
dev_err(&hbus->hdev->device,
@@ -2862,7 +2866,8 @@ static void hv_pci_onchannelcallback(void *context)
case PCI_BUS_RELATIONS2:
bus_rel2 = (struct pci_bus_relations2 *)buffer;
- if (bytes_recvd <
+ if (bytes_recvd < sizeof(*bus_rel2) ||
+ bytes_recvd <
struct_size(bus_rel2, func,
bus_rel2->device_count)) {
dev_err(&hbus->hdev->device,
@@ -2876,6 +2881,11 @@ static void hv_pci_onchannelcallback(void *context)
case PCI_EJECT:
dev_message = (struct pci_dev_incoming *)buffer;
+ if (bytes_recvd < sizeof(*dev_message)) {
+ dev_err(&hbus->hdev->device,
+ "eject message too small\n");
+ break;
+ }
hpdev = get_pcichild_wslot(hbus,
dev_message->wslot.slot);
if (hpdev) {
@@ -2887,6 +2897,11 @@ static void hv_pci_onchannelcallback(void *context)
case PCI_INVALIDATE_BLOCK:
inval = (struct pci_dev_inval_block *)buffer;
+ if (bytes_recvd < sizeof(*inval)) {
+ dev_err(&hbus->hdev->device,
+ "invalidate message too small\n");
+ break;
+ }
hpdev = get_pcichild_wslot(hbus,
inval->wslot.slot);
if (hpdev) {
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/2] PCI: hv: Fix synchronization between channel callback and hv_pci_bus_exit()
2022-05-11 22:32 [PATCH v2 0/2] PCI: hv: Hardening changes Andrea Parri (Microsoft)
2022-05-11 22:32 ` [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values Andrea Parri (Microsoft)
@ 2022-05-11 22:32 ` Andrea Parri (Microsoft)
2022-05-13 10:11 ` [PATCH v2 0/2] PCI: hv: Hardening changes Lorenzo Pieralisi
2022-05-13 16:58 ` Wei Liu
3 siblings, 0 replies; 7+ messages in thread
From: Andrea Parri (Microsoft) @ 2022-05-11 22:32 UTC (permalink / raw)
To: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Michael Kelley, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczynski, Bjorn Helgaas
Cc: linux-hyperv, linux-pci, linux-kernel, Andrea Parri (Microsoft)
[ Similarly to commit a765ed47e4516 ("PCI: hv: Fix synchronization
between channel callback and hv_compose_msi_msg()"): ]
The (on-stack) teardown packet becomes invalid once the completion
timeout in hv_pci_bus_exit() has expired and hv_pci_bus_exit() has
returned. Prevent the channel callback from accessing the invalid
packet by removing the ID associated to such packet from the VMbus
requestor in hv_pci_bus_exit().
Signed-off-by: Andrea Parri (Microsoft) <parri.andrea@gmail.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
---
drivers/pci/controller/pci-hyperv.c | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
index a06e2cf946580..db814f7b93baa 100644
--- a/drivers/pci/controller/pci-hyperv.c
+++ b/drivers/pci/controller/pci-hyperv.c
@@ -3664,6 +3664,7 @@ static int hv_pci_probe(struct hv_device *hdev,
static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
{
struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
+ struct vmbus_channel *chan = hdev->channel;
struct {
struct pci_packet teardown_packet;
u8 buffer[sizeof(struct pci_message)];
@@ -3671,13 +3672,14 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
struct hv_pci_compl comp_pkt;
struct hv_pci_dev *hpdev, *tmp;
unsigned long flags;
+ u64 trans_id;
int ret;
/*
* After the host sends the RESCIND_CHANNEL message, it doesn't
* access the per-channel ringbuffer any longer.
*/
- if (hdev->channel->rescind)
+ if (chan->rescind)
return 0;
if (!keep_devs) {
@@ -3714,16 +3716,26 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
pkt.teardown_packet.compl_ctxt = &comp_pkt;
pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
- ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
- sizeof(struct pci_message),
- (unsigned long)&pkt.teardown_packet,
- VM_PKT_DATA_INBAND,
- VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
+ ret = vmbus_sendpacket_getid(chan, &pkt.teardown_packet.message,
+ sizeof(struct pci_message),
+ (unsigned long)&pkt.teardown_packet,
+ &trans_id, VM_PKT_DATA_INBAND,
+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
if (ret)
return ret;
- if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0)
+ if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) {
+ /*
+ * The completion packet on the stack becomes invalid after
+ * 'return'; remove the ID from the VMbus requestor if the
+ * identifier is still mapped to/associated with the packet.
+ *
+ * Cf. hv_pci_onchannelcallback().
+ */
+ vmbus_request_addr_match(chan, trans_id,
+ (unsigned long)&pkt.teardown_packet);
return -ETIMEDOUT;
+ }
return 0;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* RE: [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values
2022-05-11 22:32 ` [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values Andrea Parri (Microsoft)
@ 2022-05-12 15:08 ` Michael Kelley (LINUX)
0 siblings, 0 replies; 7+ messages in thread
From: Michael Kelley (LINUX) @ 2022-05-12 15:08 UTC (permalink / raw)
To: Andrea Parri (Microsoft),
KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Lorenzo Pieralisi, Rob Herring, Krzysztof Wilczynski,
Bjorn Helgaas
Cc: linux-hyperv, linux-pci, linux-kernel
From: Andrea Parri (Microsoft) <parri.andrea@gmail.com> Sent: Wednesday, May 11, 2022 3:32 PM
>
> 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>
> ---
> drivers/pci/controller/pci-hyperv.c | 33 +++++++++++++++++++++--------
> 1 file changed, 24 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
> index e439b810f974b..a06e2cf946580 100644
> --- a/drivers/pci/controller/pci-hyperv.c
> +++ b/drivers/pci/controller/pci-hyperv.c
> @@ -981,11 +981,7 @@ static void hv_pci_generic_compl(void *context, struct
> pci_response *resp,
> {
> struct hv_pci_compl *comp_pkt = context;
>
> - if (resp_packet_size >= offsetofend(struct pci_response, status))
> - comp_pkt->completion_status = resp->status;
> - else
> - comp_pkt->completion_status = -1;
> -
> + comp_pkt->completion_status = resp->status;
> complete(&comp_pkt->host_event);
> }
>
> @@ -1606,8 +1602,13 @@ static void hv_pci_compose_compl(void *context, struct
> pci_response *resp,
> struct pci_create_int_response *int_resp =
> (struct pci_create_int_response *)resp;
>
> + if (resp_packet_size < sizeof(*int_resp)) {
> + comp_pkt->comp_pkt.completion_status = -1;
> + goto out;
> + }
> comp_pkt->comp_pkt.completion_status = resp->status;
> comp_pkt->int_desc = int_resp->int_desc;
> +out:
> complete(&comp_pkt->comp_pkt.host_event);
> }
>
> @@ -2291,12 +2292,14 @@ static void q_resource_requirements(void *context,
> struct pci_response *resp,
> struct q_res_req_compl *completion = context;
> struct pci_q_res_req_response *q_res_req =
> (struct pci_q_res_req_response *)resp;
> + s32 status;
> int i;
>
> - if (resp->status < 0) {
> + status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
> + if (status < 0) {
> dev_err(&completion->hpdev->hbus->hdev->device,
> "query resource requirements failed: %x\n",
> - resp->status);
> + status);
> } else {
> for (i = 0; i < PCI_STD_NUM_BARS; i++) {
> completion->hpdev->probed_bar[i] =
> @@ -2848,7 +2851,8 @@ static void hv_pci_onchannelcallback(void *context)
> case PCI_BUS_RELATIONS:
>
> bus_rel = (struct pci_bus_relations *)buffer;
> - if (bytes_recvd <
> + if (bytes_recvd < sizeof(*bus_rel) ||
> + bytes_recvd <
> struct_size(bus_rel, func,
> bus_rel->device_count)) {
> dev_err(&hbus->hdev->device,
> @@ -2862,7 +2866,8 @@ static void hv_pci_onchannelcallback(void *context)
> case PCI_BUS_RELATIONS2:
>
> bus_rel2 = (struct pci_bus_relations2 *)buffer;
> - if (bytes_recvd <
> + if (bytes_recvd < sizeof(*bus_rel2) ||
> + bytes_recvd <
> struct_size(bus_rel2, func,
> bus_rel2->device_count)) {
> dev_err(&hbus->hdev->device,
> @@ -2876,6 +2881,11 @@ static void hv_pci_onchannelcallback(void *context)
> case PCI_EJECT:
>
> dev_message = (struct pci_dev_incoming *)buffer;
> + if (bytes_recvd < sizeof(*dev_message)) {
> + dev_err(&hbus->hdev->device,
> + "eject message too small\n");
> + break;
> + }
> hpdev = get_pcichild_wslot(hbus,
> dev_message->wslot.slot);
> if (hpdev) {
> @@ -2887,6 +2897,11 @@ static void hv_pci_onchannelcallback(void *context)
> case PCI_INVALIDATE_BLOCK:
>
> inval = (struct pci_dev_inval_block *)buffer;
> + if (bytes_recvd < sizeof(*inval)) {
> + dev_err(&hbus->hdev->device,
> + "invalidate message too small\n");
> + break;
> + }
> hpdev = get_pcichild_wslot(hbus,
> inval->wslot.slot);
> if (hpdev) {
> --
> 2.25.1
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/2] PCI: hv: Hardening changes
2022-05-11 22:32 [PATCH v2 0/2] PCI: hv: Hardening changes Andrea Parri (Microsoft)
2022-05-11 22:32 ` [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values Andrea Parri (Microsoft)
2022-05-11 22:32 ` [PATCH v2 2/2] PCI: hv: Fix synchronization between channel callback and hv_pci_bus_exit() Andrea Parri (Microsoft)
@ 2022-05-13 10:11 ` Lorenzo Pieralisi
2022-05-13 16:51 ` Wei Liu
2022-05-13 16:58 ` Wei Liu
3 siblings, 1 reply; 7+ messages in thread
From: Lorenzo Pieralisi @ 2022-05-13 10:11 UTC (permalink / raw)
To: Andrea Parri (Microsoft)
Cc: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Michael Kelley, Rob Herring, Krzysztof Wilczynski,
Bjorn Helgaas, linux-hyperv, linux-pci, linux-kernel
On Thu, May 12, 2022 at 12:32:05AM +0200, Andrea Parri (Microsoft) wrote:
> Changes since v1[1]:
> - Add validation in q_resource_requirements()
>
> Patch #2 depends on changes in hyperv-next. (Acknowledging that hyperv
> is entering EOM, for review.)
I suppose then it is best for me to ACK it and ask Hyper-V maintainers
to pick it up.
For the series:
Acked-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
>
> Thanks,
> Andrea
>
> [1] https://lkml.kernel.org/r/20220504125039.2598-1-parri.andrea@gmail.com
>
> Andrea Parri (Microsoft) (2):
> PCI: hv: Add validation for untrusted Hyper-V values
> PCI: hv: Fix synchronization between channel callback and
> hv_pci_bus_exit()
>
> drivers/pci/controller/pci-hyperv.c | 59 +++++++++++++++++++++--------
> 1 file changed, 43 insertions(+), 16 deletions(-)
>
> --
> 2.25.1
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/2] PCI: hv: Hardening changes
2022-05-13 10:11 ` [PATCH v2 0/2] PCI: hv: Hardening changes Lorenzo Pieralisi
@ 2022-05-13 16:51 ` Wei Liu
0 siblings, 0 replies; 7+ messages in thread
From: Wei Liu @ 2022-05-13 16:51 UTC (permalink / raw)
To: Lorenzo Pieralisi
Cc: Andrea Parri (Microsoft),
KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Michael Kelley, Rob Herring, Krzysztof Wilczynski,
Bjorn Helgaas, linux-hyperv, linux-pci, linux-kernel
On Fri, May 13, 2022 at 11:11:32AM +0100, Lorenzo Pieralisi wrote:
> On Thu, May 12, 2022 at 12:32:05AM +0200, Andrea Parri (Microsoft) wrote:
> > Changes since v1[1]:
> > - Add validation in q_resource_requirements()
> >
> > Patch #2 depends on changes in hyperv-next. (Acknowledging that hyperv
> > is entering EOM, for review.)
>
> I suppose then it is best for me to ACK it and ask Hyper-V maintainers
> to pick it up.
>
Yes.
> For the series:
>
>Pending Acked-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
>
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/2] PCI: hv: Hardening changes
2022-05-11 22:32 [PATCH v2 0/2] PCI: hv: Hardening changes Andrea Parri (Microsoft)
` (2 preceding siblings ...)
2022-05-13 10:11 ` [PATCH v2 0/2] PCI: hv: Hardening changes Lorenzo Pieralisi
@ 2022-05-13 16:58 ` Wei Liu
3 siblings, 0 replies; 7+ messages in thread
From: Wei Liu @ 2022-05-13 16:58 UTC (permalink / raw)
To: Andrea Parri (Microsoft)
Cc: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
Dexuan Cui, Michael Kelley, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczynski, Bjorn Helgaas, linux-hyperv, linux-pci,
linux-kernel
On Thu, May 12, 2022 at 12:32:05AM +0200, Andrea Parri (Microsoft) wrote:
> Changes since v1[1]:
> - Add validation in q_resource_requirements()
>
> Patch #2 depends on changes in hyperv-next. (Acknowledging that hyperv
> is entering EOM, for review.)
>
> Thanks,
> Andrea
>
> [1] https://lkml.kernel.org/r/20220504125039.2598-1-parri.andrea@gmail.com
>
> Andrea Parri (Microsoft) (2):
> PCI: hv: Add validation for untrusted Hyper-V values
> PCI: hv: Fix synchronization between channel callback and
> hv_pci_bus_exit()
Applied to hyperv-next. Thanks.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-05-13 16:58 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-11 22:32 [PATCH v2 0/2] PCI: hv: Hardening changes Andrea Parri (Microsoft)
2022-05-11 22:32 ` [PATCH v2 1/2] PCI: hv: Add validation for untrusted Hyper-V values Andrea Parri (Microsoft)
2022-05-12 15:08 ` Michael Kelley (LINUX)
2022-05-11 22:32 ` [PATCH v2 2/2] PCI: hv: Fix synchronization between channel callback and hv_pci_bus_exit() Andrea Parri (Microsoft)
2022-05-13 10:11 ` [PATCH v2 0/2] PCI: hv: Hardening changes Lorenzo Pieralisi
2022-05-13 16:51 ` Wei Liu
2022-05-13 16:58 ` Wei Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).