From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C388C433B4 for ; Thu, 1 Apr 2021 22:42:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AD241610CC for ; Thu, 1 Apr 2021 22:42:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD241610CC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 1D2D76B012B; Thu, 1 Apr 2021 18:42:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 184146B012D; Thu, 1 Apr 2021 18:42:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3FB86B012E; Thu, 1 Apr 2021 18:42:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id D873D6B012B for ; Thu, 1 Apr 2021 18:42:12 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A127B6D75 for ; Thu, 1 Apr 2021 22:42:12 +0000 (UTC) X-FDA: 77985272904.14.EF0E96D Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf16.hostedemail.com (Postfix) with ESMTP id BEC8D80192D5 for ; Thu, 1 Apr 2021 22:42:11 +0000 (UTC) Received: by mail-pg1-f180.google.com with SMTP id k8so2487882pgf.4 for ; Thu, 01 Apr 2021 15:42:12 -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=XHU/CSlbR0tXIQ7skc+D9M4sQ8Nf+DFxpytkWh85RTc=; b=Cdeb4TJryjuUvq0PZ22+p9aZ80XrqpLOQqZBbyxQf2b3KNvemF9hZdYHkWk2rvgvr9 +SpPIdq+JMo2g9UMPmRHYMNVYDXvm5KREo94cbMizN1C/uol3pIjbIlmE2pjHUirEook l3tfPMZVPC0JP0fRuhcSZ92o1n77ukI4HC3SY= 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=XHU/CSlbR0tXIQ7skc+D9M4sQ8Nf+DFxpytkWh85RTc=; b=G+txewuYO2Cico5E/Cdkm4K/6jG2uQsZjMdSz7TPjik48Ho1waEwJxAXF+rnFfMSnC z6MqyQOtUa2eOz9sz5PjG4z1+/dgkI1dfNS1vkIX0NlKNRUmkwzVGtOLYzHR0mQbon33 LCrQL43wOXIomO3VhzjZjAslXt8Ez6X7XlP3cSXAJu85LgiadcXusPj4RlxpiESEi1yZ k4OnaWol4csoyxLPjUaTw15Jvgz4RNLeN3POUtIxtb+CSzETByUeQrZ/YHgXRIzSxeTP Qp/oDpcwOAkLXtFXm1txMbkwlGGK7C5oQKYjYO9+ARnWSlBKohz9BO662oWt/KwXg0cw WuyA== X-Gm-Message-State: AOAM531xboTTtCjC0R4VkyoKepEVaB7XqKOA77uPZ3u5jv96n0v+rbbg Jedr4MqmGpeOn7TDMIlATx0FCiM2L3yIVw== X-Google-Smtp-Source: ABdhPJy5OYaYtgibg+iSJ6TbNgMEMp0Li4wFRje6LCbJMZHyUSADLtOfsJcpWv8pBVkoh3+T2eGIQA== X-Received: by 2002:a65:56cc:: with SMTP id w12mr9470880pgs.354.1617316931239; Thu, 01 Apr 2021 15:42:11 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id a7sm6246426pfo.105.2021.04.01.15.42.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Apr 2021 15:42:10 -0700 (PDT) Date: Thu, 1 Apr 2021 15:42:08 -0700 From: Kees Cook To: David Laight Cc: 'Will Deacon' , Thomas Gleixner , Elena Reshetova , "x86@kernel.org" , Andy Lutomirski , Peter Zijlstra , Catalin Marinas , Mark Rutland , Alexander Potapenko , Alexander Popov , Ard Biesheuvel , Jann Horn , Vlastimil Babka , David Hildenbrand , Mike Rapoport , Andrew Morton , Jonathan Corbet , Randy Dunlap , "kernel-hardening@lists.openwall.com" , "linux-hardening@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v8 3/6] stack: Optionally randomize kernel stack offset each syscall Message-ID: <202104011447.2E7F543@keescook> References: <20210330205750.428816-1-keescook@chromium.org> <20210330205750.428816-4-keescook@chromium.org> <20210401083034.GA8554@willie-the-truck> <61ae9398a03d4fe7868b68c9026d5998@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <61ae9398a03d4fe7868b68c9026d5998@AcuMS.aculab.com> X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: BEC8D80192D5 X-Stat-Signature: 5th6uqjtsbk35smtcoo51adoaewcqsiq Received-SPF: none (chromium.org>: No applicable sender policy available) receiver=imf16; identity=mailfrom; envelope-from=""; helo=mail-pg1-f180.google.com; client-ip=209.85.215.180 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617316931-838455 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Apr 01, 2021 at 11:15:43AM +0000, David Laight wrote: > From: Will Deacon > > Sent: 01 April 2021 09:31 > ... > > > +/* > > > + * These macros must be used during syscall entry when interrupts and > > > + * preempt are disabled, and after user registers have been stored to > > > + * the stack. > > > + */ > > > +#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(KSTACK_OFFSET_MAX(offset)); \ > > > + asm volatile("" : "=m"(*ptr) :: "memory"); \ > > > > Using the "m" constraint here is dangerous if you don't actually evaluate it > > inside the asm. For example, if the compiler decides to generate an > > addressing mode relative to the stack but with writeback (autodecrement), then > > the stack pointer will be off by 8 bytes. Can you use "o" instead? I see other examples of empty asm, but it's true, none are using "=m" read constraints. But, yes, using "o" appears to work happily. > Is it allowed to use such a mode? > It would have to know that the "m" was substituted exactly once. > I think there are quite a few examples with 'strange' uses of memory > asm arguments. > > However, in this case, isn't it enough to ensure the address is 'saved'? > So: > asm volatile("" : "=r"(ptr) ); > should be enough. It isn't, it seems. Here's a comparison: https://godbolt.org/z/xYGn9GfGY So, I'll resend with "o", and with raw_cpu_*(). Thanks! -- Kees Cook