Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2713831pxb; Thu, 3 Feb 2022 12:33:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzE22HUa79ptWd9MUiaaCjKWSB+8kid8P9OzfQr+ceyiB52q9rcgF82WDzow596IbyXOptm X-Received: by 2002:a63:6a85:: with SMTP id f127mr29380030pgc.250.1643920389879; Thu, 03 Feb 2022 12:33:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643920389; cv=none; d=google.com; s=arc-20160816; b=wBI1OeJxHDOzARNe3QAeXPCGXV8EJURCJpddvaw07BHMFlcGOv+oPuWoGYYwLtCnjQ eLJhpGX+QxhxDeD/luSBnDqAKvKAzCtI4mAWne6tQtOmTqUD+aYAvRUpBXrjr2ntgwRG ZRMFyJOol10UiSp9/HfuDyKl4LlUVu0sC3EMpPeTffcJouGs+Op9TmBa2MR0IgAvL4Tc avR38zsFSQG8uFqExqo/ELtontQLpmZrpJRSj+ZKmWAsG1B1FXBcXaZjhoZgDSq02FC9 LVOKGxxVULYBScpd5jHJO2ozUPIsc5jQMGU0MCHDmNM3HPk79Rv3sXjA8HeJBUNETQE7 yBig== 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; bh=dtXJ+uHJsD1xI7oNtrmhuumdJIE4H4sAfwduXIYd0gk=; b=QukpJgCiGJrgloP866jfAsfo/WdE8EZQJHeFa+cFU8NaIztu3okQh7n+beRIwixlLM 0m//ywmrO8P3/Sdsgzq+6d2yhRyEzuYHO5SW7FojARynzYdALzgmp4xcweIdEfEcIUAd UW3L5SEHvPFsUZJuQx7AH1FYL5EXgy6WHla7p2tabUesoxWKLmPWpO5uDma8O4t7haFL ft4atbPtQl/i+04mulUcAfhM1FiUNc7iEtz9A9I1S++ao0B25f6pf/UvWP+efFHl47oe veqJwcAUuVrCNTl18D4jiEJkeYhuafW3cGt891TF6UET4kVNvzFDSQuC6JCe+2foniek eQ0w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d185si22758199pgc.646.2022.02.03.12.32.56; Thu, 03 Feb 2022 12:33:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345278AbiBBRT5 (ORCPT + 99 others); Wed, 2 Feb 2022 12:19:57 -0500 Received: from mta-11-4.privateemail.com ([198.54.127.104]:45250 "EHLO MTA-11-4.privateemail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230363AbiBBRT4 (ORCPT ); Wed, 2 Feb 2022 12:19:56 -0500 Received: from mta-11.privateemail.com (localhost [127.0.0.1]) by mta-11.privateemail.com (Postfix) with ESMTP id CAA4C18000AA; Wed, 2 Feb 2022 12:19:55 -0500 (EST) Received: from localhost.localdomain (unknown [10.20.151.154]) by mta-11.privateemail.com (Postfix) with ESMTPA id 108D618000AE; Wed, 2 Feb 2022 12:19:52 -0500 (EST) From: Jordy Zomer To: linux-kernel@vger.kernel.org Cc: Jordy Zomer , Oded Gabbay , Arnd Bergmann , Greg Kroah-Hartman , Ofir Bitton , Dani Liberman , Omer Shpigelman , Sagiv Ozeri , Yuri Nudelman , farah kassabri , Koby Elbaz Subject: [PATCHv2] habanalabs: fix potential spectre v1 gadgets Date: Wed, 2 Feb 2022 18:19:24 +0100 Message-Id: <20220202171931.3525220-1-jordy@pwning.systems> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It appears like nr could be a Spectre v1 gadget as it's supplied by a user and used as an array index. Prevent the contents of kernel memory from being leaked to userspace via speculative execution by using array_index_nospec. Thanks for noticing Oded, made the changes. Signed-off-by: Jordy Zomer --- Changes v1 -> v2: Added the correct offsets --- drivers/misc/habanalabs/common/habanalabs_ioctl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/misc/habanalabs/common/habanalabs_ioctl.c b/drivers/misc/habanalabs/common/habanalabs_ioctl.c index 3ba3a8ffda3e..c1cdf712a10d 100644 --- a/drivers/misc/habanalabs/common/habanalabs_ioctl.c +++ b/drivers/misc/habanalabs/common/habanalabs_ioctl.c @@ -14,6 +14,7 @@ #include #include #include +#include static u32 hl_debug_struct_size[HL_DEBUG_OP_TIMESTAMP + 1] = { [HL_DEBUG_OP_ETR] = sizeof(struct hl_debug_params_etr), @@ -849,6 +850,7 @@ long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) } if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) { + nr = array_index_nospec(nr, HL_COMMAND_END); ioctl = &hl_ioctls[nr]; } else { dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n", @@ -872,6 +874,7 @@ long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg) } if (nr == _IOC_NR(HL_IOCTL_INFO)) { + nr = array_index_nospec(nr, _IOC_NR(HL_IOCTL_INFO)+1); ioctl = &hl_ioctls_control[nr]; } else { dev_err(hdev->dev_ctrl, "invalid ioctl: pid=%d, nr=0x%02x\n", -- 2.27.0