Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3079893ybt; Mon, 22 Jun 2020 14:33:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgTlQZNzLqN3VPhEq4TM4T3W2Yo19ub2sSctI1W75ggMnCmSDX6NsHeMsepMl5mDLOIC/j X-Received: by 2002:a17:906:2e4b:: with SMTP id r11mr11950803eji.227.1592861593249; Mon, 22 Jun 2020 14:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592861593; cv=none; d=google.com; s=arc-20160816; b=bN7FbWvAFHMZsr307BxuX5DBb63gs1MZE3TxDzNRSbdPta2Kr/1ER13U/Hr+h3Lipv hgRDB+bryyC9Le5GxiHTLRyHPIyOeRf9UQBCCpHpGLZVZ0l4MTFZQ50/p0BJpImR8CBB +awvsWR0lvghXAAglPKNoDxfMVx3PG1A84xYz/SFTUU6oNZ4BaAERDN8QrK6wEbqHlHf 8+b+P2GdFCjvjLYYhAIUhW192pJOKiW2ZDgU3UW/kcXj6svW6Umxd8//7EsOIOJtPPpa lOkmSC/n9tY1GxNKiycRKhTiSHvI/Hr7Ba5ONPUhCSQboRJo4oQSjEGAficebr+OjNug Ht+g== 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=pLN2RaPddxRloDpN+pRwQoiNiSyh25YEOfza1Wb+VHE=; b=HsfKecTRZo7/zITeP9rxh+Ssk3Q1sFEKAp3lD/GUIAjMH/aUERsXltvyd0CDHfyYab BqXvbCBKU/qgWVgx54zzT5BmS1LmPWUapiWOu7F5N94RaZEWqWS1YPHH+VFcUpkKDxoc e2ZNcnpmq8xWSviDmcvBY1wgjWW+Z+yGlMsJtc5f3XUNdJxgdBjOPsF2TnbXgkJEQX6I JnYZb4KNpO8/ZEF3RDez8DY7PhbRJOLw6M8tkmUOqVzI+ZjXKsMXsMtiuJK6X6ic01i/ bmA92D07iwGKsH0YVUyGyLMDbpL44fHXZ+fueXqjPbwE4NsKITsQ/uuUtYUI9XGYjBZZ rmxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PbXo+AAU; 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 dn17si13807284ejc.556.2020.06.22.14.32.50; Mon, 22 Jun 2020 14:33:13 -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=PbXo+AAU; 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 S1730523AbgFVVaU (ORCPT + 99 others); Mon, 22 Jun 2020 17:30:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730460AbgFVVaT (ORCPT ); Mon, 22 Jun 2020 17:30:19 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC662C061573 for ; Mon, 22 Jun 2020 14:30:19 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id j4so8147018plk.3 for ; Mon, 22 Jun 2020 14:30:19 -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=pLN2RaPddxRloDpN+pRwQoiNiSyh25YEOfza1Wb+VHE=; b=PbXo+AAUJ5jVd5yBXYTaqNpkqWjS+viBecyCQ4cn0lZ7K1T7oe9cXDqosUKpFoYZZu FtVG00wDnjPQEg/jKU5F5eqvyPGU1ZgCNOYpKqeywY4h2JUzArVp7p4RLYBb0oVl4kmj 3ANK8t3QGcMtwG+m1ybJ7nlXfeTZQpbBr6POo= 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=pLN2RaPddxRloDpN+pRwQoiNiSyh25YEOfza1Wb+VHE=; b=n7O/rANtgkxi1i5KIROsvXHwDIrj47tvDnk8K+EgPuhQEKdasJaYHnuBCdiflpPqnQ U7mtUj6SW/kwbq/Mb9/rGjbYWBhLvT/rMPiYm5lP3UDI5R3zff++31qSwQ0CLP4Nrbgy EFV0G6Ffi/NlcLk6Jv1aJHj0Ys0C3PcWBkVetsa/mkIogFcxOQ2z2tLjKjcOsi861y4u Q9mVWowG+ZfiGRjv18BCQ3tVK91EjdhpyXBmmCB39tupdaYo3VacDatQPtsrTxhsNVy1 qyryP2qURRwu0JrAwuQHakKFJ2QAzY3lxWXHohxUvaqvD8tWmNfBMJYqZTOqSPbl/M8q LuKA== X-Gm-Message-State: AOAM530VJjPNevtfg69hEdopstSri73ulMURR5MlStpCHC+StNF0SjlU aq/4LVt05f2CP3S6MzrZoDu3gQ== X-Received: by 2002:a17:902:6908:: with SMTP id j8mr18923742plk.124.1592861419296; Mon, 22 Jun 2020 14:30:19 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p11sm15103897pfq.10.2020.06.22.14.30.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 14:30:18 -0700 (PDT) Date: Mon, 22 Jun 2020 14:30:17 -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: <202006221426.CEEE0B8@keescook> References: <20200622193146.2985288-1-keescook@chromium.org> <20200622193146.2985288-4-keescook@chromium.org> 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 10:07:37PM +0200, Jann Horn wrote: > On Mon, Jun 22, 2020 at 9:31 PM Kees Cook wrote: > > This provides the ability for architectures to enable kernel stack base > > address offset randomization. This feature is controlled by the boot > > param "randomize_kstack_offset=on/off", with its default value set by > > CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT. > [...] > > +#define add_random_kstack_offset() do { \ > > + 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); \ > > + asm volatile("" : "=m"(*ptr)); \ > > + } \ > > +} while (0) > > clang generates better code here if the mask is stack-aligned - > otherwise it needs to round the stack pointer / the offset: Interesting. I was hoping to avoid needing to know the architecture stack alignment (leaving it up to the compiler). > > $ cat alloca_align.c > #include > void callee(void); > > void alloca_blah(unsigned long rand) { > asm volatile(""::"r"(alloca(rand & MASK))); > callee(); > } > $ clang -O3 -c -o alloca_align.o alloca_align.c -DMASK=0x3ff > $ objdump -d alloca_align.o > [...] > 0: 55 push %rbp > 1: 48 89 e5 mov %rsp,%rbp > 4: 81 e7 ff 03 00 00 and $0x3ff,%edi > a: 83 c7 0f add $0xf,%edi > d: 83 e7 f0 and $0xfffffff0,%edi > 10: 48 89 e0 mov %rsp,%rax > 13: 48 29 f8 sub %rdi,%rax > 16: 48 89 c4 mov %rax,%rsp > 19: e8 00 00 00 00 callq 1e > 1e: 48 89 ec mov %rbp,%rsp > 21: 5d pop %rbp > 22: c3 retq > $ clang -O3 -c -o alloca_align.o alloca_align.c -DMASK=0x3f0 > $ objdump -d alloca_align.o > [...] > 0: 55 push %rbp > 1: 48 89 e5 mov %rsp,%rbp > 4: 48 89 e0 mov %rsp,%rax > 7: 81 e7 f0 03 00 00 and $0x3f0,%edi > d: 48 29 f8 sub %rdi,%rax > 10: 48 89 c4 mov %rax,%rsp > 13: e8 00 00 00 00 callq 18 > 18: 48 89 ec mov %rbp,%rsp > 1b: 5d pop %rbp > 1c: c3 retq > $ > > (From a glance at the assembly, gcc seems to always assume that the > length may be misaligned.) Right -- this is why I didn't bother with it, since it didn't seem to notice what I'd already done to the alloca() argument. (But from what I could measure on cycle counts, the additional ALU didn't seem to really make much difference ... it _would_ be nice to avoid it, of course.) > Maybe this should be something along the lines of > __builtin_alloca(offset & (0x3ff & ARCH_STACK_ALIGN_MASK)) (with > appropriate definitions of the stack alignment mask depending on the > architecture's choice of stack alignment for kernel code). Is that explicitly selected anywhere in the kernel? I thought the alignment was left up to the compiler (as in I've seen bugs fixed where the kernel had to deal with the alignment choices the compiler was making...) -- Kees Cook