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: 10738159 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 149611399 for ; Wed, 19 Dec 2018 21:34:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 05357286B2 for ; Wed, 19 Dec 2018 21:34:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED71F286B4; Wed, 19 Dec 2018 21:34:11 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D646286B2 for ; Wed, 19 Dec 2018 21:34:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D3228E0019; Wed, 19 Dec 2018 16:34:09 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 256988E0001; Wed, 19 Dec 2018 16:34:09 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 122018E0019; Wed, 19 Dec 2018 16:34:09 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by kanga.kvack.org (Postfix) with ESMTP id 9BFE48E0001 for ; Wed, 19 Dec 2018 16:34:08 -0500 (EST) Received: by mail-lj1-f197.google.com with SMTP id t22-v6so5935902lji.14 for ; Wed, 19 Dec 2018 13:34:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:reply-to:mime-version :content-transfer-encoding; bh=zzEjOKOluUUWksw+oXnin6HXgBufUkueAJaYjC1prj4=; b=MGirugYsYM+kxQFed/PEAN6z5LAnKnY3bc+DRktYEje9J4HFIWIyEi88meomXynMW6 x6E30+9zzX6KbOMCasRTzpGi0Bc/BxL9BX34uRq588xTEwzSAna1aQOdve1OdIAYntQb HE2Qw7hqfNAztVji1dheUKfLie/gPOQDUBLGxszFrsvgB04xfAbxIukN78WVVv+eu3Gs K5W6SBQZNWNGKFDU4UYDc7nCdWuT9N7moMNkCsXyG5z/HaVMXy6SnGusKUd/IRd8fw9o qqPhT5LXzazdGjekFUXNWbcoIp0QghpKfTO7yZawCcFgSY7tqybZKpMOW2eczXMju1/w cGxw== X-Gm-Message-State: AA+aEWboadfYL9gbaf+NhoCbjZwGiWR/dKpnAztu/u00cuqeHdLaPNhg 93PxH6kjg8L3fss6bXsgOkIkH6/aatEAqkHc/iLZJ8Wq4YNk4gYNy1+UD1i52nk6GsFos9r8AJa p2o2mVUha5rgf6pLA7Bo1ffTAZxLapYb2zj+0kQvzQoxsFHWnB54ABflR/WE/vJY7KhkRJC0XOH lq0r1llqnS/g+KWg/YbiAdyABvGnJOXq5aI3dOJ8nWzZST8fn0w+Gu61dOHhMEgVi+fnLxHiqPA rMrcRy21NHWtuSsVBwhMDWsZ2FDPf/zBv3Kl3H+N2uCFiu2VNdCpUiVcyLunQuAQSGutqUjWd+H r53Oj5vYBrRk9Bb0Ay4nfF4PNFPmikVi70O0Ph7mkKQ8FVx9H8s+Jse0t/+kSstz0SB/MGvNJra E X-Received: by 2002:ac2:4215:: with SMTP id y21mr13364714lfh.6.1545255248033; Wed, 19 Dec 2018 13:34:08 -0800 (PST) X-Received: by 2002:ac2:4215:: with SMTP id y21mr13364689lfh.6.1545255247005; Wed, 19 Dec 2018 13:34:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545255246; cv=none; d=google.com; s=arc-20160816; b=l6yGS/NKrkImc7YdCvmi45BqfTMoiAyjuWS1+vYC/cX0ZedDDiSVHbYl9vjNvUi3ah HPoPQevSiRt93bWY71pWxLeyIQieGFgNvd3xlqfYGXNKzxZi84a234dsGYvMGENgE2LD 4X6wph1ljS3JTHmbwCfTtvYvQm/IvFgDb54AxDsv9yD+SGajDWnoGceU2SiKMjed3REB HQgdfj1GhMDX7XC/FP+jlKIEXqpNWB+dxVB3wr7hYWCzkiVujMMXKPUZo1m265JnGiKa S50/+WmkLdUMk3ZF1kUqKFzO/SJJwt/XfaTvThtQIN5J8Zz+1gC5tklXWiYUTZatMzBN Awvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=zzEjOKOluUUWksw+oXnin6HXgBufUkueAJaYjC1prj4=; b=RiSDFcPOwBf1ExW83IYRremU/cIUEI794f2TMw0quyEE3ExmE3lUq52vsGfIP2+2vT PZNu6k21ExcqVXHJ49g8Y1x2D1FezmVftZ8xSDtdrJCbRAGlwANCr6Kivqs4X2SF3bVM JMjI8zXaBSjQgMx2/nsG5qrPu5k+bPztydstdJmt0iIGTEEiv33/dlmd+WTOTc5qePOl M7knisjHl18yFsyCuPHC1fdk6OUbqHYHQ2qZo4VuHuOkH58WIPxc0oDacZQLuHJIUDAb dkOGxCJ/szmluvpbsbjSeSbi4X/P3g59ddRtKqlpVmA3EvdZ8v+NyttogfNBAAZMEHCv FpDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=CWCHozxt; spf=pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=igor.stoppa@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id u10sor5741072lfu.33.2018.12.19.13.34.06 for (Google Transport Security); Wed, 19 Dec 2018 13:34:06 -0800 (PST) Received-SPF: pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=CWCHozxt; spf=pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=igor.stoppa@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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-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) Received: from localhost.localdomain (dmhwpt3bffxn8z3-j6k-4.rev.dnainternet.fi. [2001:14bb:51:a4c8:5c24:24d7:ca5f:e7d2]) by smtp.gmail.com with ESMTPSA id v64sm3996867lfa.48.2018.12.19.13.34.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Dec 2018 13:34:05 -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> Reply-To: Igor Stoppa MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 */