All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jordy Zomer <jordy@pwning.systems>
To: linux-kernel@vger.kernel.org
Cc: Jordy Zomer <jordy@pwning.systems>,
	Oded Gabbay <ogabbay@kernel.org>, Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Ofir Bitton <obitton@habana.ai>,
	Dani Liberman <dliberman@habana.ai>,
	Omer Shpigelman <oshpigelman@habana.ai>,
	Sagiv Ozeri <sozeri@habana.ai>,
	Yuri Nudelman <ynudelman@habana.ai>,
	farah kassabri <fkassabri@habana.ai>,
	Koby Elbaz <kelbaz@habana.ai>
Subject: [PATCHv2] habanalabs: fix potential spectre v1 gadgets
Date: Wed,  2 Feb 2022 18:19:24 +0100	[thread overview]
Message-ID: <20220202171931.3525220-1-jordy@pwning.systems> (raw)
In-Reply-To: <CAFCwf12QygXk=8WbH9qDhyyqKY6ZGVCUSJM93viQ0armc8dgfg@mail.gmail.com>

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 <jordy@pwning.systems>

---
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 <linux/fs.h>
 #include <linux/uaccess.h>
 #include <linux/slab.h>
+#include <linux/nospec.h>
 
 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


  reply	other threads:[~2022-02-02 17:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-01 17:24 [PATCH] habanalabs: fix potential spectre v1 gadgets Jordy Zomer
2022-02-02  7:50 ` Oded Gabbay
2022-02-02 10:22   ` Oded Gabbay
2022-02-02 17:19     ` Jordy Zomer [this message]
2022-02-02 19:02       ` [PATCHv2] " Greg Kroah-Hartman
2022-02-02 19:11         ` [PATCHv3] " Jordy Zomer
2022-02-02 19:13           ` Jordy Zomer
2022-02-02 21:01           ` Andrew Cooper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220202171931.3525220-1-jordy@pwning.systems \
    --to=jordy@pwning.systems \
    --cc=arnd@arndb.de \
    --cc=dliberman@habana.ai \
    --cc=fkassabri@habana.ai \
    --cc=gregkh@linuxfoundation.org \
    --cc=kelbaz@habana.ai \
    --cc=linux-kernel@vger.kernel.org \
    --cc=obitton@habana.ai \
    --cc=ogabbay@kernel.org \
    --cc=oshpigelman@habana.ai \
    --cc=sozeri@habana.ai \
    --cc=ynudelman@habana.ai \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.