LKML Archive on lore.kernel.org
 help / Atom feed
From: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
To: Thomas Gleixner <tglx@linutronix.de>,
	LKML <linux-kernel@vger.kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	David Woodhouse <dwmw@amazon.co.uk>,
	Dan Williams <dan.j.williams@intel.com>,
	Greg KH <gregkh@linuxfoundation.org>
Subject: Re: [PATCH] posix-timers: Protect posix clock array access against speculation
Date: Thu, 15 Feb 2018 15:05:08 +0100
Message-ID: <45f8dece-e235-0831-4fe5-89ee7d27b959@prevas.dk> (raw)
In-Reply-To: <alpine.DEB.2.21.1802151424490.1296@nanos.tec.linutronix.de>

On 2018-02-15 14:27, Thomas Gleixner wrote:
> The (clock) id argument of clockid_to_kclock() comes straight from user
> space via various syscalls and is used as index into the posix_clocks
> array.
> 
> Protect it against spectre v1 array out of bounds speculation.
> 
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> Cc: stable@vger.kernel.org
> ---
>  kernel/time/posix-timers.c |    6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> --- a/kernel/time/posix-timers.c
> +++ b/kernel/time/posix-timers.c
> @@ -50,6 +50,7 @@
>  #include <linux/export.h>
>  #include <linux/hashtable.h>
>  #include <linux/compat.h>
> +#include <linux/nospec.h>
>  
>  #include "timekeeping.h"
>  #include "posix-timers.h"
> @@ -1346,11 +1347,14 @@ static const struct k_clock * const posi
>  
>  static const struct k_clock *clockid_to_kclock(const clockid_t id)
>  {
> +	clockid_t idx = id;
> +
>  	if (id < 0)
>  		return (id & CLOCKFD_MASK) == CLOCKFD ?
>  			&clock_posix_dynamic : &clock_posix_cpu;
>  
>  	if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
>  		return NULL;
> -	return posix_clocks[id];
> +
> +	return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
>  }
> 

Stupid questions from someone trying to learn what the rules for when
and how to apply these _nospec macros:

(1) why introduce the idx var? There's no assignment to it other than
the initialization. Is it some magic in array_index_nospec that prevents
the use of a const-qualified expression?

(2) The line "if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])"
still seems to allow speculatively accessing posix_clocks[id]. Is that
ok, and even if so, wouldn't it be cleaner to elide the
!posix_clocks[id] check and just return the NULL safely fetched from the
array in the following line?

Rasmus

  reply index

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-15 13:27 Thomas Gleixner
2018-02-15 14:05 ` Rasmus Villemoes [this message]
2018-02-15 14:39   ` Dan Williams
2018-02-15 14:53     ` Thomas Gleixner
2018-02-15 16:21       ` [PATCH V2] " Thomas Gleixner
2018-02-15 17:01         ` Peter Zijlstra
2018-03-07 18:13           ` Dan Williams
2018-03-22 11:34         ` [tip:timers/urgent] " tip-bot for Thomas Gleixner
2018-02-15 19:52     ` [PATCH] linux/nospec.h: allow index argument to have const-qualified type Rasmus Villemoes
2018-02-15 20:59       ` Linus Torvalds
2018-02-15 21:56         ` Dan Williams
2018-02-15 22:03           ` Linus Torvalds
2018-02-15 22:08             ` Dan Williams

Reply instructions:

You may reply publically 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=45f8dece-e235-0831-4fe5-89ee7d27b959@prevas.dk \
    --to=rasmus.villemoes@prevas.dk \
    --cc=dan.j.williams@intel.com \
    --cc=dwmw@amazon.co.uk \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /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

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org linux-kernel@archiver.kernel.org
	public-inbox-index lkml


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox