Skip to content

Commit adae1e9

Browse files
Andres Beltranliuw
authored andcommitted
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. Signed-off-by: Andres Beltran <[email protected]> Co-developed-by: Andrea Parri (Microsoft) <[email protected]> Signed-off-by: Andrea Parri (Microsoft) <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]>
1 parent 03b30cc commit adae1e9

File tree

11 files changed

+143
-25
lines changed

11 files changed

+143
-25
lines changed

drivers/hv/channel.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,12 +662,15 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
662662
newchannel->onchannel_callback = onchannelcallback;
663663
newchannel->channel_callback_context = context;
664664

665-
err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages);
665+
if (!newchannel->max_pkt_size)
666+
newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
667+
668+
err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0);
666669
if (err)
667670
goto error_clean_ring;
668671

669-
err = hv_ringbuffer_init(&newchannel->inbound,
670-
&page[send_pages], recv_pages);
672+
err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
673+
recv_pages, newchannel->max_pkt_size);
671674
if (err)
672675
goto error_clean_ring;
673676

drivers/hv/hv_fcopy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ int hv_fcopy_init(struct hv_util_service *srv)
349349
{
350350
recv_buffer = srv->recv_buffer;
351351
fcopy_transaction.recv_channel = srv->channel;
352+
fcopy_transaction.recv_channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2;
352353

353354
/*
354355
* When this driver loads, the user level daemon that

drivers/hv/hv_kvp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ hv_kvp_init(struct hv_util_service *srv)
757757
{
758758
recv_buffer = srv->recv_buffer;
759759
kvp_transaction.recv_channel = srv->channel;
760+
kvp_transaction.recv_channel->max_pkt_size = HV_HYP_PAGE_SIZE * 4;
760761

761762
/*
762763
* When this driver loads, the user level daemon that

drivers/hv/hyperv_vmbus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ extern int hv_synic_cleanup(unsigned int cpu);
174174
void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
175175

176176
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
177-
struct page *pages, u32 pagecnt);
177+
struct page *pages, u32 pagecnt, u32 max_pkt_size);
178178

179179
void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
180180

drivers/hv/ring_buffer.c

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void hv_ringbuffer_pre_init(struct vmbus_channel *channel)
181181

182182
/* Initialize the ring buffer. */
183183
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
184-
struct page *pages, u32 page_cnt)
184+
struct page *pages, u32 page_cnt, u32 max_pkt_size)
185185
{
186186
int i;
187187
struct page **pages_wraparound;
@@ -223,6 +223,14 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
223223
sizeof(struct hv_ring_buffer);
224224
ring_info->priv_read_index = 0;
225225

226+
/* Initialize buffer that holds copies of incoming packets */
227+
if (max_pkt_size) {
228+
ring_info->pkt_buffer = kzalloc(max_pkt_size, GFP_KERNEL);
229+
if (!ring_info->pkt_buffer)
230+
return -ENOMEM;
231+
ring_info->pkt_buffer_size = max_pkt_size;
232+
}
233+
226234
spin_lock_init(&ring_info->ring_lock);
227235

228236
return 0;
@@ -235,6 +243,9 @@ void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
235243
vunmap(ring_info->ring_buffer);
236244
ring_info->ring_buffer = NULL;
237245
mutex_unlock(&ring_info->ring_buffer_mutex);
246+
247+
kfree(ring_info->pkt_buffer);
248+
ring_info->pkt_buffer_size = 0;
238249
}
239250

240251
/* Write to the ring buffer. */
@@ -375,7 +386,7 @@ int hv_ringbuffer_read(struct vmbus_channel *channel,
375386
memcpy(buffer, (const char *)desc + offset, packetlen);
376387

377388
/* Advance ring index to next packet descriptor */
378-
__hv_pkt_iter_next(channel, desc);
389+
__hv_pkt_iter_next(channel, desc, true);
379390

380391
/* Notify host of update */
381392
hv_pkt_iter_close(channel);
@@ -401,6 +412,22 @@ static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
401412
return (rbi->ring_datasize - priv_read_loc) + write_loc;
402413
}
403414

415+
/*
416+
* Get first vmbus packet without copying it out of the ring buffer
417+
*/
418+
struct vmpacket_descriptor *hv_pkt_iter_first_raw(struct vmbus_channel *channel)
419+
{
420+
struct hv_ring_buffer_info *rbi = &channel->inbound;
421+
422+
hv_debug_delay_test(channel, MESSAGE_DELAY);
423+
424+
if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
425+
return NULL;
426+
427+
return (struct vmpacket_descriptor *)(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
428+
}
429+
EXPORT_SYMBOL_GPL(hv_pkt_iter_first_raw);
430+
404431
/*
405432
* Get first vmbus packet from ring buffer after read_index
406433
*
@@ -409,17 +436,49 @@ static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
409436
struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
410437
{
411438
struct hv_ring_buffer_info *rbi = &channel->inbound;
412-
struct vmpacket_descriptor *desc;
439+
struct vmpacket_descriptor *desc, *desc_copy;
440+
u32 bytes_avail, pkt_len, pkt_offset;
413441

414-
hv_debug_delay_test(channel, MESSAGE_DELAY);
415-
if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
442+
desc = hv_pkt_iter_first_raw(channel);
443+
if (!desc)
416444
return NULL;
417445

418-
desc = hv_get_ring_buffer(rbi) + rbi->priv_read_index;
419-
if (desc)
420-
prefetch((char *)desc + (desc->len8 << 3));
446+
bytes_avail = min(rbi->pkt_buffer_size, hv_pkt_iter_avail(rbi));
447+
448+
/*
449+
* Ensure the compiler does not use references to incoming Hyper-V values (which
450+
* could change at any moment) when reading local variables later in the code
451+
*/
452+
pkt_len = READ_ONCE(desc->len8) << 3;
453+
pkt_offset = READ_ONCE(desc->offset8) << 3;
454+
455+
/*
456+
* If pkt_len is invalid, set it to the smaller of hv_pkt_iter_avail() and
457+
* rbi->pkt_buffer_size
458+
*/
459+
if (pkt_len < sizeof(struct vmpacket_descriptor) || pkt_len > bytes_avail)
460+
pkt_len = bytes_avail;
461+
462+
/*
463+
* If pkt_offset is invalid, arbitrarily set it to
464+
* the size of vmpacket_descriptor
465+
*/
466+
if (pkt_offset < sizeof(struct vmpacket_descriptor) || pkt_offset > pkt_len)
467+
pkt_offset = sizeof(struct vmpacket_descriptor);
468+
469+
/* Copy the Hyper-V packet out of the ring buffer */
470+
desc_copy = (struct vmpacket_descriptor *)rbi->pkt_buffer;
471+
memcpy(desc_copy, desc, pkt_len);
472+
473+
/*
474+
* Hyper-V could still change len8 and offset8 after the earlier read.
475+
* Ensure that desc_copy has legal values for len8 and offset8 that
476+
* are consistent with the copy we just made
477+
*/
478+
desc_copy->len8 = pkt_len >> 3;
479+
desc_copy->offset8 = pkt_offset >> 3;
421480

422-
return desc;
481+
return desc_copy;
423482
}
424483
EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
425484

@@ -431,7 +490,8 @@ EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
431490
*/
432491
struct vmpacket_descriptor *
433492
__hv_pkt_iter_next(struct vmbus_channel *channel,
434-
const struct vmpacket_descriptor *desc)
493+
const struct vmpacket_descriptor *desc,
494+
bool copy)
435495
{
436496
struct hv_ring_buffer_info *rbi = &channel->inbound;
437497
u32 packetlen = desc->len8 << 3;
@@ -444,7 +504,7 @@ __hv_pkt_iter_next(struct vmbus_channel *channel,
444504
rbi->priv_read_index -= dsize;
445505

446506
/* more data? */
447-
return hv_pkt_iter_first(channel);
507+
return copy ? hv_pkt_iter_first(channel) : hv_pkt_iter_first_raw(channel);
448508
}
449509
EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
450510

drivers/net/hyperv/hyperv_net.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,16 @@ static inline u32 netvsc_rqstor_size(unsigned long ringbytes)
895895
ringbytes / NETVSC_MIN_IN_MSG_SIZE;
896896
}
897897

898+
/* XFER PAGE packets can specify a maximum of 375 ranges for NDIS >= 6.0
899+
* and a maximum of 64 ranges for NDIS < 6.0 with no RSC; with RSC, this
900+
* limit is raised to 562 (= NVSP_RSC_MAX).
901+
*/
902+
#define NETVSC_MAX_XFER_PAGE_RANGES NVSP_RSC_MAX
898903
#define NETVSC_XFER_HEADER_SIZE(rng_cnt) \
899904
(offsetof(struct vmtransfer_page_packet_header, ranges) + \
900905
(rng_cnt) * sizeof(struct vmtransfer_page_range))
906+
#define NETVSC_MAX_PKT_SIZE (NETVSC_XFER_HEADER_SIZE(NETVSC_MAX_XFER_PAGE_RANGES) + \
907+
sizeof(struct nvsp_message) + (sizeof(u32) * VRSS_SEND_TAB_SIZE))
901908

902909
struct multi_send_data {
903910
struct sk_buff *skb; /* skb containing the pkt */

drivers/net/hyperv/netvsc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,8 @@ struct netvsc_device *netvsc_device_add(struct hv_device *device,
16501650

16511651
/* Open the channel */
16521652
device->channel->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
1653+
device->channel->max_pkt_size = NETVSC_MAX_PKT_SIZE;
1654+
16531655
ret = vmbus_open(device->channel, netvsc_ring_bytes,
16541656
netvsc_ring_bytes, NULL, 0,
16551657
netvsc_channel_cb, net_device->chan_table);

drivers/net/hyperv/rndis_filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,8 @@ static void netvsc_sc_open(struct vmbus_channel *new_sc)
12601260
nvchan->channel = new_sc;
12611261

12621262
new_sc->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
1263+
new_sc->max_pkt_size = NETVSC_MAX_PKT_SIZE;
1264+
12631265
ret = vmbus_open(new_sc, netvsc_ring_bytes,
12641266
netvsc_ring_bytes, NULL, 0,
12651267
netvsc_channel_cb, nvchan);

drivers/scsi/storvsc_drv.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ static void storvsc_on_channel_callback(void *context);
406406
#define STORVSC_IDE_MAX_TARGETS 1
407407
#define STORVSC_IDE_MAX_CHANNELS 1
408408

409+
/*
410+
* Upper bound on the size of a storvsc packet. vmscsi_size_delta is not
411+
* included in the calculation because it is set after STORVSC_MAX_PKT_SIZE
412+
* is used in storvsc_connect_to_vsp
413+
*/
414+
#define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
415+
sizeof(struct vstor_packet))
416+
409417
struct storvsc_cmd_request {
410418
struct scsi_cmnd *cmd;
411419

@@ -701,6 +709,7 @@ static void handle_sc_creation(struct vmbus_channel *new_sc)
701709
return;
702710

703711
memset(&props, 0, sizeof(struct vmstorage_channel_properties));
712+
new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
704713

705714
/*
706715
* The size of vmbus_requestor is an upper bound on the number of requests
@@ -1294,6 +1303,7 @@ static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
12941303

12951304
memset(&props, 0, sizeof(struct vmstorage_channel_properties));
12961305

1306+
device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
12971307
/*
12981308
* The size of vmbus_requestor is an upper bound on the number of requests
12991309
* that can be in-progress at any one time across all channels.

include/linux/hyperv.h

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ struct hv_ring_buffer_info {
181181
* being freed while the ring buffer is being accessed.
182182
*/
183183
struct mutex ring_buffer_mutex;
184+
185+
/* Buffer that holds a copy of an incoming host packet */
186+
void *pkt_buffer;
187+
u32 pkt_buffer_size;
184188
};
185189

186190

@@ -799,6 +803,8 @@ struct vmbus_device {
799803
bool allowed_in_isolated;
800804
};
801805

806+
#define VMBUS_DEFAULT_MAX_PKT_SIZE 4096
807+
802808
struct vmbus_channel {
803809
struct list_head listentry;
804810

@@ -1021,6 +1027,9 @@ struct vmbus_channel {
10211027
/* request/transaction ids for VMBus */
10221028
struct vmbus_requestor requestor;
10231029
u32 rqstor_size;
1030+
1031+
/* The max size of a packet on this channel */
1032+
u32 max_pkt_size;
10241033
};
10251034

10261035
u64 vmbus_next_request_id(struct vmbus_requestor *rqstor, u64 rqst_addr);
@@ -1662,32 +1671,55 @@ static inline u32 hv_pkt_datalen(const struct vmpacket_descriptor *desc)
16621671
}
16631672

16641673

1674+
struct vmpacket_descriptor *
1675+
hv_pkt_iter_first_raw(struct vmbus_channel *channel);
1676+
16651677
struct vmpacket_descriptor *
16661678
hv_pkt_iter_first(struct vmbus_channel *channel);
16671679

16681680
struct vmpacket_descriptor *
16691681
__hv_pkt_iter_next(struct vmbus_channel *channel,
1670-
const struct vmpacket_descriptor *pkt);
1682+
const struct vmpacket_descriptor *pkt,
1683+
bool copy);
16711684

16721685
void hv_pkt_iter_close(struct vmbus_channel *channel);
16731686

1674-
/*
1675-
* Get next packet descriptor from iterator
1676-
* If at end of list, return NULL and update host.
1677-
*/
16781687
static inline struct vmpacket_descriptor *
1679-
hv_pkt_iter_next(struct vmbus_channel *channel,
1680-
const struct vmpacket_descriptor *pkt)
1688+
hv_pkt_iter_next_pkt(struct vmbus_channel *channel,
1689+
const struct vmpacket_descriptor *pkt,
1690+
bool copy)
16811691
{
16821692
struct vmpacket_descriptor *nxt;
16831693

1684-
nxt = __hv_pkt_iter_next(channel, pkt);
1694+
nxt = __hv_pkt_iter_next(channel, pkt, copy);
16851695
if (!nxt)
16861696
hv_pkt_iter_close(channel);
16871697

16881698
return nxt;
16891699
}
16901700

1701+
/*
1702+
* Get next packet descriptor without copying it out of the ring buffer
1703+
* If at end of list, return NULL and update host.
1704+
*/
1705+
static inline struct vmpacket_descriptor *
1706+
hv_pkt_iter_next_raw(struct vmbus_channel *channel,
1707+
const struct vmpacket_descriptor *pkt)
1708+
{
1709+
return hv_pkt_iter_next_pkt(channel, pkt, false);
1710+
}
1711+
1712+
/*
1713+
* Get next packet descriptor from iterator
1714+
* If at end of list, return NULL and update host.
1715+
*/
1716+
static inline struct vmpacket_descriptor *
1717+
hv_pkt_iter_next(struct vmbus_channel *channel,
1718+
const struct vmpacket_descriptor *pkt)
1719+
{
1720+
return hv_pkt_iter_next_pkt(channel, pkt, true);
1721+
}
1722+
16911723
#define foreach_vmbus_pkt(pkt, channel) \
16921724
for (pkt = hv_pkt_iter_first(channel); pkt; \
16931725
pkt = hv_pkt_iter_next(channel, pkt))

0 commit comments

Comments
 (0)