From patchwork Wed Dec 19 21:33:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738169 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 08B6F14E5 for ; Wed, 19 Dec 2018 21:34:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF66F286B2 for ; Wed, 19 Dec 2018 21:34:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E125B28700; Wed, 19 Dec 2018 21:34:27 +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 EF12E286B2 for ; Wed, 19 Dec 2018 21:34:26 +0000 (UTC) Received: (qmail 30193 invoked by uid 550); 19 Dec 2018 21:34:18 -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 30108 invoked from network); 19 Dec 2018 21:34:17 -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=zzEjOKOluUUWksw+oXnin6HXgBufUkueAJaYjC1prj4=; b=CWCHozxtXPaF/R34zIBV9kgAuN1gg0PddkrPC5aWleCfLISFSDUnCKYlt1SLfa1Qdf AbBcVlgs7IbX/SocNOSs7TJQFHB47Mb19vHQatLapdRgx2CO4a1kc145mUHuA0wziXHH QzZ8yM8QB4lZEUShcVat6wzXvLInx64kvRel1qGAK2FWOTDIZ18UCt14KqSiQ4ShML9x rAO14BDpxcuPoIQYRNZAzrNtOpMAw7VGp3iOilzQ4tI6o+kS0TZkiyLu0KCkKArZ5UO2 dPjUBiO0nyOIiEfyvkENuq9RLsdpeA4AjYW6bWLilccCTnnGGCpqqA/Quth/AjuruGh3 tVQg== 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=zzEjOKOluUUWksw+oXnin6HXgBufUkueAJaYjC1prj4=; b=bvCJXLU2lS4QWXWbHwy7J3rX0NVg5sZKB3Z/K3Tgy05/d+byEePuDCAVMNCvtKrIyV 8iF3Przt3qrLsB3I+wtexSUuEcURH78bEd8BJMfr/0aLIbTY9vzRoJ/9DIEhuEyMi7X0 acZTb6qdAsVncB2bGopNUnyaKNmYe9nuVZbZDL5+ff2hAFFHz579dCMQgXo6suSYjtFS 4cJorpITPzl1E2n+mZ3Mu52KIalfiH5iLMNO6HOvj5uGz3sAhiDaf4dNb5SyYNEp9+jc SKdwNopEdl6zqOhs5RHvjJWUNK7v+m9c8d/FG5kHMOOLiSeYbl/GxGBI8TYnJtZ+wapC MXBQ== X-Gm-Message-State: AA+aEWYaxq32Q3bawkHp4XtFyW1gH+vLriMomr+Yl53wmg5C/ddzELKf LG6MVz5bx7KC7oFaPB//sh0= X-Google-Smtp-Source: AFSGD/WxAkE6ImfOjvKiqC6CvFghm5iWxm8HjnpQw4GJB94Y1GY0BzHPXMXipEe8534UoMRhUUvpBw== X-Received: by 2002:a19:6514:: with SMTP id z20mr6123044lfb.31.1545255246483; Wed, 19 Dec 2018 13:34:06 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 01/12] x86_64: memset_user() Date: Wed, 19 Dec 2018 23:33:27 +0200 Message-Id: <20181219213338.26619-2-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Create x86_64 specific version of memset for user space, based on clear_user(). This will be used for implementing wr_memset() in the __wr_after_init scenario, where write-rare variables have an alternate mapping for writing. 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: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/x86/include/asm/uaccess_64.h | 6 ++++ arch/x86/lib/usercopy_64.c | 54 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/arch/x86/include/asm/uaccess_64.h b/arch/x86/include/asm/uaccess_64.h index a9d637bc301d..f194bfce4866 100644 --- a/arch/x86/include/asm/uaccess_64.h +++ b/arch/x86/include/asm/uaccess_64.h @@ -213,4 +213,10 @@ copy_user_handle_tail(char *to, char *from, unsigned len); unsigned long mcsafe_handle_tail(char *to, char *from, unsigned len); +unsigned long __must_check +memset_user(void __user *mem, int c, unsigned long len); + +unsigned long __must_check +__memset_user(void __user *mem, int c, unsigned long len); + #endif /* _ASM_X86_UACCESS_64_H */ diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c index 1bd837cdc4b1..84f8f8a20b30 100644 --- a/arch/x86/lib/usercopy_64.c +++ b/arch/x86/lib/usercopy_64.c @@ -9,6 +9,60 @@ #include #include +/* + * Memset Userspace + */ + +unsigned long __memset_user(void __user *addr, int c, unsigned long size) +{ + long __d0; + unsigned long pattern = 0; + int i; + + for (i = 0; i < 8; i++) + pattern = (pattern << 8) | (0xFF & c); + might_fault(); + /* no memory constraint: gcc doesn't know about this memory */ + stac(); + asm volatile( + " movq %[val], %%rdx\n" + " testq %[size8],%[size8]\n" + " jz 4f\n" + "0: mov %%rdx,(%[dst])\n" + " addq $8,%[dst]\n" + " decl %%ecx ; jnz 0b\n" + "4: movq %[size1],%%rcx\n" + " testl %%ecx,%%ecx\n" + " jz 2f\n" + "1: movb %%dl,(%[dst])\n" + " incq %[dst]\n" + " decl %%ecx ; jnz 1b\n" + "2:\n" + ".section .fixup,\"ax\"\n" + "3: lea 0(%[size1],%[size8],8),%[size8]\n" + " jmp 2b\n" + ".previous\n" + _ASM_EXTABLE_UA(0b, 3b) + _ASM_EXTABLE_UA(1b, 2b) + : [size8] "=&c"(size), [dst] "=&D" (__d0) + : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr), + [val] "ri"(pattern) + : "rdx"); + + clac(); + return size; +} +EXPORT_SYMBOL(__memset_user); + +unsigned long memset_user(void __user *to, int c, unsigned long n) +{ + if (access_ok(VERIFY_WRITE, to, n)) + return __memset_user(to, c, n); + return n; +} +EXPORT_SYMBOL(memset_user); + + /* * Zero Userspace */