From patchwork Fri Dec 21 18:14:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10740809 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 44EFD13AD for ; Fri, 21 Dec 2018 18:14:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 37D892835B for ; Fri, 21 Dec 2018 18:14:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 292F328618; Fri, 21 Dec 2018 18:14:52 +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=unavailable 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 0F9702835B for ; Fri, 21 Dec 2018 18:14:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6BB88E0003; Fri, 21 Dec 2018 13:14:49 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id CF2C88E0001; Fri, 21 Dec 2018 13:14:49 -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 BE19B8E0003; Fri, 21 Dec 2018 13:14:49 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by kanga.kvack.org (Postfix) with ESMTP id 50DF28E0001 for ; Fri, 21 Dec 2018 13:14:49 -0500 (EST) Received: by mail-lj1-f199.google.com with SMTP id g12-v6so1905065lji.3 for ; Fri, 21 Dec 2018 10:14:49 -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=9pkoDpWnQ0hyqDvaxlvdmZrME9c2qqzlc2zOv6te5xY=; b=nyIa6Oha7bR0uNnzEEnGN1n9hjNuqerd+czl57WHgdzXinMwSF0Z9dlPu5ZGcX9o4V s9PKIRm4uXYbxN9BLnKKiw7urP/wjmU9+0gh4xI1susgWoNyJM7s2guA0EDSRv8KWNMj pLBfGVar7SSxgZbVypf+pXFX+lO69tH6wPCKYQsBnRvWNu/G/aNEPpKLcjqfI4zNWae8 ZrqbZtJL5LnpQj8hC7MkeWFkk4BcchDWNhR04PHf3wXgQnP48GZHOQ2lKtEVZGhKdZ85 +ccJDMVuXJW0dIYbU/uEUjchocbovASbm0OkLAmCIp9chERUElDw4lkrozw0X2QCAlvv qpZQ== X-Gm-Message-State: AJcUuke9XmMd0c5dLLd+y0AbBkgCaV7wtJ2zAerSAPG8zpEGz7fLSswI +lTtY5ha9CO3b7DIQSVFBEHcTcUFF3vR7deBhm+u7X1tuGMBibZSskaaQJN1imzonyJC3WhOGJn 3hI3bRJ2+rKE0XKgI+bHXyWUBTIBbhrNR5TRSNqP6hAibEs0SPEj1EmEQWTS6Ifj44BB3CKa75H WsuWBvBsuN0iYkuSxYao9UYQ+QLyagcUA9lOSmeuihYVmogykmjrlvjgJO+pTQLl6bFo4sPpCpk NbQDxnqqgHAz/gtjEApwsdNIYXq0Dy7X9WI79bgZMu8Fg6J4/ITOBZATers2Dy0T8Ehe2ywADj8 y1s5eSHSjdGM75Ftu9ODDjiRCtkJOdeDDnHQTssS4eF38NCbHcYBcSBQJDdZIFfyoeLRgcvdkBA H X-Received: by 2002:a2e:2b85:: with SMTP id r5-v6mr2216835ljr.91.1545416088450; Fri, 21 Dec 2018 10:14:48 -0800 (PST) X-Received: by 2002:a2e:2b85:: with SMTP id r5-v6mr2216792ljr.91.1545416087015; Fri, 21 Dec 2018 10:14:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545416086; cv=none; d=google.com; s=arc-20160816; b=V4f8Pml0nFBTudqUGxR9ZXzZOiNoZlf9c6AuO0RCzy/KuorFlLupCdRQFEAtShbkCC Qfo6Rr8GXvZGCwTyQ0GS9JoZVmDeapgR20TBoE/T+Eo+dSe7slBJBfGhuA/KKIOLfdiI yFqeIieYhlyB7hqLDTHKSqqjVMldJrRW0Cc0wcg9f9VVV0Q+aCxdKlJYz65WJYoLgYTH QEAjJ1+4DMaS42VYgYFTPiggOlbjeM2CUya6J8fVlm2hPFSADdFGbXquHITKOaL7wZJZ TNiZHr2ryI3EiZKFk/m3pjAjdGsKc0s1TmhlRNt5shzPrhNjZwAAEbLTPgeFAwWOt5rT FIog== 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=9pkoDpWnQ0hyqDvaxlvdmZrME9c2qqzlc2zOv6te5xY=; b=MGdXG/WEyWWnsFMH3yg8u7P2m9hXDcAGRT0wa8gVJeOzi3NQOjbmWjTZoKW9ujlQu6 WR2iQqVE2uYBh3lAaswc3uORn/MFRfTfQQlykCAusVeMqHFCPF/fV1AVAMb/acXKLaOK SMpRZurwdHaRvvScDI4VvSkoS/RJDhgfCbdUFxYtv0Xlau3FijcUIA7rs9Lot4OEhzLa iMzQeHpfvdUi4jVFzhJeK9VUXhEizEZkqROA9A0SByZe3dK2n2NtHj3DgPZPhjFZF7rt cGIsP4rbgXiMQRtwnbll8rpnLM0ZFrLV9UeCyxau/Y2AAfFA//KuI+xzaNEEfMJrBUkC TzBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=N2niqEom; 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 a66-v6sor16166926ljf.31.2018.12.21.10.14.46 for (Google Transport Security); Fri, 21 Dec 2018 10:14:46 -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=N2niqEom; 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=9pkoDpWnQ0hyqDvaxlvdmZrME9c2qqzlc2zOv6te5xY=; b=N2niqEomFo+rals4bkw3xGbTBjGwxa/HjF9TepnB4pLUjTMQIoZ7v9NVFAHvYtA9gY /0oYQRkwRCEM/u8qr8aLfkPZbFq08vKAZrJbeRw+uPsmd1krMueHpwqVSWjWPyHJzZaw PZNRYzHlOXJQisYA87u0/ENAQ4P71tLBmcdqbPDndtRNBd4/Iko2g/ZGDVNsdjKMAj6O HEe4/wIgfiQh4kQzYdNojImGEYlQXTiGQwU/CvMINRzDx+6hF2dWBiUbCqC8PVrSR4RO zs+3oyb3DLgLu8gImsLRxllVJm3T8bTtcMSk7ZqGUzsimoakWJXB9iCFE24/c/a4ekVm 7ZRQ== X-Google-Smtp-Source: AFSGD/XIkFyHzT2h0WZYCzCstedNVsVpCOdIZJXFreII8gK7lJaMNaWsCWFp0aHljWPuf+HZlBsPEQ== X-Received: by 2002:a2e:908b:: with SMTP id l11-v6mr2285752ljg.150.1545416086374; Fri, 21 Dec 2018 10:14:46 -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 m63-v6sm5444564lje.81.2018.12.21.10.14.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Dec 2018 10:14:45 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar , Thiago Jung Bauermann Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , Ahmed Soliman , 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: Fri, 21 Dec 2018 20:14:12 +0200 Message-Id: <20181221181423.20455-2-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181221181423.20455-1-igor.stoppa@huawei.com> References: <20181221181423.20455-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: 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/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 */