Received: by 10.223.185.116 with SMTP id b49csp1035704wrg; Wed, 21 Feb 2018 10:58:13 -0800 (PST) X-Google-Smtp-Source: AH8x224qc8jQMJqrCECjN1AYHBwrXWnjgVF2xemVgmZ5Da2mPS8PWO3g0qmu8z8xCQok3xZI2R3m X-Received: by 10.101.81.2 with SMTP id f2mr3444687pgq.361.1519239493457; Wed, 21 Feb 2018 10:58:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519239493; cv=none; d=google.com; s=arc-20160816; b=T22uW9tSi4z2Z29n0l1s55g28d91arnaf3zmt/FLn/3uOAcyuOUQmBh+iCvfTbsgP2 6Z86lPbPWGSpC2LgLROA9ESU/qAmw7P7PbpmsaqDTHevQhue/rP9mmHfILS742IZNhp9 uEZ4dtn89xFiGIcACrvaqW31vZ3/V4LX4+IjvHJCAPku/pkqgwFfPCEq/flCH1SZOLC+ J1hWAo8U3ugR63wvroCU56hQ64+1X81oUc6QjxuMlSVY2t37I8DQNnoEtceavkZCHBZW tT8KmtXOB0mbnLcl7q9Z9+q3yS2AU9N7BZJq9i6TgS136cfAc116QjdAsbS6bWTm33ZN Zcbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=FksaBsgauULptsajEQ/36336sUB2iNvu7aKDkQaajfQ=; b=K0Zmoa8+y79XuYwqpxXzOiXxEcg6Xit68isFjw1Z2P3c2RktlhtJ3r5+3d+7jjZTlO RFIXVJHJWenMsbigKHq4un4un7q8FqCbT0zXXLTNO5zlOKAIewIvhibDu1eGl+xjnd2A VxAqnM9ZwWf+dxSbF8k0OREMktXIeevET6JTWu2849MHwMwrqmDGj5bfU6fWXYjfZyBf dHEVspGcjK4OcRdWcPDJ3AQd1t+POYQ8fN9C9Xyl+3MMHkoUByoA2PcUiLI2U8Yke9K4 pKyn3DaQtzo0M6UO3JXUZKoLatZtZIzSi5RTmRlDzlw2ggF4MDFgCBjOigYcjsnZN+pZ /B+w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n16-v6si12133476pll.360.2018.02.21.10.57.59; Wed, 21 Feb 2018 10:58:13 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938269AbeBUPjD (ORCPT + 99 others); Wed, 21 Feb 2018 10:39:03 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:56992 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935612AbeBUPjC (ORCPT ); Wed, 21 Feb 2018 10:39:02 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0A15180D; Wed, 21 Feb 2018 07:39:02 -0800 (PST) Received: from salmiak (usa-sjc-mx-foss1.foss.arm.com [217.140.101.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C4F9F3F53D; Wed, 21 Feb 2018 07:38:59 -0800 (PST) Date: Wed, 21 Feb 2018 15:38:51 +0000 From: Mark Rutland To: Laura Abbott Cc: Alexander Popov , Kees Cook , Ard Biesheuvel , kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] arm64: Clear the stack Message-ID: <20180221153850.ywpzsigfnz3etoun@salmiak> References: <20180221011303.20392-1-labbott@redhat.com> <20180221011303.20392-3-labbott@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180221011303.20392-3-labbott@redhat.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Laura, On Tue, Feb 20, 2018 at 05:13:03PM -0800, Laura Abbott wrote: > Implementation of stackleak based heavily on the x86 version Neat! > diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S > index ec2ee720e33e..b909b436293a 100644 > --- a/arch/arm64/kernel/entry.S > +++ b/arch/arm64/kernel/entry.S > @@ -401,6 +401,11 @@ tsk .req x28 // current thread_info > > .text > > + .macro erase_kstack > +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK > + bl __erase_kstack > +#endif > + .endm > /* > * Exception vectors. > */ > @@ -901,6 +906,7 @@ work_pending: > */ > ret_to_user: > disable_daif > + erase_kstack I *think* this should happen in finish_ret_to_user a few lines down, since we can call C code if we branch to work_pending, dirtying the stack. > ldr x1, [tsk, #TSK_TI_FLAGS] > and x2, x1, #_TIF_WORK_MASK > cbnz x2, work_pending > @@ -1337,3 +1343,105 @@ alternative_else_nop_endif > ENDPROC(__sdei_asm_handler) > NOKPROBE(__sdei_asm_handler) > #endif /* CONFIG_ARM_SDE_INTERFACE */ > + > +/* > + * This is what the stack looks like > + * > + * +---+ <- task_stack_page(p) + THREAD_SIZE > + * | | > + * +---+ <- task_stack_page(p) + THREAD_START_SP > + * | | > + * | | > + * +---+ <- task_pt_regs(p) THREAD_START_SP got killed off in commit 34be98f4944f9907 as part of the VMAP_STACK rework, so this can be: +---+ <- task_stack_page(p) + THREAD_SIZE | | | | +---+ <- task_pt_regs(p) ... > + * | | > + * | | > + * | | <- current_sp > + * ~~~~~ > + * > + * ~~~~~ > + * | | <- lowest_stack > + * | | > + * | | > + * +---+ <- task_stack_page(p) > + * > + * This function is desgned to poison the memory between the lowest_stack > + * and the current stack pointer. After clearing the stack, the lowest > + * stack is reset. > + */ > + > +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK > +ENTRY(__erase_kstack) > + mov x10, x0 // save x0 for the fast path AFAICT, we only call this from ret_to_user, where x0 doesn't need to be preserved. Is that for ret_fast_syscall? In some cases, ret_fast_syscall can bypass ret_to_user and calls kernel_exit directly, so we might need a call there. > + > + get_thread_info x0 > + ldr x1, [x0, #TSK_TI_LOWEST_STACK] > + > + /* get the number of bytes to check for lowest stack */ > + mov x3, x1 > + and x3, x3, #THREAD_SIZE - 1 > + lsr x3, x3, #3 > + > + /* generate addresses from the bottom of the stack */ > + mov x4, sp > + movn x2, #THREAD_SIZE - 1 > + and x1, x4, x2 Can we replace the MOVN;AND with a single instruction to clear the low bits? e.g. mov x4, sp bic x1, x4, #THREAD_SIZE - 1 ... IIUC BIC is an alias for the bitfield instructions, though I can't recall exactly which one(s). > + > + mov x2, #STACKLEAK_POISON > + > + mov x5, #0 > +1: > + /* > + * As borrowed from the x86 logic, start from the lowest_stack > + * and go to the bottom to find the poison value. > + * The check of 16 is to hopefully avoid false positives. > + */ > + cbz x3, 4f > + ldr x4, [x1, x3, lsl #3] > + cmp x4, x2 > + csinc x5, xzr, x5, ne > + tbnz x5, #STACKLEAK_POISON_CHECK_DEPTH/4, 4f // found 16 poisons? > + sub x3, x3, #1 > + b 1b > + > +4: > + /* total number of bytes to poison */ > + add x5, x1, x3, lsl #3 > + mov x4, sp > + sub x8, x4, x5 > + > + cmp x8, #THREAD_SIZE // sanity check the range > + b.lo 5f > + ASM_BUG() > + > +5: > + /* > + * We may have hit a path where the stack did not get used, > + * no need to do anything here > + */ > + cbz x8, 7f > + > + sub x8, x8, #1 // don't poison the current stack pointer > + > + lsr x8, x8, #3 > + add x3, x3, x8 > + > + /* > + * The logic of this loop ensures the last stack word isn't > + * ovewritten. > + */ Is that to ensure that we don't clobber the word at the current sp value? > +6: > + cbz x8, 7f > + str x2, [x1, x3, lsl #3] > + sub x3, x3, #1 > + sub x8, x8, #1 > + b 6b > + > + /* Reset the lowest stack to the top of the stack */ > +7: > + mov x1, sp > + str x1, [x0, #TSK_TI_LOWEST_STACK] > + > + mov x0, x10 > + ret > +ENDPROC(__erase_kstack) > +#endif [...] > diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile > index 7b3ba40f0745..35ebbc1b17ff 100644 > --- a/drivers/firmware/efi/libstub/Makefile > +++ b/drivers/firmware/efi/libstub/Makefile > @@ -20,7 +20,8 @@ cflags-$(CONFIG_EFI_ARMSTUB) += -I$(srctree)/scripts/dtc/libfdt > KBUILD_CFLAGS := $(cflags-y) -DDISABLE_BRANCH_PROFILING \ > -D__NO_FORTIFY \ > $(call cc-option,-ffreestanding) \ > - $(call cc-option,-fno-stack-protector) > + $(call cc-option,-fno-stack-protector) \ > + $(DISABLE_STACKLEAK_PLUGIN) I believe the KVM hyp code will also need to opt-out of this. Thanks, Mark.