Received: by 10.223.176.5 with SMTP id f5csp2832475wra; Mon, 5 Feb 2018 10:35:55 -0800 (PST) X-Google-Smtp-Source: AH8x226ocJa/al87CIOZyE+qEf95bnYkcf1uIqsLflyCixR1AypGSD51Orr1gOQSOEXLHqAg9r4l X-Received: by 10.98.87.144 with SMTP id i16mr14715850pfj.176.1517855755775; Mon, 05 Feb 2018 10:35:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517855755; cv=none; d=google.com; s=arc-20160816; b=Cw55Z1sSPRGsZLFub6z2RJ9p31BKsrf41jKg0IOuL+oxmcgXt+5JGNJfFQdFE7RgS/ 8JLGvQVZSq5cnCB4WQEI17CXhISK+dUxy6UoOUHGebknpst8/uMedsRvPB4sVLyrqKq1 nP7BMkX1MudjH1hJmy/TkWIOVXFgCnmlqD+FXD+Vc2gjhftbVsuho2t4DPCr4oaoDcO/ MreRtlWb8DlRiNXLvGmqOyzMnC7RdidiRGI07KMxe/gsZ5IzsogIX8qiA4T3SCgGtPA5 Rkjp6doyFWetVH4ptK3kDULauyTfpw8VOzslWWQ0B81ncUTvUdHasnRwsNQC62eINTAs CMGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=kGZP3higEqeYxmBKeoVGHRwOyUjijKJvmWQkLcgktlE=; b=CELDZe5Q+nVcC2cN39Td4wsJryKXMfIM/9b3wf6+WK0gM061UBKFhWV9IcAuItftld 5hbtl3acLsb8T2Vn4OrQ+7xVY0P8yHlK5jZl0Ic8VqzrYTfzyTdK6pGze+7kPDTSi1nz msHMjLR17i+/EJuEUm7x/lX5MxTfx8hnuZpoAUYzZ69XDoBRkN96HBlF/IREKZ2jxxSz gr1RE7bZvHYmmKt/pTbUzmOPdScvgRpJNmwm51VpnWwOMfYbAz/jeemxz1OtLZKY5MuV ymWYPANbEQFA5o9ZgqEJ6MtGnH8QLfHcPaKOkjJa5jr9h73j+e4APj3isPlA6RHIs8HC 9jTA== 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 r14-v6si208773plj.226.2018.02.05.10.35.41; Mon, 05 Feb 2018 10:35:55 -0800 (PST) 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 S1754099AbeBESau (ORCPT + 99 others); Mon, 5 Feb 2018 13:30:50 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:52698 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753749AbeBESZ3 (ORCPT ); Mon, 5 Feb 2018 13:25:29 -0500 Received: from localhost (unknown [104.132.1.108]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 690DF1136; Mon, 5 Feb 2018 18:25:19 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Dan Williams , Thomas Gleixner , linux-arch@vger.kernel.org, kernel-hardening@lists.openwall.com, alan@linux.intel.com Subject: [PATCH 4.15 34/60] x86: Implement array_index_mask_nospec Date: Mon, 5 Feb 2018 10:23:07 -0800 Message-Id: <20180205182215.361264698@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180205182213.902626065@linuxfoundation.org> References: <20180205182213.902626065@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Williams dan.j.williams@intel.com commit babdde2698d482b6c0de1eab4f697cf5856c5859 array_index_nospec() uses a mask to sanitize user controllable array indexes, i.e. generate a 0 mask if 'index' >= 'size', and a ~0 mask otherwise. While the default array_index_mask_nospec() handles the carry-bit from the (index - size) result in software. The x86 array_index_mask_nospec() does the same, but the carry-bit is handled in the processor CF flag without conditional instructions in the control flow. Suggested-by: Linus Torvalds Signed-off-by: Dan Williams Signed-off-by: Thomas Gleixner Cc: linux-arch@vger.kernel.org Cc: kernel-hardening@lists.openwall.com Cc: gregkh@linuxfoundation.org Cc: alan@linux.intel.com Link: https://lkml.kernel.org/r/151727414808.33451.1873237130672785331.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/barrier.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) --- a/arch/x86/include/asm/barrier.h +++ b/arch/x86/include/asm/barrier.h @@ -24,6 +24,30 @@ #define wmb() asm volatile("sfence" ::: "memory") #endif +/** + * array_index_mask_nospec() - generate a mask that is ~0UL when the + * bounds check succeeds and 0 otherwise + * @index: array element index + * @size: number of elements in array + * + * Returns: + * 0 - (index < size) + */ +static inline unsigned long array_index_mask_nospec(unsigned long index, + unsigned long size) +{ + unsigned long mask; + + asm ("cmp %1,%2; sbb %0,%0;" + :"=r" (mask) + :"r"(size),"r" (index) + :"cc"); + return mask; +} + +/* Override the default implementation from linux/nospec.h. */ +#define array_index_mask_nospec array_index_mask_nospec + #ifdef CONFIG_X86_PPRO_FENCE #define dma_rmb() rmb() #else