hv_utils: Add validation for untrusted Hyper-V values
Subsystems (2)
PATCH v2 1/1 hv_utils: 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 indexing off the end of the icversion_data array in vmbus_prep_negotiate_resp().
Signed-off-by Andres Beltran <[email protected]>
---
Changes in v2:
-Use ratelimited form of kernel logging to print error messages.
drivers/hv/channel_mgmt.c | 17 ++- drivers/hv/hv_fcopy.c | 36 +++-- drivers/hv/hv_kvp.c | 122 +++++++++-------- drivers/hv/hv_snapshot.c | 89 +++++++------ drivers/hv/hv_util.c | 267 +++++++++++++++++++++++--------------- include/linux/hyperv.h | 9 +- 6 files changed, 332 insertions(+), 208 deletions(-)
drivers/hv/channel_mgmt.c +12 -5
@@ -202,8 +202,8 @@ static u16 hv_get_dev_type(const struct vmbus_channel *channel)* Set up and fill in default negotiate response message. * Mainly used by Hyper-V drivers. */ -bool vmbus_prep_negotiate_resp (struct icmsg_hdr *icmsghdrp, -u8 *buf,const int *fw_version,int fw_vercnt, +bool vmbus_prep_negotiate_resp (struct icmsg_hdr *icmsghdrp,u8 *buf, +u32 buflen,const int *fw_version,int fw_vercnt,const int *srv_version,int srv_vercnt,int *nego_fw_version,int *nego_srv_version) {
@@ -216,9 +216,7 @@ bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,struct icmsg_negotiate *negop; icmsghdrp->icmsgsize =0x10 ; - negop = (struct icmsg_negotiate *)&buf[ -sizeof (struct vmbuspipe_hdr ) + -sizeof (struct icmsg_hdr )]; + negop = (struct icmsg_negotiate *)&buf[ICMSG_HDR ]; icframe_major = negop->icframe_vercnt; icframe_minor =0 ;
@@ -226,6 +224,15 @@ bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, icmsg_major = negop->icmsg_vercnt; icmsg_minor =0 ; +/* Validate negop packet */ +if (icframe_major >IC_VERSION_NEGOTIATION_MAX_VER_COUNT || + icmsg_major >IC_VERSION_NEGOTIATION_MAX_VER_COUNT || +ICMSG_NEGOTIATE_PKT_SIZE (icframe_major, icmsg_major) > buflen) { +pr_err_ratelimited ("Invalid icmsg negotiate - icframe_major: %u, icmsg_major: %u" , + icframe_major, icmsg_major); + goto fw_error; + } +/* * Select the framework version number we will * support.
drivers/hv/hv_fcopy.c +28 -8
@@ -235,15 +235,27 @@ void hv_fcopy_onchannelcallback(void *context)if (fcopy_transaction.state >HVUTIL_READY )return ; -vmbus_recvpacket (channel, recv_buffer,HV_HYP_PAGE_SIZE *2 , &recvlen, - &requestid); -if (recvlen <=0 ) +if (vmbus_recvpacket (channel, recv_buffer,HV_HYP_PAGE_SIZE *2 , &recvlen, &requestid)) { +pr_err_ratelimited ("Fcopy request received. Could not read into recv buf\n" );return ; + } + +if (!recvlen) +return ; + +/* Ensure recvlen is big enough to read header data */ +if (recvlen <ICMSG_HDR ) { +pr_err_ratelimited ("Fcopy request received. Packet length too small: %d\n" , + recvlen); +return ; + } icmsghdr = (struct icmsg_hdr *)&recv_buffer[sizeof (struct vmbuspipe_hdr )]; +if (icmsghdr->icmsgtype ==ICMSGTYPE_NEGOTIATE ) { -if (vmbus_prep_negotiate_resp (icmsghdr, recv_buffer, +if (vmbus_prep_negotiate_resp (icmsghdr, + recv_buffer, recvlen, fw_versions,FW_VER_COUNT , fcopy_versions,FCOPY_VER_COUNT ,NULL , &fcopy_srv_version)) {
@@ -252,10 +264,14 @@ void hv_fcopy_onchannelcallback(void *context) fcopy_srv_version >>16 , fcopy_srv_version & 0xFFFF); } - }else { - fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[ -sizeof (struct vmbuspipe_hdr ) + -sizeof (struct icmsg_hdr )]; + }else if (icmsghdr->icmsgtype ==ICMSGTYPE_FCOPY ) { +/* Ensure recvlen is big enough to contain hv_fcopy_hdr */ +if (recvlen <ICMSG_HDR +sizeof (struct hv_fcopy_hdr )) { +pr_err_ratelimited ("Invalid Fcopy hdr. Packet length too small: %u\n" , + recvlen); +return ; + } + fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[ICMSG_HDR ];/* * Stash away this global state for completing the
@@ -280,6 +296,10 @@ void hv_fcopy_onchannelcallback(void *context)schedule_delayed_work (&fcopy_timeout_work,HV_UTIL_TIMEOUT *HZ );return ; + }else { +pr_err_ratelimited ("Fcopy request received. Invalid msg type: %d\n" , + icmsghdr->icmsgtype); +return ; } icmsghdr->icflags =ICMSGHDRFLAG_TRANSACTION |ICMSGHDRFLAG_RESPONSE ; vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
drivers/hv/hv_kvp.c +69 -53
140 lines not on this page · raw message
drivers/hv/hv_snapshot.c +52 -37
101 lines not on this page · raw message
drivers/hv/hv_util.c +163 -104
317 lines not on this page · raw message
include/linux/hyperv.h +8 -1
@@ -1422,6 +1422,7 @@ void vmbus_free_mmio(resource_size_t start, resource_size_t size);#define ICMSGTYPE_SHUTDOWN 3#define ICMSGTYPE_TIMESYNC 4#define ICMSGTYPE_VSS 5 +#define ICMSGTYPE_FCOPY 7#define ICMSGHDRFLAG_TRANSACTION 1#define ICMSGHDRFLAG_REQUEST 2
@@ -1465,6 +1466,12 @@ struct icmsg_hdr {u8 reserved[2 ]; } __packed; +#define IC_VERSION_NEGOTIATION_MAX_VER_COUNT 100 +#define ICMSG_HDR (sizeof(struct vmbuspipe_hdr) + sizeof(struct icmsg_hdr)) +#define ICMSG_NEGOTIATE_PKT_SIZE (icframe_vercnt, icmsg_vercnt) \ + (ICMSG_HDR + offsetof(struct icmsg_negotiate, icversion_data) + \ + (((icframe_vercnt) + (icmsg_vercnt)) * sizeof(struct ic_version))) +struct icmsg_negotiate {u16 icframe_vercnt;u16 icmsg_vercnt;
@@ -1520,7 +1527,7 @@ struct hyperv_service_callback { };#define MAX_SRV_VER 0x7ffffff -extern bool vmbus_prep_negotiate_resp (struct icmsg_hdr *icmsghdrp,u8 *buf, +extern bool vmbus_prep_negotiate_resp (struct icmsg_hdr *icmsghdrp,u8 *buf,u32 buflen,const int *fw_version,int fw_vercnt,const int *srv_version,int srv_vercnt,int *nego_fw_version,int *nego_srv_version);
Signature · 1 line
2.25.1