Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3334101imu; Sun, 11 Nov 2018 12:36:15 -0800 (PST) X-Google-Smtp-Source: AJdET5d0m7kTJgVd9Yd6QxBkiN0wWkP+bDmjUnkRxQnF/PowBXp9rwHezmTuzsBzzpsSbVGpnkRX X-Received: by 2002:a62:9402:: with SMTP id m2mr742253pfe.34.1541968575423; Sun, 11 Nov 2018 12:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541968575; cv=none; d=google.com; s=arc-20160816; b=WYF4p8UnbiU+DdoltX5hEyy80a67Q7BLDE4iQ0B4N/2wxq4661hRyPSVDW7d7QwBB0 Pt2AiJYhEih4nYyPEIgiUUYpU1SoQBdYQParPOeSiqphLDGaS6YZbM67pztg8lOvB7F+ zk3PbxdxcNJs4AeusUwCMFh3GdB9DRotYxCgd4WG1EtWdXM1YU5SHXKQW8Vu5SgRUo8Z RUUvyaqOG4e1uM1H4lr8VeI8M2swgsJJOzWWkOWKLiCF4D4QoGPRGPLF8OwBveHBFLfD Tn9CejdLBa8sOhbKBqOPp54xcSPF1UrS9jdYZsPc3z/qVpVMfX61WiF+qcneEzvLAjhu oNVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=Mf2hhDrOWG9Z94qooMP+WRp+IEd0q5rzK1OBSijbTjs=; b=Gz2rmVldiUm+DYOBbyMQ8h+dnnLeNaE/hXcf++hEGKKM7JulgZJMLPbsxYMmLjdX8u tUjObCVrWlLnGGYoca9a6SeTol3HREamHh+PdbcmmPz+0mjy92E6OhDv798voskoeKBh 8wdv0lcuE3l4JI3ZB2suEM3E0DUXQo1MD7pYcEcDSiufVHSi4l0A6DyNfoGJZ/BfJT4P UUdm2DQpxinFDdJ/k/fbDNpyiscKnLjY/sk8NkA3cCdI9cZY8bL2MBSDpqxPITTQasfo vGLyWZNlyQ4iMjhqT13E2Qoyt9mTDy29rcTANQAk6ROjuZwplbZbuYoIdTHtXpRyq7U6 /Hqw== ARC-Authentication-Results: i=1; mx.google.com; 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 l15-v6si4998052pfd.174.2018.11.11.12.36.00; Sun, 11 Nov 2018 12:36:15 -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; 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 S1730571AbeKLGYx (ORCPT + 99 others); Mon, 12 Nov 2018 01:24:53 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:49932 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730203AbeKLFsN (ORCPT ); Mon, 12 Nov 2018 00:48:13 -0500 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLvsZ-0000lG-6C; Sun, 11 Nov 2018 19:58:43 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gLvsW-0001iL-3t; Sun, 11 Nov 2018 19:58:40 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Gustavo A. R. Silva" , "Jiri Kosina" Date: Sun, 11 Nov 2018 19:49:05 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 242/366] HID: hiddev: fix potential Spectre v1 In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.61-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: "Gustavo A. R. Silva" commit 4f65245f2d178b9cba48350620d76faa4a098841 upstream. uref->field_index, uref->usage_index, finfo.field_index and cinfo.index can be indirectly controlled by user-space, hence leading to a potential exploitation of the Spectre variant 1 vulnerability. This issue was detected with the help of Smatch: drivers/hid/usbhid/hiddev.c:473 hiddev_ioctl_usage() warn: potential spectre issue 'report->field' (local cap) drivers/hid/usbhid/hiddev.c:477 hiddev_ioctl_usage() warn: potential spectre issue 'field->usage' (local cap) drivers/hid/usbhid/hiddev.c:757 hiddev_ioctl() warn: potential spectre issue 'report->field' (local cap) drivers/hid/usbhid/hiddev.c:801 hiddev_ioctl() warn: potential spectre issue 'hid->collection' (local cap) Fix this by sanitizing such structure fields before using them to index report->field, field->usage and hid->collection Notice that given that speculation windows are large, the policy is to kill the speculation on the first load and not worry if it can be completed with a dependent load/store [1]. [1] https://marc.info/?l=linux-kernel&m=152449131114778&w=2 Signed-off-by: Gustavo A. R. Silva Signed-off-by: Jiri Kosina Signed-off-by: Ben Hutchings --- drivers/hid/usbhid/hiddev.c | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/drivers/hid/usbhid/hiddev.c +++ b/drivers/hid/usbhid/hiddev.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "usbhid.h" #ifdef CONFIG_USB_DYNAMIC_MINORS @@ -478,10 +479,14 @@ static noinline int hiddev_ioctl_usage(s if (uref->field_index >= report->maxfield) goto inval; + uref->field_index = array_index_nospec(uref->field_index, + report->maxfield); field = report->field[uref->field_index]; if (uref->usage_index >= field->maxusage) goto inval; + uref->usage_index = array_index_nospec(uref->usage_index, + field->maxusage); uref->usage_code = field->usage[uref->usage_index].hid; @@ -508,6 +513,8 @@ static noinline int hiddev_ioctl_usage(s if (uref->field_index >= report->maxfield) goto inval; + uref->field_index = array_index_nospec(uref->field_index, + report->maxfield); field = report->field[uref->field_index]; @@ -761,6 +768,8 @@ static long hiddev_ioctl(struct file *fi if (finfo.field_index >= report->maxfield) break; + finfo.field_index = array_index_nospec(finfo.field_index, + report->maxfield); field = report->field[finfo.field_index]; memset(&finfo, 0, sizeof(finfo)); @@ -801,6 +810,8 @@ static long hiddev_ioctl(struct file *fi if (cinfo.index >= hid->maxcollection) break; + cinfo.index = array_index_nospec(cinfo.index, + hid->maxcollection); cinfo.type = hid->collection[cinfo.index].type; cinfo.usage = hid->collection[cinfo.index].usage;