Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2747616pxb; Mon, 31 Jan 2022 03:31:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJxPcvcGa01npItc5YQtY/6YgzzmxOVLp2/z+JBCEDlO3UXyjUVYcj6lFN1/7YXNNvH1+nVs X-Received: by 2002:a17:902:8f8a:: with SMTP id z10mr20786519plo.59.1643628677954; Mon, 31 Jan 2022 03:31:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643628677; cv=none; d=google.com; s=arc-20160816; b=YNaLDXlJeMdQp70he8ZHuOY8lZgcJ9zbgPI5leqfjuV9h+OtqKrrL0FBRlVNTiWMyV Rm0CYQuIcOmO5+n0mgAoDQAa55SdC1kfpCS7vgaIKLi5ncJOXsHrzOQDdvE+kFoisOcf 51VQPyeQV//viEmVd8k7Pq0qWVt+DRC8hclVEjbfHYhHh+YRcCxWc/8O+6BIQefixbQW p65U4cjFu0xV6yMHuklqwwC+sd2R8x6CITiQGc2lZN8wLTWnfmzAd2B3rwQ38xk9992t 8hPtX+g/0iwmF4otaqUfXpeSnbFxHSSc1SZ2YYCfOSp1RRIYboi67TiUuzbGknZjMauy e09g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=YwLM5LLsvNIv509ATUMaoBoNHSGTOlpO9PFSlugm/sQ=; b=S3otgAv5xzvYg53btgNSq3uBnzsQLbzaDYgtZM/VlKBf8aBNLr2gSCS3ytTFQZSE5N o9IOgQwoA1/zRZWmJDrW7JrrsOY0UX6v9O5W5zw9YBmqbPyTxWJADrPmbPdzzT0DoLGv 8Azank5RDIs9roJ1lXQH8GCvSKH9B/zkyxLc43I/E1YmjUI8Wc36xVJFvyvsRvaUUwcg ++KWri64O6abZpQKKrAY7DtTuf9KOI5Y4eFbwgqdD5tST6z25aAZzogY8isBkR8F+tBQ VGc+4JvxqQosQEN1TI7iQujaqVIXaBSHipHlUOOzDoCTv52VYyDhdUj6dDzAdqt5G+yJ m/HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bbUpNxC2; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p4si1708881pfo.150.2022.01.31.03.31.04; Mon, 31 Jan 2022 03:31:17 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=bbUpNxC2; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350622AbiA1SqD (ORCPT + 99 others); Fri, 28 Jan 2022 13:46:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238911AbiA1SqC (ORCPT ); Fri, 28 Jan 2022 13:46:02 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79762C061714 for ; Fri, 28 Jan 2022 10:46:02 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9B44E61D0D for ; Fri, 28 Jan 2022 18:46:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF8B3C340E6; Fri, 28 Jan 2022 18:45:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643395561; bh=tmg5tqWxrNUjL1aQmmYQBWTKs6R9ohKioeAuiOX9yVo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bbUpNxC2KlnIW8Zf7qUELgsBoXzne8oOqLQQqyG7KGdmdVQjYOYhJhXUMdQYuoGCT UJmCkm4Fj/XMm0BuZD/M8GMm6wpqacwY3rMOFwK2sgdiO3FWg+4D28Vjtsaqk4JKcP 1CCii4/NWqjns2vzsiDFHRiYxkm2Ioa/sV3PfbOlF9GBRh+WQSw4zCEjmZQdyenHdu BhyJm8B7QalLAIDlExaa+B4WOmMlnbynWKVSeBqODrTho1O6s+4NkaQtxnD47N7InI O5FpQHGhH2+LAdNZf5xs697XX3WQalBnnbEiEe/zERfx9y/1U8qd9pdhHhRKgDFF6+ VXdhVpoDLa70A== Date: Fri, 28 Jan 2022 11:45:55 -0700 From: Nathan Chancellor To: Marco Elver Cc: Thomas Gleixner , Kees Cook , Peter Zijlstra , Ingo Molnar , Nick Desaulniers , Elena Reshetova , Alexander Potapenko , llvm@lists.linux.dev, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] stack: Introduce CONFIG_RANDOMIZE_KSTACK_OFFSET Message-ID: References: <20220128114446.740575-1-elver@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220128114446.740575-1-elver@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 28, 2022 at 12:44:45PM +0100, Marco Elver wrote: > The randomize_kstack_offset feature is unconditionally compiled in when > the architecture supports it. > > To add constraints on compiler versions, we require a dedicated Kconfig > variable. Therefore, introduce RANDOMIZE_KSTACK_OFFSET. > > Furthermore, this option is now also configurable by EXPERT kernels: > while the feature is supposed to have zero performance overhead when > disabled, due to its use of static branches, there are few cases where > giving a distribution the option to disable the feature entirely makes > sense. For example, in very resource constrained environments, which > would never enable the feature to begin with, in which case the > additional kernel code size increase would be redundant. > > Signed-off-by: Marco Elver From a Kconfig perspective: Reviewed-by: Nathan Chancellor > --- > arch/Kconfig | 23 ++++++++++++++++++----- > include/linux/randomize_kstack.h | 5 +++++ > init/main.c | 2 +- > 3 files changed, 24 insertions(+), 6 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index 678a80713b21..2cde48d9b77c 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -1159,16 +1159,29 @@ config HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET > to the compiler, so it will attempt to add canary checks regardless > of the static branch state. > > -config RANDOMIZE_KSTACK_OFFSET_DEFAULT > - bool "Randomize kernel stack offset on syscall entry" > +config RANDOMIZE_KSTACK_OFFSET > + bool "Support for randomizing kernel stack offset on syscall entry" if EXPERT > + default y > depends on HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET > help > The kernel stack offset can be randomized (after pt_regs) by > roughly 5 bits of entropy, frustrating memory corruption > attacks that depend on stack address determinism or > - cross-syscall address exposures. This feature is controlled > - by kernel boot param "randomize_kstack_offset=on/off", and this > - config chooses the default boot state. > + cross-syscall address exposures. > + > + The feature is controlled via the "randomize_kstack_offset=on/off" > + kernel boot param, and if turned off has zero overhead due to its use > + of static branches (see JUMP_LABEL). > + > + If unsure, say Y. > + > +config RANDOMIZE_KSTACK_OFFSET_DEFAULT > + bool "Default state of kernel stack offset randomization" > + depends on RANDOMIZE_KSTACK_OFFSET > + help > + Kernel stack offset randomization is controlled by kernel boot param > + "randomize_kstack_offset=on/off", and this config chooses the default > + boot state. > > config ARCH_OPTIONAL_KERNEL_RWX > def_bool n > diff --git a/include/linux/randomize_kstack.h b/include/linux/randomize_kstack.h > index bebc911161b6..91f1b990a3c3 100644 > --- a/include/linux/randomize_kstack.h > +++ b/include/linux/randomize_kstack.h > @@ -2,6 +2,7 @@ > #ifndef _LINUX_RANDOMIZE_KSTACK_H > #define _LINUX_RANDOMIZE_KSTACK_H > > +#ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET > #include > #include > #include > @@ -50,5 +51,9 @@ void *__builtin_alloca(size_t size); > raw_cpu_write(kstack_offset, offset); \ > } \ > } while (0) > +#else /* CONFIG_RANDOMIZE_KSTACK_OFFSET */ > +#define add_random_kstack_offset() do { } while (0) > +#define choose_random_kstack_offset(rand) do { } while (0) > +#endif /* CONFIG_RANDOMIZE_KSTACK_OFFSET */ > > #endif > diff --git a/init/main.c b/init/main.c > index 65fa2e41a9c0..560f45c27ffe 100644 > --- a/init/main.c > +++ b/init/main.c > @@ -853,7 +853,7 @@ static void __init mm_init(void) > pti_init(); > } > > -#ifdef CONFIG_HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET > +#ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET > DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, > randomize_kstack_offset); > DEFINE_PER_CPU(u32, kstack_offset); > -- > 2.35.0.rc0.227.g00780c9af4-goog >