Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3135114ybt; Mon, 22 Jun 2020 16:10:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPplxbyTAvVrtkRVrSXcVL+0PayUMfEa/2dQLqp3ckuQpPpLmFNF1u0Znb/+IpGf/U3ZQE X-Received: by 2002:a17:906:4989:: with SMTP id p9mr18549009eju.417.1592867452422; Mon, 22 Jun 2020 16:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592867452; cv=none; d=google.com; s=arc-20160816; b=vVJmgt3rpqWaLFJA7yb/g5CLMFDkqjy5zcSDr3/1lY3PTeTz+uWwDsE6sfxkYznt1Q 88KsAf3RGYpcIBHiYRpR0Rym+kVgYKfAW1FGtWVtQETTrAyx2vnuMIdj6lAC0o/3YDjH whpHg0FrGvkLFKAiV6PrUGI7GOtotAJdp5SMnzj83M02P9crh/MSVF5YnS0U5SVZAOfC TKoe2kAAmjOan4348gCCTE3ILbzG3fDZj5NrWZkkiG+TAtnNcgce5FMQhP4bEZdzf60p zfVByxbk7AHqTWCipegUhCP4tST+rxBtxrycYXV1NGaPGRe6kjaudko4CN4nsFzyb3HH nmyQ== 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=z20zxmu7BXaJTc0tzUXoDHwEuwGLB9cwURxhD+oh4+g=; b=r+0YXF29aIOPCKa3oIqwGKdXCFQATaUTeqUi95s4CH1bIyDdf6nbVNehfKclE8Ww/D Pg6LVmjTL+7nWE62BVYxAeteEMj6NOP4cqFUvgWzESbsz7AX9woRS7khDmJvFnVDC7q/ Km2vTz+6HHouHPi63lbpbpaHOfqrC63stN6C4B0A9DHU6SeKC1cOCdTFKbJBjzghkEpl wV/bYvSDhIRShpB5tf3/J4BmZe2Nq8Vf7q+ylu8BTe0e76Q9LtJ+ROmtmb/1oYiH1Q3a AxqZ5uCy4K512gFKrzs0C9e/cAU5BDuaiXQBuwE/2Qr1RanMgsFK9uTKi9bKaT/mPtPs auzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XG1VeH52; 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 i7si11171364edb.116.2020.06.22.16.10.29; Mon, 22 Jun 2020 16:10:52 -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=XG1VeH52; 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 S1731271AbgFVXHO (ORCPT + 99 others); Mon, 22 Jun 2020 19:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726977AbgFVXHN (ORCPT ); Mon, 22 Jun 2020 19:07:13 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73E37C061573 for ; Mon, 22 Jun 2020 16:07:13 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id ga6so594453pjb.1 for ; Mon, 22 Jun 2020 16:07:13 -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=z20zxmu7BXaJTc0tzUXoDHwEuwGLB9cwURxhD+oh4+g=; b=XG1VeH52GZUyvhcE9tbM4IoixaqvBmVHBTbaAPa1sHlwz6UTK5qC09lefX0IGKu1sw vcczs5K9f5eGPiFG77ESmvfY4NW193Q4M2tAwPqTwmKe/Aw8vn4j561cnqYHf7qIoFQz RUWRINhgB754vJIqeOOwfgYGnA7h4+Ot4qxfo= 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=z20zxmu7BXaJTc0tzUXoDHwEuwGLB9cwURxhD+oh4+g=; b=lrs6RXu6yz4hkC+LTRAt2h3/ryuwPepop/Rz87tHA9x0HU6w/QXvlhsCm+NRVFu9Ks F2ONv69zKha+D/WAqGxdxGFGimNZS//2uiEYAr25Tpj+9o+k9VegZh80Z/G9Kzi6kXnP DykSuYVo+myb0z2WfMVhSVtNyEPNfq/vzOcFVk/wnGk8PQJT/UTNCi0xuJQ6gaU9Iuja t1I8j32CQFH5LOJQPW8osQ/Lqtkv3dAOTxAauNXWI8p4iZbtnAVDRFPYM3y64wiJl/AF tyEGsR1nY31CKHk03LN9mesSZFeVAFJ654mrOZyB6Z6g/CstqN/ON0kDUvCE/vsy+Y41 TUJQ== X-Gm-Message-State: AOAM532ioSYQOdcjNYxaBoezeJayCdIrHd45S0MvgnDQyzfBmkPeAE5e FacG4B1NLrrsnsO56HtVI6pn9Q== X-Received: by 2002:a17:902:7611:: with SMTP id k17mr21505128pll.255.1592867232960; Mon, 22 Jun 2020 16:07:12 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k126sm15106305pfd.129.2020.06.22.16.07.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 16:07:12 -0700 (PDT) Date: Mon, 22 Jun 2020 16:07:11 -0700 From: Kees Cook To: Arvind Sankar Cc: Thomas Gleixner , Elena Reshetova , x86@kernel.org, Andy Lutomirski , Peter Zijlstra , Catalin Marinas , Will Deacon , Mark Rutland , Alexander Potapenko , Alexander Popov , Ard Biesheuvel , Jann Horn , kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 3/5] stack: Optionally randomize kernel stack offset each syscall Message-ID: <202006221604.871B13DE3@keescook> References: <20200622193146.2985288-1-keescook@chromium.org> <20200622193146.2985288-4-keescook@chromium.org> <20200622225615.GA3511702@rani.riverdale.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200622225615.GA3511702@rani.riverdale.lan> 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 06:56:15PM -0400, Arvind Sankar wrote: > On Mon, Jun 22, 2020 at 12:31:44PM -0700, Kees Cook wrote: > > + > > +#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) > > This feels a little fragile. ptr doesn't escape the block, so the > compiler is free to restore the stack immediately after this block. In > fact, given that all you've said is that the asm modifies *ptr, but > nothing uses that output, the compiler could eliminate the whole thing, > no? > > https://godbolt.org/z/HT43F5 > > gcc restores the stack immediately, if no function calls come after it. > > clang completely eliminates the code if no function calls come after. nothing uses the stack in your example. And adding a barrier (which is what the "=m" is, doesn't change it. > I'm not sure why function calls should affect it, though, given that > those functions can't possibly access ptr or the memory it points to. It seems to work correctly: https://godbolt.org/z/c3W5BW -- Kees Cook