From patchwork Mon Feb 11 23:27:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10806983 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BCCCC1390 for ; Mon, 11 Feb 2019 23:29:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEC3E2A823 for ; Mon, 11 Feb 2019 23:29:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2A042A84E; Mon, 11 Feb 2019 23:29:06 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 7474D2A823 for ; Mon, 11 Feb 2019 23:29:05 +0000 (UTC) Received: (qmail 30636 invoked by uid 550); 11 Feb 2019 23:28:33 -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 Received: (qmail 30550 invoked from network); 11 Feb 2019 23:28:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=ZdMEqkaTdkU9cQtnyBwyKsKlsrmvMKKieThBtq30bZY=; b=Gqq17HZaZ6nYNJ2Waw8C45/pT84leeq75yxS8fGSjHCrkQFeSHGV6+O/G32DVn0ZPW LRntTc/gWGiCzbHknpYD0WIGdPBsxF48Pi2Eqezr+FYqE5EmvTh3gCPWTYFSLfM9T3S4 BuGCmH14QZYFnQPwRp0pjsUno0lKnQ68bBnb4VDLucg0jV9j8RZusWMJ6QcjhNcdNBeC Ud9mamr/FU72AoO/EsCppelcnPqu7/6ZEELqCQRUybUL7VLnarMjsDuTrtKoX/Uuclke S1yE7MCcTW62lyFf8XSPh0C8fTcP7AG+sX97NdgWIG8QKtRNmwZABW2W7CFA8P6s6CRg aDhA== 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:reply-to:mime-version:content-transfer-encoding; bh=ZdMEqkaTdkU9cQtnyBwyKsKlsrmvMKKieThBtq30bZY=; b=EPwvp98vtB1ZSJDzXMq5/4p/ZWMngYeHl1EMu0W6LnE90+lKjBGwwRLhGRJpoXgAZe r8luSJWRPwPxMZN2oAkdGSG0PnnIL45rptMa1C9UznFgZ1BE56cLrT7BS4jX4NlBI+84 B+a8iIgVINP0HqZxbvfSRjb2esYHkfycSu0EI/tt/JbFEw44zXP5ZeDHNesVjzGOs9nl 8CIHRhTSMLF9sQIuo/MvR245c1kEuwcgGLkuY9nemGYQn79wGW1sBbQrmcH8b0A4qoRU ZSJBqcTbd2DuO8ZSSMUF8oHYv8zKoae1iDme8bU0n+ADZe4/VBKZ6nU16tCGCiGLI4zi Ewiw== X-Gm-Message-State: AHQUAuZrlzrLi9n1oCLJg1yfyq5aL11SQ9PW8HDu4WCWO/Q5FlL1dNmh PFJfs+JCjD7WwD8KK6Nq8BA= X-Google-Smtp-Source: AHgI3IaRbYzOvYY9aR2Sas9wiYW3Vep8Wn7Wc3iPNf+FO4IJVM9hmiyDf+hkZLdGHUnUWgdhWnBgeQ== X-Received: by 2002:a1c:f916:: with SMTP id x22mr488708wmh.87.1549927701124; Mon, 11 Feb 2019 15:28:21 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Cc: Igor Stoppa , Andy Lutomirski , Nadav Amit , Matthew Wilcox , Peter Zijlstra , Kees Cook , Dave Hansen , Mimi Zohar , Thiago Jung Bauermann , Ahmed Soliman , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v4 05/12] __wr_after_init: arm64: memset_user() Date: Tue, 12 Feb 2019 01:27:42 +0200 Message-Id: <165661e29f9a2a6aa36e51ae79a06f03b7c8718e.1549927666.git.igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP arm64 specific version of memset() for user space, memset_user() In the __wr_after_init scenario, write-rare variables have: - a primary read-only mapping in kernel memory space - an alternate, writable mapping, implemented as user-space mapping The write rare implementation expects the arch code to privide a memset_user() function, which is currently missing. clear_user() is the base for memset_user() Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: Thiago Jung Bauermann CC: Ahmed Soliman CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/arm64/include/asm/uaccess.h | 9 +++++ arch/arm64/lib/Makefile | 2 +- arch/arm64/lib/memset_user.S (new) | 63 ++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 547d7a0c9d05..0094f92a8f1b 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -415,6 +415,15 @@ extern unsigned long __must_check __arch_copy_in_user(void __user *to, const voi #define INLINE_COPY_TO_USER #define INLINE_COPY_FROM_USER +extern unsigned long __must_check __arch_memset_user(void __user *to, int c, unsigned long n); +static inline unsigned long __must_check __memset_user(void __user *to, int c, unsigned long n) +{ + if (access_ok(to, n)) + n = __arch_memset_user(__uaccess_mask_ptr(to), c, n); + return n; +} +#define memset_user __memset_user + extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n); static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n) { diff --git a/arch/arm64/lib/Makefile b/arch/arm64/lib/Makefile index 5540a1638baf..614b090888de 100644 --- a/arch/arm64/lib/Makefile +++ b/arch/arm64/lib/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -lib-y := clear_user.o delay.o copy_from_user.o \ +lib-y := clear_user.o memset_user.o delay.o copy_from_user.o \ copy_to_user.o copy_in_user.o copy_page.o \ clear_page.o memchr.o memcpy.o memmove.o memset.o \ memcmp.o strcmp.o strncmp.o strlen.o strnlen.o \ diff --git a/arch/arm64/lib/memset_user.S b/arch/arm64/lib/memset_user.S new file mode 100644 index 000000000000..1bfbda3d112b --- /dev/null +++ b/arch/arm64/lib/memset_user.S @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * memset_user.S - memset for userspace on arm64 + * + * (C) Copyright 2018 Huawey Technologies Co. Ltd. + * Author: Igor Stoppa + * + * Based on arch/arm64/lib/clear_user.S + */ + +#include + +#include + + .text + +/* Prototype: int __arch_memset_user(void *addr, int c, size_t n) + * Purpose : set n bytes of user memory at "addr" to the value "c" + * Params : x0 - addr, user memory address to set + * : x1 - c, byte value + * : x2 - n, number of bytes to set + * Returns : number of bytes NOT set + * + * Alignment fixed up by hardware. + */ +ENTRY(__arch_memset_user) + uaccess_enable_not_uao x3, x4, x5 + // replicate the byte to the whole register + and x1, x1, 0xff + lsl x3, x1, 8 + orr x1, x3, x1 + lsl x3, x1, 16 + orr x1, x3, x1 + lsl x3, x1, 32 + orr x1, x3, x1 + mov x3, x2 // save the size for fixup return + subs x2, x2, #8 + b.mi 2f +1: +uao_user_alternative 9f, str, sttr, x1, x0, 8 + subs x2, x2, #8 + b.pl 1b +2: adds x2, x2, #4 + b.mi 3f +uao_user_alternative 9f, str, sttr, x1, x0, 4 + sub x2, x2, #4 +3: adds x2, x2, #2 + b.mi 4f +uao_user_alternative 9f, strh, sttrh, w1, x0, 2 + sub x2, x2, #2 +4: adds x2, x2, #1 + b.mi 5f +uao_user_alternative 9f, strb, sttrb, w1, x0, 0 +5: mov x0, #0 + uaccess_disable_not_uao x3, x4 + ret +ENDPROC(__arch_memset_user) + + .section .fixup,"ax" + .align 2 +9: mov x0, x3 // return the original size + ret + .previous