Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2221097imm; Thu, 7 Jun 2018 07:16:47 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJLoOeZqrxWF2Ybc4wAyZPHrbVJiVzxRcUb9nbfMcBiwUpGuoHo6SYMC5NGAHMeEOf0NHyN X-Received: by 2002:a17:902:820a:: with SMTP id x10-v6mr2258255pln.179.1528381007619; Thu, 07 Jun 2018 07:16:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528381007; cv=none; d=google.com; s=arc-20160816; b=UOietZ5zqiezsOlwc+Lw2o+ldM9zetjikp73IwT+tizETw9aFGwdG+40UNY9QDllpd K9nBFUlwT8jYVxa3zD1czve4XYax02rdIdrjgrfZX8/ST0u+lnCU1lw71i5vjaZ4g6vb cX9mRFY9biOokxNI7wAkf9yDGzDp+JEa5wgI5w4uPfdJ+fJW92Lbo5B1AxH4GHLYP0+t TZv4Bnduw4XoBKmaZ3hVQbfJXIXv17yXNBBc7p6x/SoN87PY32GHQwulFNuMeLeVGXMp 83Xr5lYmRFHGRLNzwIxTLJkL8RjfTIqzMYiPFdEi83oN1fgoY8kCuhQp5AxNf1RHkdP1 Oysw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=S0YFVFu6HBs/A0VUi08n4njmtBzAPlvJpURMTOtVzUw=; b=jeKYmnDUUh5kjTNgSJQo4F1QWinq+cjpcLWpA5PNx/do6tMT6si+0FribnWBTCr7pb DsHMrLi7LDhPkrAyUxVEc5Z+hss6RvnjILaM6KO84oQ4sht42+nL/8a9SQOmB5wZqmvV Fy/z4scxyyw2obMQzWhywxJ7yL9p76FTblq1jY7HiDM/GCqgUqi3Vd2Cb1nbbA8lFDMR PvzBM2hZiDpQB+sM9GS43mqhehWscbWXbFogj6kHYCH3NV68+5uFZM0Ws9KXr9aUznKG cvUaFD1OHxh5TvUhc/MUgV+7gW8vLRpjUDp4bMuCTIjKHeD52Kjtv8qB4uuMUtHIJLOc phaw== 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 s4-v6si28279459pgc.634.2018.06.07.07.16.33; Thu, 07 Jun 2018 07:16:47 -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 S933148AbeFGOPa (ORCPT + 99 others); Thu, 7 Jun 2018 10:15:30 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:39501 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933155AbeFGOJi (ORCPT ); Thu, 7 Jun 2018 10:09:38 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbM-0005Zm-8Q; Thu, 07 Jun 2018 15:09:20 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvbE-0003IC-7r; Thu, 07 Jun 2018 15:09:12 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "David Woodhouse" , "Peter Zijlstra (Intel)" , "Rasmus Villemoes" , "Greg KH" , "Linus Torvalds" , "Thomas Gleixner" , "Dan Williams" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 389/410] posix-timers: Protect posix clock array access against speculation In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Thomas Gleixner commit 19b558db12f9f4e45a22012bae7b4783e62224da upstream. 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: Linus Torvalds Cc: David Woodhouse Link: https://lkml.kernel.org/r/alpine.DEB.2.21.1802151718320.1296@nanos.tec.linutronix.de [bwh: Backported to 3.16: - Move the test of the clock_getres field below the lookup using array_index_nospec() - Adjust filename, context] Signed-off-by: Ben Hutchings --- --- a/kernel/posix-timers.c +++ b/kernel/posix-timers.c @@ -48,6 +48,7 @@ #include #include #include +#include /* * Management arrays for POSIX timers. Timers are now kept in static hash table @@ -578,13 +579,21 @@ static void release_posix_timer(struct k static struct k_clock *clockid_to_kclock(const clockid_t id) { - if (id < 0) + clockid_t idx = id; + struct k_clock *kc; + + if (id < 0) { return (id & CLOCKFD_MASK) == CLOCKFD ? &clock_posix_dynamic : &clock_posix_cpu; + } + + if (id >= ARRAY_SIZE(posix_clocks)) + return NULL; - if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres) + kc = &posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))]; + if (!kc->clock_getres) return NULL; - return &posix_clocks[id]; + return kc; } static int common_timer_create(struct k_itimer *new_timer)