Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2983460imu; Mon, 19 Nov 2018 08:59:05 -0800 (PST) X-Google-Smtp-Source: AJdET5c/RhbZ+pzdZkugIikE2YQsT2ElBLYRx3L77R0MsvbxgznQbyMSLrrMuRLt+yM5PFWoJEA4 X-Received: by 2002:a62:19d5:: with SMTP id 204mr1730449pfz.33.1542646745893; Mon, 19 Nov 2018 08:59:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542646745; cv=none; d=google.com; s=arc-20160816; b=Orwcw+hkWaAwJSUK4qqClfX5haqP1qT58KjrDvfg3RvzPZZVA31NP7c2p/9QLXbRRN Ei2qF9ooNamoYxpbwB0QeEWdqYYpGNSrk+N+2rXNJr7sZUh3Os05Dry+gjZnlFrNkhq8 WfO9/wdY8pOo9urCyi7QvLqbWw0HIsOUhauIbNgAINxZ7NyWQ281B71M8Yu+D/wgESfC 175udqI+EoNRFXqKaJxWM3nce6L5rcwGL6NY3/XjnrASaj5Slp0FtlPlKQSCMtpMkLgp /WJExk8oqFvPNdMtiDJ/uo3Vu9FHFkc1GT6m/VVOPLtc8EwcJvDlFG/i5fjofRCjSP8a +dKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SDQMJbBabq7d1aFkY3ikrvdfQxvIZ7FqgMgEBvCFtus=; b=0TQbAxu1/Ct9DlEHYO7P8FVhqGqvD4IVj11mSy6l5tCaphgQgQOF0kXa4THAeWIHK0 /iGTARmzWUXgyBLcVevwpMQbXquizL1J+aemhkCdFUHBNE8KIY32AkHxI1biTHrZHkas SZu/WsBcLQ1AhLqeHW+iKGX+yHdKXckwlNTbQry7vPKoJ6+YxC+MKj+IsnZ08egftIvn LXHeERGZW2Toxoj2mZZp6QGsz8SWfegHO7CEXIzgVqXhk1zLqA1+v3UlJJZOB/teI6P9 KorVL8CUo6DcH27HvdUugxLUv6J/u0Sl/FOQwgEKucdTpA5Vj9I/SUk65ZpLbPmGQ9U5 Z+ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=csusB0P9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n78si22538350pfi.235.2018.11.19.08.58.51; Mon, 19 Nov 2018 08:59:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=csusB0P9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404751AbeKTDW3 (ORCPT + 99 others); Mon, 19 Nov 2018 22:22:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:34138 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403834AbeKTDW2 (ORCPT ); Mon, 19 Nov 2018 22:22:28 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4B7B62148E; Mon, 19 Nov 2018 16:58:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542646692; bh=FhAY5101g+bMmXjSTGJ/eDAU7wF8/oPjo1p80Udpk40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=csusB0P9RVRUZWJ5aFbqvm0Ekht4TKObtHs9H97XrOSvOk3WZ74BDKljC4xZfxsQd KHHTQ9T5Mx3v2AGuAE5Vw14p5hLmn9oE8pDXJMreQ2V+Pt9JUFYjW1o1Yu6zYFIK18 +wx1G3OGJEVjV3k1/94ijz/fzSga+cppbUeTEpV4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Breno Leitao , Jiri Kosina Subject: [PATCH 4.4 053/160] HID: hiddev: fix potential Spectre v1 Date: Mon, 19 Nov 2018 17:28:12 +0100 Message-Id: <20181119162636.676152532@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181119162630.031306128@linuxfoundation.org> References: <20181119162630.031306128@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Breno Leitao commit f11274396a538b31bc010f782e05c2ce3f804c13 upstream. uref->usage_index can be indirectly controlled by userspace, hence leading to a potential exploitation of the Spectre variant 1 vulnerability. This field is used as an array index by the hiddev_ioctl_usage() function, when 'cmd' is either HIDIOCGCOLLECTIONINDEX, HIDIOCGUSAGES or HIDIOCSUSAGES. For cmd == HIDIOCGCOLLECTIONINDEX case, uref->usage_index is compared to field->maxusage and then used as an index to dereference field->usage array. The same thing happens to the cmd == HIDIOC{G,S}USAGES cases, where uref->usage_index is checked against an array maximum value and then it is used as an index in an array. This is a summary of the HIDIOCGCOLLECTIONINDEX case, which matches the traditional Spectre V1 first load: copy_from_user(uref, user_arg, sizeof(*uref)) if (uref->usage_index >= field->maxusage) goto inval; i = field->usage[uref->usage_index].collection_index; return i; This patch fixes this by sanitizing field uref->usage_index before using it to index field->usage (HIDIOCGCOLLECTIONINDEX) or field->value in HIDIOC{G,S}USAGES arrays, thus, avoiding speculation in the first load. Cc: Signed-off-by: Breno Leitao v2: Contemplate cmd == HIDIOC{G,S}USAGES case Signed-off-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman --- drivers/hid/usbhid/hiddev.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) --- a/drivers/hid/usbhid/hiddev.c +++ b/drivers/hid/usbhid/hiddev.c @@ -521,14 +521,24 @@ static noinline int hiddev_ioctl_usage(s if (cmd == HIDIOCGCOLLECTIONINDEX) { if (uref->usage_index >= field->maxusage) goto inval; + uref->usage_index = + array_index_nospec(uref->usage_index, + field->maxusage); } else if (uref->usage_index >= field->report_count) goto inval; } - if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) && - (uref_multi->num_values > HID_MAX_MULTI_USAGES || - uref->usage_index + uref_multi->num_values > field->report_count)) - goto inval; + if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) { + if (uref_multi->num_values > HID_MAX_MULTI_USAGES || + uref->usage_index + uref_multi->num_values > + field->report_count) + goto inval; + + uref->usage_index = + array_index_nospec(uref->usage_index, + field->report_count - + uref_multi->num_values); + } switch (cmd) { case HIDIOCGUSAGE: