Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3479195ybb; Mon, 6 Apr 2020 09:42:29 -0700 (PDT) X-Google-Smtp-Source: APiQypLM5R502atpyfjuh9Mn9BSHhEOjrMeTANREIqZryD7DGJm1bnQNat7E9YSGV/oAPsbW8Xzj X-Received: by 2002:a05:6808:4d1:: with SMTP id a17mr129112oie.39.1586191349283; Mon, 06 Apr 2020 09:42:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586191349; cv=none; d=google.com; s=arc-20160816; b=0ERUPsdsqsQQJECbFIxDKkT6rLc+L5jAoYp7hTSGNscrRsDxS1AxPBW/I3ht5Ajzfa qwuf/pu/9N/lbqzvJLp54+8WD1coHAlo+aoJ2N4bMN8S1Libaq0ByAkgWGI4RZP4ubvK Tuau/L4VejwOu+pay1NwM5yEh+dPsZSYZqsgdm6JtXzqoHN80/sIHv5FdIhFF9ASJOCQ Ep+kj7oPZDYKn2rOhUQLc/QObMGWAPLpfxnIwZYRrSxHQ7W0HF0a9UatVtuKgZRgZ5Nt YqBBv9VAdYoWJxSkowTDlkZaHYFUHz0TesTnvz5jWoGQZdjhuijn5mBOCAg5/VQWmWMc 1SeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=dVFwXUGExu3yMa+UTv67d2nterjPqRkmOOvIg26dlAo=; b=NpngfQ8TnwJ4qgKwQyuh8I7bwYU6p1n7GPRGNGk0e08HOlBIliea5clQP4bGtDFZyE M8F0B39Y6i3xXGh12ey0T3vOAjvDlWzlFERtkmCqsa8YTtIoglRqj7e2tDoFrrmiwoiB aWlimDsrNzIjwzmIl6do8Gq6slv8Wljwd9OaxKif6EqaEu4tvzu5QX3nij/lEr8dYQOJ 0mSlDBTXI1EkzWDY393lwgdi/KVO7waHYKUMV54X2Oslf+uhT+TeASHpbhKnDcpauIa5 Hr1M7WMTZmDWOpJryemDF55gKKLdBZWObMgPQx3r0Jt0j1PL12YctKohugAy+Y8rOX/r UZRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=b0BIjk7a; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z139si23632ooa.70.2020.04.06.09.42.16; Mon, 06 Apr 2020 09:42:29 -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=@google.com header.s=20161025 header.b=b0BIjk7a; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729539AbgDFQle (ORCPT + 99 others); Mon, 6 Apr 2020 12:41:34 -0400 Received: from mail-qt1-f201.google.com ([209.85.160.201]:41395 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728962AbgDFQld (ORCPT ); Mon, 6 Apr 2020 12:41:33 -0400 Received: by mail-qt1-f201.google.com with SMTP id w3so277653qtc.8 for ; Mon, 06 Apr 2020 09:41:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=dVFwXUGExu3yMa+UTv67d2nterjPqRkmOOvIg26dlAo=; b=b0BIjk7azRvv07MTUoifqmMIFXJfm2PVhLzKAFxXBN4iGo1ayrT7YNfDZRd5C2Huf8 jk/fDzDUVFYX9V3NcfVGt6im04bxYhSZpDcu80cuBVgwiwvmtPFBa0Rck96uf2na2VLA JeSPovShITQljVBCHuEs8kHt3KW7TvIRQdl+QhB7bKc944cZETdeYnZEXvqhB1YNkWDl O4vidgOSM8GQ5Vhnk8cmiZjPmGhxUfv1d1MFjhNFqie5Vso2ozFmMph7Y3XkgWsrnsRG Kmf5SeuyQ0zuPuyxfnuXE91N86AE64/swhIbGS60sQwuuTe0GGgTPJj/dCtjlKDn1rpE Q3qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dVFwXUGExu3yMa+UTv67d2nterjPqRkmOOvIg26dlAo=; b=iSumERqBHff72g7/d/36/1yjx+WlIJqyBRGvGQC12JSNx6pLLmnYVH+K1+c40KbRPo ek1bKw2M4f3OZMNCVPHOV3Qp4cX1VZ0ehPUuMhDPQ+7ItRXvvdTb46mDwJUhAKs4visl LArYMakf0DZqK2HmvCvNST86ZxS+kF5wY2MKqfoSNEXJim3nJA5YkU0oVBwZWsBqPrXo nq6souyqb6C/FZAg7eMFhAIbentQDU7EJfmpQKXZkBmHngSo2vCCx+/5Dtr3jlNefU0m 7oD8Hjkn7RTt53ih7lYvbBTvxj1ll9P+JPfsXkB1jriBYI6UXvIXX4am6+P7ZcGeNwGT sp8w== X-Gm-Message-State: AGi0PuYHFZ3HxdRSvPF56cgYzmD2LUYCnVjAGxH+QcwYH4+s8qsyVCKJ oA6/wc2+Y5yclWohtRSFeKURwYlNjXWTMKYp2qU= X-Received: by 2002:ac8:7351:: with SMTP id q17mr247532qtp.237.1586191290072; Mon, 06 Apr 2020 09:41:30 -0700 (PDT) Date: Mon, 6 Apr 2020 09:41:09 -0700 In-Reply-To: <20191018161033.261971-1-samitolvanen@google.com> Message-Id: <20200406164121.154322-1-samitolvanen@google.com> Mime-Version: 1.0 References: <20191018161033.261971-1-samitolvanen@google.com> X-Mailer: git-send-email 2.26.0.292.g33ef6b2f38-goog Subject: [PATCH v10 00/12] add support for Clang's Shadow Call Stack From: Sami Tolvanen To: Will Deacon , Catalin Marinas , James Morse , Steven Rostedt , Masami Hiramatsu , Ard Biesheuvel , Mark Rutland Cc: Dave Martin , Kees Cook , Laura Abbott , Marc Zyngier , Nick Desaulniers , Jann Horn , Miguel Ojeda , Masahiro Yamada , clang-built-linux@googlegroups.com, kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sami Tolvanen Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch series adds support for Clang's Shadow Call Stack (SCS) mitigation, which uses a separately allocated shadow stack to protect against return address overwrites. More information can be found here: https://clang.llvm.org/docs/ShadowCallStack.html SCS provides better protection against traditional buffer overflows than CONFIG_STACKPROTECTOR_*, but it should be noted that SCS security guarantees in the kernel differ from the ones documented for user space. The kernel must store addresses of shadow stacks used by inactive tasks and interrupt handlers in memory, which means an attacker capable reading and writing arbitrary memory may be able to locate them and hijack control flow by modifying shadow stacks that are not currently in use. SCS is currently supported only on arm64, where the compiler requires the x18 register to be reserved for holding the current task's shadow stack pointer. With -fsanitize=shadow-call-stack, the compiler injects instructions to all non-leaf C functions to store the return address to the shadow stack, and unconditionally load it again before returning. As a result, SCS is currently incompatible with features that rely on modifying function return addresses in the kernel stack to alter control flow. A copy of the return address is still kept in the kernel stack for compatibility with stack unwinding, for example. SCS has a minimal performance overhead, but allocating shadow stacks increases kernel memory usage. The feature is therefore mostly useful on hardware that lacks support for PAC instructions. Changes in v10: - Removed an unnecessary include from head.S. Changes in v9: - Fixed grammar in the Kconfig help text. - Changed Kconfig to allow SCS to be selected with the patchable- function-entry graph tracer. - Changed the EFI stub patch to not filter out -ffixed-x18, only SCS flags. Changes in v8: - Added __noscs to __hyp_text instead of filtering SCS flags from the entire arch/arm64/kvm/hyp directory. - Added a patch to filter out -ffixed-x18 and SCS flags from the EFI stub. Changes in v7: - Changed irq_stack_entry/exit to store the shadow stack pointer in x24 instead of x20 as kernel_entry uses x20-x23 to store data that can be used later. Updated the comment as well. - Changed the Makefile in arch/arm64/kvm/hyp to also filter out -ffixed-x18. - Changed SHADOW_CALL_STACK to depend on !FUNCTION_GRAPH_TRACER instead of not selecting HAVE_FUNCTION_GRAPH_TRACER with SCS. - Removed ifdefs from the EFI wrapper and updated the comment to explain why we are restoring x18. - Rebased as Ard's x18 patches that were part of this series have already been merged. Changes in v6: - Updated comment in the EFI RT wrapper to include the explanation from the commit message. - Fixed the SHADOW_CALL_STACK_VMAP config option and the compilation errors in scs_init_irq() - Updated the comment in entry.S to Mark's suggestion - Fixed the WARN_ON in scs_init() to trip only when the return value for cpuhp_setup_state() is < 0. - Removed ifdefs from the code in arch/arm64/kernel/scs.c and added separate shadow stacks for the SDEI handler Changes in v5: - Updated the comment in __scs_base() to Mark's suggestion - Changed all instances of uintptr_t to unsigned long - Added allocation poisoning for KASAN to catch unintentional shadow stack accesses; moved set_set_magic before poisoning and switched scs_used() and scs_corrupted() to access the buffer using READ_ONCE_NOCHECK() instead - Changed scs_free() to check for NULL instead of zero - Renamed SCS_CACHE_SIZE to NR_CACHED_SCS - Added a warning if cpuhp_setup_state fails in scs_init() - Dropped patches disabling kretprobes after confirming there's no functional conflict with SCS instrumentation - Added an explanation to the commit message why function graph tracing and SCS are incompatible - Removed the ifdefs from arch/arm64/mm/proc.S and added comments explaining why we are saving and restoring x18 - Updated scs_check_usage format to include process information Changes in v4: - Fixed authorship for Ard's patches - Added missing commit messages - Commented code that clears SCS from thread_info - Added a comment about SCS_END_MAGIC being non-canonical Changes in v3: - Switched to filter-out for removing SCS flags in Makefiles - Changed the __noscs attribute to use __no_sanitize__("...") instead of no_sanitize("...") - Cleaned up inline function definitions and moved task_scs() into a macro - Cleaned up scs_free() and scs_magic() - Moved SCS initialization into dup_task_struct() and removed the now unused scs_task_init() - Added comments to __scs_base() and scs_task_reset() to better document design choices - Changed copy_page to make the offset and bias explicit Changes in v2: - Changed Ard's KVM patch to use x29 instead of x18 for the guest context, which makes restore_callee_saved_regs cleaner - Updated help text (and commit messages) to point out differences in security properties compared to user space SCS - Cleaned up config options: removed the ROP protection choice, replaced the CC_IS_CLANG dependency with an arch-specific cc-option test, and moved disabling of incompatible config options to an arch-specific Kconfig - Added CC_FLAGS_SCS, which are filtered out where needed instead of using DISABLE_SCS - Added a __has_feature guard around __noscs for older clang versions Sami Tolvanen (12): add support for Clang's Shadow Call Stack (SCS) scs: add accounting scs: add support for stack usage debugging scs: disable when function graph tracing is enabled arm64: reserve x18 from general allocation with SCS arm64: preserve x18 when CPU is suspended arm64: efi: restore x18 if it was corrupted arm64: vdso: disable Shadow Call Stack arm64: disable SCS for hypervisor code arm64: implement Shadow Call Stack arm64: scs: add shadow stacks for SDEI efi/libstub: disable SCS Makefile | 6 + arch/Kconfig | 35 ++++ arch/arm64/Kconfig | 5 + arch/arm64/Makefile | 4 + arch/arm64/include/asm/kvm_hyp.h | 2 +- arch/arm64/include/asm/scs.h | 39 ++++ arch/arm64/include/asm/suspend.h | 2 +- arch/arm64/include/asm/thread_info.h | 3 + arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/asm-offsets.c | 3 + arch/arm64/kernel/efi-rt-wrapper.S | 11 +- arch/arm64/kernel/entry.S | 47 ++++- arch/arm64/kernel/head.S | 8 + arch/arm64/kernel/irq.c | 2 + arch/arm64/kernel/process.c | 2 + arch/arm64/kernel/scs.c | 114 ++++++++++++ arch/arm64/kernel/sdei.c | 7 + arch/arm64/kernel/smp.c | 4 + arch/arm64/kernel/vdso/Makefile | 2 +- arch/arm64/mm/proc.S | 14 ++ drivers/base/node.c | 6 + drivers/firmware/efi/libstub/Makefile | 3 + fs/proc/meminfo.c | 4 + include/linux/compiler-clang.h | 6 + include/linux/compiler_types.h | 4 + include/linux/mmzone.h | 3 + include/linux/scs.h | 57 ++++++ init/init_task.c | 8 + kernel/Makefile | 1 + kernel/fork.c | 9 + kernel/sched/core.c | 2 + kernel/scs.c | 246 ++++++++++++++++++++++++++ mm/page_alloc.c | 6 + mm/vmstat.c | 3 + 34 files changed, 662 insertions(+), 7 deletions(-) create mode 100644 arch/arm64/include/asm/scs.h create mode 100644 arch/arm64/kernel/scs.c create mode 100644 include/linux/scs.h create mode 100644 kernel/scs.c base-commit: a10c9c710f9ecea87b9f4bbb837467893b4bef01 -- 2.26.0.292.g33ef6b2f38-goog