2018-07-28 01:34:03

by Jinbum Park

[permalink] [raw]
Subject: [PATCH] pktcdvd: Fix possible Spectre-v1 for pkt_devs

User controls @dev_minor which to be used as index of pkt_devs.
So, It can be exploited via Spectre-like attack. (speculative execution)

This kind of attack leaks address of pkt_devs, [1]
It leads an attacker to bypass security mechanism such as KASLR.

So sanitize @dev_minor before using it to prevent attack.

[1] https://github.com/jinb-park/linux-exploit/
tree/master/exploit-remaining-spectre-gadget/leak_pkt_devs.c

Signed-off-by: Jinbum Park <[email protected]>
---
drivers/block/pktcdvd.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c
index c61d20c..665760d 100644
--- a/drivers/block/pktcdvd.c
+++ b/drivers/block/pktcdvd.c
@@ -67,8 +67,8 @@
#include <scsi/scsi.h>
#include <linux/debugfs.h>
#include <linux/device.h>
-
#include <linux/uaccess.h>
+#include <linux/nospec.h>

#define DRIVER_NAME "pktcdvd"

@@ -2229,9 +2229,13 @@ static void pkt_release_dev(struct pktcdvd_device *pd, int flush)

static struct pktcdvd_device *pkt_find_dev_from_minor(unsigned int dev_minor)
{
+ unsigned int idx = 0;
+
if (dev_minor >= MAX_WRITERS)
return NULL;
- return pkt_devs[dev_minor];
+
+ idx = array_index_nospec(dev_minor, MAX_WRITERS);
+ return pkt_devs[idx];
}

static int pkt_open(struct block_device *bdev, fmode_t mode)
--
1.9.1



2018-07-28 01:49:42

by Gustavo A. R. Silva

[permalink] [raw]
Subject: Re: [PATCH] pktcdvd: Fix possible Spectre-v1 for pkt_devs

Hi Jinbum,

Thanks for working on this.

Please, see some comments below.

On 07/27/2018 08:31 PM, Jinbum Park wrote:
> User controls @dev_minor which to be used as index of pkt_devs.
> So, It can be exploited via Spectre-like attack. (speculative execution)
>
> This kind of attack leaks address of pkt_devs, [1]
> It leads an attacker to bypass security mechanism such as KASLR.
>
> So sanitize @dev_minor before using it to prevent attack.
>
> [1] https://github.com/jinb-park/linux-exploit/
> tree/master/exploit-remaining-spectre-gadget/leak_pkt_devs.c
>
> Signed-off-by: Jinbum Park <[email protected]>
> ---
> drivers/block/pktcdvd.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c
> index c61d20c..665760d 100644
> --- a/drivers/block/pktcdvd.c
> +++ b/drivers/block/pktcdvd.c
> @@ -67,8 +67,8 @@
> #include <scsi/scsi.h>
> #include <linux/debugfs.h>
> #include <linux/device.h>
> -
> #include <linux/uaccess.h>
> +#include <linux/nospec.h>
>
> #define DRIVER_NAME "pktcdvd"
>
> @@ -2229,9 +2229,13 @@ static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
>
> static struct pktcdvd_device *pkt_find_dev_from_minor(unsigned int dev_minor)
> {
> + unsigned int idx = 0;
> +

There is no need for a new variable in this case.

> if (dev_minor >= MAX_WRITERS)
> return NULL;
> - return pkt_devs[dev_minor];
> +
> + idx = array_index_nospec(dev_minor, MAX_WRITERS);
> + return pkt_devs[idx];

Also, the following style is preferred:

if (dev_minor >= MAX_WRITERS)
return NULL;
dev_minor = array_index_nospec(dev_minor, MAX_WRITERS);


Thanks
--
Gustavo