Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp360951imm; Mon, 1 Oct 2018 11:04:28 -0700 (PDT) X-Google-Smtp-Source: ACcGV62QfelIavTDpSXPLTcZ9pQhqrAfNfRsn00+oQwMFtZ7SIrMII+thMS/djfTLyt54x/IPB39 X-Received: by 2002:a63:c306:: with SMTP id c6-v6mr2528069pgd.133.1538417068042; Mon, 01 Oct 2018 11:04:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538417068; cv=none; d=google.com; s=arc-20160816; b=fYGBTwUo1uOFkTcjsGZLYxVmoCROK9vBylo0fTXH29ppHrQRnKhmAooWk7ozuX3s8j IRXg08qvaDO7Ts8x3NJecwq1K290UdDLQS98NXBetjN3n01QGruPtCy1dkLNCW87FCzg lwA9Uo7GS5PvawTnJvAtFJqYyvOx73VKn4A3r7OQ5/+uoHMs/xFLDYmr58KrhZHDC4Vb 9MH1xdBpyd8ui075b/NuAkdfZHEEJvGXRhRMeTxXReJQk6vFMHVy5/lpzhV6MeS3RYO+ JOreyXfZm/2SvZH86TtB1HAu1mfd7iYcsswLcuaec69cODAejfto9Xk519khmZpojc0L vjLw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ijUdNdbgwzalLKcfY2p5LLIba6a37k6wFwoZbX38RZY=; b=Aow4OE6jqS+xTNza97jyfNNs4QHgpf/u322Mz7nzpjT48e4Fs8EnPRR/4RqRjNG5nL bDjlDdVkZA3mVwH/0qb+oMy9T2GQomtXLfBDdLxR+0UrOCmPc6RXdhT4Vv4E0BkW38r1 dwDmmf9DT0wN5FU/99JYGRfi4LqDEYM4Ksqe/OHvNtfQdzIVJJvoQhSOfJhlir2J49EH OuFBW5YT+LK6HKcDCtfQjiZYzME1pDrx5XTUfFVvhtZfhHjxnQCSTSOq1/ENpm3Iz5/y 3yP6iqKudiurpFhRYWgjFSPvpNdpDZX7gelUHz+t9An9R1i7QFoRNFq09HUNDwpWFML6 GS6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=GshEb5qY; 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=NONE dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z189-v6si13951926pfb.26.2018.10.01.11.04.07; Mon, 01 Oct 2018 11:04:28 -0700 (PDT) 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=@android.com header.s=20161025 header.b=GshEb5qY; 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=NONE dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726640AbeJBAmT (ORCPT + 99 others); Mon, 1 Oct 2018 20:42:19 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:36358 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725948AbeJBAmS (ORCPT ); Mon, 1 Oct 2018 20:42:18 -0400 Received: by mail-pf1-f194.google.com with SMTP id b7-v6so9802765pfo.3 for ; Mon, 01 Oct 2018 11:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ijUdNdbgwzalLKcfY2p5LLIba6a37k6wFwoZbX38RZY=; b=GshEb5qYf0a3DJm1Erm6SE3myXILl5hYsKhYDizWVJqBfGqtByl/nM+U7KLYOxVnYl lCIUdffTPGC2rC/EF9WhcW1smqOr49cepA6aI/JtM1zSQBvJHskvV70PoMtYcNkqE99z rdpxFpqKoSUzrN27SE/Rnr7DwUgpliOLdBIDSlDSOuwxECaxCiZnUu1qYA6MMdv4LTtB uaxFSXxEJ7jpgGU59OVvXvH7FX2HxWyXwL1q1uTaAX4Pp7OAdzLzjKLq3mmbfjn1Ra7P TB9DxjGVJeOBbYBgepCLrtxOYSpXy+gNOh/jqNhPEnqnTjjTWE6p6d1RAQDo3jTfTLTs bymg== 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:mime-version:content-transfer-encoding; bh=ijUdNdbgwzalLKcfY2p5LLIba6a37k6wFwoZbX38RZY=; b=FDPuIVmAgX51njNL0EUrElDeSSOLj2YCq47fix0Z0S/d5CO4oSVabx2+4Biy0qIpCs d32rZci+GHa6kL0eHBfw8hz6sGrDfqrBm5qcWv6Ox9XHRhjJzC1fPArfZ3wxIw/TorLs 1PD/sBmkFSYnAYYW6SidzLIM/KpoKAnsyz75q/3xgK2/bUImZWj7/9e3B+PmZtKemG4f 00i6UZgWKORRe9xsAvkZws7GCchcRx0K7NO3w8DxArTsHVh4CEVthrll28Huzh/VwFjV 4o0tIIP1U2w/6sprP06xfUmARktw5jNpRp6TY8z0px0KwilOid7DnuAK2/71DpNL/9Z3 X2mA== X-Gm-Message-State: ABuFfoi6bAUpfEeFHWqKIugNl9zByzU9LLcrVT7dbMZNkJS73wsOzjmR xYHM8C6z8m1dEoKpv16mUTNU4yPaW7M= X-Received: by 2002:a62:db46:: with SMTP id f67-v6mr12503273pfg.1.1538416999547; Mon, 01 Oct 2018 11:03:19 -0700 (PDT) Received: from nebulus.mtv.corp.google.com ([2620:0:1000:1612:b4fb:6752:f21f:3502]) by smtp.gmail.com with ESMTPSA id d186-v6sm24469261pfg.173.2018.10.01.11.03.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Oct 2018 11:03:18 -0700 (PDT) From: Mark Salyzyn To: linux-kernel@vger.kernel.org Cc: Kevin Brodsky , Mark Salyzyn , James Morse , Russell King , Catalin Marinas , Will Deacon , Andy Lutomirski , Dmitry Safonov , John Stultz , Mark Rutland , Laura Abbott , Kees Cook , Ard Biesheuvel , Andy Gross , Andrew Pinski , Thomas Gleixner , linux-arm-kernel@lists.infradead.org, Jeremy Linton , android-kernel@android.com, Dave Martin , Marc Zyngier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Yury Norov , "Eric W. Biederman" Subject: RESEND [PATCH v3 1/3] arm64: compat: Split the sigreturn trampolines and kuser helpers (C sources) Date: Mon, 1 Oct 2018 10:58:34 -0700 Message-Id: <20181001175845.168430-15-salyzyn@android.com> X-Mailer: git-send-email 2.19.0.605.g01d371f741-goog In-Reply-To: <20181001175845.168430-1-salyzyn@android.com> References: <20181001175845.168430-1-salyzyn@android.com> 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 From: Kevin Brodsky AArch32 processes are currently installed a special [vectors] page that contains the sigreturn trampolines and the kuser helpers, at the fixed address mandated by the kuser helpers ABI. Having both functionalities in the same page has become problematic, because: * It makes it impossible to disable the kuser helpers (the sigreturn trampolines cannot be removed), which is possible on arm. * A future 32-bit vDSO would provide the sigreturn trampolines itself, making those in [vectors] redundant. This patch addresses the problem by moving the sigreturn trampolines to a separate [sigpage] page, mirroring [sigpage] on arm. Even though [vectors] has always been a misnomer on arm64/compat, as there is no AArch32 vector there (and now only the kuser helpers), its name has been left unchanged, for compatibility with arm (there are reports of software relying on [vectors] being there as the last mapping in /proc/maps). mm->context.vdso used to point to the [vectors] page, which is unnecessary (as its address is fixed). It now points to the [sigpage] page (whose address is randomized like a vDSO). Signed-off-by: Kevin Brodsky Signed-off-by: Mark Salyzyn Tested-by: Mark Salyzyn Cc: James Morse Cc: Russell King Cc: Catalin Marinas Cc: Will Deacon Cc: Andy Lutomirski Cc: Dmitry Safonov Cc: John Stultz Cc: Mark Rutland Cc: Laura Abbott Cc: Kees Cook Cc: Ard Biesheuvel Cc: Andy Gross Cc: Andrew Pinski Cc: Thomas Gleixner Cc: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: Jeremy Linton Cc: android-kernel@android.com v2: - reduce churniness (and defer later to vDSO patches) - vectors_page and compat_vdso_spec as array of 2 - free sigpage if vectors allocation failed v3: - rebase --- arch/arm64/include/asm/processor.h | 4 +- arch/arm64/include/asm/signal32.h | 2 - arch/arm64/kernel/signal32.c | 5 +- arch/arm64/kernel/vdso.c | 82 ++++++++++++++++++++---------- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index 79657ad91397..bc6bb256ea4c 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -66,9 +66,9 @@ #define STACK_TOP_MAX TASK_SIZE_64 #ifdef CONFIG_COMPAT -#define AARCH32_VECTORS_BASE 0xffff0000 +#define AARCH32_KUSER_HELPERS_BASE 0xffff0000 #define STACK_TOP (test_thread_flag(TIF_32BIT) ? \ - AARCH32_VECTORS_BASE : STACK_TOP_MAX) + AARCH32_KUSER_HELPERS_BASE : STACK_TOP_MAX) #else #define STACK_TOP STACK_TOP_MAX #endif /* CONFIG_COMPAT */ diff --git a/arch/arm64/include/asm/signal32.h b/arch/arm64/include/asm/signal32.h index 81abea0b7650..58e288aaf0ba 100644 --- a/arch/arm64/include/asm/signal32.h +++ b/arch/arm64/include/asm/signal32.h @@ -20,8 +20,6 @@ #ifdef CONFIG_COMPAT #include -#define AARCH32_KERN_SIGRET_CODE_OFFSET 0x500 - int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set, struct pt_regs *regs); int compat_setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, diff --git a/arch/arm64/kernel/signal32.c b/arch/arm64/kernel/signal32.c index 24b09003f821..52f0d44417c8 100644 --- a/arch/arm64/kernel/signal32.c +++ b/arch/arm64/kernel/signal32.c @@ -398,14 +398,13 @@ static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka, retcode = ptr_to_compat(ka->sa.sa_restorer); } else { /* Set up sigreturn pointer */ + void *sigreturn_base = current->mm->context.vdso; unsigned int idx = thumb << 1; if (ka->sa.sa_flags & SA_SIGINFO) idx += 3; - retcode = AARCH32_VECTORS_BASE + - AARCH32_KERN_SIGRET_CODE_OFFSET + - (idx << 2) + thumb; + retcode = ptr_to_compat(sigreturn_base) + (idx << 2) + thumb; } regs->regs[0] = usig; diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index 8dd2ad220a0f..5398f6454ce1 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -1,5 +1,7 @@ /* - * VDSO implementation for AArch64 and vector page setup for AArch32. + * Additional userspace pages setup for AArch64 and AArch32. + * - AArch64: vDSO pages setup, vDSO data page update. + * - AArch32: sigreturn and kuser helpers pages setup. * * Copyright (C) 2012 ARM Limited * @@ -53,32 +55,51 @@ struct vdso_data *vdso_data = &vdso_data_store.data; /* * Create and map the vectors page for AArch32 tasks. */ -static struct page *vectors_page[1] __ro_after_init; +static struct page *vectors_page[] __ro_after_init; +static const struct vm_special_mapping compat_vdso_spec[] = { + { + /* Must be named [sigpage] for compatibility with arm. */ + .name = "[sigpage]", + .pages = &vectors_page[0], + }, + { + .name = "[kuserhelpers]", + .pages = &vectors_page[1], + }, +}; +static struct page *vectors_page[ARRAY_SIZE(compat_vdso_spec)] __ro_after_init; static int __init alloc_vectors_page(void) { extern char __kuser_helper_start[], __kuser_helper_end[]; - extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[]; + size_t kuser_sz = __kuser_helper_end - __kuser_helper_start; + unsigned long kuser_vpage; - int kuser_sz = __kuser_helper_end - __kuser_helper_start; - int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start; - unsigned long vpage; - - vpage = get_zeroed_page(GFP_ATOMIC); + extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[]; + size_t sigret_sz = + __aarch32_sigret_code_end - __aarch32_sigret_code_start; + unsigned long sigret_vpage; - if (!vpage) + sigret_vpage = get_zeroed_page(GFP_ATOMIC); + if (!sigret_vpage) return -ENOMEM; - /* kuser helpers */ - memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start, - kuser_sz); + kuser_vpage = get_zeroed_page(GFP_ATOMIC); + if (!kuser_vpage) { + free_page(sigret_vpage); + return -ENOMEM; + } /* sigreturn code */ - memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET, - __aarch32_sigret_code_start, sigret_sz); + memcpy((void *)sigret_vpage, __aarch32_sigret_code_start, sigret_sz); + flush_icache_range(sigret_vpage, sigret_vpage + PAGE_SIZE); + vectors_page[0] = virt_to_page(sigret_vpage); - flush_icache_range(vpage, vpage + PAGE_SIZE); - vectors_page[0] = virt_to_page(vpage); + /* kuser helpers */ + memcpy((void *)kuser_vpage + 0x1000 - kuser_sz, __kuser_helper_start, + kuser_sz); + flush_icache_range(kuser_vpage, kuser_vpage + PAGE_SIZE); + vectors_page[1] = virt_to_page(kuser_vpage); return 0; } @@ -87,23 +108,32 @@ arch_initcall(alloc_vectors_page); int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp) { struct mm_struct *mm = current->mm; - unsigned long addr = AARCH32_VECTORS_BASE; - static const struct vm_special_mapping spec = { - .name = "[vectors]", - .pages = vectors_page, - - }; + unsigned long addr; void *ret; if (down_write_killable(&mm->mmap_sem)) return -EINTR; - current->mm->context.vdso = (void *)addr; + addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0); + if (IS_ERR_VALUE(addr)) { + ret = ERR_PTR(addr); + goto out; + } - /* Map vectors page at the high address. */ ret = _install_special_mapping(mm, addr, PAGE_SIZE, - VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC, - &spec); + VM_READ|VM_EXEC| + VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, + &compat_vdso_spec[0]); + if (IS_ERR(ret)) + goto out; + current->mm->context.vdso = (void *)addr; + + /* Map the kuser helpers at the ABI-defined high address. */ + ret = _install_special_mapping(mm, AARCH32_KUSER_HELPERS_BASE, + PAGE_SIZE, + VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC, + &compat_vdso_spec[1]); +out: up_write(&mm->mmap_sem); return PTR_ERR_OR_ZERO(ret); -- 2.19.0.605.g01d371f741-goog