Received: by 10.213.65.68 with SMTP id h4csp599661imn; Thu, 22 Mar 2018 04:41:06 -0700 (PDT) X-Google-Smtp-Source: AG47ELtmswW3ozX9nTO9Odk+3CckG3DBnbfGSdQIs3DOujg1YYXUUd7XPEZP8NUwHRDkAe5i4FHx X-Received: by 2002:a17:902:5793:: with SMTP id l19-v6mr6585673pli.386.1521718866129; Thu, 22 Mar 2018 04:41:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521718866; cv=none; d=google.com; s=arc-20160816; b=ZggXITsNRiLwGvX4LLVeEYh3woRahGWlptFz5NWV71U9fj0sfpR+wnVmTnqTLtm4PC JZ6Q9CyV5dGsCrd0SpTKaZstcvsoA535k+pnlazPEZQ6ysvYqnadmEXrlZezPw2wLMXx QZSq7BKyeFPOqosH6t638B0YwbUn3aprOZipPuWBRsVj9S51aHNyGUwh9efPNSorJksS r/5JTgULwZEFbwvHsJNfclAOsd6plPCZJI8ZC2EPFxCpvSS7EtEz126Srpl5uXYXtg1I vPmmzIYzqG2N4ek7Mo6LJCIH2ZebVBYxXzg1hqN7YpbKtU3bmiRSZBqk0mpdLaJYS5Sl bhcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=Ij30qKQTRew4BWVgEsmupxA1y6SDQyLpL315HZpzG7w=; b=WfKZZpHqZmPFqAMzR7+bpWlLu4k+ENpH2DJRqtRhpyvF5mx7r9aVbaYPVCG1euT0Gz Y7DvDTXYWunF1pwFYbcD7tn/Q8IudkXNanOxLWR1knWAMFJrMNLRW5eTweEzppF5S6Hz ndJmnZ+mo3Ro1M+6L6mypHxbBFv5fefDlTVmL3f/eQ13WuZjE5PcszYWNmG3R6EtE6Oe cQIqn8WpWnt3iQTtAdMAR2XFbZfdwAgiiykv72MQDSFkTWjr2vBNtEz6DmBXUD7h5Ejh ZEqenuIifhXeVsEc2qDXivZQUtowLPXZPP5s+IOdNz+1FVEpYhj2tPRHnLDjxfINXvZz vmZw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a4-v6si6528866plm.660.2018.03.22.04.40.52; Thu, 22 Mar 2018 04:41:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753688AbeCVLfz (ORCPT + 99 others); Thu, 22 Mar 2018 07:35:55 -0400 Received: from terminus.zytor.com ([198.137.202.136]:51335 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751080AbeCVLfu (ORCPT ); Thu, 22 Mar 2018 07:35:50 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTP id w2MBZ0qX012890; Thu, 22 Mar 2018 04:35:00 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w2MBYtgp012886; Thu, 22 Mar 2018 04:34:55 -0700 Date: Thu, 22 Mar 2018 04:34:55 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Thomas Gleixner Message-ID: Cc: dwmw@amazon.co.uk, linux-kernel@vger.kernel.org, mingo@kernel.org, peterz@infradead.org, rasmus.villemoes@prevas.dk, dan.j.williams@intel.com, torvalds@linux-foundation.org, tglx@linutronix.de, gregkh@linuxfoundation.org, hpa@zytor.com Reply-To: rasmus.villemoes@prevas.dk, dan.j.williams@intel.com, tglx@linutronix.de, torvalds@linux-foundation.org, hpa@zytor.com, gregkh@linuxfoundation.org, dwmw@amazon.co.uk, mingo@kernel.org, peterz@infradead.org, linux-kernel@vger.kernel.org In-Reply-To: References: To: linux-tip-commits@vger.kernel.org Subject: [tip:timers/urgent] posix-timers: Protect posix clock array access against speculation Git-Commit-ID: 19b558db12f9f4e45a22012bae7b4783e62224da X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 19b558db12f9f4e45a22012bae7b4783e62224da Gitweb: https://git.kernel.org/tip/19b558db12f9f4e45a22012bae7b4783e62224da Author: Thomas Gleixner AuthorDate: Thu, 15 Feb 2018 17:21:55 +0100 Committer: Thomas Gleixner CommitDate: Thu, 22 Mar 2018 12:29:27 +0100 posix-timers: Protect posix clock array access against speculation The clockid 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. Remove the redundant check for !posix_clock[id] as this is another source for speculation and does not provide any advantage over the return posix_clock[id] path which returns NULL in that case anyway. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: Dan Williams Cc: Rasmus Villemoes Cc: Greg KH Cc: stable@vger.kernel.org Cc: Linus Torvalds Cc: David Woodhouse Link: https://lkml.kernel.org/r/alpine.DEB.2.21.1802151718320.1296@nanos.tec.linutronix.de --- kernel/time/posix-timers.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 75043046914e..10b7186d0638 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -50,6 +50,7 @@ #include #include #include +#include #include "timekeeping.h" #include "posix-timers.h" @@ -1346,11 +1347,15 @@ static const struct k_clock * const posix_clocks[] = { static const struct k_clock *clockid_to_kclock(const clockid_t id) { - if (id < 0) + 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]) + if (id >= ARRAY_SIZE(posix_clocks)) return NULL; - return posix_clocks[id]; + + return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))]; }