Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2215849imu; Sat, 22 Dec 2018 15:37:43 -0800 (PST) X-Google-Smtp-Source: AFSGD/UpVkrrjiA0rVQ49p1DRigoqE9ZEEc9JuGRkuRpP34WwpnzxtQx7NSOizevBQslsice2yjJ X-Received: by 2002:a62:d701:: with SMTP id b1mr7750955pfh.34.1545521863639; Sat, 22 Dec 2018 15:37:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545521863; cv=none; d=google.com; s=arc-20160816; b=0TU1jdqNUy6IVTpkylFus/Oja8Uoui9NWyhDXRws+21vOM1epK1W0aLZq7dCKGSbS0 v1pkL2Src63PH2TdbOSDIVzGPoQEyGlWYRtgV0X+LNOPRiyKD7tzGIQXzI5IMTCrVwCM 4EwqqEa78M2oibOEqQCr7GvuSo1xYW9kQ+DKND0K9OQvP+5TxbqqnO4DnTGMzmpOxI7p h4yk+FNJULO4dOzOQ8Qx8lS5tLWWYKVQiEj2BtzBM1shmAkuQD+ZEZ2UiHeFe0yrl9Zg XYwk566t0Lho6pmz+xA+zEu/VsCNidrQlhI4co4V3qXe2JLGm0KLe/BZY0/vtKXOhrh4 OCvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :reply-to:references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9pkoDpWnQ0hyqDvaxlvdmZrME9c2qqzlc2zOv6te5xY=; b=J6IJCEb+qjS5P0m4vRhl3JOPE9Ut8wHQKmZko1loa0I6tCq5RLwNQXoag0oPbT3kf/ 03PK1mcLXe7eEkUsWCRpUroP5Lty2ZlIuk4rs9wohGEkWw8WSwOsK5ChNF/Vhg5box13 6JpR+3omyTyOeKuCOqIK22+mILlwXw4nCWtydpUc+IiyJP+bQW46+jUhIegGG8x2Hh05 bnChnnZrzxvCufpTycd89A4hd7oXiRMk/VgcXJxA1A5WumSVmPGoDccyKEjbDoQ6sXnL NS5zL2OTngg4Mp+Qo8yK/hsKhafwC8TmX3H1OlBFeorS/BRO1dJ9rRYJyQBmGpq3C4pR LmWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=N2niqEom; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c3si24692351pgi.370.2018.12.22.15.36.57; Sat, 22 Dec 2018 15:37:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=N2niqEom; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404234AbeLUSOt (ORCPT + 99 others); Fri, 21 Dec 2018 13:14:49 -0500 Received: from mail-lj1-f193.google.com ([209.85.208.193]:44811 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725814AbeLUSOs (ORCPT ); Fri, 21 Dec 2018 13:14:48 -0500 Received: by mail-lj1-f193.google.com with SMTP id k19-v6so5507732lji.11; Fri, 21 Dec 2018 10:14:47 -0800 (PST) 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-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=9pkoDpWnQ0hyqDvaxlvdmZrME9c2qqzlc2zOv6te5xY=; b=NVqXG+BFhbt2A/vL/JEC4ALdmu2D2r700TDJa9iSSW1qcdLJhxErj1RdQLZ0LkvJgs PAnUjfO3XSB15XyGSrKb1WXFFdAEw8LKdMHvJ7wOqh0F9dpdrFo3J7OIuAieA/606Ezz FWaLt9zWHQs4nLeY8vWdr7RUK9GIUo9nxeORrKnhOQcctvBg/R4jg8aHOPW/bUzRF0wK /6xyjQB2PHgKk5Igu5kB9s3JkPkqTsd3mTjRCge2yfwYtaPffrgSmIu+1RS4PKE6inyN x6OEdMbYOFonv+gQnBgRX3rvMUTCU5g6IH51S/BRZWMyXhf9//OUgNmXZR6qD64Nzrfa VOFA== X-Gm-Message-State: AJcUukfF+KmPbk8Sh/UETMumFGzhH7+grPDRBuQdxuxGihhqx5U46CAC EAHVrhU7EXbfZIRpOdnnwkA= 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 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 */ -- 2.19.1