Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp27907ybx; Thu, 31 Oct 2019 22:13:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzN1+JPWCgxj8VsAuDElbammsMoEyAHQQzWCdczbpaANV26lrfnu4if7X2oY3Jr+3sOZN7S X-Received: by 2002:a17:906:e2c2:: with SMTP id gr2mr7809574ejb.31.1572585238862; Thu, 31 Oct 2019 22:13:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572585238; cv=none; d=google.com; s=arc-20160816; b=cUWV7LCLguEA+pvxznEWSzhODE9S72d+YWfuBxpuyB0LHU5NBTl4vATvE4KUr8b6Ir 2T3pNkWvmB687jGBTjtaWujn0QFRkBzArv3rg2QY17aHQNlPWkgNWXGqwUYCmJT3athX tFgY9vUbode8uEcmcS2EcZnGxmRuqTc/b5GcPfnVVt4JcH4G6QvkkLHLGSZygnB49eUu BGxu4yCLB5u0WrCnmu9aJ6i9FfTDGKKVaL38/La/+LFV0KGm9wBACk7CniA7UZ2qy71S V7c7LRv2ZUF5F9NF9e7SiXO/KhVA0IKrsKK/7mWCM2crpuOc3PPy488w0WcT/LE454sy Z5LQ== 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=zSoUcnGkSf7yfDdI4UPFWole6s3LI6NqVWKcqfKw3S8=; b=FadwC7+5ItS3m0VPAA6c+mjz4BPTgbcAoFKEVEXRXilrJVx08JATXx0W0bRhxuMI8U UBqwovyrNRcn+Q2thGxuxNR4TbkNht5LwL135NEfmcYJHaGYAk1rTsMW3gHLc39dQ8ic crdZo4raQUi9HPW2+peeg8dIXfPu6H/u9Nya/+TuCZ36VOZEsmk8jtD3pZMAT4Urs4Cj oA6jWZ7pnRE+p1cZ+k7JYfNSH7tfHMBJ17ycabFKsznCWw2Tdw8O6h9RuExy70YPfDhw +jkK/HLHoIj+WUNitUdF3LCRbUBwoh7OxBdzy3QV4o121rAFGdthQ7mRFSvOF7YCdkIf 7gDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gdpHWq36; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n26si5199475ejy.153.2019.10.31.22.13.21; Thu, 31 Oct 2019 22:13:58 -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=@chromium.org header.s=google header.b=gdpHWq36; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729766AbfKADzH (ORCPT + 99 others); Thu, 31 Oct 2019 23:55:07 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:39770 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726793AbfKADzG (ORCPT ); Thu, 31 Oct 2019 23:55:06 -0400 Received: by mail-pf1-f195.google.com with SMTP id x28so2865956pfo.6 for ; Thu, 31 Oct 2019 20:55:05 -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=zSoUcnGkSf7yfDdI4UPFWole6s3LI6NqVWKcqfKw3S8=; b=gdpHWq36NRa5kgf+gRe/MKxYGjAw3bBoglJCiCxqDG/Z5bLWefay/odv1qRX70dC6u xEQoc/cnbPdiJcOxSIkOgyhVuljqbWzwmQF4cqReWLlUHB1iREij3Bw5hMnU19ZGaFou 2x2RfVCm1NB8MMDbP9OX8d28IFnaJuv0SjjDk= 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=zSoUcnGkSf7yfDdI4UPFWole6s3LI6NqVWKcqfKw3S8=; b=DF26ucLGwWshAGDLr7eq+d2zfCALBYoTlKMKnDd3e3k/f4QRREqG/KYJ94YEZZXvrZ Qil5tuMWjX5qfNNa9euEjkd7OSMDKVLIhySWTLVlsW//54QO0uqfHirCTyY0SS8HzF5T VmU/SLZrll4HOwQpFF+z9z2jAn/XasejdGrt58aqF/JqVEW4MD3ylCgP7g9e7Yvstwxh NmanW/ahbGGvW+xhmBHDoUHMwEJ5x0kSUAoM16ljqZiOjCU43QqkO2MaVLAdVU6dzEv8 sBQ9+hxU64V3iwNY8JSPz5DOfseyOvTFgkin0fQbIJLiIp310dTaWC/3XuAZZxCBszaI gnuQ== X-Gm-Message-State: APjAAAU8CT4Ta99aJNuAp5KlEToS264unsaE58nASZ/GrRSz9bOLohtt +pKhYa/l+Swruuji0XhhCTxycLtPB5w= X-Received: by 2002:a17:90a:bb0b:: with SMTP id u11mr12349099pjr.94.1572580505585; Thu, 31 Oct 2019 20:55:05 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id f25sm7693672pfk.10.2019.10.31.20.55.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2019 20:55:04 -0700 (PDT) Date: Thu, 31 Oct 2019 20:55:03 -0700 From: Kees Cook To: samitolvanen@google.com Cc: Will Deacon , Catalin Marinas , Steven Rostedt , Masami Hiramatsu , Ard Biesheuvel , Dave Martin , Laura Abbott , Mark Rutland , 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 Subject: Re: [PATCH v3 07/17] scs: add support for stack usage debugging Message-ID: <201910312054.3064999E@keescook> References: <20191018161033.261971-1-samitolvanen@google.com> <20191031164637.48901-1-samitolvanen@google.com> <20191031164637.48901-8-samitolvanen@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191031164637.48901-8-samitolvanen@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 31, 2019 at 09:46:27AM -0700, samitolvanen@google.com wrote: > Implements CONFIG_DEBUG_STACK_USAGE for shadow stacks. Did I miss it, or is there no Kconfig section for this? I just realized I can't find it. I was going to say "this commit log should explain why/when this option is used", but then figured it might be explained in the Kconfig ... but I couldn't find it. ;) -Kees > > Signed-off-by: Sami Tolvanen > --- > kernel/scs.c | 39 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 39 insertions(+) > > diff --git a/kernel/scs.c b/kernel/scs.c > index 7780fc4e29ac..67c43af627d1 100644 > --- a/kernel/scs.c > +++ b/kernel/scs.c > @@ -167,6 +167,44 @@ int scs_prepare(struct task_struct *tsk, int node) > return 0; > } > > +#ifdef CONFIG_DEBUG_STACK_USAGE > +static inline unsigned long scs_used(struct task_struct *tsk) > +{ > + unsigned long *p = __scs_base(tsk); > + unsigned long *end = scs_magic(tsk); > + uintptr_t s = (uintptr_t)p; > + > + while (p < end && *p) > + p++; > + > + return (uintptr_t)p - s; > +} > + > +static void scs_check_usage(struct task_struct *tsk) > +{ > + static DEFINE_SPINLOCK(lock); > + static unsigned long highest; > + unsigned long used = scs_used(tsk); > + > + if (used <= highest) > + return; > + > + spin_lock(&lock); > + > + if (used > highest) { > + pr_info("%s: highest shadow stack usage %lu bytes\n", > + __func__, used); > + highest = used; > + } > + > + spin_unlock(&lock); > +} > +#else > +static inline void scs_check_usage(struct task_struct *tsk) > +{ > +} > +#endif > + > bool scs_corrupted(struct task_struct *tsk) > { > return *scs_magic(tsk) != SCS_END_MAGIC; > @@ -181,6 +219,7 @@ void scs_release(struct task_struct *tsk) > return; > > WARN_ON(scs_corrupted(tsk)); > + scs_check_usage(tsk); > > scs_account(tsk, -1); > task_set_scs(tsk, NULL); > -- > 2.24.0.rc0.303.g954a862665-goog > -- Kees Cook