Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3220964ybt; Mon, 22 Jun 2020 18:53:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfraPkCqe2fqzNBsKIKJuFZJzHTQe9sQvjf1J/dUSkpujpy8ff4NitCIoumwYSohjahlzN X-Received: by 2002:a17:906:470c:: with SMTP id y12mr18740048ejq.336.1592877217947; Mon, 22 Jun 2020 18:53:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592877217; cv=none; d=google.com; s=arc-20160816; b=KeHkYb7ZUs4gxjtdlClaOlcWQFPL5dHMTHgvga/O9Zmq95NjoABAHC3m+RpoPBXCFd TpzPq3rWrGEgq13063N2AsrdpxRW+kJa/rtYtN2xZFcjeYs12Imx3/CnkVptuKSN9U01 kSAN0jAHv5wf1NuEm9oEUFlLGZ7JajOW4Dz+rdMsPmF3RAGmjjT2pfwuC0JBRve4S/xG 8Fv9/zdddOCTOnz5o47GEVUmmt9duL6rpV+qdXl73o+85GTERnAOtYX0TjFiWs72MxBG 1fPqME8e7DVW2R6gw3o0F6LCbtverTCPOkb9dxUD09UQ6WZ+a59zfy34929OykCnaAd0 yzYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=tiLvWsLlWI5Mz8yJb26QV5MdQxD2Cn7z8TLlvShCGfU=; b=fbv6j3nnMqwLiN/kZRIjp2OI+WbbS7URF5Sud9EYjeuyUCjnzgvF41TdmjLa+i314h vEZbRcG++ecHel0d/9JoDYEmtthxgPhapIyVoVJbyNBsrV+BVC0uTfjnEvdKMPF+Hr5L o6B2CGIcQCPghuxTRyso4HnCy9s4vKWpReycUsfgYp1sUmLENrO8IgMEZ9s5RasE+Pho teTPaSOvZnOMv2R0e9EGcAZmB2IjpU5S9exyv8E1/5vMEd2TVOqX2lMITtj7gJX9s4gu WiAASNyfHurIYEz2ZjUxH3XbCxkPQqmbj0M4voQtODtvtHY91EdGDNsT5CPT2J/NlqyB nkpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Om+hyYPX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r16si10750499eds.6.2020.06.22.18.53.14; Mon, 22 Jun 2020 18:53:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Om+hyYPX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730871AbgFVWEo (ORCPT + 99 others); Mon, 22 Jun 2020 18:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730736AbgFVWEo (ORCPT ); Mon, 22 Jun 2020 18:04:44 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC36BC061573 for ; Mon, 22 Jun 2020 15:04:42 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id d10so6282777pls.5 for ; Mon, 22 Jun 2020 15:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=tiLvWsLlWI5Mz8yJb26QV5MdQxD2Cn7z8TLlvShCGfU=; b=Om+hyYPXJ8zWabYbc6j0NJhRsYt8jMK2RZ8VkZvizn/0iWwTmfV5CpCspe1f49JHQW 6gV2PbvOVcBmbS6Dkxhwac9tkJmvLeq9OhxwTB6IOmUuEIfpwzibKOjcJaaH8shijbHK ngd3RTBVPCWAn6skAEgoIsGV4+tetMcj/rxhM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=tiLvWsLlWI5Mz8yJb26QV5MdQxD2Cn7z8TLlvShCGfU=; b=epbO8KT8LB9Q1IVhMLys4dOQWroGvQypxCgdqypUH3khLZMsdlzz3eDBEqouDVDGHt MzuaEKkIZoarWVdTdp1jUYBI2OX+58uxbaFj05MuwHlcLsV/Wm/J9BufLUNyo58Jc3+R gkzUDcNQ8bds/HohtziI7bxS7gLuGiQzWY8pSfJmBdV0mmapU0w4ojM7FKyDvYHbNl7o giXAvpcB3CFU6vuFz32e4+U1Qzzf/sKe8+zW6TOGZcwzGmRLtoEvpmdYYYLnhN6wC/BE UWpxpIIj+4Js6NUHfxafDtReIdcEydJWZKu49C7o/4XXo31G5ZC/gnNmKWEWgEaHfQoH SRDg== X-Gm-Message-State: AOAM531jfxxKOgrNN/npt8R3ZbpPDLks5zGNT/OlitmGflITt0Wt9EsJ rFSTozeEhF0M4Z5wxt05aFYVpg== X-Received: by 2002:a17:902:8342:: with SMTP id z2mr21209390pln.300.1592863482442; Mon, 22 Jun 2020 15:04:42 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n8sm12462000pgi.18.2020.06.22.15.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 15:04:41 -0700 (PDT) Date: Mon, 22 Jun 2020 15:04:40 -0700 From: Kees Cook To: Jann Horn Cc: Thomas Gleixner , Elena Reshetova , the arch/x86 maintainers , Andy Lutomirski , Peter Zijlstra , Catalin Marinas , Will Deacon , Mark Rutland , Alexander Potapenko , Alexander Popov , Ard Biesheuvel , Kernel Hardening , Linux ARM , Linux-MM , kernel list Subject: Re: [PATCH v4 3/5] stack: Optionally randomize kernel stack offset each syscall Message-ID: <202006221451.2E80C90FF7@keescook> References: <20200622193146.2985288-1-keescook@chromium.org> <20200622193146.2985288-4-keescook@chromium.org> <202006221426.CEEE0B8@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 22, 2020 at 11:42:29PM +0200, Jann Horn wrote: > No, at least on x86-64 and x86 Linux overrides the normal ABI. From > arch/x86/Makefile: Ah! Thanks for the pointer. > > # For gcc stack alignment is specified with -mpreferred-stack-boundary, > # clang has the option -mstack-alignment for that purpose. > ifneq ($(call cc-option, -mpreferred-stack-boundary=4),) > cc_stack_align4 := -mpreferred-stack-boundary=2 > cc_stack_align8 := -mpreferred-stack-boundary=3 > else ifneq ($(call cc-option, -mstack-alignment=16),) > cc_stack_align4 := -mstack-alignment=4 > cc_stack_align8 := -mstack-alignment=8 > endif > [...] > ifeq ($(CONFIG_X86_32),y) > [...] > # Align the stack to the register width instead of using the default > # alignment of 16 bytes. This reduces stack usage and the number of > # alignment instructions. > KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align4)) > [...] > else > [...] > # By default gcc and clang use a stack alignment of 16 bytes for x86. > # However the standard kernel entry on x86-64 leaves the stack on an > # 8-byte boundary. If the compiler isn't informed about the actual > # alignment it will generate extra alignment instructions for the > # default alignment which keep the stack *mis*aligned. > # Furthermore an alignment to the register width reduces stack usage > # and the number of alignment instructions. > KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align8)) > [...] > endif And it seems that only x86 does this. No other architecture specifies -mpreferred-stack-boundary... > Normal x86-64 ABI has 16-byte stack alignment; Linux kernel x86-64 ABI > has 8-byte stack alignment. > Similarly, the normal Linux 32-bit x86 ABI is 16-byte aligned; > meanwhile Linux kernel x86 ABI has 4-byte stack alignment. > > This is because userspace code wants the stack to be sufficiently > aligned for fancy SSE instructions and such; the kernel, on the other > hand, never uses those in normal code, and cares about stack usage and > such very much. This makes it nicer for Clang: diff --git a/include/linux/randomize_kstack.h b/include/linux/randomize_kstack.h index 1df0dc52cadc..f7e1f68fb50c 100644 --- a/include/linux/randomize_kstack.h +++ b/include/linux/randomize_kstack.h @@ -10,6 +10,14 @@ DECLARE_STATIC_KEY_MAYBE(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, randomize_kstack_offset); DECLARE_PER_CPU(u32, kstack_offset); +#ifdef CONFIG_X86_64 +#define ARCH_STACK_ALIGN_MASK ~((1 << 8) - 1) +#elif defined(CONFIG_X86_32) +#define ARCH_STACK_ALIGN_MASK ~((1 << 4) - 1) +#else +#define ARCH_STACK_ALIGN_MASK ~(0) +#endif + /* * Do not use this anywhere else in the kernel. This is used here because * it provides an arch-agnostic way to grow the stack with correct @@ -23,7 +31,8 @@ void *__builtin_alloca(size_t size); if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \ &randomize_kstack_offset)) { \ u32 offset = this_cpu_read(kstack_offset); \ - u8 *ptr = __builtin_alloca(offset & 0x3FF); \ + u8 *ptr = __builtin_alloca(offset & 0x3FF & \ + ARCH_STACK_ALIGN_MASK); \ asm volatile("" : "=m"(*ptr)); \ } \ } while (0) But I don't like open-coding the x86-ony stack alignment... it should be in Kconfig or something, I think? -- Kees Cook