From patchwork Fri Feb 23 10:42:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinpu Wang X-Patchwork-Id: 10237443 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 74DDC602DC for ; Fri, 23 Feb 2018 11:25:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 66E3029530 for ; Fri, 23 Feb 2018 11:25:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 584E029537; Fri, 23 Feb 2018 11:25:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 17ABA29530 for ; Fri, 23 Feb 2018 11:25:15 +0000 (UTC) Received: (qmail 16278 invoked by uid 550); 23 Feb 2018 11:22:49 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 5259 invoked from network); 23 Feb 2018 10:42:48 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=profitbricks-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=BMGld0+R5II2i4tyIUhJFiy2RltvL+J4qIsapNY82zQ=; b=N0kooGGyCUBBYlhYLACPDODzewL/eJKBmoAkE3iZnb7Je3ZuFBaBGMXoIK/3Yv5oBD YZ+Qj78PmjO9PtLOVvz5/XVo80mIrugAvS4Y+6OPg782ZsnheuBMXIie1GU6gpkZCjp5 9J31oSO6jE3E1S/pHT9+INwCMdSwOenxuo1kxh70yPlwQFge0PBMQk3Q+KGiGWbt8DgO huLDH3viZidWofC056Gr4rTVDoKpMNpnbp+4LDwyBON6C3eIdUHw4VrghiNdoS3A17Zr 53rioGPsGgaKJ2f5I9IxSMDqLMCjyHw8yxwMLYh7Xysj25TunSqVr6WKMlz4/SfW2pr7 ac8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BMGld0+R5II2i4tyIUhJFiy2RltvL+J4qIsapNY82zQ=; b=FrLliFFp3LYQjSbOEdOCkoG3xyA4t4Aguc8A9oyqY+PAFXOsCHYGlKkjkGMCawHmF0 BKYVVVphOwY0fkOsa3SU1EpazK2P0vshU8fwdU4INBJ5prnzPjrN298wENuprHLTgskv JSqKxpwz8pLxKIOrCZ8/gWiYUvbSzJt6agocMyBFf3JtLIU8Vxr5qJt4BtSreyHHofHV r4JzXHd3AT+GtuKKUGpsE0/pXk+e/Sxxwx+uG9uzKf5efqBisGdBMyzY0+sH03QUQVCK 72vd9Gn4mhUmI7q1qRGIq8qssdZlN2AjaPPCbmpvB5ioda5Ax+E42bnJapfGC2UHWUXn yQHQ== X-Gm-Message-State: APf1xPDSJyS3aKPzU5tVvvheFX/M0UO9k+FVKUCq3XlZA/fe0lq0sF2n ol5x8VoAByxh201XmVfLXkoABw== X-Google-Smtp-Source: AH8x224eqD4w1gSCQ8QKQo/M4x12LQ/IeBrkirGUfioHJa4sfBHnF2pt76/6dN9zR4eA9WPeiq5mow== X-Received: by 10.223.195.204 with SMTP id d12mr1231340wrg.116.1519382557383; Fri, 23 Feb 2018 02:42:37 -0800 (PST) From: Jack Wang X-Google-Original-From: Jack Wang To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: Dan Williams , Thomas Gleixner , linux-arch@vger.kernel.org, Kees Cook , kernel-hardening@lists.openwall.com, Al Viro , Andy Lutomirski , torvalds@linux-foundation.org, alan@linux.intel.com, David Woodhouse , Jack Wang Subject: [stable 4.4 14/29] x86/get_user: Use pointer masking to limit speculation Date: Fri, 23 Feb 2018 11:42:03 +0100 Message-Id: <1519382538-15143-15-git-send-email-jinpu.wangl@profitbricks.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519382538-15143-1-git-send-email-jinpu.wangl@profitbricks.com> References: <1519382538-15143-1-git-send-email-jinpu.wangl@profitbricks.com> X-Virus-Scanned: ClamAV using ClamSMTP From: Dan Williams (cherry picked from commit c7f631cb07e7da06ac1d231ca178452339e32a94) Quoting Linus: I do think that it would be a good idea to very expressly document the fact that it's not that the user access itself is unsafe. I do agree that things like "get_user()" want to be protected, but not because of any direct bugs or problems with get_user() and friends, but simply because get_user() is an excellent source of a pointer that is obviously controlled from a potentially attacking user space. So it's a prime candidate for then finding _subsequent_ accesses that can then be used to perturb the cache. Unlike the __get_user() case get_user() includes the address limit check near the pointer de-reference. With that locality the speculation can be mitigated with pointer narrowing rather than a barrier, i.e. array_index_nospec(). Where the narrowing is performed by: cmp %limit, %ptr sbb %mask, %mask and %mask, %ptr With respect to speculation the value of %ptr is either less than %limit or NULL. Co-developed-by: Linus Torvalds Signed-off-by: Dan Williams Signed-off-by: Thomas Gleixner Cc: linux-arch@vger.kernel.org Cc: Kees Cook Cc: kernel-hardening@lists.openwall.com Cc: gregkh@linuxfoundation.org Cc: Al Viro Cc: Andy Lutomirski Cc: torvalds@linux-foundation.org Cc: alan@linux.intel.com Link: https://lkml.kernel.org/r/151727417469.33451.11804043010080838495.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: David Woodhouse Signed-off-by: Greg Kroah-Hartman [jwang: port to 4.4] Signed-off-by: Jack Wang --- arch/x86/lib/getuser.S | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/lib/getuser.S b/arch/x86/lib/getuser.S index 46668cd..490b2ee 100644 --- a/arch/x86/lib/getuser.S +++ b/arch/x86/lib/getuser.S @@ -38,6 +38,8 @@ ENTRY(__get_user_1) GET_THREAD_INFO(%_ASM_DX) cmp TI_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ + and %_ASM_DX, %_ASM_AX ASM_STAC 1: movzbl (%_ASM_AX),%edx xor %eax,%eax @@ -51,6 +53,8 @@ ENTRY(__get_user_2) GET_THREAD_INFO(%_ASM_DX) cmp TI_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ + and %_ASM_DX, %_ASM_AX ASM_STAC 2: movzwl -1(%_ASM_AX),%edx xor %eax,%eax @@ -64,6 +68,8 @@ ENTRY(__get_user_4) GET_THREAD_INFO(%_ASM_DX) cmp TI_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ + and %_ASM_DX, %_ASM_AX ASM_STAC 3: movl -3(%_ASM_AX),%edx xor %eax,%eax @@ -78,6 +84,8 @@ ENTRY(__get_user_8) GET_THREAD_INFO(%_ASM_DX) cmp TI_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ + and %_ASM_DX, %_ASM_AX ASM_STAC 4: movq -7(%_ASM_AX),%rdx xor %eax,%eax @@ -89,6 +97,8 @@ ENTRY(__get_user_8) GET_THREAD_INFO(%_ASM_DX) cmp TI_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user_8 + sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ + and %_ASM_DX, %_ASM_AX ASM_STAC 4: movl -7(%_ASM_AX),%edx 5: movl -3(%_ASM_AX),%ecx