Skip to contentSkip to conversation

Drivers: hv: vmbus: Copy packets sent by Hyper-V out of the ring buffer

PATCH Superseded Andres Beltran 2 participants 2 messages Jul 14, 2020

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drivers: hv: vmbus: Copy packets sent by Hyper-V out of the ring buffer

Pointers to ring-buffer packets sent by Hyper-V are used 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, return a copy of the incoming VMBus packet after validating its length and offset fields in hv_pkt_iter_first(). In this way, the packet can no longer be modified by the host.

Cc James E.J. Bottomley <[email protected]>
Cc Martin K. Petersen <[email protected]>
Cc David S. Miller <[email protected]>
Cc Jakub Kicinski <[email protected]>
---
 drivers/hv/channel.c              |  9 +++--
 drivers/hv/hyperv_vmbus.h         |  2 +-
 drivers/hv/ring_buffer.c          | 61 ++++++++++++++++++++++++++++---
 drivers/net/hyperv/hyperv_net.h   |  7 ++++
 drivers/net/hyperv/netvsc.c       |  2 +
 drivers/net/hyperv/rndis_filter.c |  2 +
 drivers/scsi/storvsc_drv.c        | 12 ++++++
 include/linux/hyperv.h            |  9 +++++
 8 files changed, 95 insertions(+), 9 deletions(-)
drivers/hv/channel.c +6 -3
index c16ddd3e5ce1..369628fe811d 100644
@@ -206,12 +206,15 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 	newchannel->onchannel_callback = onchannelcallback;
 	newchannel->channel_callback_context = context;
 
-	err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages);
+	if (!newchannel->max_pkt_size)
+		newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
+
+	err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0);
 	if (err)
 		goto error_clean_ring;
 
-	err = hv_ringbuffer_init(&newchannel->inbound,
-				 &page[send_pages], recv_pages);
+	err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
+				 recv_pages, newchannel->max_pkt_size);
 	if (err)
 		goto error_clean_ring;
 
drivers/hv/hyperv_vmbus.h +1 -1
index 40e2b9f91163..ff755e5d65fd 100644
@@ -174,7 +174,7 @@ extern int hv_synic_cleanup(unsigned int cpu);
 void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
 
 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
-		       struct page *pages, u32 pagecnt);
+		       struct page *pages, u32 pagecnt, u32 max_pkt_size);
 
 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
 
drivers/hv/ring_buffer.c +56 -5

91 lines not on this page · raw message

drivers/net/hyperv/hyperv_net.h +7 -0
index f43b614f2345..a394f73b9821 100644
@@ -860,6 +860,13 @@ static inline u32 netvsc_rqstor_size(unsigned long ringbytes)
 	       ringbytes / NETVSC_MIN_IN_MSG_SIZE;
 }
 
+#define NETVSC_MAX_XFER_PAGE_RANGES 375
+#define NETVSC_XFER_HEADER_SIZE(rng_cnt) \
+		(offsetof(struct vmtransfer_page_packet_header, ranges) + \
+		(rng_cnt) * sizeof(struct vmtransfer_page_range))
+#define NETVSC_MAX_PKT_SIZE (NETVSC_XFER_HEADER_SIZE(NETVSC_MAX_XFER_PAGE_RANGES) + \
+		sizeof(struct nvsp_message) + (sizeof(u32) * VRSS_SEND_TAB_SIZE))
+
 struct multi_send_data {
 	struct sk_buff *skb; /* skb containing the pkt */
 	struct hv_netvsc_packet *pkt; /* netvsc pkt pending */
drivers/net/hyperv/netvsc.c +2 -0
index 79b907a29433..9585df459841 100644
@@ -1473,6 +1473,8 @@ struct netvsc_device *netvsc_device_add(struct hv_device *device,
 
 	/* Open the channel */
 	device->channel->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
+	device->channel->max_pkt_size = NETVSC_MAX_PKT_SIZE;
+
 	ret = vmbus_open(device->channel, netvsc_ring_bytes,
 			 netvsc_ring_bytes,  NULL, 0,
 			 netvsc_channel_cb, net_device->chan_table);
drivers/net/hyperv/rndis_filter.c +2 -0
index 10489ba44a09..6de0f4e0db7b 100644
@@ -1115,6 +1115,8 @@ static void netvsc_sc_open(struct vmbus_channel *new_sc)
 	nvchan->channel = new_sc;
 
 	new_sc->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
+	new_sc->max_pkt_size = NETVSC_MAX_PKT_SIZE;
+
 	ret = vmbus_open(new_sc, netvsc_ring_bytes,
 			 netvsc_ring_bytes, NULL, 0,
 			 netvsc_channel_cb, nvchan);
drivers/scsi/storvsc_drv.c +12 -0
index 6d2df1f0fe6d..e28627cc4606 100644
@@ -414,6 +414,14 @@ static void storvsc_on_channel_callback(void *context);
 #define STORVSC_IDE_MAX_TARGETS				1
 #define STORVSC_IDE_MAX_CHANNELS			1
 
+/*
+ * Upper bound on the size of a storvsc packet. vmscsi_size_delta is not
+ * included in the calculation because it is set after STORVSC_MAX_PKT_SIZE
+ * is used in storvsc_connect_to_vsp
+ */
+#define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
+			      sizeof(struct vstor_packet))
+
 struct storvsc_cmd_request {
 	struct scsi_cmnd *cmd;
 
@@ -698,6 +706,7 @@ static void handle_sc_creation(struct vmbus_channel *new_sc)
 		return;
 
 	memset(&props, 0, sizeof(struct vmstorage_channel_properties));
+	new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
 
 	/*
 	 * The size of vmbus_requestor is an upper bound on the number of requests
@@ -1289,8 +1298,11 @@ static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
 {
 	struct vmstorage_channel_properties props;
 	int ret;
+	struct storvsc_device *stor_device;
 
 	memset(&props, 0, sizeof(struct vmstorage_channel_properties));
+	stor_device = get_out_stor_device(device);
+	device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
 
 	/*
 	 * The size of vmbus_requestor is an upper bound on the number of requests
include/linux/hyperv.h +9 -0
index d8194924983d..3524d9e481c5 100644
@@ -133,6 +133,10 @@ struct hv_ring_buffer_info {
 	 * being freed while the ring buffer is being accessed.
 	 */
 	struct mutex ring_buffer_mutex;
+
+	/* Buffer that holds a copy of an incoming host packet */
+	void *pkt_buffer;
+	u32 pkt_buffer_size;
 };
 
 
@@ -738,6 +742,8 @@ struct vmbus_device {
 	bool perf_device;
 };
 
+#define VMBUS_DEFAULT_MAX_PKT_SIZE 4096
+
 struct vmbus_channel {
 	struct list_head listentry;
 
@@ -959,6 +965,9 @@ struct vmbus_channel {
 	/* request/transaction ids for VMBus */
 	struct vmbus_requestor requestor;
 	u32 rqstor_size;
+
+	/* The max size of a packet on this channel */
+	u32 max_pkt_size;
 };
 
 u64 vmbus_next_request_id(struct vmbus_requestor *rqstor, u64 rqst_addr);
Signature · 1 line
2.25.1
#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Andres,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20200714]
[cannot apply to mkp-scsi/for-next scsi/for-next linux/master linus/master v5.8-rc5 v5.8-rc4 v5.8-rc3 v5.8-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Andres-Beltran/Drivers-hv-vmbus-Copy-packets-sent-by-Hyper-V-out-of-the-ring-buffer/20200715-002722
base:    5fb3d60423873c969058f16b94bca3ef304a68fb
config: x86_64-rhel (attached as .config)
compiler: gcc-9 (Debian 9.3.0-14) 9.3.0
reproduce (this is a W=1 build):
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

drivers/scsi/storvsc_drv.c: In function 'storvsc_connect_to_vsp':

>> drivers/scsi/storvsc_drv.c:1310:25: warning: variable 'stor_device' set but not used [-Wunused-but-set-variable]
    1310 |  struct storvsc_device *stor_device;
         |                         ^~~~~~~~~~~

vim +/stor_device +1310 drivers/scsi/storvsc_drv.c

1304
1305 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1306 bool is_fc)
1307 {
1308 struct vmstorage_channel_properties props;
1309 int ret;

> 1310		struct storvsc_device *stor_device;

1311
1312 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1313 stor_device = get_out_stor_device(device);
1314 device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
1315
1316 /*
1317 * The size of vmbus_requestor is an upper bound on the number of requests
1318 * that can be in-progress at any one time across all channels.
1319 */
1320 device->channel->rqstor_size = scsi_driver.can_queue;
1321
1322 ret = vmbus_open(device->channel,
1323 ring_size,
1324 ring_size,
1325 (void *)&props,
1326 sizeof(struct vmstorage_channel_properties),
1327 storvsc_on_channel_callback, device->channel);
1328
1329 if (ret != 0)
1330 return ret;
1331
1332 ret = storvsc_channel_init(device, is_fc);
1333
1334 return ret;
1335 }
1336

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org