From: Andrew Cooper <Andrew.Cooper3@citrix.com>
To: Jordy Zomer <jordy@pwning.systems>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: 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>,
Yuri Nudelman <ynudelman@habana.ai>,
Sagiv Ozeri <sozeri@habana.ai>, Koby Elbaz <kelbaz@habana.ai>,
farah kassabri <fkassabri@habana.ai>
Subject: Re: [PATCHv3] habanalabs: fix potential spectre v1 gadgets
Date: Wed, 2 Feb 2022 21:01:14 +0000 [thread overview]
Message-ID: <ff3ea1f6-0e4b-1dc9-de46-1d70bd730b0b@citrix.com> (raw)
In-Reply-To: <20220202191104.3526448-1-jordy@pwning.systems>
On 02/02/2022 19:11, Jordy Zomer wrote:
> 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
> @@ -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",
Sorry for jumping in the middle here, but array_index_nospec() is
finicky to use, and both examples here don't quite behave as intended.
(In reverse order), first, any competent compiler will turn:
if (nr == _IOC_NR(HL_IOCTL_INFO)) {
ioctl = &hl_ioctls_control[nr];
into
ioctl = &hl_ioctls_control[_IOC_NR(HL_IOCTL_INFO)];
which can be evaluated at compile time without reference to nr. By
using array_index_nospec(), you inhibit this optimisation, forcing the
load to be dependent on nr, and with the extra logic to try and make nr
be safe.
Second, array_index_nospec(nr, HL_COMMAND_END); only protects the upper
bound. In this case, the valid lower bound is not zero.
i.e. while you've prevented an OoB read of the hl_ioctls[] array as a
whole, there is still a Spectre-v1 gadget where you can speculatively
continue using &hl_ioctls[0] which is out of bounds from the logic's
point of view.
One fix is to use:
nr = HL_COMMAND_START +
array_index_nospec(nr, HL_COMMAND_END - HL_COMMAND_START);
while another is to confirm that the subsequent logic is safe to
confused-deputy problems. This analysis probably needs doing anyway as
there are multiple ioctl implementations that can be selected.
~Andrew
prev parent reply other threads:[~2022-02-02 21:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-01 17:24 [PATCH] " Jordy Zomer
2022-02-02 7:50 ` Oded Gabbay
2022-02-02 10:22 ` Oded Gabbay
2022-02-02 17:19 ` [PATCHv2] " Jordy Zomer
2022-02-02 19:02 ` 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 [this message]
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=ff3ea1f6-0e4b-1dc9-de46-1d70bd730b0b@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=arnd@arndb.de \
--cc=dliberman@habana.ai \
--cc=fkassabri@habana.ai \
--cc=gregkh@linuxfoundation.org \
--cc=jordy@pwning.systems \
--cc=kelbaz@habana.ai \
--cc=linux-kernel@vger.kernel.org \
--cc=obitton@habana.ai \
--cc=ogabbay@kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®