Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp722527pxu; Wed, 2 Dec 2020 01:26:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJyWp0IBViQD3671wVEdCps+jhrZBXqc33XJ0a/pWipWj9/tXcACQ95ai7c3857g0T+SSjNk X-Received: by 2002:a17:906:1405:: with SMTP id p5mr1399721ejc.282.1606901173845; Wed, 02 Dec 2020 01:26:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606901173; cv=none; d=google.com; s=arc-20160816; b=pmWhl3RyuBSNsNRAemlowFi3bo95tRvPHhLKFbumQypO4piUD7Yi6TkSeFeq99wEUa x/W80GGY6jLRm6B04gFi/TDyCvgD/HzSqaaOB8/k9yE3Mo9iALwE3YrDGz02yYoXBg/9 By5pWMPnwencM2wLq96oYCgrVqYdF8RlLuuuFqyiS1jSy2lCT+Vxj3cQC8OQMaiX1TOc /YQAlPfhQh4GOxuGsPGkRJpF3SWFz0M2VtpWeiqYsmnxypgRVJtJzNmCxKo0SGdVUMdI 8XiNRAjolqe2vpmxw/5Jkt1gf9hV+YzvvIjURXSRv1DvTrnTuIg8LnzfW3GaLBodMxJL xYdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9Rs5tsnn1mMjgVaYOmoOMGnCxyDPBfQTj3LZ6W940/g=; b=YvuncRkQrTTOYA6KIu4mwNgjPaxFDZIBHmr7fLBdZg3dWHP6cL5KMXBN7jgNzHeD7m UM21QdGdjGLX+IEJBUmPRb4uNL3ZDzU02bexsuGaP6wfv/pPE3z6y419jkCML26yHfiU cJZ27ZLZrlJzP/y1YEg3kr/+UpG1whIbGkYxUHwcy57HcIVwFoW63i9jA0e+nb8JJjhj fZ7dKjfPaNegCSFCxWt+shD1bAnGHPz7Vct6qEGILNEp2OvulkLXuHzbxH2UWADFhuEN GyIY2Qt445laf+PP3qlsSzzFCUNAEC8ItNbDeb2u5aqRno/Lv44JVbuS5DupoDh6JvkC 8lCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YKKhgYLK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f5si533115ejr.149.2020.12.02.01.25.50; Wed, 02 Dec 2020 01:26:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YKKhgYLK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387700AbgLBJXY (ORCPT + 99 others); Wed, 2 Dec 2020 04:23:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729251AbgLBJXW (ORCPT ); Wed, 2 Dec 2020 04:23:22 -0500 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06896C0617A6; Wed, 2 Dec 2020 01:22:42 -0800 (PST) Received: by mail-wr1-x444.google.com with SMTP id 64so2667830wra.11; Wed, 02 Dec 2020 01:22:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9Rs5tsnn1mMjgVaYOmoOMGnCxyDPBfQTj3LZ6W940/g=; b=YKKhgYLKvVDPfv6dXD6Xhh0a7vlAedO+KhL8084IwX02W3z3UAw8DG1TiARzgictf0 ln+jiECmWK4jYg3DQxmMvteovry7YUrGWP4bI02XqifiWvPl7Hv8ul2JirwFnQa7JGs/ HVbCbJzL8HvzjrAufM/S8dp78CnumhKa4kFl6IXz7P1ZWBoKwAzdCCcRRjpYykSNIocN iyObnBcHdnKRlOvrksBSUpQuFwU+l6qNIjVayULYnc81yQY8cvV/42AEGlFqkXY2ndei zdRUV+fYj+F9Lg3AMpZvPcidUp2/H75Mhb15Dbb5RyIIymdnoF3A7k/FAawCqdIYAlc4 zT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9Rs5tsnn1mMjgVaYOmoOMGnCxyDPBfQTj3LZ6W940/g=; b=GZb4/HsbwQQ5YHB/vGQI9krrQg0OWy5u9g+ZWUanGb0+Am+ZJfRVEN2XeslpQt5h3T duKZ5SYAFPyjVVfU8o6i1HatBa80lexx+kSCH6brD1ifN0WbBycW50LGA77//FEa1NiC mkpTgmqJ5cxBosIQaRXVmkFvHowCIPsCpt4Hm8XmtT/3LaJps1rO2XtAPziyx3YW/Lq7 FTWa6kJZ7lybcFmLaK6Umez84Bw+CryD2E2c+DWx10OnrGmpWP5bWhb62U5uBuaUzSi0 XjzMMZ6T3NKkI3Y2MIlTCOd2dXbujSQ1sGbe818p+tXX1zFmsdeptNNaN7WOZQUa0QQm blgA== X-Gm-Message-State: AOAM533iF2ml40qLIbkcqOVlD09bcqJkoKYIy5SzbHGPP/HLJhrUQWbs ddg7KlvYQ0cL9HZq/dk6VruZjuHvNU75+Q== X-Received: by 2002:adf:93e6:: with SMTP id 93mr2177942wrp.197.1606900960113; Wed, 02 Dec 2020 01:22:40 -0800 (PST) Received: from andrea.corp.microsoft.com (host-95-239-64-30.retail.telecomitalia.it. [95.239.64.30]) by smtp.gmail.com with ESMTPSA id e27sm1535936wrc.9.2020.12.02.01.22.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2020 01:22:39 -0800 (PST) From: "Andrea Parri (Microsoft)" To: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org Cc: "K . Y . Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Michael Kelley , Juan Vazquez , Saruhan Karademir , "Andrea Parri (Microsoft)" Subject: [PATCH v2 2/7] Drivers: hv: vmbus: Avoid double fetch of msgtype in vmbus_on_msg_dpc() Date: Wed, 2 Dec 2020 10:22:09 +0100 Message-Id: <20201202092214.13520-3-parri.andrea@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201202092214.13520-1-parri.andrea@gmail.com> References: <20201202092214.13520-1-parri.andrea@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org vmbus_on_msg_dpc() double fetches from msgtype. The double fetch can lead to an out-of-bound access when accessing the channel_message_table array. In turn, the use of the out-of-bound entry could lead to code execution primitive (entry->message_handler()). Avoid the double fetch by saving the value of msgtype into a local variable. Reported-by: Juan Vazquez Signed-off-by: Andrea Parri (Microsoft) --- drivers/hv/vmbus_drv.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c index 0a2711aa63a15..82b23baa446d7 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -1057,6 +1057,7 @@ void vmbus_on_msg_dpc(unsigned long data) struct hv_message *msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; struct vmbus_channel_message_header *hdr; + enum vmbus_channel_message_type msgtype; const struct vmbus_channel_message_table_entry *entry; struct onmessage_work_context *ctx; u32 message_type = msg->header.message_type; @@ -1072,12 +1073,19 @@ void vmbus_on_msg_dpc(unsigned long data) /* no msg */ return; + /* + * The hv_message object is in memory shared with the host. The host + * could erroneously or maliciously modify such object. Make sure to + * validate its fields and avoid double fetches whenever feasible. + */ + hdr = (struct vmbus_channel_message_header *)msg->u.payload; + msgtype = hdr->msgtype; trace_vmbus_on_msg_dpc(hdr); - if (hdr->msgtype >= CHANNELMSG_COUNT) { - WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype); + if (msgtype >= CHANNELMSG_COUNT) { + WARN_ONCE(1, "unknown msgtype=%d\n", msgtype); goto msg_handled; } @@ -1087,14 +1095,14 @@ void vmbus_on_msg_dpc(unsigned long data) goto msg_handled; } - entry = &channel_message_table[hdr->msgtype]; + entry = &channel_message_table[msgtype]; 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); + msgtype, msg->header.payload_size); goto msg_handled; } @@ -1115,7 +1123,7 @@ void vmbus_on_msg_dpc(unsigned long data) * by offer_in_progress and by channel_mutex. See also the * inline comments in vmbus_onoffer_rescind(). */ - switch (hdr->msgtype) { + switch (msgtype) { case CHANNELMSG_RESCIND_CHANNELOFFER: /* * If we are handling the rescind message; -- 2.25.1