Drivers: hv: cleanup VMBus messages handling
Subsystems (2)
v2 cover Drivers: hv: cleanup VMBus messages handling
A small cleanup series mostly aimed at sanitizing memory we pass to message handlers: not passing garbage/lefrtovers from other messages and making sure we fail early when hypervisor misbehaves.
No (real) functional change intended.
Changes since v1:
- Check that the payload size specified by the host is <= 240 bytes
- Add Michael's R-b tags.
Vitaly Kuznetsov (5):
Drivers: hv: copy from message page only what's needed
Drivers: hv: allocate the exact needed memory for messages
Drivers: hv: avoid passing opaque pointer to vmbus_onmessage()
Drivers: hv: make sure that 'struct vmbus_channel_message_header'
compiles correctly
Drivers: hv: check VMBus messages lengths
drivers/hv/channel_mgmt.c | 61 ++++++++++++++++++++------------------- drivers/hv/hyperv_vmbus.h | 1 + drivers/hv/vmbus_drv.c | 40 ++++++++++++++++++++----- include/linux/hyperv.h | 2 +- 4 files changed, 66 insertions(+), 38 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/5 Drivers: hv: copy from message page only what's needed
Hyper-V Interrupt Message Page (SIMP) has 16 256-byte slots for messages. Each message comes with a header (16 bytes) which specifies the payload length (up to 240 bytes). vmbus_on_msg_dpc(), however, doesn't look at the real message length and copies the whole slot to a temporary buffer before passing it to message handlers. This is potentially dangerous as hypervisor doesn't have to clean the whole slot when putting a new message there and a message handler can get access to some data which belongs to a previous message.
Note, this is not currently a problem because all message handlers are in-kernel but eventually we may e.g. get this exported to userspace.
Note also, that this is not a performance critical path: messages (unlike events) represent rare events so it doesn't really matter (from performance point of view) if we copy too much.
Fix the issue by taking into account the real message length. The temporary buffer allocated by vmbus_on_msg_dpc() remains fixed size for now. Also, check that the supplied payload length is valid (<= 240 bytes).
--- drivers/hv/vmbus_drv.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/hv/vmbus_drv.c +8 -1
@@ -1032,6 +1032,12 @@ void vmbus_on_msg_dpc(unsigned long data) goto msg_handled; } +if (msg->header.payload_size >HV_MESSAGE_PAYLOAD_BYTE_COUNT ) { +WARN_ONCE (1 ,"payload size is too large (%d)\n" , + msg->header.payload_size); + goto msg_handled; + } + entry = &channel_message_table[hdr->msgtype];if (!entry->message_handler)
@@ -1043,7 +1049,8 @@ void vmbus_on_msg_dpc(unsigned long data)return ;INIT_WORK (&ctx->work, vmbus_onmessage_work); -memcpy (&ctx->msg, msg,sizeof (*msg)); +memcpy (&ctx->msg, msg,sizeof (msg->header) + + msg->header.payload_size);/* * The host can generate a rescind message while we
Signature · 1 line
2.25.1
PATCH v2 2/5 Drivers: hv: allocate the exact needed memory for messages
In reply to Vitaly KuznetsovWhen we need to pass a buffer with Hyper-V message we don't need to always allocate 256 bytes for the message: the real message length is known from the header. Change 'struct onmessage_work_context' to make it possible to not over-allocate.
--- drivers/hv/vmbus_drv.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/hv/vmbus_drv.c +10 -5
@@ -991,7 +991,10 @@ static struct bus_type hv_bus = {struct onmessage_work_context {struct work_struct work; -struct hv_message msg; +struct { +struct hv_message_header header; +u8 payload[]; + } msg; };static void vmbus_onmessage_work (struct work_struct *work)
@@ -1044,7 +1047,8 @@ void vmbus_on_msg_dpc(unsigned long data) goto msg_handled;if (entry->handler_type ==VMHT_BLOCKING ) { - ctx =kmalloc (sizeof (*ctx),GFP_ATOMIC ); + ctx =kmalloc (sizeof (*ctx) + msg->header.payload_size, +GFP_ATOMIC );if (ctx ==NULL )return ;
@@ -1098,10 +1102,11 @@ static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)WARN_ON (!is_hvsock_channel (channel));/* -* sizeof(*ctx) is small and the allocation should really not fail, +* Allocation size is small and the allocation should really not fail, * otherwise the state of the hv_sock connections ends up in limbo. */ - ctx =kzalloc (sizeof (*ctx),GFP_KERNEL | __GFP_NOFAIL); + ctx =kzalloc (sizeof (*ctx) +sizeof (*rescind), +GFP_KERNEL | __GFP_NOFAIL);/* * So far, these are not really used by Linux. Just set them to the
@@ -1111,7 +1116,7 @@ static void vmbus_force_channel_rescinded(struct vmbus_channel *channel) ctx->msg.header.payload_size =sizeof (*rescind);/* These values are actually used by Linux. */ - rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.u.payload; + rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload; rescind->header.msgtype =CHANNELMSG_RESCIND_CHANNELOFFER ; rescind->child_relid = channel->offermsg.child_relid;
Signature · 1 line
2.25.1
PATCH v2 3/5 Drivers: hv: avoid passing opaque pointer to vmbus_onmessage()
In reply to Vitaly Kuznetsovvmbus_onmessage() doesn't need the header of the message, it only uses it to get to the payload, we can pass the pointer to the payload directly.
--- drivers/hv/channel_mgmt.c | 7 +------ drivers/hv/vmbus_drv.c | 3 ++- include/linux/hyperv.h | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-)
drivers/hv/channel_mgmt.c +1 -6
@@ -1360,13 +1360,8 @@ channel_message_table[CHANNELMSG_COUNT] = {* * This is invoked in the vmbus worker thread context. */ -void vmbus_onmessage (void *context) +void vmbus_onmessage (struct vmbus_channel_message_header *hdr) { -struct hv_message *msg = context; -struct vmbus_channel_message_header *hdr; - - hdr = (struct vmbus_channel_message_header *)msg->u.payload; -trace_vmbus_on_message (hdr);/*
drivers/hv/vmbus_drv.c +2 -1
@@ -1007,7 +1007,8 @@ static void vmbus_onmessage_work(struct work_struct *work) ctx = container_of(work, struct onmessage_work_context, work); -vmbus_onmessage (&ctx->msg); +vmbus_onmessage ((struct vmbus_channel_message_header *) + &ctx->msg.payload);kfree (ctx); }
include/linux/hyperv.h +1 -1
@@ -1017,7 +1017,7 @@ static inline void clear_low_latency_mode(struct vmbus_channel *c) c->low_latency = false; } -void vmbus_onmessage (void *context); +void vmbus_onmessage (struct vmbus_channel_message_header *hdr);int vmbus_request_offers (void );
Signature · 1 line
2.25.1
PATCH v2 4/5 Drivers: hv: make sure that 'struct vmbus_channel_message_header' compiles correctly
In reply to Vitaly KuznetsovStrictly speaking, compiler is free to use something different from 'u32' for 'enum vmbus_channel_message_type' (e.g. char) but it doesn't happen in real life, just add a BUILD_BUG_ON() guardian.
--- drivers/hv/vmbus_drv.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/hv/vmbus_drv.c +7 -0
@@ -1023,6 +1023,13 @@ void vmbus_on_msg_dpc(unsigned long data)struct onmessage_work_context *ctx;u32 message_type = msg->header.message_type; +/* +* 'enum vmbus_channel_message_type' is supposed to always be 'u32' as +* it is being used in 'struct vmbus_channel_message_header' definition +* which is supposed to match hypervisor ABI. +*/ +BUILD_BUG_ON (sizeof (enum vmbus_channel_message_type ) !=sizeof (u32)); +if (message_type ==HVMSG_NONE )/* no msg */ return ;
Signature · 1 line
2.25.1
PATCH v2 5/5 Drivers: hv: check VMBus messages lengths
In reply to Vitaly KuznetsovVMBus message handlers (channel_message_table) receive a pointer to 'struct vmbus_channel_message_header' and cast it to a structure of their choice, which is sometimes longer than the header. We, however, don't check that the message is long enough so in case hypervisor screws up we'll be accessing memory beyond what was allocated for temporary buffer.
Previously, we used to always allocate and copy 256 bytes from message page to temporary buffer but this is hardly better: in case the message is shorter than we expect we'll be trying to consume garbage as some real data and no memory guarding technique will be able to identify an issue.
Introduce 'min_payload_len' to 'struct vmbus_channel_message_table_entry' and check against it in vmbus_on_msg_dpc(). Note, we can't require the exact length as new hypervisor versions may add extra fields to messages, we only check that the message is not shorter than we expect.
--- drivers/hv/channel_mgmt.c | 54 ++++++++++++++++++++++----------------- drivers/hv/hyperv_vmbus.h | 1 + drivers/hv/vmbus_drv.c | 6 +++++ 3 files changed, 37 insertions(+), 24 deletions(-)
drivers/hv/channel_mgmt.c +30 -24
@@ -1329,30 +1329,36 @@ static void vmbus_onversion_response(/* Channel message dispatch table */ const struct vmbus_channel_message_table_entry channel_message_table[CHANNELMSG_COUNT ] = { - {CHANNELMSG_INVALID ,0 ,NULL }, - {CHANNELMSG_OFFERCHANNEL ,0 , vmbus_onoffer }, - {CHANNELMSG_RESCIND_CHANNELOFFER ,0 , vmbus_onoffer_rescind }, - {CHANNELMSG_REQUESTOFFERS ,0 ,NULL }, - {CHANNELMSG_ALLOFFERS_DELIVERED ,1 , vmbus_onoffers_delivered }, - {CHANNELMSG_OPENCHANNEL ,0 ,NULL }, - {CHANNELMSG_OPENCHANNEL_RESULT ,1 , vmbus_onopen_result }, - {CHANNELMSG_CLOSECHANNEL ,0 ,NULL }, - {CHANNELMSG_GPADL_HEADER ,0 ,NULL }, - {CHANNELMSG_GPADL_BODY ,0 ,NULL }, - {CHANNELMSG_GPADL_CREATED ,1 , vmbus_ongpadl_created }, - {CHANNELMSG_GPADL_TEARDOWN ,0 ,NULL }, - {CHANNELMSG_GPADL_TORNDOWN ,1 , vmbus_ongpadl_torndown }, - {CHANNELMSG_RELID_RELEASED ,0 ,NULL }, - {CHANNELMSG_INITIATE_CONTACT ,0 ,NULL }, - {CHANNELMSG_VERSION_RESPONSE ,1 , vmbus_onversion_response }, - {CHANNELMSG_UNLOAD ,0 ,NULL }, - {CHANNELMSG_UNLOAD_RESPONSE ,1 , vmbus_unload_response }, - {CHANNELMSG_18 ,0 ,NULL }, - {CHANNELMSG_19 ,0 ,NULL }, - {CHANNELMSG_20 ,0 ,NULL }, - {CHANNELMSG_TL_CONNECT_REQUEST ,0 ,NULL }, - {CHANNELMSG_22 ,0 ,NULL }, - {CHANNELMSG_TL_CONNECT_RESULT ,0 ,NULL }, + {CHANNELMSG_INVALID ,0 ,NULL ,0 }, + {CHANNELMSG_OFFERCHANNEL ,0 , vmbus_onoffer, +sizeof (struct vmbus_channel_offer_channel )}, + {CHANNELMSG_RESCIND_CHANNELOFFER ,0 , vmbus_onoffer_rescind, +sizeof (struct vmbus_channel_rescind_offer ) }, + {CHANNELMSG_REQUESTOFFERS ,0 ,NULL ,0 }, + {CHANNELMSG_ALLOFFERS_DELIVERED ,1 , vmbus_onoffers_delivered,0 }, + {CHANNELMSG_OPENCHANNEL ,0 ,NULL ,0 }, + {CHANNELMSG_OPENCHANNEL_RESULT ,1 , vmbus_onopen_result, +sizeof (struct vmbus_channel_open_result )}, + {CHANNELMSG_CLOSECHANNEL ,0 ,NULL ,0 }, + {CHANNELMSG_GPADL_HEADER ,0 ,NULL ,0 }, + {CHANNELMSG_GPADL_BODY ,0 ,NULL ,0 }, + {CHANNELMSG_GPADL_CREATED ,1 , vmbus_ongpadl_created, +sizeof (struct vmbus_channel_gpadl_created )}, + {CHANNELMSG_GPADL_TEARDOWN ,0 ,NULL ,0 }, + {CHANNELMSG_GPADL_TORNDOWN ,1 , vmbus_ongpadl_torndown, +sizeof (struct vmbus_channel_gpadl_torndown ) }, + {CHANNELMSG_RELID_RELEASED ,0 ,NULL ,0 }, + {CHANNELMSG_INITIATE_CONTACT ,0 ,NULL ,0 }, + {CHANNELMSG_VERSION_RESPONSE ,1 , vmbus_onversion_response, +sizeof (struct vmbus_channel_version_response )}, + {CHANNELMSG_UNLOAD ,0 ,NULL ,0 }, + {CHANNELMSG_UNLOAD_RESPONSE ,1 , vmbus_unload_response,0 }, + {CHANNELMSG_18 ,0 ,NULL ,0 }, + {CHANNELMSG_19 ,0 ,NULL ,0 }, + {CHANNELMSG_20 ,0 ,NULL ,0 }, + {CHANNELMSG_TL_CONNECT_REQUEST ,0 ,NULL ,0 }, + {CHANNELMSG_22 ,0 ,NULL ,0 }, + {CHANNELMSG_TL_CONNECT_RESULT ,0 ,NULL ,0 }, };/*
drivers/hv/hyperv_vmbus.h +1 -0
@@ -317,6 +317,7 @@ struct vmbus_channel_message_table_entry {enum vmbus_channel_message_type message_type;enum vmbus_message_handler_type handler_type;void (*message_handler)(struct vmbus_channel_message_header *msg); +u32 min_payload_len; }; extern const struct vmbus_channel_message_table_entry
drivers/hv/vmbus_drv.c +6 -0
@@ -1054,6 +1054,12 @@ void vmbus_on_msg_dpc(unsigned long data)if (!entry->message_handler) goto msg_handled; +if (msg->header.payload_size < entry->min_payload_len) { +WARN_ONCE (1 ,"message too short: msgtype=%d len=%d\n" , + hdr->msgtype, msg->header.payload_size); + goto msg_handled; + } +if (entry->handler_type ==VMHT_BLOCKING ) { ctx =kmalloc (sizeof (*ctx) + msg->header.payload_size,GFP_ATOMIC );
Signature · 1 line
2.25.1
Quoted 10 lines · Vitaly Kuznetsov
A small cleanup series mostly aimed at sanitizing memory we pass to message handlers: not passing garbage/lefrtovers from other messages and making sure we fail early when hypervisor misbehaves.
No (real) functional change intended.
Changes since v1:
- Check that the payload size specified by the host is <= 240 bytes
- Add Michael's R-b tags.
Quoted 7 lines
Vitaly Kuznetsov (5):
Drivers: hv: copy from message page only what's needed
Drivers: hv: allocate the exact needed memory for messages
Drivers: hv: avoid passing opaque pointer tovmbus_onmessage()
Drivers: hv: make sure that 'structvmbus_channel_message_header'
compiles correctly
Drivers: hv: check VMBus messages lengths
Queued. Thanks.
Wei.